summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/layout/flex_test.go
blob: cad38dc8ff7e68bb007d0f34df281540e54eae2a (plain)
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
package layout

import (
	"strings"
	"testing"
)

func TestFlexGap(t *testing.T) {
	tests := []struct {
		name     string
		opts     FlexOptions
		items    []FlexItem
		expected string
	}{
		{
			name: "Row with gap",
			opts: FlexOptions{
				Direction: Row,
				Width:     20,
				Height:    1,
				Gap:       2,
			},
			items: []FlexItem{
				{View: "A"},
				{View: "B"},
				{View: "C"},
			},
			expected: "A  B  C",
		},
		{
			name: "Column with gap",
			opts: FlexOptions{
				Direction: Column,
				Width:     1,
				Height:    5,
				Gap:       1,
				Align:     AlignStart,
			},
			items: []FlexItem{
				{View: "A", FixedSize: 1},
				{View: "B", FixedSize: 1},
				{View: "C", FixedSize: 1},
			},
			expected: "A\n \nB\n \nC",
		},
		{
			name: "Row with gap and justify space between",
			opts: FlexOptions{
				Direction: Row,
				Width:     15,
				Height:    1,
				Gap:       1,
				Justify:   JustifySpaceBetween,
			},
			items: []FlexItem{
				{View: "A"},
				{View: "B"},
				{View: "C"},
			},
			expected: "A      B      C",
		},
		{
			name: "No gap specified",
			opts: FlexOptions{
				Direction: Row,
				Width:     10,
				Height:    1,
			},
			items: []FlexItem{
				{View: "A"},
				{View: "B"},
				{View: "C"},
			},
			expected: "ABC",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := Render(tt.opts, tt.items...)
			// Trim any trailing spaces for comparison
			result = strings.TrimRight(result, " ")
			expected := strings.TrimRight(tt.expected, " ")

			if result != expected {
				t.Errorf("Render() = %q, want %q", result, expected)
			}
		})
	}
}