|
e52aae0…
|
lmata
|
1 |
"""Tests for navegador.ingestion.go — GoParser 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._text = text |
|
e52aae0…
|
lmata
|
21 |
self.children = children or [] |
|
e52aae0…
|
lmata
|
22 |
self.start_byte = start_byte |
|
e52aae0…
|
lmata
|
23 |
self.end_byte = end_byte |
|
e52aae0…
|
lmata
|
24 |
self.start_point = start_point |
|
e52aae0…
|
lmata
|
25 |
self.end_point = end_point |
|
e52aae0…
|
lmata
|
26 |
self.parent = parent |
|
e52aae0…
|
lmata
|
27 |
self._fields: dict = {} |
|
e52aae0…
|
lmata
|
28 |
for child in self.children: |
|
e52aae0…
|
lmata
|
29 |
child.parent = self |
|
e52aae0…
|
lmata
|
30 |
|
|
e52aae0…
|
lmata
|
31 |
def child_by_field_name(self, name: str): |
|
e52aae0…
|
lmata
|
32 |
return self._fields.get(name) |
|
e52aae0…
|
lmata
|
33 |
|
|
e52aae0…
|
lmata
|
34 |
def set_field(self, name: str, node): |
|
e52aae0…
|
lmata
|
35 |
self._fields[name] = node |
|
e52aae0…
|
lmata
|
36 |
node.parent = self |
|
e52aae0…
|
lmata
|
37 |
return self |
|
e52aae0…
|
lmata
|
38 |
|
|
e52aae0…
|
lmata
|
39 |
|
|
e52aae0…
|
lmata
|
40 |
def _text_node(text: bytes, type_: str = "identifier") -> MockNode: |
|
e52aae0…
|
lmata
|
41 |
return MockNode(type_, text, start_byte=0, end_byte=len(text)) |
|
e52aae0…
|
lmata
|
42 |
|
|
e52aae0…
|
lmata
|
43 |
|
|
e52aae0…
|
lmata
|
44 |
def _make_store(): |
|
e52aae0…
|
lmata
|
45 |
store = MagicMock() |
|
e52aae0…
|
lmata
|
46 |
store.query.return_value = MagicMock(result_set=[]) |
|
e52aae0…
|
lmata
|
47 |
return store |
|
e52aae0…
|
lmata
|
48 |
|
|
e52aae0…
|
lmata
|
49 |
|
|
e52aae0…
|
lmata
|
50 |
def _make_parser(): |
|
e52aae0…
|
lmata
|
51 |
from navegador.ingestion.go import GoParser |
|
e52aae0…
|
lmata
|
52 |
parser = GoParser.__new__(GoParser) |
|
e52aae0…
|
lmata
|
53 |
parser._parser = MagicMock() |
|
e52aae0…
|
lmata
|
54 |
return parser |
|
e52aae0…
|
lmata
|
55 |
|
|
e52aae0…
|
lmata
|
56 |
|
|
e52aae0…
|
lmata
|
57 |
class TestGoGetLanguage: |
|
e52aae0…
|
lmata
|
58 |
def test_raises_when_not_installed(self): |
|
e52aae0…
|
lmata
|
59 |
from navegador.ingestion.go import _get_go_language |
|
e52aae0…
|
lmata
|
60 |
with patch.dict("sys.modules", {"tree_sitter_go": None, "tree_sitter": None}): |
|
e52aae0…
|
lmata
|
61 |
with pytest.raises(ImportError, match="tree-sitter-go"): |
|
e52aae0…
|
lmata
|
62 |
_get_go_language() |
|
e52aae0…
|
lmata
|
63 |
|
|
e52aae0…
|
lmata
|
64 |
|
|
e52aae0…
|
lmata
|
65 |
class TestGoNodeText: |
|
e52aae0…
|
lmata
|
66 |
def test_extracts_bytes(self): |
|
e52aae0…
|
lmata
|
67 |
from navegador.ingestion.go import _node_text |
|
e52aae0…
|
lmata
|
68 |
source = b"hello world" |
|
e52aae0…
|
lmata
|
69 |
node = MockNode("identifier", start_byte=6, end_byte=11) |
|
e52aae0…
|
lmata
|
70 |
assert _node_text(node, source) == "world" |
|
e52aae0…
|
lmata
|
71 |
|
|
e52aae0…
|
lmata
|
72 |
|
|
e52aae0…
|
lmata
|
73 |
class TestGoHandleFunction: |
|
e52aae0…
|
lmata
|
74 |
def test_creates_function_node(self): |
|
e52aae0…
|
lmata
|
75 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
76 |
store = _make_store() |
|
e52aae0…
|
lmata
|
77 |
source = b"func Foo() {}" |
|
e52aae0…
|
lmata
|
78 |
name = _text_node(b"Foo") |
|
e52aae0…
|
lmata
|
79 |
node = MockNode("function_declaration", start_point=(0, 0), end_point=(0, 12)) |
|
e52aae0…
|
lmata
|
80 |
node.set_field("name", name) |
|
e52aae0…
|
lmata
|
81 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
82 |
parser._handle_function(node, source, "main.go", store, stats, receiver=None) |
|
e52aae0…
|
lmata
|
83 |
assert stats["functions"] == 1 |
|
e52aae0…
|
lmata
|
84 |
store.create_node.assert_called_once() |
|
e52aae0…
|
lmata
|
85 |
label = store.create_node.call_args[0][0] |
|
e52aae0…
|
lmata
|
86 |
assert label == NodeLabel.Function |
|
e52aae0…
|
lmata
|
87 |
|
|
e52aae0…
|
lmata
|
88 |
def test_creates_method_when_receiver_given(self): |
|
e52aae0…
|
lmata
|
89 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
90 |
store = _make_store() |
|
e52aae0…
|
lmata
|
91 |
source = b"func (r *Repo) Save() {}" |
|
e52aae0…
|
lmata
|
92 |
name = _text_node(b"Save") |
|
e52aae0…
|
lmata
|
93 |
node = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 23)) |
|
e52aae0…
|
lmata
|
94 |
node.set_field("name", name) |
|
e52aae0…
|
lmata
|
95 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
96 |
parser._handle_function(node, source, "repo.go", store, stats, receiver="Repo") |
|
e52aae0…
|
lmata
|
97 |
label = store.create_node.call_args[0][0] |
|
e52aae0…
|
lmata
|
98 |
assert label == NodeLabel.Method |
|
e52aae0…
|
lmata
|
99 |
|
|
e52aae0…
|
lmata
|
100 |
def test_skips_if_no_name_node(self): |
|
e52aae0…
|
lmata
|
101 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
102 |
store = _make_store() |
|
e52aae0…
|
lmata
|
103 |
node = MockNode("function_declaration", start_point=(0, 0), end_point=(0, 0)) |
|
e52aae0…
|
lmata
|
104 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
105 |
parser._handle_function(node, b"", "main.go", store, stats, receiver=None) |
|
e52aae0…
|
lmata
|
106 |
assert stats["functions"] == 0 |
|
e52aae0…
|
lmata
|
107 |
|
|
e52aae0…
|
lmata
|
108 |
|
|
e52aae0…
|
lmata
|
109 |
class TestGoHandleType: |
|
e52aae0…
|
lmata
|
110 |
def test_ingests_struct(self): |
|
e52aae0…
|
lmata
|
111 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
112 |
store = _make_store() |
|
e52aae0…
|
lmata
|
113 |
source = b"type User struct {}" |
|
e52aae0…
|
lmata
|
114 |
name_node = _text_node(b"User", "type_identifier") |
|
e52aae0…
|
lmata
|
115 |
type_node = MockNode("struct_type") |
|
e52aae0…
|
lmata
|
116 |
spec = MockNode("type_spec") |
|
e52aae0…
|
lmata
|
117 |
spec.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
118 |
spec.set_field("type", type_node) |
|
e52aae0…
|
lmata
|
119 |
decl = MockNode("type_declaration", children=[spec], |
|
e52aae0…
|
lmata
|
120 |
start_point=(0, 0), end_point=(0, 18)) |
|
e52aae0…
|
lmata
|
121 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
122 |
parser._handle_type(decl, source, "main.go", store, stats) |
|
e52aae0…
|
lmata
|
123 |
assert stats["classes"] == 1 |
|
e52aae0…
|
lmata
|
124 |
assert stats["edges"] == 1 |
|
e52aae0…
|
lmata
|
125 |
|
|
e52aae0…
|
lmata
|
126 |
def test_ingests_interface(self): |
|
e52aae0…
|
lmata
|
127 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
128 |
store = _make_store() |
|
e52aae0…
|
lmata
|
129 |
source = b"type Reader interface {}" |
|
e52aae0…
|
lmata
|
130 |
name_node = _text_node(b"Reader", "type_identifier") |
|
e52aae0…
|
lmata
|
131 |
type_node = MockNode("interface_type") |
|
e52aae0…
|
lmata
|
132 |
spec = MockNode("type_spec") |
|
e52aae0…
|
lmata
|
133 |
spec.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
134 |
spec.set_field("type", type_node) |
|
e52aae0…
|
lmata
|
135 |
decl = MockNode("type_declaration", children=[spec], |
|
e52aae0…
|
lmata
|
136 |
start_point=(0, 0), end_point=(0, 23)) |
|
e52aae0…
|
lmata
|
137 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
138 |
parser._handle_type(decl, source, "main.go", store, stats) |
|
e52aae0…
|
lmata
|
139 |
assert stats["classes"] == 1 |
|
e52aae0…
|
lmata
|
140 |
|
|
e52aae0…
|
lmata
|
141 |
def test_skips_non_struct_interface(self): |
|
e52aae0…
|
lmata
|
142 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
143 |
store = _make_store() |
|
e52aae0…
|
lmata
|
144 |
source = b"type MyInt int" |
|
e52aae0…
|
lmata
|
145 |
name_node = _text_node(b"MyInt", "type_identifier") |
|
e52aae0…
|
lmata
|
146 |
type_node = MockNode("int") |
|
e52aae0…
|
lmata
|
147 |
spec = MockNode("type_spec") |
|
e52aae0…
|
lmata
|
148 |
spec.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
149 |
spec.set_field("type", type_node) |
|
e52aae0…
|
lmata
|
150 |
decl = MockNode("type_declaration", children=[spec], |
|
e52aae0…
|
lmata
|
151 |
start_point=(0, 0), end_point=(0, 13)) |
|
e52aae0…
|
lmata
|
152 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
153 |
parser._handle_type(decl, source, "main.go", store, stats) |
|
e52aae0…
|
lmata
|
154 |
assert stats["classes"] == 0 |
|
e52aae0…
|
lmata
|
155 |
|
|
e52aae0…
|
lmata
|
156 |
|
|
e52aae0…
|
lmata
|
157 |
class TestGoImportSpec: |
|
e52aae0…
|
lmata
|
158 |
def test_ingests_single_import(self): |
|
e52aae0…
|
lmata
|
159 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
160 |
store = _make_store() |
|
e52aae0…
|
lmata
|
161 |
source = b'"fmt"' |
|
e52aae0…
|
lmata
|
162 |
path_node = MockNode("interpreted_string_literal", start_byte=0, end_byte=5) |
|
e52aae0…
|
lmata
|
163 |
spec = MockNode("import_spec") |
|
e52aae0…
|
lmata
|
164 |
spec.set_field("path", path_node) |
|
e52aae0…
|
lmata
|
165 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
166 |
parser._ingest_import_spec(spec, source, "main.go", 1, store, stats) |
|
e52aae0…
|
lmata
|
167 |
assert stats["edges"] == 1 |
|
e52aae0…
|
lmata
|
168 |
store.create_node.assert_called_once() |
|
e52aae0…
|
lmata
|
169 |
|
|
e52aae0…
|
lmata
|
170 |
def test_skips_spec_without_path(self): |
|
e52aae0…
|
lmata
|
171 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
172 |
store = _make_store() |
|
e52aae0…
|
lmata
|
173 |
spec = MockNode("import_spec") |
|
e52aae0…
|
lmata
|
174 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
175 |
parser._ingest_import_spec(spec, b"", "main.go", 1, store, stats) |
|
e52aae0…
|
lmata
|
176 |
assert stats["edges"] == 0 |
|
e52aae0…
|
lmata
|
177 |
|
|
e52aae0…
|
lmata
|
178 |
|
|
e52aae0…
|
lmata
|
179 |
class TestGoExtractCalls: |
|
e52aae0…
|
lmata
|
180 |
def test_extracts_call(self): |
|
e52aae0…
|
lmata
|
181 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
182 |
store = _make_store() |
|
2e96458…
|
lmata
|
183 |
source = b"bar" |
|
e52aae0…
|
lmata
|
184 |
callee = _text_node(b"bar") |
|
e52aae0…
|
lmata
|
185 |
call_node = MockNode("call_expression") |
|
e52aae0…
|
lmata
|
186 |
call_node.set_field("function", callee) |
|
e52aae0…
|
lmata
|
187 |
body = MockNode("block", children=[call_node]) |
|
e52aae0…
|
lmata
|
188 |
fn_node = MockNode("function_declaration") |
|
e52aae0…
|
lmata
|
189 |
fn_node.set_field("body", body) |
|
e52aae0…
|
lmata
|
190 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
191 |
parser._extract_calls(fn_node, source, "main.go", "foo", |
|
e52aae0…
|
lmata
|
192 |
NodeLabel.Function, store, stats) |
|
e52aae0…
|
lmata
|
193 |
assert stats["edges"] == 1 |
|
2e96458…
|
lmata
|
194 |
edge_call = store.create_edge.call_args[0] |
|
2e96458…
|
lmata
|
195 |
assert edge_call[4]["name"] == "bar" |
|
e52aae0…
|
lmata
|
196 |
|
|
e52aae0…
|
lmata
|
197 |
def test_no_calls_in_empty_body(self): |
|
e52aae0…
|
lmata
|
198 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
199 |
store = _make_store() |
|
e52aae0…
|
lmata
|
200 |
fn_node = MockNode("function_declaration") |
|
e52aae0…
|
lmata
|
201 |
fn_node.set_field("body", MockNode("block")) |
|
e52aae0…
|
lmata
|
202 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
203 |
parser._extract_calls(fn_node, b"", "main.go", "foo", |
|
e52aae0…
|
lmata
|
204 |
NodeLabel.Function, store, stats) |
|
e52aae0…
|
lmata
|
205 |
assert stats["edges"] == 0 |
|
e52aae0…
|
lmata
|
206 |
|
|
e52aae0…
|
lmata
|
207 |
|
|
e52aae0…
|
lmata
|
208 |
class TestGoWalkDispatch: |
|
e52aae0…
|
lmata
|
209 |
def test_walk_handles_function(self): |
|
e52aae0…
|
lmata
|
210 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
211 |
store = _make_store() |
|
e52aae0…
|
lmata
|
212 |
source = b"func foo() {}" |
|
e52aae0…
|
lmata
|
213 |
name = _text_node(b"foo") |
|
e52aae0…
|
lmata
|
214 |
fn = MockNode("function_declaration", start_point=(0, 0), end_point=(0, 12)) |
|
e52aae0…
|
lmata
|
215 |
fn.set_field("name", name) |
|
e52aae0…
|
lmata
|
216 |
root = MockNode("source_file", children=[fn]) |
|
e52aae0…
|
lmata
|
217 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
218 |
parser._walk(root, source, "main.go", store, stats) |
|
e52aae0…
|
lmata
|
219 |
assert stats["functions"] == 1 |
|
e52aae0…
|
lmata
|
220 |
|
|
e52aae0…
|
lmata
|
221 |
def test_walk_handles_type(self): |
|
e52aae0…
|
lmata
|
222 |
parser = _make_parser() |
|
e52aae0…
|
lmata
|
223 |
store = _make_store() |
|
e52aae0…
|
lmata
|
224 |
source = b"type Foo struct {}" |
|
e52aae0…
|
lmata
|
225 |
name_node = _text_node(b"Foo", "type_identifier") |
|
e52aae0…
|
lmata
|
226 |
type_node = MockNode("struct_type") |
|
e52aae0…
|
lmata
|
227 |
spec = MockNode("type_spec") |
|
e52aae0…
|
lmata
|
228 |
spec.set_field("name", name_node) |
|
e52aae0…
|
lmata
|
229 |
spec.set_field("type", type_node) |
|
e52aae0…
|
lmata
|
230 |
decl = MockNode("type_declaration", children=[spec], |
|
e52aae0…
|
lmata
|
231 |
start_point=(0, 0), end_point=(0, 17)) |
|
e52aae0…
|
lmata
|
232 |
root = MockNode("source_file", children=[decl]) |
|
e52aae0…
|
lmata
|
233 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
e52aae0…
|
lmata
|
234 |
parser._walk(root, source, "main.go", store, stats) |
|
e52aae0…
|
lmata
|
235 |
assert stats["classes"] == 1 |
|
7ae0080…
|
lmata
|
236 |
|
|
7ae0080…
|
lmata
|
237 |
|
|
7ae0080…
|
lmata
|
238 |
# ── _get_go_language happy path ─────────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
239 |
|
|
7ae0080…
|
lmata
|
240 |
class TestGoGetLanguageHappyPath: |
|
7ae0080…
|
lmata
|
241 |
def test_returns_language_object(self): |
|
7ae0080…
|
lmata
|
242 |
from navegador.ingestion.go import _get_go_language |
|
7ae0080…
|
lmata
|
243 |
mock_tsgo = MagicMock() |
|
7ae0080…
|
lmata
|
244 |
mock_ts = MagicMock() |
|
7ae0080…
|
lmata
|
245 |
with patch.dict("sys.modules", { |
|
7ae0080…
|
lmata
|
246 |
"tree_sitter_go": mock_tsgo, |
|
7ae0080…
|
lmata
|
247 |
"tree_sitter": mock_ts, |
|
7ae0080…
|
lmata
|
248 |
}): |
|
7ae0080…
|
lmata
|
249 |
result = _get_go_language() |
|
7ae0080…
|
lmata
|
250 |
assert result is mock_ts.Language.return_value |
|
7ae0080…
|
lmata
|
251 |
|
|
7ae0080…
|
lmata
|
252 |
|
|
7ae0080…
|
lmata
|
253 |
# ── GoParser init and parse_file ───────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
254 |
|
|
7ae0080…
|
lmata
|
255 |
class TestGoParserInit: |
|
7ae0080…
|
lmata
|
256 |
def test_init_creates_parser(self): |
|
7ae0080…
|
lmata
|
257 |
mock_tsgo = MagicMock() |
|
7ae0080…
|
lmata
|
258 |
mock_ts = MagicMock() |
|
7ae0080…
|
lmata
|
259 |
with patch.dict("sys.modules", { |
|
7ae0080…
|
lmata
|
260 |
"tree_sitter_go": mock_tsgo, |
|
7ae0080…
|
lmata
|
261 |
"tree_sitter": mock_ts, |
|
7ae0080…
|
lmata
|
262 |
}): |
|
7ae0080…
|
lmata
|
263 |
from navegador.ingestion.go import GoParser |
|
7ae0080…
|
lmata
|
264 |
parser = GoParser() |
|
7ae0080…
|
lmata
|
265 |
assert parser._parser is mock_ts.Parser.return_value |
|
7ae0080…
|
lmata
|
266 |
|
|
7ae0080…
|
lmata
|
267 |
def test_parse_file_creates_file_node(self): |
|
7ae0080…
|
lmata
|
268 |
import tempfile |
|
7ae0080…
|
lmata
|
269 |
from pathlib import Path |
|
7ae0080…
|
lmata
|
270 |
|
|
7ae0080…
|
lmata
|
271 |
from navegador.graph.schema import NodeLabel |
|
7ae0080…
|
lmata
|
272 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
273 |
store = _make_store() |
|
7ae0080…
|
lmata
|
274 |
mock_tree = MagicMock() |
|
7ae0080…
|
lmata
|
275 |
mock_tree.root_node.type = "source_file" |
|
7ae0080…
|
lmata
|
276 |
mock_tree.root_node.children = [] |
|
7ae0080…
|
lmata
|
277 |
parser._parser.parse.return_value = mock_tree |
|
7ae0080…
|
lmata
|
278 |
with tempfile.NamedTemporaryFile(suffix=".go", delete=False) as f: |
|
7ae0080…
|
lmata
|
279 |
f.write(b"package main\n") |
|
7ae0080…
|
lmata
|
280 |
fpath = Path(f.name) |
|
7ae0080…
|
lmata
|
281 |
try: |
|
7ae0080…
|
lmata
|
282 |
parser.parse_file(fpath, fpath.parent, store) |
|
7ae0080…
|
lmata
|
283 |
store.create_node.assert_called_once() |
|
7ae0080…
|
lmata
|
284 |
assert store.create_node.call_args[0][0] == NodeLabel.File |
|
7ae0080…
|
lmata
|
285 |
assert store.create_node.call_args[0][1]["language"] == "go" |
|
7ae0080…
|
lmata
|
286 |
finally: |
|
7ae0080…
|
lmata
|
287 |
fpath.unlink() |
|
7ae0080…
|
lmata
|
288 |
|
|
7ae0080…
|
lmata
|
289 |
|
|
7ae0080…
|
lmata
|
290 |
# ── _handle_method ──────────────────────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
291 |
|
|
7ae0080…
|
lmata
|
292 |
class TestGoHandleMethod: |
|
7ae0080…
|
lmata
|
293 |
def test_extracts_value_receiver(self): |
|
7ae0080…
|
lmata
|
294 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
295 |
store = _make_store() |
|
7ae0080…
|
lmata
|
296 |
source = b"Repo" |
|
7ae0080…
|
lmata
|
297 |
type_id = MockNode("type_identifier", start_byte=0, end_byte=4) |
|
7ae0080…
|
lmata
|
298 |
param_decl = MockNode("parameter_declaration", children=[type_id]) |
|
7ae0080…
|
lmata
|
299 |
recv_list = MockNode("parameter_list", children=[param_decl]) |
|
7ae0080…
|
lmata
|
300 |
name = _text_node(b"Save") |
|
7ae0080…
|
lmata
|
301 |
node = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 30)) |
|
7ae0080…
|
lmata
|
302 |
node.set_field("receiver", recv_list) |
|
7ae0080…
|
lmata
|
303 |
node.set_field("name", name) |
|
7ae0080…
|
lmata
|
304 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
305 |
parser._handle_method(node, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
306 |
assert stats["functions"] == 1 |
|
7ae0080…
|
lmata
|
307 |
label = store.create_node.call_args[0][0] |
|
7ae0080…
|
lmata
|
308 |
from navegador.graph.schema import NodeLabel |
|
7ae0080…
|
lmata
|
309 |
assert label == NodeLabel.Method |
|
7ae0080…
|
lmata
|
310 |
|
|
7ae0080…
|
lmata
|
311 |
def test_extracts_pointer_receiver(self): |
|
7ae0080…
|
lmata
|
312 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
313 |
store = _make_store() |
|
7ae0080…
|
lmata
|
314 |
source = b"*Repo" |
|
7ae0080…
|
lmata
|
315 |
ptr_type = MockNode("pointer_type", start_byte=0, end_byte=5) |
|
7ae0080…
|
lmata
|
316 |
param_decl = MockNode("parameter_declaration", children=[ptr_type]) |
|
7ae0080…
|
lmata
|
317 |
recv_list = MockNode("parameter_list", children=[param_decl]) |
|
7ae0080…
|
lmata
|
318 |
name = _text_node(b"Delete") |
|
7ae0080…
|
lmata
|
319 |
node = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 30)) |
|
7ae0080…
|
lmata
|
320 |
node.set_field("receiver", recv_list) |
|
7ae0080…
|
lmata
|
321 |
node.set_field("name", name) |
|
7ae0080…
|
lmata
|
322 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
323 |
parser._handle_method(node, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
324 |
# pointer receiver "*Repo" → lstrip("*") → "Repo" |
|
7ae0080…
|
lmata
|
325 |
assert stats["functions"] == 1 |
|
7ae0080…
|
lmata
|
326 |
|
|
7ae0080…
|
lmata
|
327 |
def test_no_receiver_field(self): |
|
7ae0080…
|
lmata
|
328 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
329 |
store = _make_store() |
|
7ae0080…
|
lmata
|
330 |
source = b"foo" |
|
7ae0080…
|
lmata
|
331 |
name = _text_node(b"foo") |
|
7ae0080…
|
lmata
|
332 |
node = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 10)) |
|
7ae0080…
|
lmata
|
333 |
node.set_field("name", name) |
|
7ae0080…
|
lmata
|
334 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
335 |
parser._handle_method(node, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
336 |
# No receiver → treated as plain function |
|
7ae0080…
|
lmata
|
337 |
assert stats["functions"] == 1 |
|
7ae0080…
|
lmata
|
338 |
|
|
7ae0080…
|
lmata
|
339 |
|
|
7ae0080…
|
lmata
|
340 |
# ── _handle_import with import_spec_list ───────────────────────────────────── |
|
7ae0080…
|
lmata
|
341 |
|
|
7ae0080…
|
lmata
|
342 |
class TestGoHandleImportSpecList: |
|
7ae0080…
|
lmata
|
343 |
def test_handles_grouped_imports(self): |
|
7ae0080…
|
lmata
|
344 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
345 |
store = _make_store() |
|
7ae0080…
|
lmata
|
346 |
source = b'"fmt"' |
|
7ae0080…
|
lmata
|
347 |
path_node = MockNode("interpreted_string_literal", start_byte=0, end_byte=5) |
|
7ae0080…
|
lmata
|
348 |
spec1 = MockNode("import_spec") |
|
7ae0080…
|
lmata
|
349 |
spec1.set_field("path", path_node) |
|
7ae0080…
|
lmata
|
350 |
spec_list = MockNode("import_spec_list", children=[spec1]) |
|
7ae0080…
|
lmata
|
351 |
import_node = MockNode("import_declaration", |
|
7ae0080…
|
lmata
|
352 |
children=[spec_list], |
|
7ae0080…
|
lmata
|
353 |
start_point=(0, 0)) |
|
7ae0080…
|
lmata
|
354 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
355 |
parser._handle_import(import_node, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
356 |
assert stats["edges"] == 1 |
|
7ae0080…
|
lmata
|
357 |
|
|
7ae0080…
|
lmata
|
358 |
|
|
7ae0080…
|
lmata
|
359 |
# ── _walk dispatch additions ────────────────────────────────────────────────── |
|
7ae0080…
|
lmata
|
360 |
|
|
7ae0080…
|
lmata
|
361 |
class TestGoWalkDispatchAdditional: |
|
7ae0080…
|
lmata
|
362 |
def test_walk_handles_method_declaration(self): |
|
7ae0080…
|
lmata
|
363 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
364 |
store = _make_store() |
|
7ae0080…
|
lmata
|
365 |
source = b"Repo" |
|
7ae0080…
|
lmata
|
366 |
type_id = MockNode("type_identifier", start_byte=0, end_byte=4) |
|
7ae0080…
|
lmata
|
367 |
param_decl = MockNode("parameter_declaration", children=[type_id]) |
|
7ae0080…
|
lmata
|
368 |
recv_list = MockNode("parameter_list", children=[param_decl]) |
|
7ae0080…
|
lmata
|
369 |
name = _text_node(b"Save") |
|
7ae0080…
|
lmata
|
370 |
method = MockNode("method_declaration", start_point=(0, 0), end_point=(0, 30)) |
|
7ae0080…
|
lmata
|
371 |
method.set_field("receiver", recv_list) |
|
7ae0080…
|
lmata
|
372 |
method.set_field("name", name) |
|
7ae0080…
|
lmata
|
373 |
root = MockNode("source_file", children=[method]) |
|
7ae0080…
|
lmata
|
374 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
375 |
parser._walk(root, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
376 |
assert stats["functions"] == 1 |
|
7ae0080…
|
lmata
|
377 |
|
|
7ae0080…
|
lmata
|
378 |
def test_walk_handles_import_declaration(self): |
|
7ae0080…
|
lmata
|
379 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
380 |
store = _make_store() |
|
7ae0080…
|
lmata
|
381 |
source = b'"fmt"' |
|
7ae0080…
|
lmata
|
382 |
path_node = MockNode("interpreted_string_literal", start_byte=0, end_byte=5) |
|
7ae0080…
|
lmata
|
383 |
spec = MockNode("import_spec") |
|
7ae0080…
|
lmata
|
384 |
spec.set_field("path", path_node) |
|
7ae0080…
|
lmata
|
385 |
import_node = MockNode("import_declaration", |
|
7ae0080…
|
lmata
|
386 |
children=[spec], |
|
7ae0080…
|
lmata
|
387 |
start_point=(0, 0)) |
|
7ae0080…
|
lmata
|
388 |
root = MockNode("source_file", children=[import_node]) |
|
7ae0080…
|
lmata
|
389 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
390 |
parser._walk(root, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
391 |
assert stats["edges"] == 1 |
|
7ae0080…
|
lmata
|
392 |
|
|
7ae0080…
|
lmata
|
393 |
|
|
7ae0080…
|
lmata
|
394 |
class TestGoHandleTypeContinueBranches: |
|
7ae0080…
|
lmata
|
395 |
def test_skips_non_type_spec_children(self): |
|
7ae0080…
|
lmata
|
396 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
397 |
store = _make_store() |
|
7ae0080…
|
lmata
|
398 |
source = b"" |
|
7ae0080…
|
lmata
|
399 |
# Child that is not type_spec |
|
7ae0080…
|
lmata
|
400 |
comment_child = MockNode("comment") |
|
7ae0080…
|
lmata
|
401 |
decl = MockNode("type_declaration", children=[comment_child], |
|
7ae0080…
|
lmata
|
402 |
start_point=(0, 0), end_point=(0, 10)) |
|
7ae0080…
|
lmata
|
403 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
404 |
parser._handle_type(decl, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
405 |
assert stats["classes"] == 0 |
|
7ae0080…
|
lmata
|
406 |
|
|
7ae0080…
|
lmata
|
407 |
def test_skips_type_spec_without_name_or_type(self): |
|
7ae0080…
|
lmata
|
408 |
parser = _make_parser() |
|
7ae0080…
|
lmata
|
409 |
store = _make_store() |
|
7ae0080…
|
lmata
|
410 |
source = b"" |
|
7ae0080…
|
lmata
|
411 |
# type_spec with no fields set |
|
7ae0080…
|
lmata
|
412 |
spec = MockNode("type_spec") |
|
7ae0080…
|
lmata
|
413 |
decl = MockNode("type_declaration", children=[spec], |
|
7ae0080…
|
lmata
|
414 |
start_point=(0, 0), end_point=(0, 10)) |
|
7ae0080…
|
lmata
|
415 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
7ae0080…
|
lmata
|
416 |
parser._handle_type(decl, source, "main.go", store, stats) |
|
7ae0080…
|
lmata
|
417 |
assert stats["classes"] == 0 |