-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
msg_test.go
183 lines (162 loc) · 4.03 KB
/
msg_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
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
package zenity_test
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"testing"
"time"
"github.com/ncruces/zenity"
"go.uber.org/goleak"
)
func ExampleError() {
zenity.Error("An error has occurred.",
zenity.Title("Error"),
zenity.ErrorIcon)
}
func ExampleInfo() {
zenity.Info("All updates are complete.",
zenity.Title("Information"),
zenity.InfoIcon)
}
func ExampleWarning() {
zenity.Warning("Are you sure you want to proceed?",
zenity.Title("Warning"),
zenity.WarningIcon)
}
func ExampleQuestion() {
zenity.Question("Are you sure you want to proceed?",
zenity.Title("Question"),
zenity.QuestionIcon)
}
var msgFuncs = []struct {
name string
fn func(string, ...zenity.Option) error
}{
{"Error", zenity.Error},
{"Info", zenity.Info},
{"Warning", zenity.Warning},
{"Question", zenity.Question},
}
func TestMessage_timeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
for _, tt := range msgFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
defer cancel()
err := tt.fn("text", zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !os.IsTimeout(err) {
t.Error("did not timeout:", err)
}
})
}
}
func TestMessage_cancel(t *testing.T) {
for _, tt := range msgFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := tt.fn("text", zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
})
}
}
func TestMessage_script(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
for _, tt := range msgFuncs {
t.Run(tt.name+"OK", func(t *testing.T) {
err := tt.fn("Please, press OK.", zenity.OKLabel("OK"))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil {
t.Errorf("%s() = %v; want nil", tt.name, err)
}
})
t.Run(tt.name+"Extra", func(t *testing.T) {
err := tt.fn("Please, press Extra.", zenity.ExtraButton("Extra"))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err != zenity.ErrExtraButton {
t.Errorf("%s() = %v; want %v", tt.name, err, zenity.ErrExtraButton)
}
})
}
tests := []struct {
name string
call string
err error
}{
{name: "QuestionYes", call: "press Yes"},
{name: "QuestionNo", call: "press No", err: zenity.ErrExtraButton},
{name: "QuestionCancel", call: "cancel", err: zenity.ErrCanceled},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := zenity.Question(fmt.Sprintf("Please, %s.", tt.call),
zenity.OKLabel("Yes"),
zenity.ExtraButton("No"),
zenity.CancelLabel("Cancel"))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err != tt.err {
t.Errorf("Question() = %v; want %v", err, tt.err)
}
})
}
}
func TestMessage_icon(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
err := zenity.Question("Does this dialog have an error icon?",
zenity.OKLabel("Yes"),
zenity.CancelLabel("No"),
zenity.ErrorIcon)
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil {
t.Errorf("Question() = %v; want nil", err)
}
}
func TestMessage_customIcon(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
err := zenity.Question("Does this dialog have a custom icon?",
zenity.Title(""),
zenity.OKLabel("Yes"),
zenity.CancelLabel("No"),
zenity.Icon("testdata/icon.png"))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil && (runtime.GOOS == "windows" || runtime.GOOS == "darwin") {
t.Errorf("Question() = %v; want nil", err)
}
}
func TestMessage_callbacks(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
for i := 0; i < 2000; i++ {
zenity.Error("text", zenity.Context(ctx))
}
}