import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import java.util.function.Function;
-
import org.testng.annotations.Test;
public class KeyedMatcherTest
/*
* a numeric matcher - MatcherTest covers more conditions
*/
- MatcherI m1 = new Matcher(Condition.GE, -2f);
- KeyedMatcherI km = new KeyedMatcher("AF", m1);
+ KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
assertTrue(km.matches(key -> "-2"));
assertTrue(km.matches(key -> "-1"));
assertFalse(km.matches(key -> "-3"));
/*
* a string pattern matcher
*/
- MatcherI m2 = new Matcher(Condition.Contains, "Cat");
- km = new KeyedMatcher("AF", m2);
- assertTrue(km.matches(key -> "AF".equals(key) ? "raining cats and dogs"
- : "showers"));
+ km = new KeyedMatcher(Condition.Contains, "Cat", "AF");
+ assertTrue(
+ km.matches(key -> "AF".equals(key[0]) ? "raining cats and dogs"
+ : "showers"));
}
@Test
- public void testAnd()
+ public void testToString()
{
- // condition1: AF value contains "dog" (matches)
- KeyedMatcherI km1 = new KeyedMatcher("AF", new Matcher(
- Condition.Contains, "dog"));
-
- Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
- : "showers";
- assertTrue(km1.matches(vp));
+ /*
+ * toString uses the i18n translation of the enum conditions
+ */
+ KeyedMatcherI km = new KeyedMatcher(Condition.LT, 1.2f, "AF");
+ assertEquals(km.toString(), "AF < 1.2");
- // condition 2: CSQ value does not contain "how" (does not match)
- KeyedMatcherI km2 = km1.and("CSQ", new Matcher(Condition.NotContains,
- "how"));
- assertFalse(km2.matches(vp));
+ /*
+ * Present / NotPresent omit the value pattern
+ */
+ km = new KeyedMatcher(Condition.Present, "", "AF");
+ assertEquals(km.toString(), "AF Is present");
+ km = new KeyedMatcher(Condition.NotPresent, "", "AF");
+ assertEquals(km.toString(), "AF Is not present");
}
@Test
- public void testToString()
+ public void testGetKey()
{
- KeyedMatcherI km = new KeyedMatcher("AF",
- new Matcher(Condition.LT, 1.2f));
- assertEquals(km.toString(), "AF LT 1.2");
-
- /*
- * add an AND condition
- */
- km = km.and("CLIN_SIG", new Matcher(Condition.NotContains, "path"));
- assertEquals(km.toString(), "CLIN_SIG NotContains PATH AND (AF LT 1.2)");
+ KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
+ assertEquals(km.getKey(), new String[] { "AF" });
/*
- * add an OR condition
+ * compound key (attribute / subattribute)
*/
- km = km.or("CSQ", new Matcher(Condition.Contains, "benign"));
- assertEquals(km.toString(),
- "CSQ Contains BENIGN OR (CLIN_SIG NotContains PATH AND (AF LT 1.2))");
+ km = new KeyedMatcher(Condition.GE, -2F, "CSQ", "Consequence");
+ assertEquals(km.getKey(), new String[] { "CSQ", "Consequence" });
}
@Test
- public void testOr()
+ public void testGetMatcher()
{
- // condition1: AF value contains "dog" (matches)
- KeyedMatcherI km1 = new KeyedMatcher("AF", new Matcher(
- Condition.Contains, "dog"));
-
- Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
- : "showers";
- assertTrue(km1.matches(vp));
-
- // condition 2: CSQ value does not contain "how" (does not match)
- // the OR combination still passes
- KeyedMatcherI km2 = km1.or("CSQ", new Matcher(Condition.NotContains,
- "how"));
- assertTrue(km2.matches(vp));
+ KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
+ assertEquals(km.getMatcher().getCondition(), Condition.GE);
+ assertEquals(km.getMatcher().getFloatValue(), -2F);
+ assertEquals(km.getMatcher().getPattern(), "-2.0");
}
}