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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
package gopls
import (
"strings"
"testing"
"github.com/grindlemire/go-tui/internal/tuigen"
)
func TestGenerateVirtualGo_Imports(t *testing.T) {
type tc struct {
imports []tuigen.Import
wantContains []string
}
tests := map[string]tc{
"single import no alias": {
imports: []tuigen.Import{{Path: "fmt"}},
wantContains: []string{"import \"fmt\"\n"},
},
"single import with alias": {
imports: []tuigen.Import{{Path: "github.com/grindlemire/go-tui", Alias: "tui"}},
wantContains: []string{"import tui \"github.com/grindlemire/go-tui\"\n"},
},
"multiple imports mixed": {
imports: []tuigen.Import{
{Path: "fmt"},
{Path: "github.com/grindlemire/go-tui", Alias: "tui"},
},
wantContains: []string{
"import (\n\t\"fmt\"\n\ttui \"github.com/grindlemire/go-tui\"\n)\n",
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
file := &tuigen.File{Package: "main", Imports: tt.imports}
source, _ := GenerateVirtualGo(file)
if !strings.HasPrefix(source, "package main\n") {
t.Errorf("missing package clause:\n%s", source)
}
for _, want := range tt.wantContains {
if !strings.Contains(source, want) {
t.Errorf("generated source missing %q:\n%s", want, source)
}
}
})
}
}
func TestGenerateVirtualGo_TopLevelFunc(t *testing.T) {
file := &tuigen.File{
Package: "main",
Funcs: []*tuigen.GoFunc{
{
Code: "func helper(s string) string {\n\treturn s\n}",
Position: tuigen.Position{Line: 10, Column: 1},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
want := "func helper(s string) string {\n\treturn s\n}\n"
if !strings.Contains(source, want) {
t.Errorf("generated source missing function body:\n%s", source)
}
if sourceMap.Len() != 3 {
t.Fatalf("sourceMap.Len() = %d, want 3", sourceMap.Len())
}
goLine, goCol, found := sourceMap.TuiToGo(9, 5)
if !found {
t.Fatal("no mapping for first function line")
}
if goLine != 2 || goCol != 5 {
t.Errorf("TuiToGo(9, 5) = (%d, %d), want (2, 5)", goLine, goCol)
}
if _, _, found := sourceMap.TuiToGo(11, 0); !found {
t.Error("no mapping for closing brace line of function")
}
}
func TestGenerateVirtualGo_LetBindings(t *testing.T) {
type tc struct {
binding *tuigen.LetBinding
wantDecl string
wantTuiCol int
wantMapping bool
}
tests := map[string]tc{
"short form maps name at position": {
binding: &tuigen.LetBinding{
Name: "label",
IsShortForm: true,
Position: tuigen.Position{Line: 4, Column: 2},
},
wantDecl: "\tvar label any\n",
wantTuiCol: 1,
wantMapping: true,
},
"var form maps name after var keyword": {
binding: &tuigen.LetBinding{
Name: "footer",
IsVarForm: true,
Position: tuigen.Position{Line: 4, Column: 2},
},
wantDecl: "\tvar footer any\n",
wantTuiCol: 5,
wantMapping: true,
},
"fallback form": {
binding: &tuigen.LetBinding{
Name: "x",
Position: tuigen.Position{Line: 4, Column: 2},
},
wantDecl: "\tvar x any\n",
wantTuiCol: 1,
wantMapping: true,
},
"binding with element generates child expressions": {
binding: &tuigen.LetBinding{
Name: "row",
IsShortForm: true,
Position: tuigen.Position{Line: 4, Column: 2},
Element: &tuigen.Element{
Tag: "span",
Position: tuigen.Position{Line: 4, Column: 9},
Children: []tuigen.Node{
&tuigen.GoExpr{Code: "row.Title", Position: tuigen.Position{Line: 4, Column: 15}},
},
},
},
wantDecl: "\tvar row any\n",
wantTuiCol: 1,
wantMapping: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{tt.binding},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
if !strings.Contains(source, tt.wantDecl) {
t.Errorf("generated source missing %q:\n%s", tt.wantDecl, source)
}
if tt.binding.Element != nil && !strings.Contains(source, "_ = row.Title") {
t.Errorf("generated source missing element child expression:\n%s", source)
}
if tt.wantMapping {
m := sourceMap.FindMappingForTuiPosition(tt.binding.Position.Line-1, tt.wantTuiCol)
if m == nil {
t.Fatalf("no mapping at gsx %d:%d\nmappings: %+v",
tt.binding.Position.Line-1, tt.wantTuiCol, sourceMap.AllMappings())
}
if m.Length != len(tt.binding.Name) {
t.Errorf("mapping length = %d, want %d", m.Length, len(tt.binding.Name))
}
if m.GoCol != len("\t")+len("var ") {
t.Errorf("mapping GoCol = %d, want %d", m.GoCol, len("\t")+len("var "))
}
}
})
}
}
func TestGenerateVirtualGo_RawGoExpr(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
&tuigen.RawGoExpr{Code: " ", Position: tuigen.Position{Line: 4, Column: 2}},
&tuigen.RawGoExpr{Code: "myRef", Position: tuigen.Position{Line: 5, Column: 8}},
},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
if !strings.Contains(source, "\t_ = myRef\n") {
t.Errorf("generated source missing raw expression:\n%s", source)
}
m := sourceMap.FindMappingForTuiPosition(4, 8)
if m == nil {
t.Fatalf("no mapping for raw expr, mappings: %+v", sourceMap.AllMappings())
}
if m.Length != len("myRef") || m.GoCol != len("\t")+len("_ = ") {
t.Errorf("mapping = %+v, want Length 5 GoCol 5", m)
}
if found := sourceMap.FindMappingForTuiPosition(3, 2); found != nil {
t.Errorf("blank raw expr should not be mapped, got %+v", found)
}
}
func TestGenerateVirtualGo_GoCodeVariants(t *testing.T) {
type tc struct {
code string
wantEmitted string
wantMapping bool
}
tests := map[string]tc{
"plain statement emitted with mapping": {
code: "x := compute()",
wantEmitted: "\tx := compute()\n",
wantMapping: true,
},
"empty code skipped": {
code: " ",
wantEmitted: "",
wantMapping: false,
},
"state declaration skipped by generateGoCode": {
code: "count := tui.NewState(0)",
wantEmitted: "\tcount := tui.NewState(0)\n",
wantMapping: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
&tuigen.GoCode{Code: tt.code, Position: tuigen.Position{Line: 4, Column: 2}},
},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
if tt.wantEmitted != "" {
if got := strings.Count(source, tt.wantEmitted); got != 1 {
t.Errorf("code emitted %d times, want exactly 1:\n%s", got, source)
}
} else if strings.Contains(source, strings.TrimSpace(tt.code)) && strings.TrimSpace(tt.code) != "" {
t.Errorf("empty code should not be emitted:\n%s", source)
}
if tt.wantMapping && sourceMap.Len() == 0 {
t.Error("expected at least one mapping")
}
if !tt.wantMapping && sourceMap.Len() != 0 {
t.Errorf("expected no mappings, got %+v", sourceMap.AllMappings())
}
})
}
}
func TestGenerateVirtualGo_ControlFlowAndCalls(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
&tuigen.ForLoop{
Value: "item",
Iterable: "items",
Position: tuigen.Position{Line: 4, Column: 2},
Body: []tuigen.Node{
&tuigen.GoExpr{Code: "item", Position: tuigen.Position{Line: 5, Column: 9}},
},
},
&tuigen.IfStmt{
Condition: "visible",
Position: tuigen.Position{Line: 7, Column: 2},
Then: []tuigen.Node{
&tuigen.GoExpr{Code: "shownLabel", Position: tuigen.Position{Line: 8, Column: 9}},
},
Else: []tuigen.Node{
&tuigen.GoExpr{Code: "hiddenLabel", Position: tuigen.Position{Line: 10, Column: 9}},
},
},
&tuigen.ComponentCall{
Name: "Header",
Position: tuigen.Position{Line: 12, Column: 2},
Children: []tuigen.Node{
&tuigen.GoExpr{Code: "title", Position: tuigen.Position{Line: 13, Column: 9}},
},
},
&tuigen.ComponentCall{
Name: "Footer",
Args: `"hi", 2`,
Position: tuigen.Position{Line: 15, Column: 2},
},
&tuigen.TextContent{Text: "plain text", Position: tuigen.Position{Line: 16, Column: 2}},
&tuigen.ChildrenSlot{Position: tuigen.Position{Line: 17, Column: 2}},
},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
wantLines := []string{
"\tfor _, item := range items {\n",
"\t\t_ = item\n",
"\tif visible {\n",
"\t\t_ = shownLabel\n",
"\t} else {\n",
"\t\t_ = hiddenLabel\n",
"\t_ = Header()\n",
"\t\t_ = title\n",
"\t_ = Footer(\"hi\", 2)\n",
}
for _, want := range wantLines {
if !strings.Contains(source, want) {
t.Errorf("generated source missing %q:\n%s", want, source)
}
}
if strings.Contains(source, "plain text") {
t.Errorf("text content leaked into generated source:\n%s", source)
}
argsCol := 2 - 1 + len("@") + len("Footer") + 1
if m := sourceMap.FindMappingForTuiPosition(14, argsCol); m == nil || m.Length != len(`"hi", 2`) {
t.Errorf("no args mapping for Footer at 14:%d, mappings: %+v", argsCol, sourceMap.AllMappings())
}
}
func TestGenerateVirtualGo_ElementAttributesAndReceiver(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "Render",
Receiver: "s *sidebar",
ReceiverName: "s",
ReceiverType: "*sidebar",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
&tuigen.Element{
Tag: "div",
Position: tuigen.Position{Line: 4, Column: 2},
Attributes: []*tuigen.Attribute{
{
Name: "onActivate",
Value: &tuigen.GoExpr{Code: "s.activate", Position: tuigen.Position{Line: 4, Column: 20}},
},
{
Name: "class",
Value: &tuigen.StringLit{Value: "flex-col"},
},
},
Children: []tuigen.Node{
&tuigen.GoExpr{Code: "", Position: tuigen.Position{Line: 5, Column: 3}},
&tuigen.GoExpr{Code: "s.title", Position: tuigen.Position{Line: 6, Column: 9}},
},
},
},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
if !strings.Contains(source, "func (s *sidebar) Render() *element.Element {\n") {
t.Errorf("missing method signature:\n%s", source)
}
if !strings.Contains(source, "\t_ = s.activate\n") {
t.Errorf("missing attribute expression:\n%s", source)
}
if !strings.Contains(source, "\t_ = s.title\n") {
t.Errorf("missing child expression:\n%s", source)
}
if strings.Contains(source, "flex-col") {
t.Errorf("string literal attribute leaked into generated source:\n%s", source)
}
if sourceMap.Len() != 2 {
t.Errorf("sourceMap.Len() = %d, want 2: %+v", sourceMap.Len(), sourceMap.AllMappings())
}
}
func TestGenerateVirtualGo_DefaultReturnType(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{Name: "App", Position: tuigen.Position{Line: 3, Column: 1}},
},
}
source, _ := GenerateVirtualGo(file)
if !strings.Contains(source, "func App() *element.Element {\n") {
t.Errorf("expected default return type *element.Element:\n%s", source)
}
}
func TestGenerateVirtualGo_NilNodesAreSkipped(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
(*tuigen.Element)(nil),
(*tuigen.GoExpr)(nil),
(*tuigen.ForLoop)(nil),
(*tuigen.IfStmt)(nil),
(*tuigen.LetBinding)(nil),
(*tuigen.ComponentCall)(nil),
(*tuigen.GoCode)(nil),
(*tuigen.RawGoExpr)(nil),
},
},
},
}
source, sourceMap := GenerateVirtualGo(file)
want := "package main\n\nfunc App() *element.Element {\n\treturn nil\n}\n\n"
if source != want {
t.Errorf("source = %q, want %q", source, want)
}
if sourceMap.Len() != 0 {
t.Errorf("expected no mappings, got %+v", sourceMap.AllMappings())
}
}
func TestGenerateVirtualGo_RefsInNestedNodes(t *testing.T) {
file := &tuigen.File{
Package: "main",
Components: []*tuigen.Component{
{
Name: "App",
ReturnType: "*element.Element",
Position: tuigen.Position{Line: 3, Column: 1},
Body: []tuigen.Node{
&tuigen.IfStmt{
Condition: "cond",
Position: tuigen.Position{Line: 4, Column: 2},
Then: []tuigen.Node{
&tuigen.Element{
Tag: "div",
RefExpr: &tuigen.GoExpr{Code: "thenRef", Position: tuigen.Position{Line: 5, Column: 12}},
Position: tuigen.Position{Line: 5, Column: 3},
},
},
Else: []tuigen.Node{
&tuigen.Element{
Tag: "div",
RefExpr: &tuigen.GoExpr{Code: "elseRef", Position: tuigen.Position{Line: 7, Column: 12}},
Position: tuigen.Position{Line: 7, Column: 3},
},
},
},
&tuigen.LetBinding{
Name: "bound",
IsShortForm: true,
Position: tuigen.Position{Line: 9, Column: 2},
Element: &tuigen.Element{
Tag: "span",
RefExpr: &tuigen.GoExpr{Code: "letRef", Position: tuigen.Position{Line: 9, Column: 22}},
Position: tuigen.Position{Line: 9, Column: 11},
},
},
&tuigen.ComponentCall{
Name: "Card",
Position: tuigen.Position{Line: 11, Column: 2},
Children: []tuigen.Node{
&tuigen.Element{
Tag: "p",
RefExpr: &tuigen.GoExpr{Code: "callRef", Position: tuigen.Position{Line: 12, Column: 10}},
Position: tuigen.Position{Line: 12, Column: 3},
},
},
},
},
},
},
}
source, _ := GenerateVirtualGo(file)
for _, ref := range []string{"thenRef", "elseRef", "letRef", "callRef"} {
if !strings.Contains(source, "\t_ = "+ref+"\n") {
t.Errorf("generated source missing ref hoist for %s:\n%s", ref, source)
}
}
}
|