-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_code_parser.py
More file actions
377 lines (287 loc) Β· 8.32 KB
/
Copy pathtest_code_parser.py
File metadata and controls
377 lines (287 loc) Β· 8.32 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
Test script for multi-language code parser.
"""
import os
# Set mock env vars before importing app modules
os.environ["GEMINI_API_KEYS"] = "fake_key"
os.environ["APP_ID"] = "123"
os.environ["PRIVATE_KEY_PATH"] = "fake_path"
os.environ["WEBHOOK_SECRET"] = "fake_secret"
from app.core.code_parser import code_parser, CodeSymbols
def test_python():
"""Test Python parsing."""
print("\n" + "=" * 60)
print("TEST: Python")
print("=" * 60)
code = """
import os
from typing import List, Dict
class UserService:
def get_user(self, id: int) -> Dict:
return {"id": id}
def main():
service = UserService()
user = service.get_user(1)
print(user)
"""
symbols = code_parser.parse(code, "app/service.py")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"π¦ From Imports: {symbols.from_imports}")
print(f"ποΈ Classes: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "python"
assert "os" in symbols.imports
assert len(symbols.classes) >= 1
assert "main" in symbols.functions
print("β
Python test PASSED!")
def test_javascript():
"""Test JavaScript parsing."""
print("\n" + "=" * 60)
print("TEST: JavaScript")
print("=" * 60)
code = """
import React from 'react';
import { useState } from 'react';
class Component extends React.Component {
render() {
return <div>Hello</div>;
}
}
function App() {
const [count, setCount] = useState(0);
return <div>{count}</div>;
}
const helper = () => console.log('hi');
"""
symbols = code_parser.parse(code, "app/App.jsx")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"ποΈ Classes: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "javascript"
assert "react" in symbols.imports
assert "Component" in symbols.classes
assert "App" in symbols.functions
print("β
JavaScript test PASSED!")
def test_typescript():
"""Test TypeScript parsing."""
print("\n" + "=" * 60)
print("TEST: TypeScript")
print("=" * 60)
code = """
import axios from 'axios';
interface User {
id: number;
name: string;
}
class UserAPI {
async getUser(id: number): Promise<User> {
const response = await axios.get(`/users/${id}`);
return response.data;
}
}
function createUser(name: string): User {
return { id: 1, name };
}
"""
symbols = code_parser.parse(code, "api/user.ts")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"ποΈ Classes: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "typescript"
assert "axios" in symbols.imports
assert "UserAPI" in symbols.classes
assert "createUser" in symbols.functions
print("β
TypeScript test PASSED!")
def test_java():
"""Test Java parsing."""
print("\n" + "=" * 60)
print("TEST: Java")
print("=" * 60)
code = """
package com.example;
import java.util.List;
import java.util.ArrayList;
public class UserService {
private List<User> users = new ArrayList<>();
public User findById(Long id) {
return users.stream()
.filter(u -> u.getId().equals(id))
.findFirst()
.orElse(null);
}
public void addUser(User user) {
users.add(user);
}
}
"""
symbols = code_parser.parse(code, "UserService.java")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"ποΈ Classes: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "java"
assert any("List" in imp for imp in symbols.imports)
assert "UserService" in symbols.classes
assert "findById" in symbols.functions
print("β
Java test PASSED!")
def test_go():
"""Test Go parsing."""
print("\n" + "=" * 60)
print("TEST: Go")
print("=" * 60)
code = """
package main
import (
"fmt"
"net/http"
)
type User struct {
ID int
Name string
}
func (u *User) String() string {
return fmt.Sprintf("User{ID: %d, Name: %s}", u.ID, u.Name)
}
func GetUser(id int) *User {
return &User{ID: id, Name: "John"}
}
func main() {
user := GetUser(1)
fmt.Println(user)
}
"""
symbols = code_parser.parse(code, "main.go")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"ποΈ Types: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "go"
assert "fmt" in symbols.imports
assert "User" in symbols.classes
assert "GetUser" in symbols.functions or "main" in symbols.functions
print("β
Go test PASSED!")
def test_rust():
"""Test Rust parsing."""
print("\n" + "=" * 60)
print("TEST: Rust")
print("=" * 60)
code = """
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct User {
id: u32,
name: String,
}
impl User {
fn new(id: u32, name: String) -> Self {
User { id, name }
}
}
fn get_user(id: u32) -> Option<User> {
Some(User::new(id, "John".to_string()))
}
fn main() {
let user = get_user(1);
println!("{:?}", user);
}
"""
symbols = code_parser.parse(code, "main.rs")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Imports: {symbols.imports}")
print(f"ποΈ Structs: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "rust"
assert any("HashMap" in imp or "std" in imp for imp in symbols.imports)
assert "User" in symbols.classes
assert "get_user" in symbols.functions or "main" in symbols.functions
print("β
Rust test PASSED!")
def test_cpp():
"""Test C++ parsing."""
print("\n" + "=" * 60)
print("TEST: C++")
print("=" * 60)
code = """
#include <iostream>
#include <vector>
#include "user.h"
class UserManager {
public:
void addUser(const User& user) {
users.push_back(user);
}
User* findById(int id) {
for (auto& user : users) {
if (user.id == id) return &user;
}
return nullptr;
}
private:
std::vector<User> users;
};
int main() {
UserManager manager;
return 0;
}
"""
symbols = code_parser.parse(code, "main.cpp")
print(f"π¦ Language: {symbols.language}")
print(f"π¦ Includes: {symbols.imports}")
print(f"ποΈ Classes: {symbols.classes}")
print(f"π§ Functions: {symbols.functions}")
assert symbols.language == "cpp"
assert "iostream" in symbols.imports
assert "UserManager" in symbols.classes
assert "main" in symbols.functions
print("β
C++ test PASSED!")
def test_summary():
"""Test summary generation."""
print("\n" + "=" * 60)
print("TEST: Summary Generation")
print("=" * 60)
code = """
import os
from typing import List
class MyClass:
pass
def my_function():
pass
"""
summary = code_parser.get_summary(code, "test.py")
print(f"π Summary:\n{summary}")
assert "python" in summary.lower() or "Python" in summary
assert "MyClass" in summary
assert "my_function" in summary
print("β
Summary test PASSED!")
if __name__ == "__main__":
try:
test_python()
test_javascript()
test_typescript()
test_java()
test_go()
test_rust()
test_cpp()
test_summary()
print("\n" + "=" * 60)
print("π ALL MULTI-LANGUAGE TESTS PASSED!")
print("=" * 60)
print("\nSupported languages:")
print(" β
Python")
print(" β
JavaScript/JSX")
print(" β
TypeScript/TSX")
print(" β
Java")
print(" β
Go")
print(" β
Rust")
print(" β
C/C++")
except AssertionError as e:
print(f"\nβ TEST FAILED: {e}")
import traceback
traceback.print_exc()
except Exception as e:
print(f"\nβ ERROR: {e}")
import traceback
traceback.print_exc()