1 package jalview.util.matcher;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertSame;
6 import static org.testng.Assert.assertTrue;
8 import java.util.Iterator;
9 import java.util.function.Function;
11 import org.testng.annotations.Test;
13 public class KeyedMatcherSetTest
15 @Test(groups = "Functional")
16 public void testMatches()
19 * a numeric matcher - MatcherTest covers more conditions
21 KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
22 KeyedMatcherSetI kms = new KeyedMatcherSet();
24 assertTrue(kms.matches(key -> "-2"));
25 assertTrue(kms.matches(key -> "-1"));
26 assertFalse(kms.matches(key -> "-3"));
27 assertFalse(kms.matches(key -> ""));
28 assertFalse(kms.matches(key -> "junk"));
29 assertFalse(kms.matches(key -> null));
32 * a string pattern matcher
34 km = new KeyedMatcher(Condition.Contains, "Cat", "AF");
35 kms = new KeyedMatcherSet();
38 .matches(key -> "AF".equals(key[0]) ? "raining cats and dogs"
42 @Test(groups = "Functional")
45 // condition1: AF value contains "dog" (matches)
46 KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
47 // condition 2: CSQ value does not contain "how" (does not match)
48 KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
51 Function<String[], String> vp = key -> "AF".equals(key[0])
52 ? "raining cats and dogs"
54 assertTrue(km1.matches(vp));
55 assertFalse(km2.matches(vp));
57 KeyedMatcherSetI kms = new KeyedMatcherSet();
58 assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass
60 assertTrue(kms.matches(vp));
62 assertFalse(kms.matches(vp));
65 @Test(groups = "Functional")
66 public void testToString()
68 KeyedMatcherI km1 = new KeyedMatcher(Condition.LT, 1.2f, "AF");
69 assertEquals(km1.toString(), "AF < 1.2");
71 KeyedMatcher km2 = new KeyedMatcher(Condition.NotContains, "path",
73 assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'");
78 KeyedMatcherSetI kms = new KeyedMatcherSet();
79 assertEquals(kms.toString(), "");
81 assertEquals(kms.toString(), "(AF < 1.2)");
83 assertEquals(kms.toString(),
84 "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')");
89 kms = new KeyedMatcherSet();
90 assertEquals(kms.toString(), "");
92 assertEquals(kms.toString(), "(AF < 1.2)");
94 assertEquals(kms.toString(),
95 "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
98 @Test(groups = "Functional")
101 // condition1: AF value contains "dog" (matches)
102 KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
103 // condition 2: CSQ value does not contain "how" (does not match)
104 KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
107 Function<String[], String> vp = key -> "AF".equals(key[0])
108 ? "raining cats and dogs"
110 assertTrue(km1.matches(vp));
111 assertFalse(km2.matches(vp));
113 KeyedMatcherSetI kms = new KeyedMatcherSet();
115 assertFalse(kms.matches(vp));
117 assertTrue(kms.matches(vp));
120 @Test(groups = "Functional")
121 public void testIsEmpty()
123 KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
124 KeyedMatcherSetI kms = new KeyedMatcherSet();
125 assertTrue(kms.isEmpty());
127 assertFalse(kms.isEmpty());
130 @Test(groups = "Functional")
131 public void testGetMatchers()
133 KeyedMatcherSetI kms = new KeyedMatcherSet();
138 Iterator<KeyedMatcherI> iterator = kms.getMatchers().iterator();
139 assertFalse(iterator.hasNext());
144 KeyedMatcherI km1 = new KeyedMatcher(Condition.GE, -2F, "AF");
146 iterator = kms.getMatchers().iterator();
147 assertSame(km1, iterator.next());
148 assertFalse(iterator.hasNext());
153 KeyedMatcherI km2 = new KeyedMatcher(Condition.LT, 8F, "AF");
155 iterator = kms.getMatchers().iterator();
156 assertSame(km1, iterator.next());
157 assertSame(km2, iterator.next());
158 assertFalse(iterator.hasNext());
162 * Tests for the 'compound attribute' key i.e. where first key's value is a map
163 * from which we take the value for the second key, e.g. CSQ : Consequence
165 @Test(groups = "Functional")
166 public void testMatches_compoundKey()
169 * a numeric matcher - MatcherTest covers more conditions
171 KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "CSQ",
173 KeyedMatcherSetI kms = new KeyedMatcherSet();
175 assertTrue(kms.matches(key -> "-2"));
176 assertTrue(kms.matches(key -> "-1"));
177 assertFalse(kms.matches(key -> "-3"));
178 assertFalse(kms.matches(key -> ""));
179 assertFalse(kms.matches(key -> "junk"));
180 assertFalse(kms.matches(key -> null));
183 * a string pattern matcher
185 km = new KeyedMatcher(Condition.Contains, "Cat", "CSQ", "Consequence");
186 kms = new KeyedMatcherSet();
188 assertTrue(kms.matches(key -> "csq".equalsIgnoreCase(key[0])
189 && "Consequence".equalsIgnoreCase(key[1])
190 ? "raining cats and dogs"