JAL- 2835 support filter on nested attribute keys
[jalview.git] / test / jalview / util / matcher / KeyedMatcherSetTest.java
index 0d2767d..3d597d2 100644 (file)
@@ -2,21 +2,23 @@ package jalview.util.matcher;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
+import java.util.Iterator;
 import java.util.function.Function;
 
 import org.testng.annotations.Test;
 
 public class KeyedMatcherSetTest
 {
-  @Test
+  @Test(groups = "Functional")
   public void testMatches()
   {
     /*
      * a numeric matcher - MatcherTest covers more conditions
      */
-    KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
     KeyedMatcherSetI kms = new KeyedMatcherSet();
     kms.and(km);
     assertTrue(kms.matches(key -> "-2"));
@@ -29,24 +31,25 @@ public class KeyedMatcherSetTest
     /*
      * a string pattern matcher
      */
-    km = new KeyedMatcher("AF", Condition.Contains, "Cat");
+    km = new KeyedMatcher(Condition.Contains, "Cat", "AF");
     kms = new KeyedMatcherSet();
     kms.and(km);
     assertTrue(kms
-            .matches(key -> "AF".equals(key) ? "raining cats and dogs"
+            .matches(key -> "AF".equals(key[0]) ? "raining cats and dogs"
             : "showers"));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testAnd()
   {
     // condition1: AF value contains "dog" (matches)
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
     // condition 2: CSQ value does not contain "how" (does not match)
-    KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
-            "how");
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
+            "CSQ");
 
-    Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
+    Function<String[], String> vp = key -> "AF".equals(key[0])
+            ? "raining cats and dogs"
             : "showers";
     assertTrue(km1.matches(vp));
     assertFalse(km2.matches(vp));
@@ -59,14 +62,15 @@ public class KeyedMatcherSetTest
     assertFalse(kms.matches(vp));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testToString()
   {
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.LT, 1.2f);
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.LT, 1.2f, "AF");
     assertEquals(km1.toString(), "AF < 1.2");
 
-    KeyedMatcher km2 = new KeyedMatcher("CLIN_SIG", Condition.NotContains, "path");
-    assertEquals(km2.toString(), "CLIN_SIG Does not contain PATH");
+    KeyedMatcher km2 = new KeyedMatcher(Condition.NotContains, "path",
+            "CLIN_SIG");
+    assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'");
 
     /*
      * AND them
@@ -77,7 +81,7 @@ public class KeyedMatcherSetTest
     assertEquals(kms.toString(), "(AF < 1.2)");
     kms.and(km2);
     assertEquals(kms.toString(),
-            "(AF < 1.2) AND (CLIN_SIG Does not contain PATH)");
+            "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')");
 
     /*
      * OR them
@@ -88,19 +92,20 @@ public class KeyedMatcherSetTest
     assertEquals(kms.toString(), "(AF < 1.2)");
     kms.or(km2);
     assertEquals(kms.toString(),
-            "(AF < 1.2) OR (CLIN_SIG Does not contain PATH)");
+            "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testOr()
   {
     // condition1: AF value contains "dog" (matches)
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
     // condition 2: CSQ value does not contain "how" (does not match)
-    KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
-            "how");
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
+            "CSQ");
 
-    Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
+    Function<String[], String> vp = key -> "AF".equals(key[0])
+            ? "raining cats and dogs"
             : "showers";
     assertTrue(km1.matches(vp));
     assertFalse(km2.matches(vp));
@@ -112,13 +117,77 @@ public class KeyedMatcherSetTest
     assertTrue(kms.matches(vp));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testIsEmpty()
   {
-    KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
     KeyedMatcherSetI kms = new KeyedMatcherSet();
     assertTrue(kms.isEmpty());
     kms.and(km);
     assertFalse(kms.isEmpty());
   }
+
+  @Test(groups = "Functional")
+  public void testGetMatchers()
+  {
+    KeyedMatcherSetI kms = new KeyedMatcherSet();
+
+    /*
+     * empty iterable:
+     */
+    Iterator<KeyedMatcherI> iterator = kms.getMatchers().iterator();
+    assertFalse(iterator.hasNext());
+
+    /*
+     * one matcher:
+     */
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.GE, -2F, "AF");
+    kms.and(km1);
+    iterator = kms.getMatchers().iterator();
+    assertSame(km1, iterator.next());
+    assertFalse(iterator.hasNext());
+
+    /*
+     * two matchers:
+     */
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.LT, 8F, "AF");
+    kms.and(km2);
+    iterator = kms.getMatchers().iterator();
+    assertSame(km1, iterator.next());
+    assertSame(km2, iterator.next());
+    assertFalse(iterator.hasNext());
+  }
+
+  /**
+   * 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()
+  {
+    /*
+     * a numeric matcher - MatcherTest covers more conditions
+     */
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "CSQ",
+            "Consequence");
+    KeyedMatcherSetI kms = new KeyedMatcherSet();
+    kms.and(km);
+    assertTrue(kms.matches(key -> "-2"));
+    assertTrue(kms.matches(key -> "-1"));
+    assertFalse(kms.matches(key -> "-3"));
+    assertFalse(kms.matches(key -> ""));
+    assertFalse(kms.matches(key -> "junk"));
+    assertFalse(kms.matches(key -> null));
+  
+    /*
+     * a string pattern matcher
+     */
+    km = new KeyedMatcher(Condition.Contains, "Cat", "CSQ", "Consequence");
+    kms = new KeyedMatcherSet();
+    kms.and(km);
+    assertTrue(kms.matches(key -> "csq".equalsIgnoreCase(key[0])
+            && "Consequence".equalsIgnoreCase(key[1])
+                    ? "raining cats and dogs"
+                    : "showers"));
+  }
 }