1 package jalview.datamodel.features;
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.assertTrue;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.util.MessageManager;
10 import jalview.util.matcher.Condition;
12 import java.util.Locale;
14 import org.testng.annotations.Test;
16 public class FeatureMatcherTest
19 public void testMatches_byLabel()
21 SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
25 * contains - not case sensitive
28 FeatureMatcher.byLabel(Condition.Contains, "IS").matches(sf));
29 assertTrue(FeatureMatcher.byLabel(Condition.Contains, "").matches(sf));
31 FeatureMatcher.byLabel(Condition.Contains, "ISNT").matches(sf));
36 assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "isnt")
38 assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "is")
44 assertTrue(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY label")
46 assertFalse(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY")
52 assertFalse(FeatureMatcher
53 .byLabel(Condition.NotMatches, "THIS is MY label").matches(sf));
54 assertTrue(FeatureMatcher.byLabel(Condition.NotMatches, "THIS is MY")
58 * is present / not present
60 assertTrue(FeatureMatcher.byLabel(Condition.Present, "").matches(sf));
62 FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf));
66 public void testMatches_byScore()
68 SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
71 assertTrue(FeatureMatcher.byScore(Condition.LT, "3.3").matches(sf));
72 assertFalse(FeatureMatcher.byScore(Condition.LT, "3.2").matches(sf));
73 assertFalse(FeatureMatcher.byScore(Condition.LT, "2.2").matches(sf));
75 assertTrue(FeatureMatcher.byScore(Condition.LE, "3.3").matches(sf));
76 assertTrue(FeatureMatcher.byScore(Condition.LE, "3.2").matches(sf));
77 assertFalse(FeatureMatcher.byScore(Condition.LE, "2.2").matches(sf));
79 assertFalse(FeatureMatcher.byScore(Condition.EQ, "3.3").matches(sf));
80 assertTrue(FeatureMatcher.byScore(Condition.EQ, "3.2").matches(sf));
82 assertFalse(FeatureMatcher.byScore(Condition.GE, "3.3").matches(sf));
83 assertTrue(FeatureMatcher.byScore(Condition.GE, "3.2").matches(sf));
84 assertTrue(FeatureMatcher.byScore(Condition.GE, "2.2").matches(sf));
86 assertFalse(FeatureMatcher.byScore(Condition.GT, "3.3").matches(sf));
87 assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf));
88 assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf));
91 public void testMatches_byAttribute()
94 * a numeric matcher - MatcherTest covers more conditions
96 FeatureMatcherI fm = FeatureMatcher
97 .byAttribute(Condition.GE, "-2", "AF");
98 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
99 assertFalse(fm.matches(sf));
100 sf.setValue("AF", "foobar");
101 assertFalse(fm.matches(sf));
102 sf.setValue("AF", "-2");
103 assertTrue(fm.matches(sf));
104 sf.setValue("AF", "-1");
105 assertTrue(fm.matches(sf));
106 sf.setValue("AF", "-3");
107 assertFalse(fm.matches(sf));
108 sf.setValue("AF", "");
109 assertFalse(fm.matches(sf));
112 * a string pattern matcher
114 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
115 assertFalse(fm.matches(sf));
116 sf.setValue("AF", "raining cats and dogs");
117 assertTrue(fm.matches(sf));
119 fm = FeatureMatcher.byAttribute(Condition.Present, "", "AC");
120 assertFalse(fm.matches(sf));
121 sf.setValue("AC", "21");
122 assertTrue(fm.matches(sf));
124 fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AC_Females");
125 assertTrue(fm.matches(sf));
126 sf.setValue("AC_Females", "21");
127 assertFalse(fm.matches(sf));
131 public void testToString()
133 Locale.setDefault(Locale.ENGLISH);
136 * toString uses the i18n translation of the enum conditions
138 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
140 assertEquals(fm.toString(), "AF < 1.2");
143 * Present / NotPresent omit the value pattern
145 fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
146 assertEquals(fm.toString(), "AF is present");
147 fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
148 assertEquals(fm.toString(), "AF is not present");
153 fm = FeatureMatcher.byLabel(Condition.Matches, "foobar");
154 assertEquals(fm.toString(),
155 MessageManager.getString("label.label") + " matches 'foobar'");
160 fm = FeatureMatcher.byScore(Condition.GE, "12.2");
161 assertEquals(fm.toString(),
162 MessageManager.getString("label.score") + " >= 12.2");
166 public void testGetAttribute()
168 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
170 assertEquals(fm.getAttribute(), new String[] { "AF" });
173 * compound key (attribute / subattribute)
175 fm = FeatureMatcher.byAttribute(Condition.GE, "-2F", "CSQ",
177 assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" });
180 * answers null if match is by Label or by Score
182 assertNull(FeatureMatcher.byLabel(Condition.NotContains, "foo")
184 assertNull(FeatureMatcher.byScore(Condition.LE, "-1").getAttribute());
188 public void testIsByLabel()
190 assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "foo")
192 assertFalse(FeatureMatcher.byScore(Condition.LE, "-1").isByLabel());
193 assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
198 public void testIsByScore()
200 assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo")
202 assertTrue(FeatureMatcher.byScore(Condition.LE, "-1").isByScore());
203 assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
208 public void testGetMatcher()
210 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f",
212 assertEquals(fm.getMatcher().getCondition(), Condition.GE);
213 assertEquals(fm.getMatcher().getFloatValue(), -2F);
214 assertEquals(fm.getMatcher().getPattern(), "-2.0");