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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
package tuigen
import (
"strings"
"testing"
)
func TestParser_GenericFuncCapturedRaw(t *testing.T) {
input := `package x
func identity[T any](v T) T {
return v
}
templ App() {
<div></div>
}`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Funcs) != 1 {
t.Fatalf("expected 1 raw Go func, got %d", len(file.Funcs))
}
if !strings.Contains(file.Funcs[0].Code, "func identity[T any](v T) T") {
t.Errorf("raw func code missing generic signature, got:\n%s", file.Funcs[0].Code)
}
if len(file.Components) != 1 || file.Components[0].Name != "App" {
t.Errorf("expected App component to still parse, got %+v", file.Components)
}
}
func TestParser_MethodFuncCapturedRaw(t *testing.T) {
input := `package x
func (s *store) get(key string) string {
return s.data[key]
}
templ App() {
<div></div>
}`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Funcs) != 1 {
t.Fatalf("expected 1 raw Go func, got %d", len(file.Funcs))
}
if !strings.Contains(file.Funcs[0].Code, "func (s *store) get(key string) string") {
t.Errorf("raw func code missing method signature, got:\n%s", file.Funcs[0].Code)
}
}
func TestParser_OldStyleElementComponent(t *testing.T) {
input := `package x
func Card() Element {
<div>
<span>hi</span>
</div>
}`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Components) != 1 {
t.Fatalf("expected 1 component, got %d", len(file.Components))
}
comp := file.Components[0]
if comp.Name != "Card" {
t.Errorf("component name = %q, want %q", comp.Name, "Card")
}
if comp.ReturnType != "*element.Element" {
t.Errorf("return type = %q, want %q", comp.ReturnType, "*element.Element")
}
if len(comp.Body) == 0 {
t.Error("component body is empty, want parsed element tree")
}
}
func TestParser_FuncAndTemplErrors(t *testing.T) {
type tc struct {
input string
wantErr string
}
tests := map[string]tc{
"func without name": {
input: `package x
func 123() {}
`,
wantErr: "expected function name",
},
"unterminated function": {
input: `package x
func helper() {
x := 1`,
wantErr: "unterminated function definition",
},
"unterminated method func": {
input: `package x
func (s *store) helper() {
x := 1`,
wantErr: "unterminated function definition",
},
"templ without name": {
input: `package x
templ 123() {
<div></div>
}`,
wantErr: "expected component name or method receiver",
},
"method templ wrong method name": {
input: `package x
templ (c *shell) Draw() {
<div></div>
}`,
wantErr: "method templ name must be 'Render'",
},
"method templ with parameters": {
input: `package x
templ (c *shell) Render(w int) {
<div></div>
}`,
wantErr: "method templ Render() must not have parameters",
},
"method templ missing receiver name": {
input: `package x
templ (*shell) Render() {
<div></div>
}`,
wantErr: "expected receiver name",
},
"method templ non-ident method name": {
input: `package x
templ (c *shell) 123() {
<div></div>
}`,
wantErr: "expected method name after receiver",
},
"param without name": {
input: `package x
templ App(123 int) {
<div></div>
}`,
wantErr: "expected parameter name",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
l := NewLexer("test.gsx", tt.input)
p := NewParser(l)
_, err := p.ParseFile()
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErr)
}
})
}
}
func TestParser_GoDeclForms(t *testing.T) {
type tc struct {
input string
wantKind string
wantContains string
}
tests := map[string]tc{
"simple var": {
input: `package x
var count = 42
templ App() {
<div></div>
}`,
wantKind: "var",
wantContains: "var count = 42",
},
"grouped const": {
input: `package x
const (
A = 1
B = 2
)
templ App() {
<div></div>
}`,
wantKind: "const",
wantContains: "B = 2",
},
"grouped var": {
input: `package x
var (
x = "a"
y = "b"
)
templ App() {
<div></div>
}`,
wantKind: "var",
wantContains: `y = "b"`,
},
"type struct": {
input: `package x
type model struct {
name string
}
templ App() {
<div></div>
}`,
wantKind: "type",
wantContains: "type model struct {",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
l := NewLexer("test.gsx", tt.input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Decls) != 1 {
t.Fatalf("expected 1 decl, got %d", len(file.Decls))
}
decl := file.Decls[0]
if decl.Kind != tt.wantKind {
t.Errorf("decl kind = %q, want %q", decl.Kind, tt.wantKind)
}
if !strings.Contains(decl.Code, tt.wantContains) {
t.Errorf("decl code missing %q, got:\n%s", tt.wantContains, decl.Code)
}
})
}
}
func TestParser_GoDeclAtEOF(t *testing.T) {
input := `package x
var trailing = 1`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Decls) != 1 {
t.Fatalf("expected 1 decl, got %d", len(file.Decls))
}
if !strings.Contains(file.Decls[0].Code, "var trailing = 1") {
t.Errorf("decl code = %q, want it to contain %q", file.Decls[0].Code, "var trailing = 1")
}
}
func TestParser_TemplParamsTrailingComma(t *testing.T) {
input := `package x
templ App(
title string,
count int,
) {
<div></div>
}`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Components) != 1 {
t.Fatalf("expected 1 component, got %d", len(file.Components))
}
params := file.Components[0].Params
if len(params) != 2 {
t.Fatalf("expected 2 params, got %d", len(params))
}
if params[0].Name != "title" || params[0].Type != "string" {
t.Errorf("param 0 = (%q, %q), want (title, string)", params[0].Name, params[0].Type)
}
if params[1].Name != "count" || params[1].Type != "int" {
t.Errorf("param 1 = (%q, %q), want (count, int)", params[1].Name, params[1].Type)
}
}
func TestParser_MethodTemplReceiverWithParens(t *testing.T) {
input := `package x
templ (c (shell)) Render() {
<div></div>
}`
l := NewLexer("test.gsx", input)
p := NewParser(l)
file, err := p.ParseFile()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(file.Components) != 1 {
t.Fatalf("expected 1 component, got %d", len(file.Components))
}
comp := file.Components[0]
if comp.ReceiverType != "(shell)" {
t.Errorf("receiver type = %q, want %q", comp.ReceiverType, "(shell)")
}
if comp.ReceiverName != "c" {
t.Errorf("receiver name = %q, want %q", comp.ReceiverName, "c")
}
}
|