package jalview.util.matcher; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; import java.util.Iterator; import java.util.function.Function; import org.testng.annotations.Test; public class KeyedMatcherSetTest { @Test(groups = "Functional") public void testMatches() { /* * a numeric matcher - MatcherTest covers more conditions */ KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF"); KeyedMatcherSetI kms = new KeyedMatcherSet(); kms.and(km); assertTrue(kms.matches(key -> "-2")); assertTrue(kms.matches(key -> "-1")); assertFalse(kms.matches(key -> "-3")); assertFalse(kms.matches(key -> "")); assertFalse(kms.matches(key -> "junk")); assertFalse(kms.matches(key -> null)); /* * a string pattern matcher */ km = new KeyedMatcher(Condition.Contains, "Cat", "AF"); kms = new KeyedMatcherSet(); kms.and(km); assertTrue(kms .matches(key -> "AF".equals(key[0]) ? "raining cats and dogs" : "showers")); } @Test(groups = "Functional") public void testAnd() { // condition1: AF value contains "dog" (matches) KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF"); // condition 2: CSQ value does not contain "how" (does not match) KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how", "CSQ"); Function vp = key -> "AF".equals(key[0]) ? "raining cats and dogs" : "showers"; assertTrue(km1.matches(vp)); assertFalse(km2.matches(vp)); KeyedMatcherSetI kms = new KeyedMatcherSet(); assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass kms.and(km1); assertTrue(kms.matches(vp)); kms.and(km2); assertFalse(kms.matches(vp)); } @Test(groups = "Functional") public void testToString() { KeyedMatcherI km1 = new KeyedMatcher(Condition.LT, 1.2f, "AF"); assertEquals(km1.toString(), "AF < 1.2"); KeyedMatcher km2 = new KeyedMatcher(Condition.NotContains, "path", "CLIN_SIG"); assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'"); /* * AND them */ KeyedMatcherSetI kms = new KeyedMatcherSet(); assertEquals(kms.toString(), ""); kms.and(km1); assertEquals(kms.toString(), "(AF < 1.2)"); kms.and(km2); assertEquals(kms.toString(), "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')"); /* * OR them */ kms = new KeyedMatcherSet(); assertEquals(kms.toString(), ""); kms.or(km1); assertEquals(kms.toString(), "(AF < 1.2)"); kms.or(km2); assertEquals(kms.toString(), "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')"); } @Test(groups = "Functional") public void testOr() { // condition1: AF value contains "dog" (matches) KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF"); // condition 2: CSQ value does not contain "how" (does not match) KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how", "CSQ"); Function vp = key -> "AF".equals(key[0]) ? "raining cats and dogs" : "showers"; assertTrue(km1.matches(vp)); assertFalse(km2.matches(vp)); KeyedMatcherSetI kms = new KeyedMatcherSet(); kms.or(km2); assertFalse(kms.matches(vp)); kms.or(km1); assertTrue(kms.matches(vp)); } @Test(groups = "Functional") public void testIsEmpty() { KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF"); KeyedMatcherSetI kms = new KeyedMatcherSet(); assertTrue(kms.isEmpty()); kms.and(km); assertFalse(kms.isEmpty()); } @Test(groups = "Functional") public void testGetMatchers() { KeyedMatcherSetI kms = new KeyedMatcherSet(); /* * empty iterable: */ Iterator iterator = kms.getMatchers().iterator(); assertFalse(iterator.hasNext()); /* * one matcher: */ KeyedMatcherI km1 = new KeyedMatcher(Condition.GE, -2F, "AF"); kms.and(km1); iterator = kms.getMatchers().iterator(); assertSame(km1, iterator.next()); assertFalse(iterator.hasNext()); /* * two matchers: */ KeyedMatcherI km2 = new KeyedMatcher(Condition.LT, 8F, "AF"); kms.and(km2); iterator = kms.getMatchers().iterator(); assertSame(km1, iterator.next()); assertSame(km2, iterator.next()); assertFalse(iterator.hasNext()); } /** * Tests for the 'compound attribute' key i.e. where first key's value is a map * from which we take the value for the second key, e.g. CSQ : Consequence */ @Test(groups = "Functional") public void testMatches_compoundKey() { /* * a numeric matcher - MatcherTest covers more conditions */ KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "CSQ", "Consequence"); KeyedMatcherSetI kms = new KeyedMatcherSet(); kms.and(km); assertTrue(kms.matches(key -> "-2")); assertTrue(kms.matches(key -> "-1")); assertFalse(kms.matches(key -> "-3")); assertFalse(kms.matches(key -> "")); assertFalse(kms.matches(key -> "junk")); assertFalse(kms.matches(key -> null)); /* * a string pattern matcher */ km = new KeyedMatcher(Condition.Contains, "Cat", "CSQ", "Consequence"); kms = new KeyedMatcherSet(); kms.and(km); assertTrue(kms.matches(key -> "csq".equalsIgnoreCase(key[0]) && "Consequence".equalsIgnoreCase(key[1]) ? "raining cats and dogs" : "showers")); } }