X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fdatamodel%2Ffeatures%2FFeatureMatcherSetTest.java;h=47c47a9f674344ee856c27f3c4c114cae1b53a60;hb=57738a1f3c19b1c3a00bd3ac5108f8cd0af32f99;hp=a98013b6e9d66ed4d1fb0d83db38e31507c63ef0;hpb=1aa039ed9dfe0528cb7b40231734cf3004452c39;p=jalview.git diff --git a/test/jalview/datamodel/features/FeatureMatcherSetTest.java b/test/jalview/datamodel/features/FeatureMatcherSetTest.java index a98013b..47c47a9 100644 --- a/test/jalview/datamodel/features/FeatureMatcherSetTest.java +++ b/test/jalview/datamodel/features/FeatureMatcherSetTest.java @@ -2,6 +2,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 static org.testng.Assert.fail; @@ -11,6 +12,7 @@ import jalview.util.matcher.Condition; import java.util.HashMap; import java.util.Iterator; +import java.util.Locale; import java.util.Map; import org.testng.annotations.Test; @@ -102,7 +104,8 @@ public class FeatureMatcherSetTest * OR failed attribute and score conditions with matched label condition */ fms = new FeatureMatcherSet(); - fms.or(fm2).or(byScoreFail); + fms.or(fm2); + fms.or(byScoreFail); assertFalse(fms.matches(sf)); fms.or(byLabelPass); assertTrue(fms.matches(sf)); @@ -111,14 +114,14 @@ public class FeatureMatcherSetTest @Test(groups = "Functional") public void testToString() { + Locale.setDefault(Locale.ENGLISH); FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.LT, "1.2", "AF"); assertEquals(fm1.toString(), "AF < 1.2"); FeatureMatcher fm2 = FeatureMatcher.byAttribute(Condition.NotContains, - "path", - "CLIN_SIG"); - assertEquals(fm2.toString(), "CLIN_SIG Does not contain 'PATH'"); + "path", "CLIN_SIG"); + assertEquals(fm2.toString(), "CLIN_SIG does not contain 'path'"); /* * AND them @@ -126,10 +129,10 @@ public class FeatureMatcherSetTest FeatureMatcherSetI fms = new FeatureMatcherSet(); assertEquals(fms.toString(), ""); fms.and(fm1); - assertEquals(fms.toString(), "(AF < 1.2)"); + assertEquals(fms.toString(), "AF < 1.2"); fms.and(fm2); assertEquals(fms.toString(), - "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')"); + "(AF < 1.2) and (CLIN_SIG does not contain 'path')"); /* * OR them @@ -137,10 +140,10 @@ public class FeatureMatcherSetTest fms = new FeatureMatcherSet(); assertEquals(fms.toString(), ""); fms.or(fm1); - assertEquals(fms.toString(), "(AF < 1.2)"); + assertEquals(fms.toString(), "AF < 1.2"); fms.or(fm2); assertEquals(fms.toString(), - "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')"); + "(AF < 1.2) or (CLIN_SIG does not contain 'path')"); try { @@ -237,8 +240,8 @@ public class FeatureMatcherSetTest } /** - * Tests for the 'compound attribute' key i.e. where first key's value is a map - * from which we take the value for the second key, e.g. CSQ : Consequence + * Tests for the 'compound attribute' key i.e. where first key's value is a + * map from which we take the value for the second key, e.g. CSQ : Consequence */ @Test(groups = "Functional") public void testMatches_compoundKey() @@ -265,7 +268,7 @@ public class FeatureMatcherSetTest assertFalse(fms.matches(sf)); csq.put("Consequence", "junk"); assertFalse(fms.matches(sf)); - + /* * a string pattern matcher */ @@ -281,4 +284,136 @@ public class FeatureMatcherSetTest csq.put("Consequence", "Catastrophic"); assertTrue(fms.matches(sf)); } + + /** + * Tests for toStableString which (unlike toString) does not i18n the + * conditions + * + * @see FeatureMatcherTest#testToStableString() + */ + @Test(groups = "Functional") + public void testToStableString() + { + FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.LT, "1.2", + "AF"); + assertEquals(fm1.toStableString(), "AF LT 1.2"); + + FeatureMatcher fm2 = FeatureMatcher.byAttribute(Condition.NotContains, + "path", "CLIN_SIG"); + assertEquals(fm2.toStableString(), "CLIN_SIG NotContains path"); + + /* + * AND them + */ + FeatureMatcherSetI fms = new FeatureMatcherSet(); + assertEquals(fms.toStableString(), ""); + fms.and(fm1); + // no brackets needed if a single condition + assertEquals(fms.toStableString(), "AF LT 1.2"); + // brackets if more than one condition + fms.and(fm2); + assertEquals(fms.toStableString(), + "(AF LT 1.2) AND (CLIN_SIG NotContains path)"); + + /* + * OR them + */ + fms = new FeatureMatcherSet(); + assertEquals(fms.toStableString(), ""); + fms.or(fm1); + assertEquals(fms.toStableString(), "AF LT 1.2"); + fms.or(fm2); + assertEquals(fms.toStableString(), + "(AF LT 1.2) OR (CLIN_SIG NotContains path)"); + + /* + * attribute or value including space is quoted + */ + FeatureMatcher fm3 = FeatureMatcher.byAttribute(Condition.NotMatches, + "foo bar", "CSQ", "Poly Phen"); + assertEquals(fm3.toStableString(), + "'CSQ:Poly Phen' NotMatches 'foo bar'"); + fms.or(fm3); + assertEquals(fms.toStableString(), + "(AF LT 1.2) OR (CLIN_SIG NotContains path) OR ('CSQ:Poly Phen' NotMatches 'foo bar')"); + + try + { + fms.and(fm1); + fail("Expected exception"); + } catch (IllegalStateException e) + { + // expected + } + } + + /** + * Tests for parsing a string representation of a FeatureMatcherSet + * + * @see FeatureMatcherSetTest#testToStableString() + */ + @Test(groups = "Functional") + public void testFromString() + { + String descriptor = "AF LT 1.2"; + FeatureMatcherSetI fms = FeatureMatcherSet.fromString(descriptor); + + /* + * shortcut asserts by verifying a 'roundtrip', + * which we trust if other tests pass :-) + */ + assertEquals(fms.toStableString(), descriptor); + + // brackets optional, quotes optional, condition case insensitive + fms = FeatureMatcherSet.fromString("('AF' lt '1.2')"); + assertEquals(fms.toStableString(), descriptor); + + descriptor = "(AF LT 1.2) AND (CLIN_SIG NotContains path)"; + fms = FeatureMatcherSet.fromString(descriptor); + assertEquals(fms.toStableString(), descriptor); + + // AND is not case-sensitive + fms = FeatureMatcherSet + .fromString("(AF LT 1.2) and (CLIN_SIG NotContains path)"); + assertEquals(fms.toStableString(), descriptor); + + descriptor = "(AF LT 1.2) OR (CLIN_SIG NotContains path)"; + fms = FeatureMatcherSet.fromString(descriptor); + assertEquals(fms.toStableString(), descriptor); + + // OR is not case-sensitive + fms = FeatureMatcherSet + .fromString("(AF LT 1.2) or (CLIN_SIG NotContains path)"); + assertEquals(fms.toStableString(), descriptor); + + // can get away without brackets on last match condition + fms = FeatureMatcherSet + .fromString("(AF LT 1.2) or CLIN_SIG NotContains path"); + assertEquals(fms.toStableString(), descriptor); + + descriptor = "(AF LT 1.2) OR (CLIN_SIG NotContains path) OR ('CSQ:Poly Phen' NotMatches 'foo bar')"; + fms = FeatureMatcherSet.fromString(descriptor); + assertEquals(fms.toStableString(), descriptor); + + // can't mix OR and AND + descriptor = "(AF LT 1.2) OR (CLIN_SIG NotContains path) AND ('CSQ:Poly Phen' NotMatches 'foo bar')"; + assertNull(FeatureMatcherSet.fromString(descriptor)); + + // can't mix AND and OR + descriptor = "(AF LT 1.2) and (CLIN_SIG NotContains path) or ('CSQ:Poly Phen' NotMatches 'foo bar')"; + assertNull(FeatureMatcherSet.fromString(descriptor)); + + // brackets missing + assertNull(FeatureMatcherSet + .fromString("AF LT 1.2 or CLIN_SIG NotContains path")); + + // invalid conjunction + assertNull(FeatureMatcherSet.fromString("(AF LT 1.2) but (AF GT -2)")); + + // unbalanced quote (1) + assertNull(FeatureMatcherSet.fromString("('AF lt '1.2')")); + + // unbalanced quote (2) + assertNull(FeatureMatcherSet.fromString("('AF' lt '1.2)")); + } }