-
Notifications
You must be signed in to change notification settings - Fork 1
/
textinput_test.go
143 lines (124 loc) · 3.47 KB
/
textinput_test.go
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
package goey
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"testing"
"testing/quick"
"bitbucket.org/rj/goey/base"
)
func ExampleTextInput() {
// In a full application, this variable would be updated to point to
// the main window for the application.
var mainWindow *Window
// These functions are used to update the GUI. See below
var update func()
var render func() base.Widget
// Update function
update = func() {
err := mainWindow.SetChild(render())
if err != nil {
panic(err)
}
}
// Render function generates a tree of Widgets to describe the desired
// state of the GUI.
render = func() base.Widget {
// Prep - text for the button
// The GUI contains a single widget, this button.
return &VBox{Children: []base.Widget{
&Label{Text: "Enter you text below:"},
&TextInput{
Value: "",
Placeholder: "Enter your data here",
OnChange: func(value string) {
fmt.Println("Change: ", value)
// In a real example, you would update your data, and then
// need to render the window again.
update()
},
},
}}
}
}
func textinputValues(values []reflect.Value, rand *rand.Rand) {
// Get a string
labelValues(values, rand)
// Create a choices for disabled and default
values[1] = reflect.ValueOf(rand.Uint64()%2 == 0)
values[2] = reflect.ValueOf(rand.Uint64()%2 == 0)
values[3] = reflect.ValueOf(rand.Uint64()%2 == 0)
}
func TestTextInputMount(t *testing.T) {
testingMountWidgets(t,
&TextInput{Value: "A"},
&TextInput{Value: "B", Placeholder: "..."},
&TextInput{Value: "C", Disabled: true},
&TextInput{Value: "D", ReadOnly: true},
&TextInput{Value: "E", Password: true},
)
t.Run("QuickCheck", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
f := func(value string, disabled, password, readonly bool) bool {
return testingMountWidget(t, &TextInput{Value: value, Disabled: disabled, Password: password, ReadOnly: readonly})
}
if err := quick.Check(f, &quick.Config{Values: textinputValues}); err != nil {
t.Errorf("quick: %s", err)
}
})
}
func TestTextInputClose(t *testing.T) {
testingCloseWidgets(t,
&TextInput{Value: "A"},
&TextInput{Value: "B", Placeholder: "..."},
&TextInput{Value: "C", Disabled: true},
&TextInput{Value: "D", ReadOnly: true},
&TextInput{Value: "E", Password: true},
)
}
func TestTextInputOnFocus(t *testing.T) {
testingCheckFocusAndBlur(t,
&TextInput{},
&TextInput{},
&TextInput{},
)
}
func TestTextInputOnChange(t *testing.T) {
log := bytes.NewBuffer(nil)
testingTypeKeys(t, "Hello",
&TextInput{OnChange: func(v string) {
log.WriteString(v)
log.WriteString("\x1E")
}})
const want = "H\x1EHe\x1EHel\x1EHell\x1EHello\x1E"
if got := log.String(); got != want {
t.Errorf("Wanted %v, got %v", want, got)
}
}
func TestTextInputOnEnterKey(t *testing.T) {
log := bytes.NewBuffer(nil)
testingTypeKeys(t, "Hello\n",
&TextInput{OnEnterKey: func(v string) {
log.WriteString(v)
}})
const want = "Hello"
if got := log.String(); got != want {
t.Errorf("Wanted %v, got %v", want, got)
}
}
func TestTextInputUpdateProps(t *testing.T) {
testingUpdateWidgets(t, []base.Widget{
&TextInput{Value: "A"},
&TextInput{Value: "B", Placeholder: "..."},
&TextInput{Value: "C", Disabled: true},
&TextInput{Value: "D", ReadOnly: true},
}, []base.Widget{
&TextInput{Value: "AA", ReadOnly: true},
&TextInput{Value: "BA", Disabled: true},
&TextInput{Value: "CA", Placeholder: "***", Disabled: false},
&TextInput{Value: "DA"},
})
}