def test_keywords_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(e=4, ??=??)")) it = apf.scan_ast(self.ast) assert_ast_like(next(it), ast.Call(keywords=[ast.keyword(arg='d'), ast.keyword(arg='e'),]) ) self.assert_no_more(it)
def test_keywords_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(e=4, ??=??)")) it = apf.scan_ast(self.ast) assert_ast_like( next(it), ast.Call(keywords=[ ast.keyword(arg='d'), ast.keyword(arg='e'), ])) self.assert_no_more(it)
def test_pos_final_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(1, ??)")) it = apf.scan_ast(self.ast) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1)])) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1), ast.Num(n=2)])) assert_ast_like(next(it), ast.Call(starargs=ast.Name(id='c'))) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1)], keywords=[ast.keyword(arg='d'), ast.keyword(arg='e'), ]) ) assert_ast_like(next(it), ast.Call(kwargs=ast.Name(id='k'))) self.assert_no_more(it)
def test_pos_final_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(1, ??)")) it = apf.scan_ast(self.ast) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1)])) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1), ast.Num(n=2)])) assert_ast_like(next(it), ast.Call(starargs=ast.Name(id='c'))) assert_ast_like( next(it), ast.Call(args=[ast.Num(n=1)], keywords=[ ast.keyword(arg='d'), ast.keyword(arg='e'), ])) assert_ast_like(next(it), ast.Call(kwargs=ast.Name(id='k'))) self.assert_no_more(it)
def test_all_divisions(self): pat = ast.BinOp(op=ast.Div()) it = ASTPatternFinder(pat).scan_file(StringIO(division_sample)) assert_ast_like(next(it), ast.BinOp(left=ast.Num(n=1))) assert_ast_like(next(it), ast.BinOp(right=ast.Name(id='c'))) assert_ast_like(next(it), ast.BinOp(left=ast.Name(id='x'))) self.assert_no_more(it)
def test_pos_leading_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(??, 2)")) it = apf.scan_ast(self.ast) assert_ast_like(next(it), ast.Call(args=[ast.Num(n=1), ast.Num(n=2)])) self.assert_no_more(it)
def test_wildcard_all(self): apf = ASTPatternFinder(prepare_pattern("f(??)")) matches = list(apf.scan_ast(self.ast)) assert len(matches) == 8
def get_matching_names(self, pat): apf = ASTPatternFinder(prepare_pattern(pat)) matches = apf.scan_ast(self.ast) return {f.name for f in matches}
def test_plain(self): pat = ast.BinOp(left=ast.Num(1), right=ast.Num(2), op=ast.Div()) it = ASTPatternFinder(pat).scan_file(StringIO(division_sample)) assert next(it).left.n == 1 self.assert_no_more(it)
def test_mixed_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(??, d=?)")) matches = list(apf.scan_ast(self.ast)) assert len(matches) == 4 assert_ast_like(matches[-1], ast.Call(kwargs=ast.Name(id='k')))
def test_block_must_exist(self): pat = ast.If(orelse=must_exist_checker) it = ASTPatternFinder(pat).scan_file(StringIO(if_sample)) assert_ast_like(next(it), ast.If(test=ast.Name(id='a'))) self.assert_no_more(it)
def test_keywords_wildcard2(self): apf = ASTPatternFinder(prepare_pattern("f(d=?, ??=??)")) matches = list(apf.scan_ast(self.ast)) assert len(matches) == 2
def test_single_and_multi_wildcard(self): apf = ASTPatternFinder(prepare_pattern("f(?, ??)")) matches = list(apf.scan_ast(self.ast)) assert len(matches) == 5
def _get_matches(pattern, ast_tree): return list(ASTPatternFinder(pattern).scan_ast(ast_tree))
def get_matches(pattern, sample_code): sample_ast = ast.parse(sample_code) print(ast.dump(sample_ast)) return list(ASTPatternFinder(pattern).scan_ast(sample_ast))