forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_test.go
198 lines (162 loc) · 3.74 KB
/
magic_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"errors"
"fmt"
"testing"
)
// Mock magic action
type mockMA struct {
keyCalled bool
descCalled bool
runTextCalled bool
runCalled bool
returnError bool
keyword string
}
func (a *mockMA) Keyword() string {
a.keyCalled = true
if a.keyword != "" {
return a.keyword
}
return "test"
}
func (a *mockMA) Description() string {
a.descCalled = true
return "Just a test"
}
func (a *mockMA) RunText() string {
a.runTextCalled = true
return "Performing test…"
}
func (a *mockMA) Run() error {
a.runCalled = true
if a.returnError {
return errors.New("requested error")
}
return nil
}
// Returns an error if the MA wasn't "shown".
// That means MagicActions didn't show a list of actions.
func (a *mockMA) ValidateShown() error {
if !a.keyCalled {
return errors.New("Keyword() not called")
}
if !a.descCalled {
return errors.New("Description() not called")
}
if a.runCalled {
return errors.New("Run() called")
}
if a.runTextCalled {
return errors.New("RunText() called")
}
return nil
}
// Returns an error if the MA wasn't run.
func (a *mockMA) ValidateRun() error {
if !a.keyCalled {
return errors.New("Keyword() not called")
}
if a.descCalled {
return errors.New("Description() called")
}
if !a.runCalled {
return errors.New("Run() not called")
}
if !a.runTextCalled {
return errors.New("RunText() not called")
}
return nil
}
// TestNonMagicArgs tests that normal arguments aren't ignored
func TestNonMagicArgs(t *testing.T) {
t.Parallel()
data := []struct {
in, x []string
}{
{[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
}
for _, td := range data {
wf := New()
ma := wf.MagicActions
args, handled := ma.handleArgs(td.in, DefaultMagicPrefix)
if handled {
t.Error("handled")
}
if !slicesEqual(args, td.x) {
t.Errorf("not equal. Expected=%v, Got=%v", td.x, args)
}
}
}
func TestMagicDefaults(t *testing.T) {
t.Parallel()
wf := New()
ma := wf.MagicActions
x := 6
v := len(ma.actions)
if v != x {
t.Errorf("Bad MagicAction count. Expected=%d, Got=%d", x, v)
}
}
func TestMagicActions(t *testing.T) {
t.Parallel()
tests := []struct {
in string
handled bool
shown bool
run bool
}{
{"workflow:tes", true, true, false},
{"workflow:test", true, false, false},
}
for _, td := range tests {
td := td // capture variable
t.Run(fmt.Sprintf("MagicAction(%q)", td.in), func(t *testing.T) {
t.Parallel()
var (
wf = New()
ta = &mockMA{}
)
wf.MagicActions.Register(ta)
_, v := wf.MagicActions.handleArgs([]string{td.in}, DefaultMagicPrefix)
if v != td.handled {
t.Errorf("Bad Handled. Expected=%v, Got=%v", td.handled, v)
}
if err := ta.ValidateShown(); err != nil && td.shown {
t.Error("Not Shown")
}
if err := ta.ValidateRun(); err != nil && td.run {
t.Error("Not Run")
}
})
}
}
// Test automatically-added updateMA.
func TestMagicUpdate(t *testing.T) {
t.Parallel()
u := &mockUpdater{}
// Workflow automatically adds a MagicAction to call the Updater
wf := New(Update(u))
ma := wf.MagicActions
// Incomplete keyword = search query
_, v := ma.handleArgs([]string{"workflow:upda"}, DefaultMagicPrefix)
if !v {
t.Errorf("Bad handled. Expected=%v, Got=%v", true, v)
}
// Keyword of update MA
_, v = ma.handleArgs([]string{"workflow:update"}, DefaultMagicPrefix)
if !v {
t.Errorf("Bad handled. Expected=%v, Got=%v", true, v)
}
if !u.checkForUpdateCalled {
t.Errorf("Bad update. CheckForUpdate not called")
}
if !u.updateAvailableCalled {
t.Errorf("Bad update. UpdateAvailable not called")
}
if !u.installCalled {
t.Errorf("Bad update. Install not called")
}
}