gitstack

grindlemire/go-tui code browser

4.1 KB Go 188 lines 2026-06-12 · 21be938 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
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
package formatter

import (
	"strings"
	"testing"
)

// TestFormatWithResultCoverage exercises the changed/unchanged/error paths.
func TestFormatWithResultCoverage(t *testing.T) {
	type tc struct {
		input       string
		wantContent string
		wantChanged bool
		wantErr     string
	}

	tests := map[string]tc{
		"already formatted is unchanged": {
			input: `package main

templ A() {
	<span>hi</span>
}
`,
			wantContent: `package main

templ A() {
	<span>hi</span>
}
`,
			wantChanged: false,
		},
		"unformatted input is changed": {
			input: `package main

templ A() {
<span>hi</span>
}
`,
			wantContent: `package main

templ A() {
	<span>hi</span>
}
`,
			wantChanged: true,
		},
		"parse error returns empty result": {
			input:   "package main\n\ntempl A() {\n<span>\n}\n",
			wantErr: "test.gsx:6:1: error: expected closing tag </span>",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			fmtr := newTestFormatter()
			got, err := fmtr.FormatWithResult("test.gsx", tt.input)

			if tt.wantErr != "" {
				if err == nil {
					t.Fatalf("FormatWithResult() expected error containing %q, got nil", tt.wantErr)
				}
				if !strings.Contains(err.Error(), tt.wantErr) {
					t.Errorf("FormatWithResult() error = %q, want it to contain %q", err.Error(), tt.wantErr)
				}
				if got.Content != "" || got.Changed {
					t.Errorf("FormatWithResult() result = %+v, want zero value on error", got)
				}
				return
			}

			if err != nil {
				t.Fatalf("FormatWithResult() error = %v", err)
			}
			if got.Content != tt.wantContent {
				t.Errorf("Content mismatch:\ngot:\n%s\nwant:\n%s", got.Content, tt.wantContent)
			}
			if got.Changed != tt.wantChanged {
				t.Errorf("Changed = %v, want %v", got.Changed, tt.wantChanged)
			}
		})
	}
}

// TestFixImportsTuiFilename verifies the .tui filename is converted for
// goimports resolution and imports are still fixed.
func TestFixImportsTuiFilename(t *testing.T) {
	input := `package main

templ A() {
<span>{fmt.Sprintf("hi")}</span>
}
`
	want := `package main

import (
	"fmt"
	tui "github.com/grindlemire/go-tui"
)

templ A() {
	<span>{fmt.Sprintf("hi")}</span>
}
`

	fmtr := New() // FixImports enabled
	got, err := fmtr.Format("test.tui", input)
	if err != nil {
		t.Fatalf("Format() error = %v", err)
	}
	if got != want {
		t.Errorf("Format() mismatch:\ngot:\n%s\nwant:\n%s", got, want)
	}
}

// TestFixImportsGenerationFailure verifies that when the generator cannot
// produce valid Go (invalid expression code), import fixing is skipped but
// formatting still succeeds with the original imports untouched.
func TestFixImportsGenerationFailure(t *testing.T) {
	input := `package main

templ A() {
<span>{a +}</span>
}
`
	want := `package main

templ A() {
	<span>{a +}</span>
}
`

	fmtr := New() // FixImports enabled
	got, err := fmtr.Format("test.gsx", input)
	if err != nil {
		t.Fatalf("Format() error = %v", err)
	}
	if got != want {
		t.Errorf("Format() mismatch:\ngot:\n%s\nwant:\n%s", got, want)
	}
}

// TestExtractImports exercises the import extraction helper directly,
// including the parse error path.
func TestExtractImports(t *testing.T) {
	type tc struct {
		src       string
		wantPaths []string
		wantErr   bool
	}

	tests := map[string]tc{
		"valid imports": {
			src:       "package p\n\nimport (\n\t\"fmt\"\n\talias \"strings\"\n)\n",
			wantPaths: []string{"fmt", "strings"},
		},
		"invalid go source": {
			src:     "not go code",
			wantErr: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got, err := extractImports([]byte(tt.src))
			if tt.wantErr {
				if err == nil {
					t.Fatal("extractImports() expected error, got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("extractImports() error = %v", err)
			}
			if len(got) != len(tt.wantPaths) {
				t.Fatalf("extractImports() returned %d imports, want %d", len(got), len(tt.wantPaths))
			}
			for i, p := range tt.wantPaths {
				if got[i].Path != p {
					t.Errorf("import[%d].Path = %q, want %q", i, got[i].Path, p)
				}
			}
			if got[1].Alias != "alias" {
				t.Errorf("import[1].Alias = %q, want %q", got[1].Alias, "alias")
			}
		})
	}
}