def test_type_only(self): parsed, e = expression('~a') self.assertEqual( ast_matchers.UnaryOp().match(matcher.MatchContext(parsed), e), matcher.MatchInfo( matcher.LexicalASTMatch(e, parsed.text, e.first_token, e.last_token)))
def test_ancestor(self): """The matcher won't traverse into child nodes.""" parsed = matcher.parse_ast('~a', '<string>') self.assertIsNone( ast_matchers.UnaryOp( op=base_matchers.Unless(base_matchers.Anything())).match( matcher.MatchContext(parsed), parsed.tree.body[0]))
def test_nested_invert(self, source, matches): self.assertEqual( self.get_all_match_strings( base_matchers.MaybeWrapped( ast_matchers.Num(), lambda i: ast_matchers.UnaryOp(op=ast_matchers.Invert(), operand=i)), source), matches)
def test_eq(self): """Different RecursivelyWrapped nodes with the same structure are equal.""" x, y = [ base_matchers.RecursivelyWrapped( ast_matchers.Num(), lambda i: ast_matchers.UnaryOp(op=ast_matchers.Invert(), operand=i)) for _ in range(2) ] self.assertEqual(x, y)
def test_fully_specified_matcher(self): parsed, e = expression('~a') self.assertEqual( ast_matchers.UnaryOp( op=ast_matchers.Invert(), operand=ast_matchers.Name(ctx=ast_matchers.Load())).match( matcher.MatchContext(parsed), e), matcher.MatchInfo( matcher.LexicalASTMatch(e, parsed.text, e.first_token, e.last_token)))
def test_explicit_anything(self): parsed, e = expression('~a') self.assertEqual( ast_matchers.UnaryOp( op=base_matchers.Anything(), operand=base_matchers.Anything()).match( matcher.MatchContext(parsed), e), matcher.MatchInfo( matcher.LexicalASTMatch(e, parsed.text, e.first_token, e.last_token)))
def test_recursive_bindings(self): """Recursive matchers cover both recursive/base cases in .bind_variables. If this test fails with a RecursionError, that is a problem. """ m = base_matchers.RecursivelyWrapped( base_matchers.Bind('base_case', ast_matchers.Num()), lambda i: base_matchers.Bind( 'recursive_case', ast_matchers.UnaryOp(op=ast_matchers.Invert(), operand=i))) self.assertEqual(m.bind_variables, {'base_case', 'recursive_case'})
def test_repr(self): # It turns out attrs automatically handles cyclic objects, but we can # improve on its repr. self.assertEqual( repr( base_matchers.RecursivelyWrapped( ast_matchers.Num(), lambda i: ast_matchers.UnaryOp(op=ast_matchers.Invert(), operand=i))), 'RecursivelyWrapped(_matchers=(Num(n=Anything()),' ' UnaryOp(op=Invert(), operand=_Recurse(...))))')
def test_ne(self): base = ast_matchers.Num() recursive = lambda m: ast_matchers.UnaryOp(op=ast_matchers.Invert(), operand=m) example_matcher = base_matchers.RecursivelyWrapped(base, recursive) different_1 = base_matchers.RecursivelyWrapped( base_matchers.Unless(base), recursive) different_2 = base_matchers.RecursivelyWrapped( base, lambda m: base_matchers.Unless(recursive(m))) for different_matcher in [different_1, different_2]: self.assertNotEqual(example_matcher, different_matcher)
def test_submatcher_fail(self): parsed, e = expression('~a') self.assertIsNone( ast_matchers.UnaryOp( op=base_matchers.Unless(base_matchers.Anything())).match( matcher.MatchContext(parsed), e))
def test_type_mismatch(self): parsed, e = expression('a + b') self.assertIsNone(ast_matchers.UnaryOp().match( matcher.MatchContext(parsed), e))