-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps_invoke_test.go
172 lines (125 loc) · 4.15 KB
/
steps_invoke_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
package gourd
import (
"github.com/stretchr/testify/assert"
"testing"
)
func Test_invoking_a_pending_step_returns_pending(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Pending()
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, pending)
}
func Test_a_step_is_pending_by_default(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
testee.add_step(pattern)
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, pending)
}
func Test_invoking_a_step_that_is_set_to_always_pass_returns_success(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Pass()
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, success)
}
func Test_invoking_a_step_that_is_set_to_always_fail_returns_fail(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Fail()
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, fail)
}
func Test_invoking_a_step_with_an_unknown_id_fails(t *testing.T) {
testee := &gourd_steps{}
result, _ := testee.invoke_step("unknown id", []string{})
assert.Equal(t, result, fail)
}
func Test_invoking_a_step_executes_the_defined_action(t *testing.T) {
testee := &gourd_steps{}
executed := false
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Do(func(context interface{}, arguments Arguments) {
executed = true
})
id, _, _ := testee.matching_step(pattern)
testee.invoke_step(id, []string{})
assert.True(t, executed)
}
func Test_invoking_a_step_whos_action_does_not_panic_returns_success(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Do(func(context interface{}, arguments Arguments) {
})
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, success)
}
func Test_invoking_a_step_whos_action_panics_returns_fail(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Do(func(context interface{}, arguments Arguments) {
panic("")
})
id, _, _ := testee.matching_step(pattern)
result, _ := testee.invoke_step(id, []string{})
assert.Equal(t, result, fail)
}
func Test_invoking_a_failing_step_returns_the_failure_message(t *testing.T) {
testee := &gourd_steps{}
expected_message := "error message"
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Do(func(context interface{}, arguments Arguments) {
panic(expected_message)
})
id, _, _ := testee.matching_step(pattern)
_, message := testee.invoke_step(id, []string{})
assert.Equal(t, expected_message, message)
}
func Test_invoke_step_passes_the_context_created_in_begin_scenario(t *testing.T) {
type context_type struct {
value int
}
expected_context := &context_type{123}
testee := &gourd_steps{}
testee.new_context = func() interface{} {
return expected_context
}
var actual_context *context_type
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
step.Do(func(context interface{}, arguments Arguments) {
actual_context = context.(*context_type)
})
id, _, _ := testee.matching_step(pattern)
testee.begin_scenario()
testee.invoke_step(id, []string{})
assert.Equal(t, expected_context, actual_context)
assert.Exactly(t, expected_context, actual_context)
}
func Test_passed_arguments_are_initialized_with_the_string_slice(t *testing.T) {
testee := &gourd_steps{}
pattern := "arbitrary step pattern"
step := testee.add_step(pattern)
id, _, _ := testee.matching_step(pattern)
input := []string{"a b", "1234", "日本語", "\\^\\$"}
var actual Arguments
step.Do(func(context interface{}, arguments Arguments) {
actual = arguments
})
testee.invoke_step(id, input)
expected := &gourd_arguments{input}
assert.Equal(t, expected, actual)
}