xonecas.io
62159
Sy

symb / files

symb / internal / treesitter / parser_test.go

2.3 KB
1package treesitter
2
3import (
4	"strings"
5	"testing"
6)
7
8func TestParseSource_Go(t *testing.T) {
9	src := []byte(`package main
10
11import "fmt"
12
13const Version = "1.0"
14
15var Debug bool
16
17type Server struct {
18	addr string
19	port int
20}
21
22type Handler interface {
23	Handle(req string) string
24}
25
26func main() {
27	fmt.Println("hello")
28}
29
30func (s *Server) Start() error {
31	return nil
32}
33`)
34
35	syms, err := ParseSource("test.go", src)
36	if err != nil {
37		t.Fatalf("ParseSource: %v", err)
38	}
39
40	// Check we got the expected symbols (name+kind pairs to handle duplicates like "main")
41	type symKey struct {
42		name string
43		kind SymbolKind
44	}
45	want := []symKey{
46		{"main", KindPackage},
47		{"Version", KindConst},
48		{"Debug", KindVar},
49		{"Server", KindStruct},
50		{"Handler", KindInterface},
51	}
52
53	got := make(map[symKey]bool)
54	for _, s := range syms {
55		got[symKey{s.Name, s.Kind}] = true
56	}
57
58	for _, w := range want {
59		if !got[w] {
60			t.Errorf("missing symbol %q (kind=%v)", w.name, w.kind)
61		}
62	}
63
64	// Check functions/methods
65	var hasMainFunc, hasStartMethod bool
66	for _, s := range syms {
67		if s.Kind == KindFunction && s.Name == "main" {
68			hasMainFunc = true
69		}
70		if s.Kind == KindMethod && s.Name == "Start" && s.Receiver == "*Server" {
71			hasStartMethod = true
72		}
73	}
74	if !hasMainFunc {
75		t.Error("missing func main")
76	}
77	if !hasStartMethod {
78		t.Error("missing method Start on *Server")
79	}
80}
81
82func TestParseSource_Unsupported(t *testing.T) {
83	syms, err := ParseSource("test.py", []byte("print('hello')"))
84	if err != nil {
85		t.Fatalf("unexpected error: %v", err)
86	}
87	if len(syms) != 0 {
88		t.Errorf("expected no symbols for unsupported language, got %d", len(syms))
89	}
90}
91
92func TestFormatOutline(t *testing.T) {
93	snap := map[string][]Symbol{
94		"main.go": {
95			{Name: "main", Kind: KindPackage},
96			{Name: "main", Kind: KindFunction},
97			{Name: "Server", Kind: KindStruct},
98			{Name: "Start", Kind: KindMethod, Receiver: "*Server"},
99		},
100	}
101	out := FormatOutline(snap)
102	if out == "" {
103		t.Fatal("empty outline")
104	}
105	// New compact format checks
106	if !strings.Contains(out, "fn: main") {
107		t.Errorf("missing fn: main in outline:\n%s", out)
108	}
109	if !strings.Contains(out, "Server (struct)") {
110		t.Errorf("missing Server (struct) in outline:\n%s", out)
111	}
112	if !strings.Contains(out, "*Server: Start") {
113		t.Errorf("missing *Server: Start in outline:\n%s", out)
114	}
115}