|
| 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