ccf2ba25949ebcc4612944ea0483623f9ad3a00e
[jalview.git] / test / jalview / util / matcher / KeyedMatcherTest.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 KeyedMatcherTest
12 {
13   @Test
14   public void testMatches()
15   {
16     /*
17      * a numeric matcher - MatcherTest covers more conditions
18      */
19     MatcherI m1 = new Matcher(Condition.GE, -2f);
20     KeyedMatcherI km = new KeyedMatcher("AF", m1);
21     assertTrue(km.matches(key -> "-2"));
22     assertTrue(km.matches(key -> "-1"));
23     assertFalse(km.matches(key -> "-3"));
24     assertFalse(km.matches(key -> ""));
25     assertFalse(km.matches(key -> "junk"));
26     assertFalse(km.matches(key -> null));
27
28     /*
29      * a string pattern matcher
30      */
31     MatcherI m2 = new Matcher(Condition.Contains, "Cat");
32     km = new KeyedMatcher("AF", m2);
33     assertTrue(km.matches(key -> "AF".equals(key) ? "raining cats and dogs"
34             : "showers"));
35   }
36
37   @Test
38   public void testAnd()
39   {
40     // condition1: AF value contains "dog" (matches)
41     KeyedMatcherI km1 = new KeyedMatcher("AF", new Matcher(
42             Condition.Contains, "dog"));
43
44     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
45             : "showers";
46     assertTrue(km1.matches(vp));
47
48     // condition 2: CSQ value does not contain "how" (does not match)
49     KeyedMatcherI km2 = km1.and("CSQ", new Matcher(Condition.NotContains,
50             "how"));
51     assertFalse(km2.matches(vp));
52   }
53
54   @Test
55   public void testToString()
56   {
57     KeyedMatcherI km = new KeyedMatcher("AF",
58             new Matcher(Condition.LT, 1.2f));
59     assertEquals(km.toString(), "AF LT 1.2");
60
61     /*
62      * add an AND condition
63      */
64     km = km.and("CLIN_SIG", new Matcher(Condition.NotContains, "path"));
65     assertEquals(km.toString(), "CLIN_SIG NotContains PATH AND (AF LT 1.2)");
66
67     /*
68      * add an OR condition
69      */
70     km = km.or("CSQ", new Matcher(Condition.Contains, "benign"));
71     assertEquals(km.toString(),
72             "CSQ Contains BENIGN OR (CLIN_SIG NotContains PATH AND (AF LT 1.2))");
73   }
74
75   @Test
76   public void testOr()
77   {
78     // condition1: AF value contains "dog" (matches)
79     KeyedMatcherI km1 = new KeyedMatcher("AF", new Matcher(
80             Condition.Contains, "dog"));
81   
82     Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
83             : "showers";
84     assertTrue(km1.matches(vp));
85   
86     // condition 2: CSQ value does not contain "how" (does not match)
87     // the OR combination still passes
88     KeyedMatcherI km2 = km1.or("CSQ", new Matcher(Condition.NotContains,
89             "how"));
90     assertTrue(km2.matches(vp));
91   }
92 }