Files
codebase-memory-mcp/internal/parser/parser_test.go
T
Martin Vogel f63c70865e Initial release: codebase-memory-mcp v0.0.1
MCP server that indexes codebases into a queryable knowledge graph.
Single Go binary, SQLite storage, 12 languages via tree-sitter.

Features: call graph, cross-service HTTP linking, dead code detection,
route nodes, Cypher queries, incremental reindex, JSON config scanning.
2026-02-25 00:10:39 +01:00

120 lines
2.2 KiB
Go

package parser
import (
"testing"
tree_sitter "github.com/tree-sitter/go-tree-sitter"
"github.com/DeusData/codebase-memory-mcp/internal/lang"
)
func TestParseGo(t *testing.T) {
source := []byte(`package main
func Hello() string {
return "hello"
}
func Add(a, b int) int {
return a + b
}
`)
tree, err := Parse(lang.Go, source)
if err != nil {
t.Fatalf("Parse Go: %v", err)
}
defer tree.Close()
root := tree.RootNode()
if root == nil {
t.Fatal("root node is nil")
}
var funcCount int
Walk(root, func(n *tree_sitter.Node) bool {
if n.Kind() == "function_declaration" {
funcCount++
}
return true
})
if funcCount != 2 {
t.Errorf("expected 2 function_declarations, got %d", funcCount)
}
}
func TestParsePython(t *testing.T) {
source := []byte(`def greet(name):
return f"Hello, {name}"
class MyClass:
def method(self):
pass
`)
tree, err := Parse(lang.Python, source)
if err != nil {
t.Fatalf("Parse Python: %v", err)
}
defer tree.Close()
root := tree.RootNode()
var funcCount, classCount int
Walk(root, func(n *tree_sitter.Node) bool {
switch n.Kind() {
case "function_definition":
funcCount++
case "class_definition":
classCount++
}
return true
})
if funcCount != 2 {
t.Errorf("expected 2 function_definitions, got %d", funcCount)
}
if classCount != 1 {
t.Errorf("expected 1 class_definition, got %d", classCount)
}
}
func TestAllLanguagesLoad(t *testing.T) {
for _, l := range lang.AllLanguages() {
if l == lang.CSharp {
continue // C# grammar has broken Go module path upstream
}
_, err := GetLanguage(l)
if err != nil {
t.Errorf("GetLanguage(%s): %v", l, err)
}
}
}
func TestNodeText(t *testing.T) {
source := []byte(`package main
func Hello() string {
return "hello"
}
`)
tree, err := Parse(lang.Go, source)
if err != nil {
t.Fatalf("Parse: %v", err)
}
defer tree.Close()
root := tree.RootNode()
Walk(root, func(n *tree_sitter.Node) bool {
if n.Kind() == "function_declaration" {
nameNode := n.ChildByFieldName("name")
if nameNode == nil {
t.Error("function has no name node")
return false
}
name := NodeText(nameNode, source)
if name != "Hello" {
t.Errorf("expected Hello, got %s", name)
}
return false
}
return true
})
}