Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 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)
Esempio n. 6
0
 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)
Esempio n. 7
0
 def test_wildcard_all(self):
     apf = ASTPatternFinder(prepare_pattern("f(??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 8
Esempio n. 8
0
 def get_matching_names(self, pat):
     apf = ASTPatternFinder(prepare_pattern(pat))
     matches = apf.scan_ast(self.ast)
     return {f.name for f in matches}
Esempio n. 9
0
 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)
Esempio n. 10
0
 def get_matching_names(self, pat):
     apf = ASTPatternFinder(prepare_pattern(pat))
     matches = apf.scan_ast(self.ast)
     return {f.name for f in matches}
Esempio n. 11
0
 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')))
Esempio n. 12
0
 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)
Esempio n. 13
0
 def test_wildcard_all(self):
     apf = ASTPatternFinder(prepare_pattern("f(??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 8
Esempio n. 14
0
 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)
Esempio n. 15
0
 def test_keywords_wildcard2(self):
     apf = ASTPatternFinder(prepare_pattern("f(d=?, ??=??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 2
Esempio n. 16
0
 def test_keywords_wildcard2(self):
     apf = ASTPatternFinder(prepare_pattern("f(d=?, ??=??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 2
Esempio n. 17
0
 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')))
Esempio n. 18
0
 def test_single_and_multi_wildcard(self):
     apf = ASTPatternFinder(prepare_pattern("f(?, ??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 5
Esempio n. 19
0
 def test_single_and_multi_wildcard(self):
     apf = ASTPatternFinder(prepare_pattern("f(?, ??)"))
     matches = list(apf.scan_ast(self.ast))
     assert len(matches) == 5
Esempio n. 20
0
def _get_matches(pattern, ast_tree):
    return list(ASTPatternFinder(pattern).scan_ast(ast_tree))
Esempio n. 21
0
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))