X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fdatamodel%2Ffeatures%2FFeatureMatcherTest.java;h=4bd34cbf0997d570893074d786b6885f2f333fd0;hb=14b1b2a878190d5fffda298c4b9a901c72c74ad3;hp=62b03a3f0c1547dd9c93827f8c22491c674cbf1b;hpb=4073e96c3cb60f28c01cd91ec7f847cafbf01f78;p=jalview.git diff --git a/test/jalview/datamodel/features/FeatureMatcherTest.java b/test/jalview/datamodel/features/FeatureMatcherTest.java index 62b03a3..4bd34cb 100644 --- a/test/jalview/datamodel/features/FeatureMatcherTest.java +++ b/test/jalview/datamodel/features/FeatureMatcherTest.java @@ -3,6 +3,7 @@ package jalview.datamodel.features; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; import jalview.datamodel.SequenceFeature; @@ -15,7 +16,7 @@ import org.testng.annotations.Test; public class FeatureMatcherTest { - @Test + @Test(groups = "Functional") public void testMatches_byLabel() { SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11, @@ -62,7 +63,7 @@ public class FeatureMatcherTest FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf)); } - @Test + @Test(groups = "Functional") public void testMatches_byScore() { SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11, @@ -87,7 +88,8 @@ public class FeatureMatcherTest assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf)); } - @Test + + @Test(groups = "Functional") public void testMatches_byAttribute() { /* @@ -127,7 +129,7 @@ public class FeatureMatcherTest assertFalse(fm.matches(sf)); } - @Test + @Test(groups = "Functional") public void testToString() { Locale.setDefault(Locale.ENGLISH); @@ -162,7 +164,7 @@ public class FeatureMatcherTest MessageManager.getString("label.score") + " >= 12.2"); } - @Test + @Test(groups = "Functional") public void testGetAttribute() { FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2", @@ -184,7 +186,17 @@ public class FeatureMatcherTest assertNull(FeatureMatcher.byScore(Condition.LE, "-1").getAttribute()); } - @Test + @Test(groups = "Functional") + public void testIsByAttribute() + { + assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo") + .isByAttribute()); + assertFalse(FeatureMatcher.byScore(Condition.LE, "-1").isByAttribute()); + assertTrue(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC") + .isByAttribute()); + } + + @Test(groups = "Functional") public void testIsByLabel() { assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "foo") @@ -194,7 +206,7 @@ public class FeatureMatcherTest .isByLabel()); } - @Test + @Test(groups = "Functional") public void testIsByScore() { assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo") @@ -204,7 +216,7 @@ public class FeatureMatcherTest .isByScore()); } - @Test + @Test(groups = "Functional") public void testGetMatcher() { FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f", @@ -213,4 +225,128 @@ public class FeatureMatcherTest assertEquals(fm.getMatcher().getFloatValue(), -2F); assertEquals(fm.getMatcher().getPattern(), "-2.0"); } + + @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" }); + assertSame(Condition.LT, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getFloatValue(), 1.2f); + assertEquals(fm.getMatcher().getPattern(), "1.2"); + + // quotes are optional, condition is not case sensitive + fm = FeatureMatcher.fromString("AF lt '1.2'"); + assertFalse(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertEquals(fm.getAttribute(), new String[] { "AF" }); + assertSame(Condition.LT, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getFloatValue(), 1.2f); + assertEquals(fm.getMatcher().getPattern(), "1.2"); + + fm = FeatureMatcher.fromString("'AF' Present"); + assertFalse(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertEquals(fm.getAttribute(), new String[] { "AF" }); + assertSame(Condition.Present, fm.getMatcher().getCondition()); + + fm = FeatureMatcher.fromString("CSQ:Consequence contains damaging"); + assertFalse(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" }); + assertSame(Condition.Contains, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "damaging"); + + // keyword Label is not case sensitive + fm = FeatureMatcher.fromString("LABEL Matches 'foobar'"); + assertTrue(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertNull(fm.getAttribute()); + assertSame(Condition.Matches, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "foobar"); + + fm = FeatureMatcher.fromString("'Label' matches 'foo bar'"); + assertTrue(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertNull(fm.getAttribute()); + assertSame(Condition.Matches, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "foo bar"); + + // quotes optional on pattern + fm = FeatureMatcher.fromString("'Label' matches foo bar"); + assertTrue(fm.isByLabel()); + assertFalse(fm.isByScore()); + assertNull(fm.getAttribute()); + assertSame(Condition.Matches, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "foo bar"); + + fm = FeatureMatcher.fromString("Score GE 12.2"); + assertFalse(fm.isByLabel()); + assertTrue(fm.isByScore()); + assertNull(fm.getAttribute()); + assertSame(Condition.GE, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "12.2"); + assertEquals(fm.getMatcher().getFloatValue(), 12.2f); + + // keyword Score is not case sensitive + fm = FeatureMatcher.fromString("'SCORE' ge '12.2'"); + assertFalse(fm.isByLabel()); + assertTrue(fm.isByScore()); + assertNull(fm.getAttribute()); + assertSame(Condition.GE, fm.getMatcher().getCondition()); + assertEquals(fm.getMatcher().getPattern(), "12.2"); + assertEquals(fm.getMatcher().getFloatValue(), 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"); + } }