3018cb638739e0122be5f4f87b9da69f1d8e26b1
[jalview.git] / test / jalview / util / matcher / KeyedMatcherSetTest.java
1 package jalview.util.matcher;
2
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;
7
8 import java.util.Iterator;
9 import java.util.function.Function;
10
11 import org.testng.annotations.Test;
12
13 public class KeyedMatcherSetTest
14 {
15   @Test
16   public void testMatches()
17   {
18     /*
19      * a numeric matcher - MatcherTest covers more conditions
20      */
21     KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
22     KeyedMatcherSetI kms = new KeyedMatcherSet();
23     kms.and(km);
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));
30
31     /*
32      * a string pattern matcher
33      */
34     km = new KeyedMatcher("AF", Condition.Contains, "Cat");
35     kms = new KeyedMatcherSet();
36     kms.and(km);
37     assertTrue(kms
38             .matches(key -> "AF".equals(key) ? "raining cats and dogs"
39             : "showers"));
40   }
41
42   @Test
43   public void testAnd()
44   {
45     // condition1: AF value contains "dog" (matches)
46     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
47     // condition 2: CSQ value does not contain "how" (does not match)
48     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
49             "how");
50
51     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
52             : "showers";
53     assertTrue(km1.matches(vp));
54     assertFalse(km2.matches(vp));
55
56     KeyedMatcherSetI kms = new KeyedMatcherSet();
57     assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass
58     kms.and(km1);
59     assertTrue(kms.matches(vp));
60     kms.and(km2);
61     assertFalse(kms.matches(vp));
62   }
63
64   @Test
65   public void testToString()
66   {
67     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.LT, 1.2f);
68     assertEquals(km1.toString(), "AF < 1.2");
69
70     KeyedMatcher km2 = new KeyedMatcher("CLIN_SIG", Condition.NotContains, "path");
71     assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'");
72
73     /*
74      * AND them
75      */
76     KeyedMatcherSetI kms = new KeyedMatcherSet();
77     assertEquals(kms.toString(), "");
78     kms.and(km1);
79     assertEquals(kms.toString(), "(AF < 1.2)");
80     kms.and(km2);
81     assertEquals(kms.toString(),
82             "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')");
83
84     /*
85      * OR them
86      */
87     kms = new KeyedMatcherSet();
88     assertEquals(kms.toString(), "");
89     kms.or(km1);
90     assertEquals(kms.toString(), "(AF < 1.2)");
91     kms.or(km2);
92     assertEquals(kms.toString(),
93             "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
94   }
95
96   @Test
97   public void testOr()
98   {
99     // condition1: AF value contains "dog" (matches)
100     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
101     // condition 2: CSQ value does not contain "how" (does not match)
102     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
103             "how");
104
105     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
106             : "showers";
107     assertTrue(km1.matches(vp));
108     assertFalse(km2.matches(vp));
109
110     KeyedMatcherSetI kms = new KeyedMatcherSet();
111     kms.or(km2);
112     assertFalse(kms.matches(vp));
113     kms.or(km1);
114     assertTrue(kms.matches(vp));
115   }
116
117   @Test
118   public void testIsEmpty()
119   {
120     KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
121     KeyedMatcherSetI kms = new KeyedMatcherSet();
122     assertTrue(kms.isEmpty());
123     kms.and(km);
124     assertFalse(kms.isEmpty());
125   }
126
127   @Test
128   public void testGetMatchers()
129   {
130     KeyedMatcherSetI kms = new KeyedMatcherSet();
131
132     /*
133      * empty iterable:
134      */
135     Iterator<KeyedMatcherI> iterator = kms.getMatchers().iterator();
136     assertFalse(iterator.hasNext());
137
138     /*
139      * one matcher:
140      */
141     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.GE, -2F);
142     kms.and(km1);
143     iterator = kms.getMatchers().iterator();
144     assertSame(km1, iterator.next());
145     assertFalse(iterator.hasNext());
146
147     /*
148      * two matchers:
149      */
150     KeyedMatcherI km2 = new KeyedMatcher("AF", Condition.LT, 8F);
151     kms.and(km2);
152     iterator = kms.getMatchers().iterator();
153     assertSame(km1, iterator.next());
154     assertSame(km2, iterator.next());
155     assertFalse(iterator.hasNext());
156   }
157 }