JAL-2808 update spike to latest (filter range tooltip, Present condition)
[jalview.git] / test / jalview / datamodel / features / FeatureMatcherTest.java
1 package jalview.datamodel.features;
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 jalview.datamodel.SequenceFeature;
8 import jalview.util.matcher.Condition;
9
10 import org.testng.annotations.Test;
11
12 public class FeatureMatcherTest
13 {
14   @Test
15   public void testMatches_byLabel()
16   {
17     SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
18             12, "grp");
19
20     /*
21      * contains - not case sensitive
22      */
23     assertTrue(
24             FeatureMatcher.byLabel(Condition.Contains, "IS").matches(sf));
25     assertTrue(FeatureMatcher.byLabel(Condition.Contains, "").matches(sf));
26     assertFalse(
27             FeatureMatcher.byLabel(Condition.Contains, "ISNT").matches(sf));
28
29     /*
30      * does not contain
31      */
32     assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "isnt")
33             .matches(sf));
34     assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "is")
35             .matches(sf));
36
37     /*
38      * matches
39      */
40     assertTrue(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY label")
41             .matches(sf));
42     assertFalse(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY")
43             .matches(sf));
44
45     /*
46      * does not match
47      */
48     assertFalse(FeatureMatcher
49             .byLabel(Condition.NotMatches, "THIS is MY label").matches(sf));
50     assertTrue(FeatureMatcher.byLabel(Condition.NotMatches, "THIS is MY")
51             .matches(sf));
52
53     /*
54      * is present / not present
55      */
56     assertTrue(FeatureMatcher.byLabel(Condition.Present, "").matches(sf));
57     assertFalse(
58             FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf));
59   }
60
61   @Test
62   public void testMatches_byScore()
63   {
64     SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
65             12, 3.2f, "grp");
66
67     assertTrue(FeatureMatcher.byScore(Condition.LT, "3.3").matches(sf));
68     assertFalse(FeatureMatcher.byScore(Condition.LT, "3.2").matches(sf));
69     assertFalse(FeatureMatcher.byScore(Condition.LT, "2.2").matches(sf));
70
71     assertTrue(FeatureMatcher.byScore(Condition.LE, "3.3").matches(sf));
72     assertTrue(FeatureMatcher.byScore(Condition.LE, "3.2").matches(sf));
73     assertFalse(FeatureMatcher.byScore(Condition.LE, "2.2").matches(sf));
74
75     assertFalse(FeatureMatcher.byScore(Condition.EQ, "3.3").matches(sf));
76     assertTrue(FeatureMatcher.byScore(Condition.EQ, "3.2").matches(sf));
77
78     assertFalse(FeatureMatcher.byScore(Condition.GE, "3.3").matches(sf));
79     assertTrue(FeatureMatcher.byScore(Condition.GE, "3.2").matches(sf));
80     assertTrue(FeatureMatcher.byScore(Condition.GE, "2.2").matches(sf));
81
82     assertFalse(FeatureMatcher.byScore(Condition.GT, "3.3").matches(sf));
83     assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf));
84     assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf));
85   }
86   @Test
87   public void testMatches_byAttribute()
88   {
89     /*
90      * a numeric matcher - MatcherTest covers more conditions
91      */
92     FeatureMatcherI fm = FeatureMatcher
93             .byAttribute(Condition.GE, "-2", "AF");
94     SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
95     assertFalse(fm.matches(sf));
96     sf.setValue("AF", "foobar");
97     assertFalse(fm.matches(sf));
98     sf.setValue("AF", "-2");
99     assertTrue(fm.matches(sf));
100     sf.setValue("AF", "-1");
101     assertTrue(fm.matches(sf));
102     sf.setValue("AF", "-3");
103     assertFalse(fm.matches(sf));
104     sf.setValue("AF", "");
105     assertFalse(fm.matches(sf));
106
107     /*
108      * a string pattern matcher
109      */
110     fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
111     assertFalse(fm.matches(sf));
112     sf.setValue("AF", "raining cats and dogs");
113     assertTrue(fm.matches(sf));
114
115     fm = FeatureMatcher.byAttribute(Condition.Present, "", "AC");
116     assertFalse(fm.matches(sf));
117     sf.setValue("AC", "21");
118     assertTrue(fm.matches(sf));
119
120     fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AC_Females");
121     assertTrue(fm.matches(sf));
122     sf.setValue("AC_Females", "21");
123     assertFalse(fm.matches(sf));
124   }
125
126   @Test
127   public void testToString()
128   {
129     /*
130      * toString uses the i18n translation of the enum conditions
131      */
132     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
133             "AF");
134     assertEquals(fm.toString(), "AF < 1.2");
135
136     /*
137      * Present / NotPresent omit the value pattern
138      */
139     fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
140     assertEquals(fm.toString(), "AF Is present");
141     fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
142     assertEquals(fm.toString(), "AF Is not present");
143   }
144
145   @Test
146   public void testGetKey()
147   {
148     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
149             "AF");
150     assertEquals(fm.getKey(), new String[] { "AF" });
151
152     /*
153      * compound key (attribute / subattribute)
154      */
155     fm = FeatureMatcher.byAttribute(Condition.GE, "-2F", "CSQ",
156             "Consequence");
157     assertEquals(fm.getKey(), new String[] { "CSQ", "Consequence" });
158   }
159
160   @Test
161   public void testGetMatcher()
162   {
163     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f",
164             "AF");
165     assertEquals(fm.getMatcher().getCondition(), Condition.GE);
166     assertEquals(fm.getMatcher().getFloatValue(), -2F);
167     assertEquals(fm.getMatcher().getPattern(), "-2.0");
168   }
169 }