+
+ @Test(groups = "Functional")
+ public void testFromString()
+ {
+ FeatureMatcherI fm = FeatureMatcher.fromString("'AF' LT 1.2");
+ assertFalse(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertEquals(fm.getAttribute(), new String[] { "AF" });
+ MatcherI matcher = fm.getMatcher();
+ assertSame(Condition.LT, matcher.getCondition());
+ assertEquals(PA.getValue(matcher, "floatValue"), 1.2f);
+ assertSame(PA.getValue(matcher, "patternType"),
+ Matcher.PatternType.Float);
+ assertEquals(matcher.getPattern(), "1.2");
+
+ // quotes are optional, condition is not case sensitive
+ fm = FeatureMatcher.fromString("AF lt '1.2'");
+ matcher = fm.getMatcher();
+ assertFalse(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertEquals(fm.getAttribute(), new String[] { "AF" });
+ assertSame(Condition.LT, matcher.getCondition());
+ assertEquals(PA.getValue(matcher, "floatValue"), 1.2F);
+ assertEquals(matcher.getPattern(), "1.2");
+
+ fm = FeatureMatcher.fromString("'AF' Present");
+ matcher = fm.getMatcher();
+ assertFalse(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertEquals(fm.getAttribute(), new String[] { "AF" });
+ assertSame(Condition.Present, matcher.getCondition());
+ assertSame(PA.getValue(matcher, "patternType"),
+ Matcher.PatternType.String);
+
+ fm = FeatureMatcher.fromString("CSQ:Consequence contains damaging");
+ matcher = fm.getMatcher();
+ assertFalse(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" });
+ assertSame(Condition.Contains, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "damaging");
+
+ // keyword Label is not case sensitive
+ fm = FeatureMatcher.fromString("LABEL Matches 'foobar'");
+ matcher = fm.getMatcher();
+ assertTrue(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertNull(fm.getAttribute());
+ assertSame(Condition.Matches, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "foobar");
+
+ fm = FeatureMatcher.fromString("'Label' matches 'foo bar'");
+ matcher = fm.getMatcher();
+ assertTrue(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertNull(fm.getAttribute());
+ assertSame(Condition.Matches, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "foo bar");
+
+ // quotes optional on pattern
+ fm = FeatureMatcher.fromString("'Label' matches foo bar");
+ matcher = fm.getMatcher();
+ assertTrue(fm.isByLabel());
+ assertFalse(fm.isByScore());
+ assertNull(fm.getAttribute());
+ assertSame(Condition.Matches, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "foo bar");
+
+ // integer condition
+ fm = FeatureMatcher.fromString("Score GE 12");
+ matcher = fm.getMatcher();
+ assertFalse(fm.isByLabel());
+ assertTrue(fm.isByScore());
+ assertNull(fm.getAttribute());
+ assertSame(Condition.GE, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "12");
+ assertEquals(PA.getValue(matcher, "floatValue"), 0f);
+ assertEquals(PA.getValue(matcher, "longValue"), 12L);
+ assertSame(PA.getValue(matcher, "patternType"),
+ Matcher.PatternType.Integer);
+
+ // keyword Score is not case sensitive
+ fm = FeatureMatcher.fromString("'SCORE' ge '12.2'");
+ matcher = fm.getMatcher();
+ assertFalse(fm.isByLabel());
+ assertTrue(fm.isByScore());
+ assertNull(fm.getAttribute());
+ assertSame(Condition.GE, matcher.getCondition());
+ assertEquals(matcher.getPattern(), "12.2");
+ assertEquals(PA.getValue(matcher, "floatValue"), 12.2F);
+
+ // invalid numeric pattern
+ assertNull(FeatureMatcher.fromString("Score eq twelve"));
+ // unbalanced opening quote
+ assertNull(FeatureMatcher.fromString("'Score ge 12.2"));
+ // unbalanced pattern quote
+ assertNull(FeatureMatcher.fromString("'Score' ge '12.2"));
+ // pattern missing
+ assertNull(FeatureMatcher.fromString("Score ge"));
+ // condition and pattern missing
+ assertNull(FeatureMatcher.fromString("Score"));
+ // everything missing
+ assertNull(FeatureMatcher.fromString(""));
+ }
+
+ /**
+ * Tests for toStableString which (unlike toString) does not i18n the
+ * conditions
+ */
+ @Test(groups = "Functional")
+ public void testToStableString()
+ {
+ // attribute name not quoted unless it contains space
+ FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
+ "AF");
+ assertEquals(fm.toStableString(), "AF LT 1.2");
+
+ /*
+ * Present / NotPresent omit the value pattern
+ */
+ fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
+ assertEquals(fm.toStableString(), "AF Present");
+ fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
+ assertEquals(fm.toStableString(), "AF NotPresent");
+
+ /*
+ * by Label
+ * pattern not quoted unless it contains space
+ */
+ fm = FeatureMatcher.byLabel(Condition.Matches, "foobar");
+ assertEquals(fm.toStableString(), "Label Matches foobar");
+
+ fm = FeatureMatcher.byLabel(Condition.Matches, "foo bar");
+ assertEquals(fm.toStableString(), "Label Matches 'foo bar'");
+
+ /*
+ * by Score
+ */
+ fm = FeatureMatcher.byScore(Condition.GE, "12.2");
+ assertEquals(fm.toStableString(), "Score GE 12.2");
+ }