Skip to content

Commit 7b04260

Browse files
committed
added few more tests
1 parent e499d47 commit 7b04260

File tree

2 files changed

+174
-7
lines changed

2 files changed

+174
-7
lines changed

sourcecode-parser/model/node_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package model
22

33
import (
44
"testing"
5+
56
"github.com/stretchr/testify/assert"
67
)
78

@@ -102,19 +103,19 @@ func TestTreeNode(t *testing.T) {
102103
root := &TreeNode{
103104
Node: &Node{NodeType: "root", NodeID: 1},
104105
}
105-
106+
106107
child1 := &TreeNode{
107-
Node: &Node{NodeType: "child1", NodeID: 2},
108+
Node: &Node{NodeType: "child1", NodeID: 2},
108109
Parent: root,
109110
}
110-
111+
111112
child2 := &TreeNode{
112-
Node: &Node{NodeType: "child2", NodeID: 3},
113+
Node: &Node{NodeType: "child2", NodeID: 3},
113114
Parent: root,
114115
}
115-
116+
116117
grandchild1 := &TreeNode{
117-
Node: &Node{NodeType: "grandchild1", NodeID: 4},
118+
Node: &Node{NodeType: "grandchild1", NodeID: 4},
118119
Parent: child1,
119120
}
120121

@@ -129,4 +130,4 @@ func TestTreeNode(t *testing.T) {
129130
assert.Equal(t, root, child2.Parent)
130131
assert.Equal(t, child1, grandchild1.Parent)
131132
})
132-
}
133+
}

sourcecode-parser/tree/query_test.go

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package graph
2+
3+
import (
4+
"testing"
5+
6+
parser "github.com/shivasurya/code-pathfinder/sourcecode-parser/antlr"
7+
"github.com/shivasurya/code-pathfinder/sourcecode-parser/db"
8+
"github.com/shivasurya/code-pathfinder/sourcecode-parser/model"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestQueryEntities_MethodDeclarations(t *testing.T) {
13+
// Setup test database
14+
storageNode := &db.StorageNode{}
15+
16+
// Add test method declarations
17+
methods := []*model.Method{
18+
{
19+
Name: "testMethod1",
20+
QualifiedName: "com.example.TestClass.testMethod1",
21+
ReturnType: "void",
22+
Visibility: "public",
23+
Parameters: []string{"String", "int"},
24+
ParameterNames: []string{"param1", "param2"},
25+
},
26+
{
27+
Name: "testMethod2",
28+
QualifiedName: "com.example.TestClass.testMethod2",
29+
ReturnType: "String",
30+
Visibility: "private",
31+
IsStatic: true,
32+
},
33+
}
34+
35+
for _, method := range methods {
36+
storageNode.AddMethodDecl(method)
37+
}
38+
39+
// Test case 1: Query all methods
40+
t.Run("query all methods", func(t *testing.T) {
41+
query := parser.Query{
42+
SelectList: []parser.SelectList{{Entity: "method_declaration", Alias: "md"}},
43+
ExpressionTree: &parser.ExpressionNode{
44+
Type: "binary",
45+
Operator: "==",
46+
Left: &parser.ExpressionNode{
47+
Type: "method_call",
48+
Value: "getName()",
49+
Alias: "md",
50+
Entity: "method_declaration",
51+
},
52+
Right: &parser.ExpressionNode{
53+
Type: "literal",
54+
Value: "\"testMethod1\"",
55+
},
56+
},
57+
}
58+
59+
nodes, output := QueryEntities(storageNode, query)
60+
61+
assert.Equal(t, 1, len(nodes), "Should find 1 method")
62+
assert.NotNil(t, output, "Output should not be nil")
63+
})
64+
65+
// Test case 2: Query with filter
66+
t.Run("query with filter", func(t *testing.T) {
67+
query := parser.Query{
68+
SelectList: []parser.SelectList{{Entity: "method_declaration"}},
69+
ExpressionTree: &parser.ExpressionNode{
70+
Type: "binary",
71+
Operator: "==",
72+
Left: &parser.ExpressionNode{
73+
Type: "method_call",
74+
Value: "getVisibility()",
75+
Alias: "md",
76+
Entity: "method_declaration",
77+
},
78+
Right: &parser.ExpressionNode{
79+
Type: "literal",
80+
Value: "\"public\"",
81+
},
82+
},
83+
SelectOutput: []parser.SelectOutput{
84+
{SelectEntity: "md.getName()", Type: "method_chain"},
85+
{SelectEntity: "md.getVisibility()", Type: "method_chain"},
86+
},
87+
}
88+
89+
nodes, output := QueryEntities(storageNode, query)
90+
91+
assert.Equal(t, 1, len(nodes), "Should find 1 public method")
92+
assert.NotNil(t, output, "Output should not be nil")
93+
assert.Equal(t, 1, len(output), "Should have 1 output row")
94+
assert.Equal(t, "testMethod1", output[0][0], "First method should be testMethod1")
95+
assert.Equal(t, "public", output[0][1], "First method should be public")
96+
})
97+
}
98+
99+
func TestQueryEntities_ClassDeclarations(t *testing.T) {
100+
// Setup test database
101+
storageNode := &db.StorageNode{}
102+
103+
// Add test class declarations
104+
classes := []*model.Class{
105+
{
106+
ClassOrInterface: model.ClassOrInterface{
107+
RefType: model.RefType{
108+
QualifiedName: "com.example.TestClass1",
109+
Package: "com.example",
110+
},
111+
},
112+
ClassID: "1",
113+
},
114+
{
115+
ClassOrInterface: model.ClassOrInterface{
116+
RefType: model.RefType{
117+
QualifiedName: "com.example.TestClass2",
118+
Package: "com.example",
119+
},
120+
},
121+
ClassID: "2",
122+
},
123+
}
124+
125+
for _, class := range classes {
126+
storageNode.AddClassDecl(class)
127+
}
128+
129+
t.Run("query all classes", func(t *testing.T) {
130+
query := parser.Query{
131+
SelectList: []parser.SelectList{{Entity: "class_declaration", Alias: "cd"}},
132+
ExpressionTree: &parser.ExpressionNode{
133+
Type: "binary",
134+
Operator: "==",
135+
Left: &parser.ExpressionNode{
136+
Type: "method_call",
137+
Value: "getName()",
138+
Alias: "cd",
139+
Entity: "class_declaration",
140+
},
141+
Right: &parser.ExpressionNode{
142+
Type: "literal",
143+
Value: "\"com.example.TestClass1\"",
144+
},
145+
},
146+
}
147+
148+
nodes, output := QueryEntities(storageNode, query)
149+
150+
assert.Equal(t, 1, len(nodes), "Should find 1 class")
151+
assert.NotNil(t, nodes, "Nodes should not be nil")
152+
assert.NotNil(t, output, "Output should not be nil")
153+
})
154+
}
155+
156+
func TestQueryEntities_EmptyQuery(t *testing.T) {
157+
storageNode := &db.StorageNode{}
158+
159+
t.Run("empty query returns nil", func(t *testing.T) {
160+
query := parser.Query{}
161+
nodes, output := QueryEntities(storageNode, query)
162+
163+
assert.Nil(t, nodes, "Nodes should be nil for empty query")
164+
assert.Nil(t, output, "Output should be nil for empty query")
165+
})
166+
}

0 commit comments

Comments
 (0)