|
e52aae0…
|
lmata
|
1 |
"""Tests for navegador.ingestion.java — JavaParser internal methods.""" |
|
e52aae0…
|
lmata
|
2 |
|
|
e52aae0…
|
lmata
|
3 |
from unittest.mock import MagicMock, patch |
|
e52aae0…
|
lmata
|
4 |
|
|
e52aae0…
|
lmata
|
5 |
import pytest |
|
e52aae0…
|
lmata
|
6 |
|
|
e52aae0…
|
lmata
|
7 |
from navegador.graph.schema import NodeLabel |
|
e52aae0…
|
lmata
|
8 |
|
|
e52aae0…
|
lmata
|
9 |
|
|
e52aae0…
|
lmata
|
10 |
class MockNode: |
|
e52aae0…
|
lmata
|
11 |
_id_counter = 0 |
|
e52aae0…
|
lmata
|
12 |
|
|
e52aae0…
|
lmata
|
13 |
def __init__(self, type_: str, text: bytes = b"", children: list = None, |
|
e52aae0…
|
lmata
|
14 |
start_byte: int = 0, end_byte: int = 0, |
|
e52aae0…
|
lmata
|
15 |
start_point: tuple = (0, 0), end_point: tuple = (0, 0), |
|
e52aae0…
|
lmata
|
16 |
parent=None): |
|
e52aae0…
|
lmata
|
17 |
MockNode._id_counter += 1 |
|
e52aae0…
|
lmata
|
18 |
self.id = MockNode._id_counter |
|
e52aae0…
|
lmata
|
19 |
self.type = type_ |
|
e52aae0…
|
lmata
|
20 |
self.children = children or [] |
|
e52aae0…
|
lmata
|
21 |
self.start_byte = start_byte |
|
e52aae0…
|
lmata
|
22 |
self.end_byte = end_byte |
|
e52aae0…
|
lmata
|
23 |
self.start_point = start_point |
|
e52aae0…
|
lmata
|
24 |
self.end_point = end_point |
|
e52aae0…
|
lmata
|
25 |
self.parent = parent |
|
e52aae0…
|
lmata
|
26 |
self._fields: dict = {} |
|
e52aae0…
|
lmata
|
27 |
for child in self.children: |
|
e52aae0…
|
lmata
|
28 |
child.parent = self |
|
e52aae0…
|
lmata
|
29 |
|
|
e52aae0…
|
lmata
|
30 |
def child_by_field_name(self, name: str): |
|
e52aae0…
|
lmata
|
31 |
return self._fields.get(name) |
|
e52aae0…
|
lmata
|
32 |
|
|
e52aae0…
|
lmata
|
33 |
def set_field(self, name: str, node): |
|
e52aae0…
|
lmata
|
34 |
self._fields[name] = node |
|
e52aae0…
|
lmata
|
35 |
node.parent = self |
|
e52aae0…
|
lmata
|
36 |
return self |
|
e52aae0…
|
lmata
|
37 |
|
|
e52aae0…
|
lmata
|
38 |
|
|
e52aae0…
|
lmata
|
39 |
def _text_node(text: bytes, type_: str = "identifier") -> MockNode: |
|
e52aae0…
|
lmata
|
40 |
return MockNode(type_, text, start_byte=0, end_byte=len(text)) |
|
e52aae0…
|
lmata
|
41 |
|
|
e52aae0…
|
lmata
|
42 |
|
|
e52aae0…
|
lmata
|
43 |
def _make_store(): |
|
e52aae0…
|
lmata
|
44 |
store = MagicMock() |
|
e52aae0…
|
lmata
|
45 |
store.query.return_value = MagicMock(result_set=[]) |
|
e52aae0…
|
lmata
|
46 |
return store |
|
e52aae0…
|
lmata
|
47 |
|
|
e52aae0…
|
lmata
|
48 |
|
|
e52aae0…
|
lmata
|
49 |
def _make_parser(): |
|
e52aae0…
|
lmata
|
50 |
from navegador.ingestion.java import JavaParser |
|
e52aae0…
|
lmata
|
51 |
parser = JavaParser.__new__(JavaParser) |
|
e52aae0…
|
lmata
|
52 |
parser._parser = MagicMock() |
|
e52aae0…
|
lmata
|
53 |
return parser |
|
e52aae0…
|
lmata
|
54 |
|
|
e52aae0…
|
lmata
|
55 |
|
|
e52aae0…
|
lmata
|
56 |
class TestJavaGetLanguage: |
|
e52aae0…
|
lmata
|
57 |
def test_raises_when_not_installed(self): |
|
e52aae0…
|
lmata
|
58 |
from navegador.ingestion.java import _get_java_language |
|
e52aae0…
|
lmata
|
59 |
with patch.dict("sys.modules", {"tree_sitter_java": None, "tree_sitter": None}): |
|
e52aae0…
|
lmata
|
60 |
with pytest.raises(ImportError, match="tree-sitter-java"): |
|
e52aae0…
|
lmata
|
61 |
_get_java_language() |
|
e52aae0…
|
lmata
|
62 |
|
|
e52aae0…
|
lmata
|
63 |
|
|
e52aae0…
|
lmata
|
64 |
class TestJavaNodeText: |
|
e52aae0…
|
lmata
|
65 |
def test_extracts_bytes(self): |
|
e52aae0…
|
lmata
|
66 |
from navegador.ingestion.java import _node_text |
|
e52aae0…
|
lmata
|
67 |
source = b"class Foo {}" |
|
e52aae0…
|
lmata
|
68 |
node = MockNode("identifier", start_byte=6, end_byte=9) |
|
e52aae0…
|
lmata
|
69 |
assert _node_text(node, source) == "Foo" |
|
e52aae0…
|
lmata
|
70 |
|
|
e52aae0…
|
lmata
|
71 |
|
|
e52aae0…
|
lmata
|
72 |
class TestJavadoc: |
|
e52aae0…
|
lmata
|
73 |
def test_extracts_javadoc(self): |
|
e52aae0…
|
lmata
|
74 |
from navegador.ingestion.java import _javadoc |
|
e52aae0…
|
lmata
|
75 |
source = b"/** My class */\nclass Foo {}" |
|
e52aae0…
|
lmata
|
76 |
comment = MockNode("block_comment", start_byte=0, end_byte=15) |
|
e52aae0…
|
lmata
|
77 |
cls_node = MockNode("class_declaration", start_byte=16, end_byte=28) |
|
e52aae0…
|
lmata
|
78 |
_parent = MockNode("program", children=[comment, cls_node]) |
|
e52aae0…
|
lmata
|
79 |
result = _javadoc(cls_node, source) |
|
e52aae0…
|
lmata
|
80 |
assert "My class" in result |
|
e52aae0…
|
lmata
|
81 |
|
|
e52aae0…
|
lmata
|
82 |
def test_ignores_regular_block_comment(self): |
|
e52aae0…
|
lmata
|
83 |
from navegador.ingestion.java import _javadoc |
|
e52aae0…
|
lmata
|
84 |
source = b"/* regular */\nclass Foo {}" |
|
e52aae0…
|
lmata
|
85 |
comment = MockNode("block_comment", start_byte=0, end_byte=13) |
|
e52aae0…
|
lmata
|
86 |
cls_node = MockNode("class_declaration", start_byte=14, end_byte=26) |
|
e52aae0…
|
lmata
|
87 |
MockNode("program", children=[comment, cls_node]) |
|
e52aae0…
|
lmata
|
88 |
result = _javadoc(cls_node, source) |
|
e52aae0…
|
lmata
|
89 |
assert result == "" |
|
e52aae0…
|
lmata
|
90 |
|
|
e52aae0…
|
lmata
|
91 |
|
|
e52aae0…
|
lmata
|
92 |
class TestJavaHandleClass: |
|
e52aae0…
|
lmata
|
93 |
def test_creates_class_node(self): |
|
e52aae0…
|
lmata
|
94 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
95 |
store = _make_store() |
|
e52aae0…
|
lmata
|
96 |
source = b"class Foo {}" |
|
e52aae0…
|
lmata
|
97 |
name_node = _text_node(b"Foo") |
|
e52aae0…
|
lmata
|
98 |
body = MockNode("class_body") |
|
e52aae0…
|
lmata
|
99 |
node = MockNode("class_declaration", start_point=(0, 0), end_point=(0, 11)) |
|
e52aae0…
|
lmata
|
100 |
node.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
101 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
102 |
_parent = MockNode("program", children=[node]) |
|
e52aae0…
|
lmata
|
103 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
104 |
parser._handle_class(node, source, "Foo.java", store, stats) |
|
e52aae0…
|
lmata
|
105 |
assert stats["classes"] == 1 |
|
e52aae0…
|
lmata
|
106 |
label = store.create_node.call_args[0][0] |
|
e52aae0…
|
lmata
|
107 |
assert label == NodeLabel.Class |
|
e52aae0…
|
lmata
|
108 |
|
|
e52aae0…
|
lmata
|
109 |
def test_creates_inherits_edge(self): |
|
e52aae0…
|
lmata
|
110 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
111 |
store = _make_store() |
|
e52aae0…
|
lmata
|
112 |
source = b"class Child extends Parent {}" |
|
e52aae0…
|
lmata
|
113 |
name_node = _text_node(b"Child") |
|
e52aae0…
|
lmata
|
114 |
parent_id = _text_node(b"Parent", "type_identifier") |
|
e52aae0…
|
lmata
|
115 |
superclass = MockNode("superclass", children=[parent_id]) |
|
e52aae0…
|
lmata
|
116 |
body = MockNode("class_body") |
|
e52aae0…
|
lmata
|
117 |
node = MockNode("class_declaration", start_point=(0, 0), end_point=(0, 28)) |
|
e52aae0…
|
lmata
|
118 |
node.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
119 |
node.set_field("superclass", superclass) |
|
e52aae0…
|
lmata
|
120 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
121 |
MockNode("program", children=[node]) |
|
e52aae0…
|
lmata
|
122 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
123 |
parser._handle_class(node, source, "Child.java", store, stats) |
|
e52aae0…
|
lmata
|
124 |
# Should have CONTAINS edge + INHERITS edge |
|
e52aae0…
|
lmata
|
125 |
assert stats["edges"] == 2 |
|
e52aae0…
|
lmata
|
126 |
|
|
e52aae0…
|
lmata
|
127 |
def test_ingests_methods_in_body(self): |
|
e52aae0…
|
lmata
|
128 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
129 |
store = _make_store() |
|
e52aae0…
|
lmata
|
130 |
source = b"class Foo { void bar() {} }" |
|
e52aae0…
|
lmata
|
131 |
name_node = _text_node(b"Foo") |
|
e52aae0…
|
lmata
|
132 |
method_name = _text_node(b"bar") |
|
e52aae0…
|
lmata
|
133 |
method_body = MockNode("block") |
|
e52aae0…
|
lmata
|
134 |
method = MockNode("method_declaration", start_point=(1, 2), end_point=(1, 14)) |
|
e52aae0…
|
lmata
|
135 |
method.set_field("name", method_name) |
|
e52aae0…
|
lmata
|
136 |
method.set_field("body", method_body) |
|
e52aae0…
|
lmata
|
137 |
body = MockNode("class_body", children=[method]) |
|
e52aae0…
|
lmata
|
138 |
node = MockNode("class_declaration", start_point=(0, 0), end_point=(0, 26)) |
|
e52aae0…
|
lmata
|
139 |
node.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
140 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
141 |
MockNode("program", children=[node]) |
|
e52aae0…
|
lmata
|
142 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
143 |
parser._handle_class(node, source, "Foo.java", store, stats) |
|
e52aae0…
|
lmata
|
144 |
assert stats["functions"] == 1 |
|
e52aae0…
|
lmata
|
145 |
|
|
e52aae0…
|
lmata
|
146 |
def test_skips_if_no_name(self): |
|
e52aae0…
|
lmata
|
147 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
148 |
store = _make_store() |
|
e52aae0…
|
lmata
|
149 |
node = MockNode("class_declaration") |
|
e52aae0…
|
lmata
|
150 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
151 |
parser._handle_class(node, b"", "X.java", store, stats) |
|
e52aae0…
|
lmata
|
152 |
assert stats["classes"] == 0 |
|
e52aae0…
|
lmata
|
153 |
|
|
e52aae0…
|
lmata
|
154 |
|
|
e52aae0…
|
lmata
|
155 |
class TestJavaHandleInterface: |
|
e52aae0…
|
lmata
|
156 |
def test_creates_interface_node(self): |
|
e52aae0…
|
lmata
|
157 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
158 |
store = _make_store() |
|
e52aae0…
|
lmata
|
159 |
source = b"interface Saveable {}" |
|
e52aae0…
|
lmata
|
160 |
name_node = _text_node(b"Saveable") |
|
e52aae0…
|
lmata
|
161 |
body = MockNode("interface_body") |
|
e52aae0…
|
lmata
|
162 |
node = MockNode("interface_declaration", start_point=(0, 0), end_point=(0, 20)) |
|
e52aae0…
|
lmata
|
163 |
node.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
164 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
165 |
MockNode("program", children=[node]) |
|
e52aae0…
|
lmata
|
166 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
167 |
parser._handle_interface(node, source, "Saveable.java", store, stats) |
|
e52aae0…
|
lmata
|
168 |
assert stats["classes"] == 1 |
|
e52aae0…
|
lmata
|
169 |
props = store.create_node.call_args[0][1] |
|
e52aae0…
|
lmata
|
170 |
assert "interface" in props.get("docstring", "") |
|
e52aae0…
|
lmata
|
171 |
|
|
e52aae0…
|
lmata
|
172 |
def test_skips_if_no_name(self): |
|
e52aae0…
|
lmata
|
173 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
174 |
store = _make_store() |
|
e52aae0…
|
lmata
|
175 |
node = MockNode("interface_declaration") |
|
e52aae0…
|
lmata
|
176 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
177 |
parser._handle_interface(node, b"", "X.java", store, stats) |
|
e52aae0…
|
lmata
|
178 |
assert stats["classes"] == 0 |
|
e52aae0…
|
lmata
|
179 |
|
|
e52aae0…
|
lmata
|
180 |
|
|
e52aae0…
|
lmata
|
181 |
class TestJavaHandleMethod: |
|
e52aae0…
|
lmata
|
182 |
def test_creates_method_node(self): |
|
e52aae0…
|
lmata
|
183 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
184 |
store = _make_store() |
|
e52aae0…
|
lmata
|
185 |
source = b"void save() {}" |
|
e52aae0…
|
lmata
|
186 |
name_node = _text_node(b"save") |
|
e52aae0…
|
lmata
|
187 |
body = MockNode("block") |
|
e52aae0…
|
lmata
|
188 |
node = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 13)) |
|
e52aae0…
|
lmata
|
189 |
node.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
190 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
191 |
MockNode("class_body", children=[node]) |
|
e52aae0…
|
lmata
|
192 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
193 |
parser._handle_method(node, source, "Foo.java", store, stats, class_name="Foo") |
|
e52aae0…
|
lmata
|
194 |
assert stats["functions"] == 1 |
|
e52aae0…
|
lmata
|
195 |
label = store.create_node.call_args[0][0] |
|
e52aae0…
|
lmata
|
196 |
assert label == NodeLabel.Method |
|
e52aae0…
|
lmata
|
197 |
|
|
e52aae0…
|
lmata
|
198 |
def test_skips_if_no_name(self): |
|
e52aae0…
|
lmata
|
199 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
200 |
store = _make_store() |
|
e52aae0…
|
lmata
|
201 |
node = MockNode("method_declaration") |
|
e52aae0…
|
lmata
|
202 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
203 |
parser._handle_method(node, b"", "X.java", store, stats, class_name="X") |
|
e52aae0…
|
lmata
|
204 |
assert stats["functions"] == 0 |
|
e52aae0…
|
lmata
|
205 |
|
|
e52aae0…
|
lmata
|
206 |
|
|
e52aae0…
|
lmata
|
207 |
class TestJavaHandleImport: |
|
e52aae0…
|
lmata
|
208 |
def test_ingests_import(self): |
|
e52aae0…
|
lmata
|
209 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
210 |
store = _make_store() |
|
e52aae0…
|
lmata
|
211 |
source = b"import java.util.List;" |
|
e52aae0…
|
lmata
|
212 |
node = MockNode("import_declaration", start_byte=0, end_byte=22, |
|
e52aae0…
|
lmata
|
213 |
start_point=(0, 0)) |
|
e52aae0…
|
lmata
|
214 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
215 |
parser._handle_import(node, source, "Foo.java", store, stats) |
|
e52aae0…
|
lmata
|
216 |
assert stats["edges"] == 1 |
|
e52aae0…
|
lmata
|
217 |
props = store.create_node.call_args[0][1] |
|
e52aae0…
|
lmata
|
218 |
assert "java.util.List" in props["name"] |
|
e52aae0…
|
lmata
|
219 |
|
|
e52aae0…
|
lmata
|
220 |
def test_handles_static_import(self): |
|
e52aae0…
|
lmata
|
221 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
222 |
store = _make_store() |
|
e52aae0…
|
lmata
|
223 |
source = b"import static java.util.Collections.sort;" |
|
e52aae0…
|
lmata
|
224 |
node = MockNode("import_declaration", start_byte=0, end_byte=41, |
|
e52aae0…
|
lmata
|
225 |
start_point=(0, 0)) |
|
e52aae0…
|
lmata
|
226 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
227 |
parser._handle_import(node, source, "Foo.java", store, stats) |
|
e52aae0…
|
lmata
|
228 |
assert stats["edges"] == 1 |
|
e52aae0…
|
lmata
|
229 |
|
|
e52aae0…
|
lmata
|
230 |
|
|
e52aae0…
|
lmata
|
231 |
class TestJavaExtractCalls: |
|
e52aae0…
|
lmata
|
232 |
def test_extracts_method_invocation(self): |
|
e52aae0…
|
lmata
|
233 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
234 |
store = _make_store() |
|
2e96458…
|
lmata
|
235 |
source = b"bar" |
|
e52aae0…
|
lmata
|
236 |
callee = _text_node(b"bar") |
|
e52aae0…
|
lmata
|
237 |
invocation = MockNode("method_invocation") |
|
e52aae0…
|
lmata
|
238 |
invocation.set_field("name", callee) |
|
e52aae0…
|
lmata
|
239 |
body = MockNode("block", children=[invocation]) |
|
e52aae0…
|
lmata
|
240 |
node = MockNode("method_declaration") |
|
e52aae0…
|
lmata
|
241 |
node.set_field("body", body) |
|
e52aae0…
|
lmata
|
242 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
243 |
parser._extract_calls(node, source, "Foo.java", "foo", store, stats) |
|
e52aae0…
|
lmata
|
244 |
assert stats["edges"] == 1 |
|
2e96458…
|
lmata
|
245 |
edge_call = store.create_edge.call_args[0] |
|
2e96458…
|
lmata
|
246 |
assert edge_call[4]["name"] == "bar" |
|
e52aae0…
|
lmata
|
247 |
|
|
e52aae0…
|
lmata
|
248 |
def test_no_calls_in_empty_body(self): |
|
e52aae0…
|
lmata
|
249 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
250 |
store = _make_store() |
|
e52aae0…
|
lmata
|
251 |
node = MockNode("method_declaration") |
|
e52aae0…
|
lmata
|
252 |
node.set_field("body", MockNode("block")) |
|
e52aae0…
|
lmata
|
253 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
254 |
parser._extract_calls(node, b"", "X.java", "foo", store, stats) |
|
e52aae0…
|
lmata
|
255 |
assert stats["edges"] == 0 |
|
7ae0080…
|
lmata
|
256 |
|
|
7ae0080…
|
lmata
|
257 |
|
|
7ae0080…
|
lmata
|
258 |
# ── _get_java_language happy path ───────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
259 |
|
|
7ae0080…
|
lmata
|
260 |
class TestJavaGetLanguageHappyPath: |
|
7ae0080…
|
lmata
|
261 |
def test_returns_language_object(self): |
|
7ae0080…
|
lmata
|
262 |
from navegador.ingestion.java import _get_java_language |
|
7ae0080…
|
lmata
|
263 |
mock_tsjava = MagicMock() |
|
7ae0080…
|
lmata
|
264 |
mock_ts = MagicMock() |
|
7ae0080…
|
lmata
|
265 |
with patch.dict("sys.modules", { |
|
7ae0080…
|
lmata
|
266 |
"tree_sitter_java": mock_tsjava, |
|
7ae0080…
|
lmata
|
267 |
"tree_sitter": mock_ts, |
|
7ae0080…
|
lmata
|
268 |
}): |
|
7ae0080…
|
lmata
|
269 |
result = _get_java_language() |
|
7ae0080…
|
lmata
|
270 |
assert result is mock_ts.Language.return_value |
|
7ae0080…
|
lmata
|
271 |
|
|
7ae0080…
|
lmata
|
272 |
|
|
7ae0080…
|
lmata
|
273 |
# ── JavaParser init and parse_file ─────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
274 |
|
|
7ae0080…
|
lmata
|
275 |
class TestJavaParserInit: |
|
7ae0080…
|
lmata
|
276 |
def test_init_creates_parser(self): |
|
7ae0080…
|
lmata
|
277 |
mock_tsjava = MagicMock() |
|
7ae0080…
|
lmata
|
278 |
mock_ts = MagicMock() |
|
7ae0080…
|
lmata
|
279 |
with patch.dict("sys.modules", { |
|
7ae0080…
|
lmata
|
280 |
"tree_sitter_java": mock_tsjava, |
|
7ae0080…
|
lmata
|
281 |
"tree_sitter": mock_ts, |
|
7ae0080…
|
lmata
|
282 |
}): |
|
7ae0080…
|
lmata
|
283 |
from navegador.ingestion.java import JavaParser |
|
7ae0080…
|
lmata
|
284 |
parser = JavaParser() |
|
7ae0080…
|
lmata
|
285 |
assert parser._parser is mock_ts.Parser.return_value |
|
7ae0080…
|
lmata
|
286 |
|
|
7ae0080…
|
lmata
|
287 |
def test_parse_file_creates_file_node(self): |
|
7ae0080…
|
lmata
|
288 |
import tempfile |
|
7ae0080…
|
lmata
|
289 |
from pathlib import Path |
|
7ae0080…
|
lmata
|
290 |
|
|
7ae0080…
|
lmata
|
291 |
from navegador.graph.schema import NodeLabel |
|
7ae0080…
|
lmata
|
292 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
293 |
store = _make_store() |
|
7ae0080…
|
lmata
|
294 |
mock_tree = MagicMock() |
|
7ae0080…
|
lmata
|
295 |
mock_tree.root_node.type = "program" |
|
7ae0080…
|
lmata
|
296 |
mock_tree.root_node.children = [] |
|
7ae0080…
|
lmata
|
297 |
parser._parser.parse.return_value = mock_tree |
|
7ae0080…
|
lmata
|
298 |
with tempfile.NamedTemporaryFile(suffix=".java", delete=False) as f: |
|
7ae0080…
|
lmata
|
299 |
f.write(b"class Foo {}\n") |
|
7ae0080…
|
lmata
|
300 |
fpath = Path(f.name) |
|
7ae0080…
|
lmata
|
301 |
try: |
|
7ae0080…
|
lmata
|
302 |
parser.parse_file(fpath, fpath.parent, store) |
|
7ae0080…
|
lmata
|
303 |
store.create_node.assert_called_once() |
|
7ae0080…
|
lmata
|
304 |
assert store.create_node.call_args[0][0] == NodeLabel.File |
|
7ae0080…
|
lmata
|
305 |
assert store.create_node.call_args[0][1]["language"] == "java" |
|
7ae0080…
|
lmata
|
306 |
finally: |
|
7ae0080…
|
lmata
|
307 |
fpath.unlink() |
|
7ae0080…
|
lmata
|
308 |
|
|
7ae0080…
|
lmata
|
309 |
|
|
7ae0080…
|
lmata
|
310 |
# ── _walk dispatch ──────────────────────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
311 |
|
|
7ae0080…
|
lmata
|
312 |
class TestJavaWalkDispatch: |
|
7ae0080…
|
lmata
|
313 |
def test_walk_handles_class_declaration(self): |
|
7ae0080…
|
lmata
|
314 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
315 |
store = _make_store() |
|
7ae0080…
|
lmata
|
316 |
source = b"Foo" |
|
7ae0080…
|
lmata
|
317 |
name = _text_node(b"Foo") |
|
7ae0080…
|
lmata
|
318 |
body = MockNode("class_body", children=[]) |
|
7ae0080…
|
lmata
|
319 |
node = MockNode("class_declaration", start_point=(0, 0), end_point=(0, 10)) |
|
7ae0080…
|
lmata
|
320 |
node.set_field("name", name) |
|
7ae0080…
|
lmata
|
321 |
node.set_field("body", body) |
|
7ae0080…
|
lmata
|
322 |
root = MockNode("program", children=[node]) |
|
7ae0080…
|
lmata
|
323 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
324 |
parser._walk(root, source, "Foo.java", store, stats, class_name=None) |
|
7ae0080…
|
lmata
|
325 |
assert stats["classes"] == 1 |
|
7ae0080…
|
lmata
|
326 |
|
|
7ae0080…
|
lmata
|
327 |
def test_walk_handles_interface_declaration(self): |
|
7ae0080…
|
lmata
|
328 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
329 |
store = _make_store() |
|
7ae0080…
|
lmata
|
330 |
source = b"Readable" |
|
7ae0080…
|
lmata
|
331 |
name = _text_node(b"Readable") |
|
7ae0080…
|
lmata
|
332 |
body = MockNode("interface_body", children=[]) |
|
7ae0080…
|
lmata
|
333 |
node = MockNode("interface_declaration", start_point=(0, 0), end_point=(0, 20)) |
|
7ae0080…
|
lmata
|
334 |
node.set_field("name", name) |
|
7ae0080…
|
lmata
|
335 |
node.set_field("body", body) |
|
7ae0080…
|
lmata
|
336 |
root = MockNode("program", children=[node]) |
|
7ae0080…
|
lmata
|
337 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
338 |
parser._walk(root, source, "R.java", store, stats, class_name=None) |
|
7ae0080…
|
lmata
|
339 |
assert stats["classes"] == 1 |
|
7ae0080…
|
lmata
|
340 |
|
|
7ae0080…
|
lmata
|
341 |
def test_walk_handles_import_declaration(self): |
|
7ae0080…
|
lmata
|
342 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
343 |
store = _make_store() |
|
7ae0080…
|
lmata
|
344 |
source = b"import java.util.List;" |
|
7ae0080…
|
lmata
|
345 |
node = MockNode("import_declaration", start_byte=0, end_byte=22, |
|
7ae0080…
|
lmata
|
346 |
start_point=(0, 0)) |
|
7ae0080…
|
lmata
|
347 |
root = MockNode("program", children=[node]) |
|
7ae0080…
|
lmata
|
348 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
349 |
parser._walk(root, source, "Foo.java", store, stats, class_name=None) |
|
7ae0080…
|
lmata
|
350 |
assert stats["edges"] == 1 |
|
7ae0080…
|
lmata
|
351 |
|
|
7ae0080…
|
lmata
|
352 |
def test_walk_recurses_into_children(self): |
|
7ae0080…
|
lmata
|
353 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
354 |
store = _make_store() |
|
7ae0080…
|
lmata
|
355 |
source = b"import java.util.List;" |
|
7ae0080…
|
lmata
|
356 |
import_node = MockNode("import_declaration", start_byte=0, end_byte=22, |
|
7ae0080…
|
lmata
|
357 |
start_point=(0, 0)) |
|
7ae0080…
|
lmata
|
358 |
wrapper = MockNode("block", children=[import_node]) |
|
7ae0080…
|
lmata
|
359 |
root = MockNode("program", children=[wrapper]) |
|
7ae0080…
|
lmata
|
360 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
361 |
parser._walk(root, source, "Foo.java", store, stats, class_name=None) |
|
7ae0080…
|
lmata
|
362 |
assert stats["edges"] == 1 |
|
7ae0080…
|
lmata
|
363 |
|
|
7ae0080…
|
lmata
|
364 |
|
|
7ae0080…
|
lmata
|
365 |
# ── _handle_class nested inner class ───────────────────────────────────────── |
|
7ae0080…
|
lmata
|
366 |
|
|
7ae0080…
|
lmata
|
367 |
class TestJavaHandleClassNested: |
|
7ae0080…
|
lmata
|
368 |
def test_ingests_nested_inner_class(self): |
|
7ae0080…
|
lmata
|
369 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
370 |
store = _make_store() |
|
7ae0080…
|
lmata
|
371 |
source = b"Inner" |
|
7ae0080…
|
lmata
|
372 |
outer_name = _text_node(b"Outer") |
|
7ae0080…
|
lmata
|
373 |
inner_name = _text_node(b"Inner") |
|
7ae0080…
|
lmata
|
374 |
inner_class = MockNode("class_declaration", |
|
7ae0080…
|
lmata
|
375 |
start_point=(1, 4), end_point=(3, 4)) |
|
7ae0080…
|
lmata
|
376 |
inner_class.set_field("name", inner_name) |
|
7ae0080…
|
lmata
|
377 |
body = MockNode("class_body", children=[inner_class]) |
|
7ae0080…
|
lmata
|
378 |
outer_class = MockNode("class_declaration", |
|
7ae0080…
|
lmata
|
379 |
start_point=(0, 0), end_point=(4, 0)) |
|
7ae0080…
|
lmata
|
380 |
outer_class.set_field("name", outer_name) |
|
7ae0080…
|
lmata
|
381 |
outer_class.set_field("body", body) |
|
7ae0080…
|
lmata
|
382 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
383 |
parser._handle_class(outer_class, source, "Outer.java", store, stats) |
|
7ae0080…
|
lmata
|
384 |
# outer + inner class both registered |
|
7ae0080…
|
lmata
|
385 |
assert stats["classes"] == 2 |
|
7ae0080…
|
lmata
|
386 |
assert stats["edges"] == 2 # CONTAINS(File→Outer) + CONTAINS(Outer→Inner) |
|
7ae0080…
|
lmata
|
387 |
|
|
7ae0080…
|
lmata
|
388 |
|
|
7ae0080…
|
lmata
|
389 |
# ── _handle_interface with method body ─────────────────────────────────────── |
|
7ae0080…
|
lmata
|
390 |
|
|
7ae0080…
|
lmata
|
391 |
class TestJavaHandleInterfaceWithMethods: |
|
7ae0080…
|
lmata
|
392 |
def test_walks_methods_in_interface_body(self): |
|
7ae0080…
|
lmata
|
393 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
394 |
store = _make_store() |
|
7ae0080…
|
lmata
|
395 |
source = b"read" |
|
7ae0080…
|
lmata
|
396 |
iface_name = _text_node(b"Readable") |
|
7ae0080…
|
lmata
|
397 |
method_name = _text_node(b"read") |
|
7ae0080…
|
lmata
|
398 |
method = MockNode("method_declaration", |
|
7ae0080…
|
lmata
|
399 |
start_point=(1, 4), end_point=(1, 20)) |
|
7ae0080…
|
lmata
|
400 |
method.set_field("name", method_name) |
|
7ae0080…
|
lmata
|
401 |
body = MockNode("interface_body", children=[method]) |
|
7ae0080…
|
lmata
|
402 |
iface = MockNode("interface_declaration", |
|
7ae0080…
|
lmata
|
403 |
start_point=(0, 0), end_point=(2, 0)) |
|
7ae0080…
|
lmata
|
404 |
iface.set_field("name", iface_name) |
|
7ae0080…
|
lmata
|
405 |
iface.set_field("body", body) |
|
7ae0080…
|
lmata
|
406 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
407 |
parser._handle_interface(iface, source, "R.java", store, stats) |
|
7ae0080…
|
lmata
|
408 |
# interface node + method node |
|
7ae0080…
|
lmata
|
409 |
assert stats["classes"] == 1 |
|
7ae0080…
|
lmata
|
410 |
assert stats["functions"] == 1 |