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
|
package tui
import "testing"
func TestTextAreaOptions(t *testing.T) {
boldStyle := NewStyle().Bold()
italicStyle := NewStyle().Italic()
focusColor := Cyan
borderGrad := NewGradient(Red, Blue)
focusGrad := NewGradient(Green, Yellow).WithDirection(GradientVertical)
boundState := NewState("héllo")
submitted := ""
onSubmit := func(s string) { submitted = s }
type tc struct {
opts []TextAreaOption
assert func(t *testing.T, ta *TextArea)
}
tests := map[string]tc{
"defaults without options": {
opts: nil,
assert: func(t *testing.T, ta *TextArea) {
if ta.width != 40 {
t.Fatalf("width = %d, want 40", ta.width)
}
if ta.maxHeight != 0 {
t.Fatalf("maxHeight = %d, want 0", ta.maxHeight)
}
if ta.border != BorderNone {
t.Fatalf("border = %v, want BorderNone", ta.border)
}
if ta.placeholder != "" {
t.Fatalf("placeholder = %q, want empty", ta.placeholder)
}
if ta.placeholderStyle != (Style{}.Dim()) {
t.Fatal("placeholderStyle should default to dim")
}
if ta.cursorRune != '▌' {
t.Fatalf("cursorRune = %q, want '▌'", ta.cursorRune)
}
if ta.submitKey != KeyEnter {
t.Fatalf("submitKey = %v, want KeyEnter", ta.submitKey)
}
if ta.hideVirtualCursor {
t.Fatal("hideVirtualCursor should default to false")
}
if ta.autoFocus {
t.Fatal("autoFocus should default to false")
}
if ta.focusColor != nil || ta.borderGradient != nil || ta.focusGradient != nil {
t.Fatal("color and gradient pointers should default to nil")
}
if ta.onSubmit != nil {
t.Fatal("onSubmit should default to nil")
}
if ta.Text() != "" {
t.Fatalf("text = %q, want empty", ta.Text())
}
if ta.cursorPos.Get() != 0 {
t.Fatalf("cursorPos = %d, want 0", ta.cursorPos.Get())
}
},
},
"WithTextAreaWidth sets width": {
opts: []TextAreaOption{WithTextAreaWidth(25)},
assert: func(t *testing.T, ta *TextArea) {
if ta.width != 25 {
t.Fatalf("width = %d, want 25", ta.width)
}
},
},
"WithTextAreaMaxHeight sets max height": {
opts: []TextAreaOption{WithTextAreaMaxHeight(5)},
assert: func(t *testing.T, ta *TextArea) {
if ta.maxHeight != 5 {
t.Fatalf("maxHeight = %d, want 5", ta.maxHeight)
}
},
},
"WithTextAreaBorder sets border style": {
opts: []TextAreaOption{WithTextAreaBorder(BorderRounded)},
assert: func(t *testing.T, ta *TextArea) {
if ta.border != BorderRounded {
t.Fatalf("border = %v, want BorderRounded", ta.border)
}
},
},
"WithTextAreaTextStyle sets text style": {
opts: []TextAreaOption{WithTextAreaTextStyle(boldStyle)},
assert: func(t *testing.T, ta *TextArea) {
if ta.textStyle != boldStyle {
t.Fatalf("textStyle = %v, want bold", ta.textStyle)
}
},
},
"WithTextAreaPlaceholder sets placeholder text": {
opts: []TextAreaOption{WithTextAreaPlaceholder("type here")},
assert: func(t *testing.T, ta *TextArea) {
if ta.placeholder != "type here" {
t.Fatalf("placeholder = %q, want %q", ta.placeholder, "type here")
}
},
},
"WithTextAreaPlaceholderStyle overrides default dim": {
opts: []TextAreaOption{WithTextAreaPlaceholderStyle(italicStyle)},
assert: func(t *testing.T, ta *TextArea) {
if ta.placeholderStyle != italicStyle {
t.Fatalf("placeholderStyle = %v, want italic", ta.placeholderStyle)
}
},
},
"WithTextAreaCursor sets cursor rune": {
opts: []TextAreaOption{WithTextAreaCursor('_')},
assert: func(t *testing.T, ta *TextArea) {
if ta.cursorRune != '_' {
t.Fatalf("cursorRune = %q, want '_'", ta.cursorRune)
}
},
},
"WithTextAreaVirtualCursor false hides virtual cursor": {
opts: []TextAreaOption{WithTextAreaVirtualCursor(false)},
assert: func(t *testing.T, ta *TextArea) {
if !ta.hideVirtualCursor {
t.Fatal("expected hideVirtualCursor to be true")
}
},
},
"WithTextAreaVirtualCursor true keeps virtual cursor": {
opts: []TextAreaOption{WithTextAreaVirtualCursor(true)},
assert: func(t *testing.T, ta *TextArea) {
if ta.hideVirtualCursor {
t.Fatal("expected hideVirtualCursor to be false")
}
},
},
"WithTextAreaFocusColor sets focus color": {
opts: []TextAreaOption{WithTextAreaFocusColor(focusColor)},
assert: func(t *testing.T, ta *TextArea) {
if ta.focusColor == nil {
t.Fatal("expected focusColor to be set")
}
if *ta.focusColor != focusColor {
t.Fatalf("focusColor = %v, want %v", *ta.focusColor, focusColor)
}
},
},
"WithTextAreaBorderGradient sets border gradient": {
opts: []TextAreaOption{WithTextAreaBorderGradient(borderGrad)},
assert: func(t *testing.T, ta *TextArea) {
if ta.borderGradient == nil {
t.Fatal("expected borderGradient to be set")
}
if *ta.borderGradient != borderGrad {
t.Fatalf("borderGradient = %v, want %v", *ta.borderGradient, borderGrad)
}
},
},
"WithTextAreaFocusGradient sets focus gradient": {
opts: []TextAreaOption{WithTextAreaFocusGradient(focusGrad)},
assert: func(t *testing.T, ta *TextArea) {
if ta.focusGradient == nil {
t.Fatal("expected focusGradient to be set")
}
if *ta.focusGradient != focusGrad {
t.Fatalf("focusGradient = %v, want %v", *ta.focusGradient, focusGrad)
}
},
},
"WithTextAreaSubmitKey overrides default": {
opts: []TextAreaOption{WithTextAreaSubmitKey(KeyTab)},
assert: func(t *testing.T, ta *TextArea) {
if ta.submitKey != KeyTab {
t.Fatalf("submitKey = %v, want KeyTab", ta.submitKey)
}
},
},
"WithTextAreaValue binds external state and places cursor at end": {
opts: []TextAreaOption{WithTextAreaValue(boundState)},
assert: func(t *testing.T, ta *TextArea) {
if ta.text != boundState {
t.Fatal("expected text state to be the bound state")
}
if ta.Text() != "héllo" {
t.Fatalf("text = %q, want %q", ta.Text(), "héllo")
}
if got := ta.cursorPos.Get(); got != 5 {
t.Fatalf("cursorPos = %d, want 5", got)
}
},
},
"WithTextAreaAutoFocus enables auto focus": {
opts: []TextAreaOption{WithTextAreaAutoFocus(true)},
assert: func(t *testing.T, ta *TextArea) {
if !ta.autoFocus {
t.Fatal("expected autoFocus to be true")
}
},
},
"WithTextAreaOnSubmit stores a working callback": {
opts: []TextAreaOption{WithTextAreaOnSubmit(onSubmit)},
assert: func(t *testing.T, ta *TextArea) {
if ta.onSubmit == nil {
t.Fatal("expected onSubmit to be set")
}
ta.onSubmit("submitted text")
if submitted != "submitted text" {
t.Fatalf("submitted = %q, want %q", submitted, "submitted text")
}
},
},
"multiple options compose": {
opts: []TextAreaOption{
WithTextAreaWidth(30),
WithTextAreaMaxHeight(4),
WithTextAreaBorder(BorderDouble),
WithTextAreaPlaceholder("compose"),
},
assert: func(t *testing.T, ta *TextArea) {
if ta.width != 30 || ta.maxHeight != 4 {
t.Fatalf("size = (%d, %d), want (30, 4)", ta.width, ta.maxHeight)
}
if ta.border != BorderDouble {
t.Fatalf("border = %v, want BorderDouble", ta.border)
}
if ta.placeholder != "compose" {
t.Fatalf("placeholder = %q, want %q", ta.placeholder, "compose")
}
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ta := NewTextArea(tt.opts...)
tt.assert(t, ta)
})
}
}
|