|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
from menderbot import python_cst |
|
4
|
|
|
from menderbot.antlr_generated.PythonParser import PythonParser |
|
5
|
|
|
from menderbot.code import PythonLanguageStrategy |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
def parse_string_to_tree(str, lang_strat): |
|
9
|
|
|
source_bytes = bytes(str, "utf-8") |
|
10
|
|
|
return lang_strat.parse_source_to_tree(source_bytes) |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@pytest.fixture |
|
14
|
|
|
def sample_python_tree(py_strat): |
|
15
|
|
|
source = """ |
|
16
|
|
|
def foo(): |
|
17
|
|
|
pass |
|
18
|
|
|
|
|
19
|
|
|
def bar(): |
|
20
|
|
|
pass |
|
21
|
|
|
""" |
|
22
|
|
|
return parse_string_to_tree(source, py_strat) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
# @pytest.fixture |
|
26
|
|
|
# def sample_cpp_tree(): |
|
27
|
|
|
# source = """ |
|
28
|
|
|
# #include <stdio.h> |
|
29
|
|
|
# int main() { |
|
30
|
|
|
# printf("Hello, World!"); |
|
31
|
|
|
# return 0; |
|
32
|
|
|
# } |
|
33
|
|
|
# """ |
|
34
|
|
|
# source_bytes = bytes(source, "utf-8") |
|
35
|
|
|
# return parse_source_to_tree(source_bytes, CPP_LANGUAGE) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
@pytest.fixture |
|
39
|
|
|
def py_strat(): |
|
40
|
|
|
return PythonLanguageStrategy() |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
# @pytest.fixture |
|
44
|
|
|
# def cpp_strat(): |
|
45
|
|
|
# return CppLanguageStrategy() |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_function_nodes_with_python(sample_python_tree, py_strat): |
|
49
|
|
|
result = py_strat.get_function_nodes(sample_python_tree) |
|
50
|
|
|
|
|
51
|
|
|
assert len(result) == 2 |
|
52
|
|
|
assert is_function_node(result[0]) |
|
53
|
|
|
assert is_function_node(result[1]) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def is_function_node(result_): |
|
57
|
|
|
return result_.getRuleIndex() == PythonParser.RULE_funcdef |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_decorated_function_nodes_with_python(py_strat): |
|
61
|
|
|
source = """ |
|
62
|
|
|
@cli.command() |
|
63
|
|
|
@click.argument("q", nargs=-1) |
|
64
|
|
|
def foo(q): |
|
65
|
|
|
pass |
|
66
|
|
|
|
|
67
|
|
|
def bar(q): |
|
68
|
|
|
pass |
|
69
|
|
|
""" |
|
70
|
|
|
tree = parse_string_to_tree(source, py_strat) |
|
71
|
|
|
result = list(py_strat.get_function_nodes(tree)) |
|
72
|
|
|
|
|
73
|
|
|
assert len(result) == 2 |
|
74
|
|
|
assert is_function_node(result[0]) |
|
75
|
|
|
assert is_function_node(result[1]) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_class_function_nodes_with_python(py_strat): |
|
79
|
|
|
source = """ |
|
80
|
|
|
class Cls: |
|
81
|
|
|
def __init__(self): |
|
82
|
|
|
pass |
|
83
|
|
|
|
|
84
|
|
|
def foo(): |
|
85
|
|
|
pass |
|
86
|
|
|
""" |
|
87
|
|
|
tree = parse_string_to_tree(source, py_strat) |
|
88
|
|
|
result = list(py_strat.get_function_nodes(tree)) |
|
89
|
|
|
|
|
90
|
|
|
assert len(result) == 2 |
|
91
|
|
|
assert is_function_node(result[0]) |
|
92
|
|
|
assert is_function_node(result[1]) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def test_python_function_has_comment_false(py_strat): |
|
96
|
|
|
source = """ |
|
97
|
|
|
def foo(): |
|
98
|
|
|
pass |
|
99
|
|
|
""" |
|
100
|
|
|
tree = parse_string_to_tree(source, py_strat) |
|
101
|
|
|
node = py_strat.get_function_nodes(tree)[0] |
|
102
|
|
|
assert not py_strat.function_has_comment(node) |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
def test_python_function_has_comment_true(py_strat): |
|
106
|
|
|
source = """ |
|
107
|
|
|
def foo(): |
|
108
|
|
|
\"\"\"Doc string\"\"\" |
|
109
|
|
|
pass |
|
110
|
|
|
""" |
|
111
|
|
|
tree = parse_string_to_tree(source, py_strat) |
|
112
|
|
|
node = py_strat.get_function_nodes(tree)[0] |
|
113
|
|
|
assert py_strat.function_has_comment(node) |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
def test_python_function_name(py_strat): |
|
117
|
|
|
source = """ |
|
118
|
|
|
def foo(): |
|
119
|
|
|
\"\"\"Doc string\"\"\" |
|
120
|
|
|
pass |
|
121
|
|
|
""" |
|
122
|
|
|
tree = parse_string_to_tree(source, py_strat) |
|
123
|
|
|
node = py_strat.get_function_nodes(tree)[0] |
|
124
|
|
|
assert py_strat.get_function_node_name(node) == "foo" |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
# def test_cpp_function_name(cpp_strat): |
|
128
|
|
|
# source = """ |
|
129
|
|
|
# #include <stdio.h> |
|
130
|
|
|
# int main() { |
|
131
|
|
|
# printf("Hello, World!"); |
|
132
|
|
|
# return 0; |
|
133
|
|
|
# } |
|
134
|
|
|
# """ |
|
135
|
|
|
# tree = parse_string_to_tree(source, CPP_LANGUAGE) |
|
136
|
|
|
# node = cpp_strat.get_function_nodes(tree)[0] |
|
137
|
|
|
# assert cpp_strat.get_function_node_name(node) == "main" |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
def test_python_end_of_params_line_col(py_strat): |
|
141
|
|
|
# TODO: Should move into LanguageStrategy probably. |
|
142
|
|
|
code = """ |
|
143
|
|
|
#2345678901234 |
|
144
|
|
|
def foo(a, b): |
|
145
|
|
|
pass |
|
146
|
|
|
""" |
|
147
|
|
|
fn_ast = python_cst.collect_function_asts(code)[0] |
|
148
|
|
|
sig_ast = fn_ast.children_filtered(kind=python_cst.KIND_SIGNATURE)[0] |
|
149
|
|
|
sig_end = sig_ast.src_range.end |
|
150
|
|
|
assert (sig_end.line, sig_end.col) == (3, 14) |
|
151
|
|
|
|