@Test public void nullNotMatchedByDefault() { assertNoMatch(any(), null); assertNoMatch(typeOf(Integer.class), null); }
@Test public void matchObject() { assertMatch(project(), new ProjectNode(null)); assertNoMatch(project(), new ScanNode("t")); }
@Test public void optionalProperties() { Property<RelNode, RelNode> onlySource = Property.optionalProperty("onlySource", node -> Optional.of(node.getSources()) .filter(sources -> sources.size() == 1) .map((List<RelNode> sources) -> sources.get(0))); Pattern<RelNode> relNodeWithExactlyOneSource = plan() .with(onlySource.matching(any())); assertMatch(relNodeWithExactlyOneSource, new ProjectNode(new ScanNode("t"))); assertNoMatch(relNodeWithExactlyOneSource, new ScanNode("t")); assertNoMatch(relNodeWithExactlyOneSource, new JoinNode(new ScanNode("t"), new ScanNode("t"))); }
@Test public void matchNestedProperties() { Pattern<ProjectNode> pattern = project().with(source().matching(scan())); assertMatch(pattern, new ProjectNode(new ScanNode("t"))); assertNoMatch(pattern, new ScanNode("t")); //TODO this needs a custom Option type to work , or NPEs will happen. //Optional does not allow null values. //assertNoMatch(pattern, new ProjectNode(null)); assertNoMatch(pattern, new ProjectNode(new ProjectNode(null))); }
@Test public void trivialMatchers() { //any assertMatch(any(), 42); assertMatch(any(), "John Doe"); //class based assertMatch(typeOf(Integer.class), 42); assertMatch(typeOf(Number.class), 42); assertNoMatch(typeOf(Integer.class), "John Doe"); //predicate-based assertMatch(typeOf(Integer.class).matching(x -> x > 0), 42); assertNoMatch(typeOf(Integer.class).matching(x -> x > 0), -1); }
@Test public void propertyMatchers() { Pattern<String> aString = typeOf(String.class); Property<String, Integer> length = Property.property("length", String::length); String string = "a"; assertMatch(aString.with(length.equalTo(1)), string); assertMatch(project().with(source().matching(scan())), new ProjectNode(new ScanNode("T"))); assertMatch(aString.with(length.matching(any())), string); assertMatch(aString.with(length.matching(x -> x > 0)), string); assertMatch(aString.with(length.matching((Number x) -> x.intValue() > 0)), string); assertNoMatch(aString.with(length.equalTo(0)), string); assertNoMatch(project().with(source().matching(scan())), new ProjectNode(new ProjectNode(new ScanNode("T")))); assertNoMatch(aString.with(length.matching(typeOf(Void.class))), string); assertNoMatch(aString.with(length.matching(x -> x < 1)), string); assertNoMatch(aString.with(length.matching((Number x) -> x.intValue() < 1)), string); }