|
| 1 | +package graph |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/shivasurya/code-pathfinder/sourcecode-parser/db" |
| 10 | + "github.com/shivasurya/code-pathfinder/sourcecode-parser/model" |
| 11 | + sitter "github.com/smacker/go-tree-sitter" |
| 12 | + "github.com/smacker/go-tree-sitter/java" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func setupTestData(t *testing.T) (string, *db.StorageNode) { |
| 17 | + // Create a temporary directory |
| 18 | + tempDir := t.TempDir() |
| 19 | + |
| 20 | + // Create sample Java files |
| 21 | + sampleCode := []byte(` |
| 22 | + package com.example; |
| 23 | + |
| 24 | + import java.util.List; |
| 25 | + |
| 26 | + /** |
| 27 | + * Sample class documentation |
| 28 | + */ |
| 29 | + public class TestClass { |
| 30 | + private int count; |
| 31 | + |
| 32 | + public void testMethod() { |
| 33 | + int localVar = 0; |
| 34 | + if (count > 0) { |
| 35 | + while (localVar < count) { |
| 36 | + localVar++; |
| 37 | + } |
| 38 | + } |
| 39 | + assert localVar >= 0 : "Local variable must be non-negative"; |
| 40 | + } |
| 41 | + |
| 42 | + public void complexMethod() { |
| 43 | + for (int i = 0; i < 10; i++) { |
| 44 | + if (i % 2 == 0) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + doSomething(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void doSomething() { |
| 52 | + Object obj = new Object(); |
| 53 | + count = count + 1; |
| 54 | + } |
| 55 | + } |
| 56 | + `) |
| 57 | + |
| 58 | + testFile := filepath.Join(tempDir, "TestClass.java") |
| 59 | + err := os.WriteFile(testFile, sampleCode, 0644) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + // Initialize storage node |
| 65 | + storageNode := db.NewStorageNode(tempDir) |
| 66 | + return tempDir, storageNode |
| 67 | +} |
| 68 | + |
| 69 | +func TestInitialize(t *testing.T) { |
| 70 | + tempDir, storageNode := setupTestData(t) |
| 71 | + |
| 72 | + // Test Initialize function |
| 73 | + trees := Initialize(tempDir, storageNode) |
| 74 | + |
| 75 | + // Verify the results |
| 76 | + assert.NotEmpty(t, trees, "Should return non-empty tree slice") |
| 77 | + assert.Equal(t, 1, len(trees), "Should process one file") |
| 78 | + |
| 79 | + // Verify the root node |
| 80 | + root := trees[0] |
| 81 | + assert.NotNil(t, root) |
| 82 | + assert.Equal(t, "File", root.Node.NodeType) |
| 83 | + assert.Equal(t, "TestClass.java", root.Node.FileNode.File) |
| 84 | +} |
| 85 | + |
| 86 | +func TestBuildQLTreeFromAST(t *testing.T) { |
| 87 | + // Setup parser |
| 88 | + parser := sitter.NewParser() |
| 89 | + parser.SetLanguage(java.GetLanguage()) |
| 90 | + |
| 91 | + sampleCode := []byte("public class Test { private int x; }") |
| 92 | + tree, err := parser.ParseCtx(context.Background(), nil, sampleCode) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + defer tree.Close() |
| 97 | + |
| 98 | + // Create parent node and storage node |
| 99 | + parentNode := &model.TreeNode{ |
| 100 | + Node: &model.Node{ |
| 101 | + NodeType: "File", |
| 102 | + FileNode: &model.File{File: "Test.java"}, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + tempDir := t.TempDir() |
| 107 | + storageNode := db.NewStorageNode(tempDir) |
| 108 | + |
| 109 | + // Test buildQLTreeFromAST |
| 110 | + buildQLTreeFromAST(tree.RootNode(), sampleCode, "Test.java", parentNode, storageNode) |
| 111 | + |
| 112 | + // Verify the results |
| 113 | + assert.NotEmpty(t, parentNode.Children) |
| 114 | + assert.Equal(t, "ClassDeclaration", parentNode.Children[0].Node.NodeType) |
| 115 | +} |
| 116 | + |
| 117 | +func TestGetFiles(t *testing.T) { |
| 118 | + tempDir := t.TempDir() |
| 119 | + |
| 120 | + // Create test files |
| 121 | + testFiles := []string{ |
| 122 | + "Test1.java", |
| 123 | + "Test2.java", |
| 124 | + "NotAJavaFile.txt", |
| 125 | + } |
| 126 | + |
| 127 | + for _, file := range testFiles { |
| 128 | + err := os.WriteFile(filepath.Join(tempDir, file), []byte(""), 0644) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Test getFiles |
| 135 | + files, err := getFiles(tempDir) |
| 136 | + |
| 137 | + // Verify results |
| 138 | + assert.NoError(t, err) |
| 139 | + assert.Equal(t, 2, len(files), "Should only find .java files") |
| 140 | +} |
| 141 | + |
| 142 | +func TestReadFile(t *testing.T) { |
| 143 | + tempDir := t.TempDir() |
| 144 | + testContent := []byte("test content") |
| 145 | + testFile := filepath.Join(tempDir, "test.txt") |
| 146 | + |
| 147 | + // Create test file |
| 148 | + err := os.WriteFile(testFile, testContent, 0644) |
| 149 | + if err != nil { |
| 150 | + t.Fatal(err) |
| 151 | + } |
| 152 | + |
| 153 | + // Test readFile |
| 154 | + content, err := readFile(testFile) |
| 155 | + |
| 156 | + // Verify results |
| 157 | + assert.NoError(t, err) |
| 158 | + assert.Equal(t, testContent, content) |
| 159 | + |
| 160 | + // Test with non-existent file |
| 161 | + content, err = readFile(filepath.Join(tempDir, "nonexistent.txt")) |
| 162 | + assert.Error(t, err) |
| 163 | + assert.Nil(t, content) |
| 164 | +} |
0 commit comments