forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_context_test.go
67 lines (53 loc) · 2.84 KB
/
test_context_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
package godog
import (
"fmt"
"regexp"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestScenarioContext_Step(t *testing.T) {
Convey("When adding steps to ScenarioContext ", t, func() {
ctx := ScenarioContext{suite: &suite{}}
Convey("It should accept steps defined with regexp.Regexp", func() {
re := regexp.MustCompile(`(?:it is a test)?.{10}x*`)
So(func() { ctx.Step(re, okEmptyResult) }, ShouldNotPanic)
})
Convey("It should accept steps defined with bytes slice", func() {
So(func() { ctx.Step([]byte("(?:it is a test)?.{10}x*"), okEmptyResult) }, ShouldNotPanic)
})
Convey("It should accept steps handler with empty return", func() {
So(func() { ctx.Step(".*", okEmptyResult) }, ShouldNotPanic)
})
Convey("It should accept steps handler with error return", func() {
So(func() { ctx.Step(".*", okErrorResult) }, ShouldNotPanic)
})
Convey("It should accept steps handler with string slice return", func() {
So(func() { ctx.Step(".*", okSliceResult) }, ShouldNotPanic)
})
Convey("It should panic if step expression is neither a string, regex or byte slice", func() {
So(func() { ctx.Step(1251, okSliceResult) }, ShouldPanicWith, fmt.Sprintf("expecting expr to be a *regexp.Regexp or a string, got type: %T", 12))
})
Convey("It should panic if step handler", func() {
Convey("is not a function", func() {
So(func() { ctx.Step(".*", 124) }, ShouldPanicWith, fmt.Sprintf("expected handler to be func, but got: %T", 12))
})
Convey("has more than 2 return values", func() {
So(func() { ctx.Step(".*", nokLimitCase) }, ShouldPanicWith, "expected handler to return either zero, one or two values, but it has: 3")
So(func() { ctx.Step(".*", nokMore) }, ShouldPanicWith, "expected handler to return either zero, one or two values, but it has: 5")
})
Convey("return type is not an error or string slice or void", func() {
So(func() { ctx.Step(".*", nokInvalidReturnInterfaceType) }, ShouldPanicWith, "expected handler to return an error or context.Context, but got: interface")
So(func() { ctx.Step(".*", nokInvalidReturnSliceType) }, ShouldPanicWith, "expected handler to return []string for multistep, but got: []int")
So(func() { ctx.Step(".*", nokInvalidReturnOtherType) }, ShouldPanicWith, "expected handler to return an error or []string, but got: chan")
})
})
})
}
func okEmptyResult() {}
func okErrorResult() error { return nil }
func okSliceResult() []string { return nil }
func nokLimitCase() (string, int, error) { return "", 0, nil }
func nokMore() (int, int, int, int, error) { return 0, 0, 0, 0, nil }
func nokInvalidReturnInterfaceType() interface{} { return 0 }
func nokInvalidReturnSliceType() []int { return nil }
func nokInvalidReturnOtherType() chan int { return nil }