-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgodogs_test.go
270 lines (210 loc) · 6.04 KB
/
godogs_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package mruby_test
import (
"flag"
"fmt"
"os"
"regexp"
"testing"
"github.com/cucumber/godog"
"github.com/elct9620/mruby-go"
"github.com/google/go-cmp/cmp"
)
var opts = godog.Options{
Tags: "~@wip",
Format: "pretty",
Paths: []string{"features"},
}
const SuiteSuccessCode = 0
type RubyFeature struct {
mrb *mruby.State
ret mruby.Value
exc mruby.RException
}
func (feat *RubyFeature) iExecuteRubyCode(code *godog.DocString) error {
ret, err := feat.mrb.LoadString(code.Content)
if err != nil {
exc, ok := err.(mruby.RException)
if !ok {
return err
}
feat.exc = exc
}
feat.ret = ret
return nil
}
func (feat *RubyFeature) thereShouldReturnInteger(expected int) error {
actual, ok := feat.ret.(int)
if !ok {
return fmt.Errorf("expected integer, got %T (%+v)", feat.ret, feat.ret)
}
if actual != expected {
return fmt.Errorf("expected %d, got %d", expected, actual)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnTrue() error {
actual, ok := feat.ret.(bool)
if !ok {
return fmt.Errorf("expected bool, got %T (%+v)", feat.ret, feat.ret)
}
if !actual {
return fmt.Errorf("expected true, got false")
}
return nil
}
func (feat *RubyFeature) thereShouldReturnFalse() error {
actual, ok := feat.ret.(bool)
if !ok {
return fmt.Errorf("expected bool, got %T (%+v)", feat.ret, feat.ret)
}
if actual {
return fmt.Errorf("expected false, got true")
}
return nil
}
func (feat *RubyFeature) thereShouldReturnNil() error {
if feat.ret != nil {
return fmt.Errorf("expected nil, got %T (%+v)", feat.ret, feat.ret)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnString(expected string) error {
actual, ok := feat.ret.(string)
if !ok {
return fmt.Errorf("expected string, got %T (%+v)", feat.ret, feat.ret)
}
if !cmp.Equal(actual, expected) {
return fmt.Errorf("string not matched %s", cmp.Diff(actual, expected))
}
return nil
}
func (feat *RubyFeature) thereShouldReturnStringLike(expected string) error {
actual, ok := feat.ret.(string)
if !ok {
return fmt.Errorf("expected string, got %T (%+v)", feat.ret, feat.ret)
}
expr, err := regexp.Compile(expected)
if err != nil {
return err
}
if !expr.MatchString(actual) {
return fmt.Errorf("string not matched %s", cmp.Diff(actual, expected))
}
return nil
}
func (feat *RubyFeature) thereShouldReturnSymbol(expected string) error {
ret, ok := feat.ret.(mruby.Symbol)
if !ok {
return fmt.Errorf("expected symbol, got %T", feat.ret)
}
actual := feat.mrb.SymbolName(ret)
if actual != expected {
return fmt.Errorf("expected %s, got %s", expected, actual)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnObject() error {
_, ok := feat.ret.(mruby.RObject)
if !ok {
return fmt.Errorf("expected object, got %T", feat.ret)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnClass(expected string) error {
class, ok := feat.ret.(*mruby.Class)
if !ok {
return fmt.Errorf("expected class, got %T", feat.ret)
}
actual := feat.mrb.ClassName(class)
if actual != expected {
return fmt.Errorf("expected %s, got %s", expected, actual)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnModule(expected string) error {
module, ok := feat.ret.(*mruby.Module)
if !ok {
return fmt.Errorf("expected module, got %T", feat.ret)
}
actual := feat.mrb.ClassName(module)
if actual != expected {
return fmt.Errorf("expected %s, got %s", expected, actual)
}
return nil
}
func (feat *RubyFeature) thereShouldReturnAnArray(doc *godog.DocString) error {
actual, ok := feat.ret.([]mruby.Value)
if !ok {
return fmt.Errorf("expected array, got %T", feat.ret)
}
actualStr := fmt.Sprintf("%+v", actual)
expectedStr := doc.Content
if !cmp.Equal(actualStr, expectedStr) {
return fmt.Errorf("array not matched %s", cmp.Diff(actualStr, expectedStr))
}
return nil
}
func (feat *RubyFeature) thereShouldReturnAHash(doc *godog.DocString) error {
actual, ok := feat.ret.(map[mruby.Value]mruby.Value)
if !ok {
return fmt.Errorf("expected hash, got %T", feat.ret)
}
actualStr := fmt.Sprintf("%+v", actual)
expectedStr := doc.Content
if !cmp.Equal(actualStr, expectedStr) {
return fmt.Errorf("hash not matched %s", cmp.Diff(actualStr, expectedStr))
}
return nil
}
func (feat *RubyFeature) theExceptionMessageShouldBe(expected string) error {
actual := feat.exc.Error()
if actual != expected {
return fmt.Errorf("expected %s, got %s", expected, actual)
}
return nil
}
func InitializeScenario(s *godog.ScenarioContext) {
mrb, err := mruby.New()
if err != nil {
panic(err)
}
feat := RubyFeature{
mrb: mrb,
}
s.Step(`^I execute ruby code:$`, feat.iExecuteRubyCode)
s.Step(`^there should return integer (-?\d+)$`, feat.thereShouldReturnInteger)
s.Step(`^there should return true$`, feat.thereShouldReturnTrue)
s.Step(`^there should return false$`, feat.thereShouldReturnFalse)
s.Step(`^there should return nil$`, feat.thereShouldReturnNil)
s.Step(`^there should return string "([^"]*)"$`, feat.thereShouldReturnString)
s.Step(`^there should return string like "([^"]*)"$`, feat.thereShouldReturnStringLike)
s.Step(`^there should return symbol "([^"]*)"$`, feat.thereShouldReturnSymbol)
s.Step(`^there should return object$`, feat.thereShouldReturnObject)
s.Step(`^there should return class "([^"]*)"$`, feat.thereShouldReturnClass)
s.Step(`^there should return module "([^"]*)"$`, feat.thereShouldReturnModule)
s.Step(`^there should return an array$`, feat.thereShouldReturnAnArray)
s.Step(`^there should return a hash$`, feat.thereShouldReturnAHash)
s.Step(`^the exception message should be "([^"]*)"$`, feat.theExceptionMessageShouldBe)
}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
}
func TestFeatures(t *testing.T) {
o := opts
o.TestingT = t
if o.Format == "junit" {
output, err := os.OpenFile("junit.xml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Fatal(err)
}
defer output.Close()
o.Output = output
}
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &o,
}
if suite.Run() != SuiteSuccessCode {
t.Fatal("Non-zero exit code")
}
}