package jalview.datamodel.features; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import jalview.datamodel.SequenceFeature; import jalview.util.matcher.Condition; import org.testng.annotations.Test; public class FeatureMatcherTest { @Test public void testMatches_byLabel() { SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11, 12, "grp"); /* * contains - not case sensitive */ assertTrue( FeatureMatcher.byLabel(Condition.Contains, "IS").matches(sf)); assertTrue(FeatureMatcher.byLabel(Condition.Contains, "").matches(sf)); assertFalse( FeatureMatcher.byLabel(Condition.Contains, "ISNT").matches(sf)); /* * does not contain */ assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "isnt") .matches(sf)); assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "is") .matches(sf)); /* * matches */ assertTrue(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY label") .matches(sf)); assertFalse(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY") .matches(sf)); /* * does not match */ assertFalse(FeatureMatcher .byLabel(Condition.NotMatches, "THIS is MY label").matches(sf)); assertTrue(FeatureMatcher.byLabel(Condition.NotMatches, "THIS is MY") .matches(sf)); /* * is present / not present */ assertTrue(FeatureMatcher.byLabel(Condition.Present, "").matches(sf)); assertFalse( FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf)); } @Test public void testMatches_byScore() { SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11, 12, 3.2f, "grp"); assertTrue(FeatureMatcher.byScore(Condition.LT, "3.3").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.LT, "3.2").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.LT, "2.2").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.LE, "3.3").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.LE, "3.2").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.LE, "2.2").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.EQ, "3.3").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.EQ, "3.2").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.GE, "3.3").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.GE, "3.2").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.GE, "2.2").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.GT, "3.3").matches(sf)); assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf)); assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf)); } @Test public void testMatches_byAttribute() { /* * a numeric matcher - MatcherTest covers more conditions */ FeatureMatcherI fm = FeatureMatcher .byAttribute(Condition.GE, "-2", "AF"); SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp"); assertFalse(fm.matches(sf)); sf.setValue("AF", "foobar"); assertFalse(fm.matches(sf)); sf.setValue("AF", "-2"); assertTrue(fm.matches(sf)); sf.setValue("AF", "-1"); assertTrue(fm.matches(sf)); sf.setValue("AF", "-3"); assertFalse(fm.matches(sf)); sf.setValue("AF", ""); assertFalse(fm.matches(sf)); /* * a string pattern matcher */ fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF"); assertFalse(fm.matches(sf)); sf.setValue("AF", "raining cats and dogs"); assertTrue(fm.matches(sf)); fm = FeatureMatcher.byAttribute(Condition.Present, "", "AC"); assertFalse(fm.matches(sf)); sf.setValue("AC", "21"); assertTrue(fm.matches(sf)); fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AC_Females"); assertTrue(fm.matches(sf)); sf.setValue("AC_Females", "21"); assertFalse(fm.matches(sf)); } @Test public void testToString() { /* * toString uses the i18n translation of the enum conditions */ FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2", "AF"); assertEquals(fm.toString(), "AF < 1.2"); /* * Present / NotPresent omit the value pattern */ fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF"); assertEquals(fm.toString(), "AF Is present"); fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF"); assertEquals(fm.toString(), "AF Is not present"); } @Test public void testGetKey() { FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2", "AF"); assertEquals(fm.getKey(), new String[] { "AF" }); /* * compound key (attribute / subattribute) */ fm = FeatureMatcher.byAttribute(Condition.GE, "-2F", "CSQ", "Consequence"); assertEquals(fm.getKey(), new String[] { "CSQ", "Consequence" }); } @Test public void testGetMatcher() { FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f", "AF"); assertEquals(fm.getMatcher().getCondition(), Condition.GE); assertEquals(fm.getMatcher().getFloatValue(), -2F); assertEquals(fm.getMatcher().getPattern(), "-2.0"); } }