gitstack

grindlemire/go-tui code browser

6.4 KB Go 365 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
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
package formatter

import (
	"reflect"
	"testing"
)

// TestFormatControlFlowCoverage exercises control-flow printing paths:
// else-if chains (printElseBranch), let bindings to component calls,
// component expressions, elements with refs/attributes, self-closing
// elements, multi-line children, and multi-line component call args.
func TestFormatControlFlowCoverage(t *testing.T) {
	type tc struct {
		input string
		want  string
	}

	tests := map[string]tc{
		"else if without final else": {
			input: `package main

templ A(x int) {
if x == 1 {
<span>one</span>
} else if x == 2 {
<span>two</span>
}
}
`,
			want: `package main

templ A(x int) {
	if x == 1 {
		<span>one</span>
	} else if x == 2 {
		<span>two</span>
	}
}
`,
		},
		"else if chain with final else": {
			input: `package main

templ A(x int) {
if x == 1 {
<span>one</span>
} else if x == 2 {
<span>two</span>
} else if x == 3 {
<span>three</span>
} else {
<span>many</span>
}
}
`,
			want: `package main

templ A(x int) {
	if x == 1 {
		<span>one</span>
	} else if x == 2 {
		<span>two</span>
	} else if x == 3 {
		<span>three</span>
	} else {
		<span>many</span>
	}
}
`,
		},
		"else if chain without final else": {
			input: `package main

templ A(x int) {
if x == 1 {
<span>one</span>
} else if x == 2 {
<span>two</span>
} else if x == 3 {
<span>three</span>
}
}
`,
			want: `package main

templ A(x int) {
	if x == 1 {
		<span>one</span>
	} else if x == 2 {
		<span>two</span>
	} else if x == 3 {
		<span>three</span>
	}
}
`,
		},
		"else if with trailing comments": {
			input: `package main

templ A(x int) {
if x == 1 { // first
<span>one</span>
} else if x == 2 { // second
<span>two</span>
}
}
`,
			want: `package main

templ A(x int) {
	if x == 1 {  // first
		<span>one</span>
	} else if x == 2 {  // second
		<span>two</span>
	}
}
`,
		},
		"binding to component call": {
			input: `package main

templ A() {
header := @Header("hi", 1)
<div>{header}</div>
}
`,
			want: `package main

templ A() {
	header := @Header("hi", 1)
	<div>{header}</div>
}
`,
		},
		"binding to component expression": {
			input: `package main

templ (c *card) Render() {
body := @c.body
<div>{body}</div>
}
`,
			want: `package main

templ (c *card) Render() {
	body := @c.body
	<div>{body}</div>
}
`,
		},
		"binding to self-closing and ref elements": {
			input: `package main

templ A() {
rule := <hr class="w-10" />
box := <div ref={c.ref} class="p-1">hi</div>
<div>{rule}{box}</div>
}
`,
			want: `package main

templ A() {
	rule := <hr class="w-10" />
	box := <div ref={c.ref} class="p-1">hi</div>
	<div>{rule}{box}</div>
}
`,
		},
		"binding to element with multi-line children": {
			input: `package main

templ A() {
box := <div>
<span>a</span>
<span>b</span>
</div>
<div>{box}</div>
}
`,
			want: `package main

templ A() {
	box := <div>
		<span>a</span>
		<span>b</span>
	</div>
	<div>{box}</div>
}
`,
		},
		"var binding normalized to short form": {
			input: `package main

templ A() {
var box = <span>hi</span>
<div>{box}</div>
}
`,
			want: `package main

templ A() {
	box := <span>hi</span>
	<div>{box}</div>
}
`,
		},
		"multi-line component call args": {
			input: "package main\n\ntempl A() {\n@Header(\"a,b\",\n'x',\n`raw,arg`,\n[]int{1, 2},\nfmt.Sprintf(\"%d\", 3))\n}\n",
			want:  "package main\n\ntempl A() {\n\t@Header(\n\t\t\"a,b\",\n\t\t'x',\n\t\t`raw,arg`,\n\t\t[]int{1, 2},\n\t\tfmt.Sprintf(\"%d\", 3),\n\t)\n}\n",
		},
		"block comment in call args": {
			input: `package main

templ A() {
@Header(/*title*/ "hi")
}
`,
			want: `package main

templ A() {
	/* title */
	@Header("hi")
}
`,
		},
		"invalid go func preserved verbatim": {
			input: `package main

func bad() {
return +
}

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

func bad() {
return +
}

templ A() {
	<span>hi</span>
}
`,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			fmtr := newTestFormatter()
			got, err := fmtr.Format("test.gsx", tt.input)
			if err != nil {
				t.Fatalf("Format() error = %v", err)
			}
			if got != tt.want {
				t.Errorf("Format() mismatch:\ngot:\n%s\nwant:\n%s", got, tt.want)
			}

			// Formatting must be idempotent.
			again, err := fmtr.Format("test.gsx", got)
			if err != nil {
				t.Fatalf("second Format() error = %v", err)
			}
			if again != got {
				t.Errorf("Format() not idempotent:\nfirst:\n%s\nsecond:\n%s", got, again)
			}
		})
	}
}

// TestSplitTopLevelArgs exercises the argument splitter directly, including
// string, rune, and backtick literals as well as nested bracket depth.
func TestSplitTopLevelArgs(t *testing.T) {
	type tc struct {
		args string
		want []string
	}

	tests := map[string]tc{
		"empty": {
			args: "",
			want: nil,
		},
		"single arg": {
			args: "x",
			want: []string{"x"},
		},
		"simple args without trailing comma": {
			args: "a, b",
			want: []string{"a", "b"},
		},
		"trailing comma yields no empty arg": {
			args: "a, b,",
			want: []string{"a", "b"},
		},
		"comma inside double-quoted string": {
			args: `"a,b", x`,
			want: []string{`"a,b"`, "x"},
		},
		"escaped quote inside string": {
			args: `"a\",b", x`,
			want: []string{`"a\",b"`, "x"},
		},
		"comma inside rune literal": {
			args: `',', x`,
			want: []string{`','`, "x"},
		},
		"escaped rune literal": {
			args: `'\'', x`,
			want: []string{`'\''`, "x"},
		},
		"comma inside backtick string": {
			args: "`a,b`, x",
			want: []string{"`a,b`", "x"},
		},
		"comma inside nested parens": {
			args: `fmt.Sprintf("%d,%d", a, b), y`,
			want: []string{`fmt.Sprintf("%d,%d", a, b)`, "y"},
		},
		"comma inside brackets and braces": {
			args: `[]int{1, 2}, m[k]`,
			want: []string{"[]int{1, 2}", "m[k]"},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := splitTopLevelArgs(tt.args)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("splitTopLevelArgs(%q) = %#v, want %#v", tt.args, got, tt.want)
			}
		})
	}
}

// TestFormatGoCode exercises the gofmt wrapper, including the fallback when
// the code is not valid Go.
func TestFormatGoCode(t *testing.T) {
	type tc struct {
		code string
		want string
	}

	tests := map[string]tc{
		"valid function is formatted": {
			code: "func helper( x int ) int {\nreturn x\n}",
			want: "func helper(x int) int {\n\treturn x\n}",
		},
		"invalid code returned unchanged": {
			code: "func bad() {\nreturn +\n}",
			want: "func bad() {\nreturn +\n}",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := formatGoCode(tt.code)
			if got != tt.want {
				t.Errorf("formatGoCode(%q) = %q, want %q", tt.code, got, tt.want)
			}
		})
	}
}