gitstack

grindlemire/go-tui code browser

1.5 KB Go 75 lines 2026-06-12 · f0bb13a raw
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
package tuigen

import (
	"strings"
	"testing"
)

// TestAnalyzer_LetBindingNonElementRHS verifies that the analyzer handles
// let bindings whose right-hand side is a component call or component
// expression. The ref walk used to dereference LetBinding.Element without a
// nil check, so `badge := @Badge("hi")` in a function templ crashed
// tui generate with a nil pointer panic.
func TestAnalyzer_LetBindingNonElementRHS(t *testing.T) {
	type tc struct {
		input        string
		wantContains []string
	}

	tests := map[string]tc{
		"call RHS at top level of function templ": {
			input: `package x

templ Badge(label string) {
	<span>{label}</span>
}

templ App() {
	badge := @Badge("hi")
	<div>{badge}</div>
}`,
			wantContains: []string{
				"__tui_0 := Badge(\"hi\")",
				"badge := __tui_0.Root",
			},
		},
		"call RHS nested in component call children": {
			input: `package x

templ Badge(label string) {
	<span>{label}</span>
}

templ Wrapper() {
	<div>{children...}</div>
}

templ App() {
	@Wrapper() {
		badge := @Badge("hi")
		<div>{badge}</div>
	}
}`,
			wantContains: []string{
				":= Badge(\"hi\")",
				"badge := ",
			},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			output, err := parseAndGenerateSkipImports("test.gsx", tt.input)
			if err != nil {
				t.Fatalf("generation failed: %v", err)
			}

			code := string(output)
			for _, want := range tt.wantContains {
				if !strings.Contains(code, want) {
					t.Errorf("output missing expected string: %q\nGot:\n%s", want, code)
				}
			}
		})
	}
}