def test_rebind_to_skip(self): m = evaluate.compile_matcher("""AllOf( HasItem(0, Rebind(Bind("x"), on_conflict=BindConflict.SKIP)), HasItem(1, Rebind(Bind("x"), on_conflict=BindConflict.SKIP)), )""") self.assertIsNone(m.match(_FAKE_CONTEXT, [1, 1]))
def test_syntax_matchers(self): for expr in [ "syntax_matchers.ExprPattern('$bar')", "ExprPattern('$bar')" ]: with self.subTest(expr=expr): self.assertEqual(evaluate.compile_matcher(expr), syntax_matchers.ExprPattern('$bar'))
def test_rebind_to_unskip(self): m = evaluate.compile_matcher("""AllOf( HasItem(0, Rebind(Bind("x", on_conflict=BindConflict.SKIP), on_conflict=BindConflict.MERGE)), HasItem(1, Rebind(Bind("x", on_conflict=BindConflict.SKIP), on_conflict=BindConflict.MERGE)), )""") result = m.match(_FAKE_CONTEXT, [1, 1]) self.assertIsNotNone(result) self.assertEqual(result.bindings['x'].value.matched, 1)
def match_conflict(self, v1, v2, on_conflict=None, on_merge=None): """Matches v1 and v2 with conflicting Binds with the provided on_conflict. Args: v1: value for the first Bind() to match. v2: value for the second Bind() to match. on_conflict: a BindConflict enum value or None. on_merge: a BindMerge enum value or None. Returns: If the match failed, returns None. Otherwise, returns the BoundValue.value for the conflicting bind. """ expr = evaluate.compile_matcher("""AllOf( HasItem(0, Bind("x", on_conflict={on_conflict}, on_merge={on_merge})), HasItem(1, Bind("x", on_conflict={on_conflict}, on_merge={on_merge})), )""".format(on_conflict=on_conflict, on_merge=on_merge)) value = [v1, v2] result = expr.match(_FAKE_CONTEXT, value) if result is None: return None return result.bindings['x'].value.matched
def from_pattern( cls, pattern: str, templates: Optional[Dict[str, formatting.Template]] ) -> "PyMatcherRewritingSearcher": """Creates a searcher from a ``--mode=py`` matcher.""" return cls.from_matcher(evaluate.compile_matcher(pattern), templates=templates)
def test_systembind_not_user_visible(self): with self.assertRaises(ValueError): evaluate.compile_matcher('SystemBind("__foo")')
def test_whitespace(self): """Whitespace should be ignored to let people pretty-print their inputs.""" self.assertEqual( evaluate.compile_matcher(""" _ """), base_matchers.Anything())
def test_ast_matchers(self): for expr in ['ast_matchers.Name()', 'Name()']: with self.subTest(expr=expr): self.assertEqual(evaluate.compile_matcher(expr), ast_matchers.Name())
def test_base_matchers(self): for expr in ['base_matchers.Anything()', 'Anything()']: with self.subTest(expr=expr): self.assertEqual(evaluate.compile_matcher(expr), base_matchers.Anything())