Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel.features

File FeatureMatcherTest.java

 

Code metrics

0
157
11
1
352
241
11
0.07
14.27
11
1

Classes

Class Line # Actions
FeatureMatcherTest 17 157 11 0
1.0100%
 

Contributing tests

This file is covered by 11 tests. .

Source view

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.assertNull;
6    import static org.testng.Assert.assertSame;
7    import static org.testng.Assert.assertTrue;
8   
9    import jalview.datamodel.SequenceFeature;
10    import jalview.util.MessageManager;
11    import jalview.util.matcher.Condition;
12   
13    import java.util.Locale;
14   
15    import org.testng.annotations.Test;
16   
 
17    public class FeatureMatcherTest
18    {
 
19  1 toggle @Test(groups = "Functional")
20    public void testMatches_byLabel()
21    {
22  1 SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
23    12, "grp");
24   
25    /*
26    * contains - not case sensitive
27    */
28  1 assertTrue(
29    FeatureMatcher.byLabel(Condition.Contains, "IS").matches(sf));
30  1 assertTrue(FeatureMatcher.byLabel(Condition.Contains, "").matches(sf));
31  1 assertFalse(
32    FeatureMatcher.byLabel(Condition.Contains, "ISNT").matches(sf));
33   
34    /*
35    * does not contain
36    */
37  1 assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "isnt")
38    .matches(sf));
39  1 assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "is")
40    .matches(sf));
41   
42    /*
43    * matches
44    */
45  1 assertTrue(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY label")
46    .matches(sf));
47  1 assertFalse(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY")
48    .matches(sf));
49   
50    /*
51    * does not match
52    */
53  1 assertFalse(FeatureMatcher
54    .byLabel(Condition.NotMatches, "THIS is MY label").matches(sf));
55  1 assertTrue(FeatureMatcher.byLabel(Condition.NotMatches, "THIS is MY")
56    .matches(sf));
57   
58    /*
59    * is present / not present
60    */
61  1 assertTrue(FeatureMatcher.byLabel(Condition.Present, "").matches(sf));
62  1 assertFalse(
63    FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf));
64    }
65   
 
66  1 toggle @Test(groups = "Functional")
67    public void testMatches_byScore()
68    {
69  1 SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
70    12, 3.2f, "grp");
71   
72  1 assertTrue(FeatureMatcher.byScore(Condition.LT, "3.3").matches(sf));
73  1 assertFalse(FeatureMatcher.byScore(Condition.LT, "3.2").matches(sf));
74  1 assertFalse(FeatureMatcher.byScore(Condition.LT, "2.2").matches(sf));
75   
76  1 assertTrue(FeatureMatcher.byScore(Condition.LE, "3.3").matches(sf));
77  1 assertTrue(FeatureMatcher.byScore(Condition.LE, "3.2").matches(sf));
78  1 assertFalse(FeatureMatcher.byScore(Condition.LE, "2.2").matches(sf));
79   
80  1 assertFalse(FeatureMatcher.byScore(Condition.EQ, "3.3").matches(sf));
81  1 assertTrue(FeatureMatcher.byScore(Condition.EQ, "3.2").matches(sf));
82   
83  1 assertFalse(FeatureMatcher.byScore(Condition.GE, "3.3").matches(sf));
84  1 assertTrue(FeatureMatcher.byScore(Condition.GE, "3.2").matches(sf));
85  1 assertTrue(FeatureMatcher.byScore(Condition.GE, "2.2").matches(sf));
86   
87  1 assertFalse(FeatureMatcher.byScore(Condition.GT, "3.3").matches(sf));
88  1 assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf));
89  1 assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf));
90    }
91   
 
92  1 toggle @Test(groups = "Functional")
93    public void testMatches_byAttribute()
94    {
95    /*
96    * a numeric matcher - MatcherTest covers more conditions
97    */
98  1 FeatureMatcherI fm = FeatureMatcher
99    .byAttribute(Condition.GE, "-2", "AF");
100  1 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
101  1 assertFalse(fm.matches(sf));
102  1 sf.setValue("AF", "foobar");
103  1 assertFalse(fm.matches(sf));
104  1 sf.setValue("AF", "-2");
105  1 assertTrue(fm.matches(sf));
106  1 sf.setValue("AF", "-1");
107  1 assertTrue(fm.matches(sf));
108  1 sf.setValue("AF", "-3");
109  1 assertFalse(fm.matches(sf));
110  1 sf.setValue("AF", "");
111  1 assertFalse(fm.matches(sf));
112   
113    /*
114    * a string pattern matcher
115    */
116  1 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
117  1 assertFalse(fm.matches(sf));
118  1 sf.setValue("AF", "raining cats and dogs");
119  1 assertTrue(fm.matches(sf));
120   
121  1 fm = FeatureMatcher.byAttribute(Condition.Present, "", "AC");
122  1 assertFalse(fm.matches(sf));
123  1 sf.setValue("AC", "21");
124  1 assertTrue(fm.matches(sf));
125   
126  1 fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AC_Females");
127  1 assertTrue(fm.matches(sf));
128  1 sf.setValue("AC_Females", "21");
129  1 assertFalse(fm.matches(sf));
130    }
131   
 
132  1 toggle @Test(groups = "Functional")
133    public void testToString()
134    {
135  1 Locale.setDefault(Locale.ENGLISH);
136   
137    /*
138    * toString uses the i18n translation of the enum conditions
139    */
140  1 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
141    "AF");
142  1 assertEquals(fm.toString(), "AF < 1.2");
143   
144    /*
145    * Present / NotPresent omit the value pattern
146    */
147  1 fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
148  1 assertEquals(fm.toString(), "AF is present");
149  1 fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
150  1 assertEquals(fm.toString(), "AF is not present");
151   
152    /*
153    * by Label
154    */
155  1 fm = FeatureMatcher.byLabel(Condition.Matches, "foobar");
156  1 assertEquals(fm.toString(),
157    MessageManager.getString("label.label") + " matches 'foobar'");
158   
159    /*
160    * by Score
161    */
162  1 fm = FeatureMatcher.byScore(Condition.GE, "12.2");
163  1 assertEquals(fm.toString(),
164    MessageManager.getString("label.score") + " >= 12.2");
165    }
166   
 
167  1 toggle @Test(groups = "Functional")
168    public void testGetAttribute()
169    {
170  1 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
171    "AF");
172  1 assertEquals(fm.getAttribute(), new String[] { "AF" });
173   
174    /*
175    * compound key (attribute / subattribute)
176    */
177  1 fm = FeatureMatcher.byAttribute(Condition.GE, "-2F", "CSQ",
178    "Consequence");
179  1 assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" });
180   
181    /*
182    * answers null if match is by Label or by Score
183    */
184  1 assertNull(FeatureMatcher.byLabel(Condition.NotContains, "foo")
185    .getAttribute());
186  1 assertNull(FeatureMatcher.byScore(Condition.LE, "-1").getAttribute());
187    }
188   
 
189  1 toggle @Test(groups = "Functional")
190    public void testIsByAttribute()
191    {
192  1 assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo")
193    .isByAttribute());
194  1 assertFalse(FeatureMatcher.byScore(Condition.LE, "-1").isByAttribute());
195  1 assertTrue(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
196    .isByAttribute());
197    }
198   
 
199  1 toggle @Test(groups = "Functional")
200    public void testIsByLabel()
201    {
202  1 assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "foo")
203    .isByLabel());
204  1 assertFalse(FeatureMatcher.byScore(Condition.LE, "-1").isByLabel());
205  1 assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
206    .isByLabel());
207    }
208   
 
209  1 toggle @Test(groups = "Functional")
210    public void testIsByScore()
211    {
212  1 assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo")
213    .isByScore());
214  1 assertTrue(FeatureMatcher.byScore(Condition.LE, "-1").isByScore());
215  1 assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
216    .isByScore());
217    }
218   
 
219  1 toggle @Test(groups = "Functional")
220    public void testGetMatcher()
221    {
222  1 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f",
223    "AF");
224  1 assertEquals(fm.getMatcher().getCondition(), Condition.GE);
225  1 assertEquals(fm.getMatcher().getFloatValue(), -2F);
226  1 assertEquals(fm.getMatcher().getPattern(), "-2.0");
227    }
228   
 
229  1 toggle @Test(groups = "Functional")
230    public void testFromString()
231    {
232  1 FeatureMatcherI fm = FeatureMatcher.fromString("'AF' LT 1.2");
233  1 assertFalse(fm.isByLabel());
234  1 assertFalse(fm.isByScore());
235  1 assertEquals(fm.getAttribute(), new String[] { "AF" });
236  1 assertSame(Condition.LT, fm.getMatcher().getCondition());
237  1 assertEquals(fm.getMatcher().getFloatValue(), 1.2f);
238  1 assertEquals(fm.getMatcher().getPattern(), "1.2");
239   
240    // quotes are optional, condition is not case sensitive
241  1 fm = FeatureMatcher.fromString("AF lt '1.2'");
242  1 assertFalse(fm.isByLabel());
243  1 assertFalse(fm.isByScore());
244  1 assertEquals(fm.getAttribute(), new String[] { "AF" });
245  1 assertSame(Condition.LT, fm.getMatcher().getCondition());
246  1 assertEquals(fm.getMatcher().getFloatValue(), 1.2f);
247  1 assertEquals(fm.getMatcher().getPattern(), "1.2");
248   
249  1 fm = FeatureMatcher.fromString("'AF' Present");
250  1 assertFalse(fm.isByLabel());
251  1 assertFalse(fm.isByScore());
252  1 assertEquals(fm.getAttribute(), new String[] { "AF" });
253  1 assertSame(Condition.Present, fm.getMatcher().getCondition());
254   
255  1 fm = FeatureMatcher.fromString("CSQ:Consequence contains damaging");
256  1 assertFalse(fm.isByLabel());
257  1 assertFalse(fm.isByScore());
258  1 assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" });
259  1 assertSame(Condition.Contains, fm.getMatcher().getCondition());
260  1 assertEquals(fm.getMatcher().getPattern(), "damaging");
261   
262    // keyword Label is not case sensitive
263  1 fm = FeatureMatcher.fromString("LABEL Matches 'foobar'");
264  1 assertTrue(fm.isByLabel());
265  1 assertFalse(fm.isByScore());
266  1 assertNull(fm.getAttribute());
267  1 assertSame(Condition.Matches, fm.getMatcher().getCondition());
268  1 assertEquals(fm.getMatcher().getPattern(), "foobar");
269   
270  1 fm = FeatureMatcher.fromString("'Label' matches 'foo bar'");
271  1 assertTrue(fm.isByLabel());
272  1 assertFalse(fm.isByScore());
273  1 assertNull(fm.getAttribute());
274  1 assertSame(Condition.Matches, fm.getMatcher().getCondition());
275  1 assertEquals(fm.getMatcher().getPattern(), "foo bar");
276   
277    // quotes optional on pattern
278  1 fm = FeatureMatcher.fromString("'Label' matches foo bar");
279  1 assertTrue(fm.isByLabel());
280  1 assertFalse(fm.isByScore());
281  1 assertNull(fm.getAttribute());
282  1 assertSame(Condition.Matches, fm.getMatcher().getCondition());
283  1 assertEquals(fm.getMatcher().getPattern(), "foo bar");
284   
285  1 fm = FeatureMatcher.fromString("Score GE 12.2");
286  1 assertFalse(fm.isByLabel());
287  1 assertTrue(fm.isByScore());
288  1 assertNull(fm.getAttribute());
289  1 assertSame(Condition.GE, fm.getMatcher().getCondition());
290  1 assertEquals(fm.getMatcher().getPattern(), "12.2");
291  1 assertEquals(fm.getMatcher().getFloatValue(), 12.2f);
292   
293    // keyword Score is not case sensitive
294  1 fm = FeatureMatcher.fromString("'SCORE' ge '12.2'");
295  1 assertFalse(fm.isByLabel());
296  1 assertTrue(fm.isByScore());
297  1 assertNull(fm.getAttribute());
298  1 assertSame(Condition.GE, fm.getMatcher().getCondition());
299  1 assertEquals(fm.getMatcher().getPattern(), "12.2");
300  1 assertEquals(fm.getMatcher().getFloatValue(), 12.2f);
301   
302    // invalid numeric pattern
303  1 assertNull(FeatureMatcher.fromString("Score eq twelve"));
304    // unbalanced opening quote
305  1 assertNull(FeatureMatcher.fromString("'Score ge 12.2"));
306    // unbalanced pattern quote
307  1 assertNull(FeatureMatcher.fromString("'Score' ge '12.2"));
308    // pattern missing
309  1 assertNull(FeatureMatcher.fromString("Score ge"));
310    // condition and pattern missing
311  1 assertNull(FeatureMatcher.fromString("Score"));
312    // everything missing
313  1 assertNull(FeatureMatcher.fromString(""));
314    }
315   
316    /**
317    * Tests for toStableString which (unlike toString) does not i18n the
318    * conditions
319    */
 
320  1 toggle @Test(groups = "Functional")
321    public void testToStableString()
322    {
323    // attribute name not quoted unless it contains space
324  1 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
325    "AF");
326  1 assertEquals(fm.toStableString(), "AF LT 1.2");
327   
328    /*
329    * Present / NotPresent omit the value pattern
330    */
331  1 fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
332  1 assertEquals(fm.toStableString(), "AF Present");
333  1 fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
334  1 assertEquals(fm.toStableString(), "AF NotPresent");
335   
336    /*
337    * by Label
338    * pattern not quoted unless it contains space
339    */
340  1 fm = FeatureMatcher.byLabel(Condition.Matches, "foobar");
341  1 assertEquals(fm.toStableString(), "Label Matches foobar");
342   
343  1 fm = FeatureMatcher.byLabel(Condition.Matches, "foo bar");
344  1 assertEquals(fm.toStableString(), "Label Matches 'foo bar'");
345   
346    /*
347    * by Score
348    */
349  1 fm = FeatureMatcher.byScore(Condition.GE, "12.2");
350  1 assertEquals(fm.toStableString(), "Score GE 12.2");
351    }
352    }