1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package lsp
import (
"testing"
)
func TestComponentIndex(t *testing.T) {
type tc struct {
content string
wantComps []string
lookupName string
lookupExists bool
}
tests := map[string]tc{
"single component": {
content: `package main
templ Hello() {
<span>Hello</span>
}
`,
wantComps: []string{"Hello"},
lookupName: "Hello",
lookupExists: true,
},
"multiple components": {
content: `package main
templ Header() {
<span>Header</span>
}
templ Footer() {
<span>Footer</span>
}
`,
wantComps: []string{"Header", "Footer"},
lookupName: "Footer",
lookupExists: true,
},
"lookup nonexistent": {
content: `package main
templ Hello() {
<span>Hello</span>
}
`,
wantComps: []string{"Hello"},
lookupName: "NotExists",
lookupExists: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dm := NewDocumentManager()
idx := NewComponentIndex()
uri := "file:///test.gsx"
doc := dm.Open(uri, tt.content, 1)
idx.IndexDocument(uri, doc.AST)
for _, compName := range tt.wantComps {
if _, ok := idx.Lookup(compName); !ok {
t.Errorf("expected component %s to be indexed", compName)
}
}
_, exists := idx.Lookup(tt.lookupName)
if exists != tt.lookupExists {
t.Errorf("Lookup(%s) = _, %v; want _, %v", tt.lookupName, exists, tt.lookupExists)
}
})
}
}
func TestComponentIndexRemove(t *testing.T) {
dm := NewDocumentManager()
idx := NewComponentIndex()
uri := "file:///test.gsx"
content := `package main
templ Hello() {
<span>Hello</span>
}
`
doc := dm.Open(uri, content, 1)
idx.IndexDocument(uri, doc.AST)
if _, ok := idx.Lookup("Hello"); !ok {
t.Fatal("expected Hello to be indexed")
}
idx.Remove(uri)
if _, ok := idx.Lookup("Hello"); ok {
t.Fatal("expected Hello to be removed from index")
}
}
|