gitstack

grindlemire/go-tui code browser

8.6 KB Go 347 lines 2026-06-03 ยท d15bb9f 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
package tuigen

import (
	"testing"
)

func TestParseTailwindClasses_Multiple(t *testing.T) {
	type tc struct {
		input           string
		wantOptions     []string
		wantTextMethods []string
		wantImports     []string
	}

	tests := map[string]tc{
		"layout classes": {
			input: "flex flex-col gap-2 p-4",
			wantOptions: []string{
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row)",
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column)",
				"tui.WithGap(2)",
				"tui.WithPadding(4)",
			},
			wantImports: []string{"tui"},
		},
		"text styles": {
			input:           "font-bold text-cyan",
			wantTextMethods: []string{"Bold()", "Foreground(tui.Cyan)"},
			wantImports:     []string{"tui"},
		},
		"mixed classes": {
			input: "flex-col border-rounded font-bold text-red",
			wantOptions: []string{
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column)",
				"tui.WithBorder(tui.BorderRounded)",
			},
			wantTextMethods: []string{"Bold()", "Foreground(tui.Red)"},
			wantImports:     []string{"tui"},
		},
		"with unknown classes": {
			input: "flex unknown-class gap-1",
			wantOptions: []string{
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row)",
				"tui.WithGap(1)",
			},
			wantImports: []string{"tui"},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			result := ParseTailwindClasses(tt.input)

			// Check options
			if len(result.Options) != len(tt.wantOptions) {
				t.Errorf("Options count = %d, want %d", len(result.Options), len(tt.wantOptions))
			} else {
				for i, opt := range tt.wantOptions {
					if result.Options[i] != opt {
						t.Errorf("Options[%d] = %q, want %q", i, result.Options[i], opt)
					}
				}
			}

			// Check text methods
			if len(result.TextMethods) != len(tt.wantTextMethods) {
				t.Errorf("TextMethods count = %d, want %d", len(result.TextMethods), len(tt.wantTextMethods))
			} else {
				for i, method := range tt.wantTextMethods {
					if result.TextMethods[i] != method {
						t.Errorf("TextMethods[%d] = %q, want %q", i, result.TextMethods[i], method)
					}
				}
			}

			// Check imports
			for _, imp := range tt.wantImports {
				if !result.NeedsImports[imp] {
					t.Errorf("missing import %q", imp)
				}
			}
		})
	}
}

func TestBuildTextStyleOption(t *testing.T) {
	type tc struct {
		methods []string
		want    string
	}

	tests := map[string]tc{
		"empty": {
			methods: nil,
			want:    "",
		},
		"single method": {
			methods: []string{"Bold()"},
			want:    "tui.WithTextStyle(tui.NewStyle().Bold())",
		},
		"multiple methods": {
			methods: []string{"Bold()", "Foreground(tui.Cyan)", "Italic()"},
			want:    "tui.WithTextStyle(tui.NewStyle().Bold().Foreground(tui.Cyan).Italic())",
		},
	}

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

func TestParseTailwindClasses_PaddingAccumulation(t *testing.T) {
	type tc struct {
		input       string
		wantOptions []string
	}

	tests := map[string]tc{
		"single padding top": {
			input:       "pt-2",
			wantOptions: []string{"tui.WithPaddingTRBL(2, 0, 0, 0)"},
		},
		"padding top and bottom": {
			input:       "pt-2 pb-4",
			wantOptions: []string{"tui.WithPaddingTRBL(2, 0, 4, 0)"},
		},
		"all padding sides": {
			input:       "pt-1 pr-2 pb-3 pl-4",
			wantOptions: []string{"tui.WithPaddingTRBL(1, 2, 3, 4)"},
		},
		"padding with other classes": {
			input: "flex pt-2 pb-4 gap-1",
			wantOptions: []string{
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row)",
				"tui.WithGap(1)",
				"tui.WithPaddingTRBL(2, 0, 4, 0)",
			},
		},
		"padding horizontal": {
			input:       "px-3",
			wantOptions: []string{"tui.WithPaddingTRBL(0, 3, 0, 3)"},
		},
		"padding vertical": {
			input:       "py-5",
			wantOptions: []string{"tui.WithPaddingTRBL(5, 0, 5, 0)"},
		},
		"padding horizontal and top": {
			input:       "px-3 pt-2",
			wantOptions: []string{"tui.WithPaddingTRBL(2, 3, 0, 3)"},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			result := ParseTailwindClasses(tt.input)

			if len(result.Options) != len(tt.wantOptions) {
				t.Errorf("Options count = %d, want %d. Got: %v", len(result.Options), len(tt.wantOptions), result.Options)
				return
			}

			for i, opt := range tt.wantOptions {
				if result.Options[i] != opt {
					t.Errorf("Options[%d] = %q, want %q", i, result.Options[i], opt)
				}
			}
		})
	}
}

func TestParseTailwindClasses_MarginAccumulation(t *testing.T) {
	type tc struct {
		input       string
		wantOptions []string
	}

	tests := map[string]tc{
		"single margin top": {
			input:       "mt-2",
			wantOptions: []string{"tui.WithMarginTRBL(2, 0, 0, 0)"},
		},
		"margin horizontal": {
			input:       "mx-3",
			wantOptions: []string{"tui.WithMarginTRBL(0, 3, 0, 3)"},
		},
		"margin vertical": {
			input:       "my-2",
			wantOptions: []string{"tui.WithMarginTRBL(2, 0, 2, 0)"},
		},
		"margin all sides": {
			input:       "mt-1 mr-2 mb-3 ml-4",
			wantOptions: []string{"tui.WithMarginTRBL(1, 2, 3, 4)"},
		},
		"margin with other classes": {
			input: "flex mt-2 mb-4 gap-1",
			wantOptions: []string{
				"tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row)",
				"tui.WithGap(1)",
				"tui.WithMarginTRBL(2, 0, 4, 0)",
			},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			result := ParseTailwindClasses(tt.input)

			if len(result.Options) != len(tt.wantOptions) {
				t.Errorf("Options count = %d, want %d. Got: %v", len(result.Options), len(tt.wantOptions), result.Options)
				return
			}

			for i, opt := range tt.wantOptions {
				if result.Options[i] != opt {
					t.Errorf("Options[%d] = %q, want %q", i, result.Options[i], opt)
				}
			}
		})
	}
}

func TestParseTailwindClasses_PaddingAndMarginCombined(t *testing.T) {
	result := ParseTailwindClasses("pt-1 pb-2 mt-3 mb-4")

	expected := []string{
		"tui.WithPaddingTRBL(1, 0, 2, 0)",
		"tui.WithMarginTRBL(3, 0, 4, 0)",
	}

	if len(result.Options) != len(expected) {
		t.Errorf("Options count = %d, want %d. Got: %v", len(result.Options), len(expected), result.Options)
		return
	}

	for i, opt := range expected {
		if result.Options[i] != opt {
			t.Errorf("Options[%d] = %q, want %q", i, result.Options[i], opt)
		}
	}
}

func TestPaddingAccumulator(t *testing.T) {
	t.Run("merge and toOption", func(t *testing.T) {
		var acc PaddingAccumulator
		acc.Merge("top", 1)
		acc.Merge("right", 2)
		acc.Merge("bottom", 3)
		acc.Merge("left", 4)

		want := "tui.WithPaddingTRBL(1, 2, 3, 4)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("partial sides", func(t *testing.T) {
		var acc PaddingAccumulator
		acc.Merge("top", 5)
		acc.Merge("bottom", 10)

		want := "tui.WithPaddingTRBL(5, 0, 10, 0)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("merge x sets left and right", func(t *testing.T) {
		var acc PaddingAccumulator
		acc.Merge("x", 5)

		want := "tui.WithPaddingTRBL(0, 5, 0, 5)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("merge y sets top and bottom", func(t *testing.T) {
		var acc PaddingAccumulator
		acc.Merge("y", 3)

		want := "tui.WithPaddingTRBL(3, 0, 3, 0)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("empty returns empty string", func(t *testing.T) {
		var acc PaddingAccumulator
		if got := acc.ToOption(); got != "" {
			t.Errorf("empty ToOption() = %q, want empty", got)
		}
	})
}

func TestMarginAccumulator(t *testing.T) {
	t.Run("merge individual sides", func(t *testing.T) {
		var acc MarginAccumulator
		acc.Merge("top", 1)
		acc.Merge("right", 2)
		acc.Merge("bottom", 3)
		acc.Merge("left", 4)

		want := "tui.WithMarginTRBL(1, 2, 3, 4)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("merge x sets left and right", func(t *testing.T) {
		var acc MarginAccumulator
		acc.Merge("x", 5)

		want := "tui.WithMarginTRBL(0, 5, 0, 5)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("merge y sets top and bottom", func(t *testing.T) {
		var acc MarginAccumulator
		acc.Merge("y", 3)

		want := "tui.WithMarginTRBL(3, 0, 3, 0)"
		got := acc.ToOption()
		if got != want {
			t.Errorf("ToOption() = %q, want %q", got, want)
		}
	})

	t.Run("empty returns empty string", func(t *testing.T) {
		var acc MarginAccumulator
		if got := acc.ToOption(); got != "" {
			t.Errorf("empty ToOption() = %q, want empty", got)
		}
	})
}