JAL-2808 revised util.matcher package, 2 filter conditions per feature
[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     assertTrue(km.matches(key -> "-2"));
21     assertTrue(km.matches(key -> "-1"));
22     assertFalse(km.matches(key -> "-3"));
23     assertFalse(km.matches(key -> ""));
24     assertFalse(km.matches(key -> "junk"));
25     assertFalse(km.matches(key -> null));
26
27     /*
28      * a string pattern matcher
29      */
30     km = new KeyedMatcher("AF", Condition.Contains, "Cat");
31     assertTrue(km.matches(key -> "AF".equals(key) ? "raining cats and dogs"
32             : "showers"));
33   }
34
35   @Test
36   public void testAnd()
37   {
38     // condition1: AF value contains "dog" (matches)
39     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
40     // condition 2: CSQ value does not contain "how" (does not match)
41     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
42             "how");
43
44     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
45             : "showers";
46     assertTrue(km1.matches(vp));
47     assertFalse(km2.matches(vp));
48
49     KeyedMatcherSetI kms = new KeyedMatcherSet();
50     assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass
51     kms.and(km1);
52     assertTrue(kms.matches(vp));
53     kms.and(km2);
54     assertFalse(kms.matches(vp));
55   }
56
57   @Test
58   public void testToString()
59   {
60     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.LT, 1.2f);
61     assertEquals(km1.toString(), "AF LT 1.2");
62
63     KeyedMatcher km2 = new KeyedMatcher("CLIN_SIG", Condition.NotContains, "path");
64     assertEquals(km2.toString(), "CLIN_SIG NotContains PATH");
65
66     /*
67      * AND them
68      */
69     KeyedMatcherSetI kms = new KeyedMatcherSet();
70     assertEquals(kms.toString(), "");
71     kms.and(km1);
72     assertEquals(kms.toString(), "(AF LT 1.2)");
73     kms.and(km2);
74     assertEquals(kms.toString(),
75             "(AF LT 1.2) AND (CLIN_SIG NotContains PATH)");
76
77     /*
78      * OR them
79      */
80     kms = new KeyedMatcherSet();
81     assertEquals(kms.toString(), "");
82     kms.or(km1);
83     assertEquals(kms.toString(), "(AF LT 1.2)");
84     kms.or(km2);
85     assertEquals(kms.toString(),
86             "(AF LT 1.2) OR (CLIN_SIG NotContains PATH)");
87   }
88
89   /**
90    * @return
91    */
92   protected KeyedMatcher km3()
93   {
94     return new KeyedMatcher("CSQ", Condition.Contains, "benign");
95   }
96
97   @Test
98   public void testOr()
99   {
100     // condition1: AF value contains "dog" (matches)
101     KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
102     // condition 2: CSQ value does not contain "how" (does not match)
103     KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
104             "how");
105
106     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
107             : "showers";
108     assertTrue(km1.matches(vp));
109     assertFalse(km2.matches(vp));
110
111     KeyedMatcherSetI kms = new KeyedMatcherSet();
112     kms.or(km2);
113     assertFalse(kms.matches(vp));
114     kms.or(km1);
115     assertTrue(kms.matches(vp));
116   }
117 }