-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrsts_test.go
244 lines (216 loc) · 4.9 KB
/
rsts_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
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
242
243
244
// RiveScript Test Suite: Go Test Runner
package rivescript
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"reflect"
"strings"
"testing"
yaml "gopkg.in/yaml.v2"
)
// TestCase wraps each RiveScript test.
type TestCase struct {
T *testing.T
file string
name string
username string
rs *RiveScript
steps []TestStep
}
// RootSchema is the root of the YAML structure.
type RootSchema map[string]TestSchema
// TestSchema describes the YAML test files.
type TestSchema struct {
Username string `yaml:"username"`
UTF8 bool `yaml:"utf8"`
Debug bool `yaml:"debug"`
Tests []TestStep
}
// TestStep describes the YAML structure for the actual tests.
type TestStep struct {
Source string `yaml:"source"`
Input string `yaml:"input"`
Reply interface{} `yaml:"reply"`
Assert map[string]string `yaml:"assert"`
Set map[string]string `yaml:"set"`
}
// NewTestCase initializes a new test.
func NewTestCase(t *testing.T, file, name string, opts TestSchema) *TestCase {
username := opts.Username
if username == "" {
username = "localuser"
}
return &TestCase{
T: t,
file: file,
name: name,
username: username,
rs: New(&Config{
Debug: opts.Debug,
UTF8: opts.UTF8,
}),
steps: opts.Tests,
}
}
// Run steps through the test cases and runs them.
func (t *TestCase) Run() {
var hasErrors bool
for _, step := range t.steps {
var err error
if step.Source != "" {
t.source(step)
} else if step.Input != "" {
err = t.input(step)
} else if len(step.Set) > 0 {
t.set(step)
} else if len(step.Assert) > 0 {
err = t.get(step)
} else {
log.Printf("Unsupported test step")
}
if err != nil {
t.fail(err)
hasErrors = true
break
}
}
var sym string
if hasErrors {
sym = `❌`
} else {
sym = `✓`
}
fmt.Printf("%s %s#%s\n", sym, t.file, t.name)
}
// source handles a `source` step, which parses RiveScript code.
func (t *TestCase) source(step TestStep) {
t.rs.Stream(step.Source)
t.rs.SortReplies()
}
// input handles an `input` step, which tests the brain for a reply.
func (t *TestCase) input(step TestStep) error {
reply, err := t.rs.Reply(t.username, step.Input)
if err != nil {
return t.expectedError(step, reply, err)
}
// Random replies?
if expect, ok := step.Reply.([]interface{}); ok {
pass := false
for _, candidate := range expect {
cmp, ok := candidate.(string)
if !ok {
return fmt.Errorf(
"Error",
)
}
if cmp == reply {
pass = true
break
}
}
if !pass {
return fmt.Errorf(
"Did not get expected reply for input: %s\n"+
"Expected one of: %v\n"+
" Got: %s",
step.Input,
expect,
reply,
)
}
} else if expect, ok := step.Reply.(string); ok {
if reply != strings.TrimSpace(expect) {
return fmt.Errorf(
"Did not get expected reply for input: %s\n"+
"Expected: %s\n"+
" Got: %s",
step.Input,
expect,
reply,
)
}
} else {
return fmt.Errorf(
"YAML error: `reply` was neither a `string` nor a `[]string` "+
"at %s test %s (input %s); reply was: '%v' (type %s)",
t.file,
t.name,
step.Input,
step.Reply,
reflect.TypeOf(step.Reply),
)
}
return nil
}
// expectedError inspects a Reply() error to see if it was expected by the test.
func (t *TestCase) expectedError(step TestStep, reply string, err error) error {
// Map of expected errors to their string counterpart from the test file.
goodErrors := map[string]error{
"ERR: No Reply Matched": ErrNoTriggerMatched,
}
if expect, ok := goodErrors[step.Reply.(string)]; ok {
if err == expect {
return nil
}
}
return fmt.Errorf(
"Got unexpected error from Reply (input step: %s; expected: %v): %s",
step.Input,
step.Reply,
err,
)
}
// set handles a `set` step, which sets user variables.
func (t *TestCase) set(step TestStep) {
for key, value := range step.Set {
t.rs.SetUservar(t.username, key, value)
}
}
// get handles an `assert` step, which tests user variables.
func (t *TestCase) get(step TestStep) error {
for key, expect := range step.Assert {
value, err := t.rs.GetUservar(t.username, key)
if err != nil {
return err
}
if value != expect {
return fmt.Errorf(
"Did not get expected user variable: %s\n"+
"Expected: %s\n"+
" Got: %s",
key,
expect,
value,
)
}
}
return nil
}
// fail handles a failed test.
func (t *TestCase) fail(err error) {
banner := fmt.Sprintf("Failed: %s#%s", t.file, t.name)
t.T.Errorf("%s\n%s",
banner,
err,
)
}
func TestRiveScript(t *testing.T) {
tests, err := filepath.Glob("./rsts/tests/*.yml")
if err != nil {
panic(err)
}
for _, filename := range tests {
yamlSource, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
data := RootSchema{}
yaml.Unmarshal(yamlSource, &data)
for name, opts := range data {
test := NewTestCase(t, filename, name, opts)
test.Run()
}
}
}