JAL-2808 toString modified, tests corrected
[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.assertTrue;
6
7 import java.util.function.Function;
8
9 import org.testng.annotations.Test;
10
11 public class KeyedMatcherSetTest
12 {
13   @Test
14   public void testMatches()
15   {
16     /*
17      * a numeric matcher - MatcherTest covers more conditions
18      */
19     KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
20     KeyedMatcherSetI kms = new KeyedMatcherSet();
21     kms.and(km);
22     assertTrue(kms.matches(key -> "-2"));
23     assertTrue(kms.matches(key -> "-1"));
24     assertFalse(kms.matches(key -> "-3"));
25     assertFalse(kms.matches(key -> ""));
26     assertFalse(kms.matches(key -> "junk"));
27     assertFalse(kms.matches(key -> null));
28
29     /*
30      * a string pattern matcher
31      */
32     km = new KeyedMatcher("AF", Condition.Contains, "Cat");
33     kms = new KeyedMatcherSet();
34     kms.and(km);
35     assertTrue(kms
36             .matches(key -> "AF".equals(key) ? "raining cats and dogs"
37             : "showers"));
38   }
39
40   @Test
41   public void testAnd()
42   {
43     // condition1: AF value contains "dog" (matches)
44     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
45     // condition 2: CSQ value does not contain "how" (does not match)
46     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
47             "how");
48
49     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
50             : "showers";
51     assertTrue(km1.matches(vp));
52     assertFalse(km2.matches(vp));
53
54     KeyedMatcherSetI kms = new KeyedMatcherSet();
55     assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass
56     kms.and(km1);
57     assertTrue(kms.matches(vp));
58     kms.and(km2);
59     assertFalse(kms.matches(vp));
60   }
61
62   @Test
63   public void testToString()
64   {
65     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.LT, 1.2f);
66     assertEquals(km1.toString(), "AF < 1.2");
67
68     KeyedMatcher km2 = new KeyedMatcher("CLIN_SIG", Condition.NotContains, "path");
69     assertEquals(km2.toString(), "CLIN_SIG Does not contain PATH");
70
71     /*
72      * AND them
73      */
74     KeyedMatcherSetI kms = new KeyedMatcherSet();
75     assertEquals(kms.toString(), "");
76     kms.and(km1);
77     assertEquals(kms.toString(), "(AF < 1.2)");
78     kms.and(km2);
79     assertEquals(kms.toString(),
80             "(AF < 1.2) AND (CLIN_SIG Does not contain PATH)");
81
82     /*
83      * OR them
84      */
85     kms = new KeyedMatcherSet();
86     assertEquals(kms.toString(), "");
87     kms.or(km1);
88     assertEquals(kms.toString(), "(AF < 1.2)");
89     kms.or(km2);
90     assertEquals(kms.toString(),
91             "(AF < 1.2) OR (CLIN_SIG Does not contain PATH)");
92   }
93
94   @Test
95   public void testOr()
96   {
97     // condition1: AF value contains "dog" (matches)
98     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
99     // condition 2: CSQ value does not contain "how" (does not match)
100     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
101             "how");
102
103     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
104             : "showers";
105     assertTrue(km1.matches(vp));
106     assertFalse(km2.matches(vp));
107
108     KeyedMatcherSetI kms = new KeyedMatcherSet();
109     kms.or(km2);
110     assertFalse(kms.matches(vp));
111     kms.or(km1);
112     assertTrue(kms.matches(vp));
113   }
114
115   @Test
116   public void testIsEmpty()
117   {
118     KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
119     KeyedMatcherSetI kms = new KeyedMatcherSet();
120     assertTrue(kms.isEmpty());
121     kms.and(km);
122     assertFalse(kms.isEmpty());
123   }
124 }