-
Notifications
You must be signed in to change notification settings - Fork 1
/
button_test.go
132 lines (114 loc) · 2.9 KB
/
button_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
package goey
import (
"math/rand"
"reflect"
"strconv"
"testing"
"testing/quick"
"bitbucket.org/rj/goey/base"
)
func ExampleButton() {
clickCount := 0
// 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
text := "Click me!"
if clickCount > 0 {
text = text + " (" + strconv.Itoa(clickCount) + ")"
}
// The GUI contains a single widget, this button.
return &VBox{
AlignMain: MainCenter,
AlignCross: CrossCenter,
Children: []base.Widget{
&Button{Text: text, OnClick: func() {
clickCount++
update()
}},
}}
}
}
func buttonValues(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)
}
func TestButtonMount(t *testing.T) {
testingMountWidgets(t,
&Button{Text: "A"},
&Button{Text: "D", Disabled: true},
&Button{Text: "E", Default: true},
)
t.Run("QuickCheck", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
f := func(text string, disabled, def bool) bool {
return testingMountWidget(t, &Button{Text: text, Disabled: disabled, Default: def})
}
if err := quick.Check(f, &quick.Config{Values: buttonValues}); err != nil {
t.Errorf("quick: %s", err)
}
})
}
func TestButtonClose(t *testing.T) {
testingCloseWidgets(t,
&Button{Text: "A"},
&Button{Text: "D", Disabled: true},
&Button{Text: "E", Default: true},
)
}
func TestButtonFocus(t *testing.T) {
testingCheckFocusAndBlur(t,
&Button{Text: "A"},
&Button{Text: "B"},
&Button{Text: "C"},
)
}
func TestButtonClick(t *testing.T) {
testingCheckClick(t,
&Button{Text: "A"},
&Button{Text: "B"},
&Button{Text: "C"},
)
}
func TestButtonUpdate(t *testing.T) {
testingUpdateWidgets(t, []base.Widget{
&Button{Text: "A"},
&Button{Text: "D", Disabled: true},
&Button{Text: "E", Default: true},
}, []base.Widget{
&Button{Text: "AB"},
&Button{Text: "DB", Default: true},
&Button{Text: "EB", Disabled: true},
})
t.Run("QuickCheck", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
updater, closer := testingUpdateWidget(t)
defer closer()
f := func(text string, disabled, def bool) bool {
return updater(&Button{Text: text, Disabled: disabled, Default: def})
}
if err := quick.Check(f, &quick.Config{Values: buttonValues}); err != nil {
t.Errorf("quick: %s", err)
}
})
}