-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cpp_parser.py
More file actions
319 lines (248 loc) · 8.55 KB
/
Copy pathtest_cpp_parser.py
File metadata and controls
319 lines (248 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from unittest.mock import MagicMock
import pytest
from codegraphcontext_cpp._parser import CppTreeSitterParser
from codegraphcontext.utils.tree_sitter_manager import get_tree_sitter_manager
@pytest.fixture(scope="module")
def cpp_parser():
manager = get_tree_sitter_manager()
if not manager.is_language_available("cpp"):
pytest.skip("C++ tree-sitter grammar is not available in this environment")
wrapper = MagicMock()
wrapper.language_name = "cpp"
wrapper.language = manager.get_language_safe("cpp")
wrapper.parser = manager.create_parser("cpp")
return CppTreeSitterParser(wrapper)
# --- Bugfix: _find_enums NameError ---
def test_enum_parsing(cpp_parser, temp_test_dir):
code = """
enum Color { RED, GREEN, BLUE };
enum class Status { OK = 0, ERROR = 1 };
"""
f = temp_test_dir / "enums.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
enum_names = [e["name"] for e in result.get("enums", [])]
assert "Color" in enum_names
assert "Status" in enum_names
def test_file_with_enums_and_classes(cpp_parser, temp_test_dir):
"""Ensure files containing both enums and classes parse without errors."""
code = """
enum DataType { INT = 0, VARCHAR = 1 };
class Foo {
public:
void bar() {}
};
"""
f = temp_test_dir / "mixed.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
assert any(c["name"] == "Foo" for c in result["classes"])
assert any(e["name"] == "DataType" for e in result.get("enums", []))
# --- Fix 1: Inheritance / base class extraction ---
def test_single_public_inheritance(cpp_parser, temp_test_dir):
code = """
class Base {
public:
virtual void execute() {}
};
class Derived : public Base {
public:
void execute() override {}
};
"""
f = temp_test_dir / "inherit_single.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
assert "Base" in classes
assert "Derived" in classes
assert classes["Base"]["bases"] == []
assert classes["Derived"]["bases"] == ["Base"]
def test_multiple_inheritance(cpp_parser, temp_test_dir):
code = """
class A {};
class B {};
class C : public A, private B {};
"""
f = temp_test_dir / "inherit_multi.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
assert classes["C"]["bases"] == ["A", "B"]
def test_virtual_inheritance(cpp_parser, temp_test_dir):
code = """
class Base {};
class Derived : virtual public Base {};
"""
f = temp_test_dir / "inherit_virtual.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
assert classes["Derived"]["bases"] == ["Base"]
def test_template_base_class(cpp_parser, temp_test_dir):
code = """
template<typename T>
class Container {};
class IntContainer : public Container<int> {};
"""
f = temp_test_dir / "inherit_template.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
# Template args should be stripped for graph matching
assert "Container" in classes["IntContainer"]["bases"]
def test_qualified_base_class(cpp_parser, temp_test_dir):
code = """
namespace ns {
class Base {};
}
class Derived : public ns::Base {};
"""
f = temp_test_dir / "inherit_qualified.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
assert len(classes["Derived"]["bases"]) == 1
# Should capture the qualified name
assert "Base" in classes["Derived"]["bases"][0]
# --- Fix 2: Qualified function definitions (ClassName::method in .cpp files) ---
def test_qualified_method_definition(cpp_parser, temp_test_dir):
code = """
void QueueElement::execute() {
return;
}
void QueueElement::setStatus(int status) {
this->status = status;
}
void free_function() {
return;
}
"""
f = temp_test_dir / "methods.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
func_names = [fn["name"] for fn in result["functions"]]
assert "execute" in func_names
assert "setStatus" in func_names
assert "free_function" in func_names
# Verify class_context is set for qualified methods
execute_fn = next(fn for fn in result["functions"] if fn["name"] == "execute")
assert execute_fn.get("class_context") == "QueueElement"
set_status_fn = next(fn for fn in result["functions"] if fn["name"] == "setStatus")
assert set_status_fn.get("class_context") == "QueueElement"
free_fn = next(fn for fn in result["functions"] if fn["name"] == "free_function")
assert free_fn.get("class_context") is None
def test_nested_qualified_method(cpp_parser, temp_test_dir):
code = """
void Namespace::Class::method() {
return;
}
"""
f = temp_test_dir / "nested_method.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
func_names = [fn["name"] for fn in result["functions"]]
assert "method" in func_names
method_fn = next(fn for fn in result["functions"] if fn["name"] == "method")
assert method_fn.get("class_context") == "Namespace::Class"
# --- Fix 3: Call expression matching (-> and :: calls) ---
def test_arrow_method_calls(cpp_parser, temp_test_dir):
code = """
void doWork() {
obj->execute();
ptr->setStatus(1);
}
"""
f = temp_test_dir / "arrow_calls.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
call_names = [c["name"] for c in result["function_calls"]]
assert "execute" in call_names
assert "setStatus" in call_names
def test_scoped_calls(cpp_parser, temp_test_dir):
code = """
void doWork() {
std::move(x);
QueueElement::setStatus(1);
}
"""
f = temp_test_dir / "scoped_calls.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
call_names = [c["name"] for c in result["function_calls"]]
assert "move" in call_names
assert "setStatus" in call_names
# Check inferred_obj_type for scoped calls
move_call = next(c for c in result["function_calls"] if c["name"] == "move")
assert move_call["inferred_obj_type"] == "std"
status_call = next(c for c in result["function_calls"] if c["name"] == "setStatus")
assert status_call["inferred_obj_type"] == "QueueElement"
def test_this_pointer_calls(cpp_parser, temp_test_dir):
code = """
void MyClass::doWork() {
this->execute();
this->setStatus(1);
}
"""
f = temp_test_dir / "this_calls.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
call_names = [c["name"] for c in result["function_calls"]]
assert "execute" in call_names
assert "setStatus" in call_names
for call in result["function_calls"]:
assert call["inferred_obj_type"] == "this"
def test_direct_function_calls(cpp_parser, temp_test_dir):
code = """
void doWork() {
printf("hello");
free_function();
}
"""
f = temp_test_dir / "direct_calls.cpp"
f.write_text(code)
result = cpp_parser.parse(f)
call_names = [c["name"] for c in result["function_calls"]]
assert "printf" in call_names
assert "free_function" in call_names
# --- Integration: realistic C++ header with inheritance + methods ---
def test_realistic_header(cpp_parser, temp_test_dir):
code = """
class QueueElement_Manuell {
public:
virtual std::string getRequestInformation() = 0;
};
class QueueElement : public QueueElement_Manuell {
public:
virtual int execute() { return 0; }
virtual std::string getMonitorInfo() = 0;
};
class QueueElement_Dialer : public QueueElement {
public:
virtual int execute() { return 0; }
};
class QueueElement_Dialer_Export : public QueueElement_Dialer {
public:
virtual int execute();
virtual std::string getMonitorInfo();
};
class QueueElement_Dialer_Export_File : public QueueElement_Dialer_Export {
public:
QueueElement_Dialer_Export_File() {}
};
class QueueElement_Dialer_Export_Axa : public QueueElement_Dialer_Export {
public:
virtual int execute();
virtual std::string getMonitorInfo();
};
"""
f = temp_test_dir / "realistic.h"
f.write_text(code)
result = cpp_parser.parse(f)
classes = {c["name"]: c for c in result["classes"]}
assert classes["QueueElement_Manuell"]["bases"] == []
assert classes["QueueElement"]["bases"] == ["QueueElement_Manuell"]
assert classes["QueueElement_Dialer"]["bases"] == ["QueueElement"]
assert classes["QueueElement_Dialer_Export"]["bases"] == ["QueueElement_Dialer"]
assert classes["QueueElement_Dialer_Export_File"]["bases"] == ["QueueElement_Dialer_Export"]
assert classes["QueueElement_Dialer_Export_Axa"]["bases"] == ["QueueElement_Dialer_Export"]