-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
252 lines (176 loc) · 7.69 KB
/
parser_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
package gourd
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
func Test_parser_returns_success_to_begin_scenario(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("begin_scenario").Return().Once()
command := []byte(`["begin_scenario"]`)
response := testee.parse(command)
assert.Equal(t, `["success"]`, response)
}
func Test_parser_notifies_steps_when_a_new_scenario_begins(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("begin_scenario").Return().Once()
command := []byte(`["begin_scenario"]`)
testee.parse(command)
steps.Mock.AssertExpectations(t)
}
func Test_parser_returns_success_to_end_scenario(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`["end_scenario"]`)
response := testee.parse(command)
assert.Equal(t, `["success"]`, response)
}
func Test_parser_asks_for_matching_step_with_given_pattern(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
pattern := "Given pattern"
steps.On("matching_step", pattern).Return("", false, []capturing_group{}).Once()
command := []byte(`["step_matches",{"name_to_match":"` + pattern + `"}]`)
testee.parse(command)
steps.Mock.AssertExpectations(t)
}
func Test_parser_returns_success_and_empty_array_for_undefined_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("matching_step", mock.Anything).Return("", false, []capturing_group{}).Once()
command := []byte(`["step_matches",{"name_to_match":"undefined step"}]`)
response := testee.parse(command)
assert.Equal(t, `["success",[]]`, response)
}
func Test_parser_returns_success_and_id_for_defined_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
id := "123"
pattern := "defined step"
steps.On("matching_step", pattern).Return(id, true, []capturing_group{}).Once()
command := []byte(`["step_matches",{"name_to_match":"` + pattern + `"}]`)
response := testee.parse(command)
expected_response := `["success",[{"id":"` + id + `","args":[]}]]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_failure_for_unknown_command(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`["unknown_command"]`)
response := testee.parse(command)
expected_response := `["fail",{"message":"unknown command: unknown_command"}]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_snippet_text_for_given(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`["snippet_text",{"step_keyword":"Given","multiline_arg_class":"","step_name":"Step"}]`)
response := testee.parse(command)
expected_response := `["success","cucumber.Given(\"Step\").Pending()"]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_snippet_text_for_when(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`["snippet_text",{"step_keyword":"When","multiline_arg_class":"","step_name":"when step"}]`)
response := testee.parse(command)
expected_response := `["success","cucumber.When(\"when step\").Pending()"]`
assert.Equal(t, expected_response, response)
}
func Test_parser_escapes_special_characters_in_snippet(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`["snippet_text",{"step_keyword":"Then","multiline_arg_class":"","step_name":"step with '\"quotes\"'"}]`)
response := testee.parse(command)
expected_response := `["success","cucumber.Then(\"step with '\\\"quotes\\\"'\").Pending()"]`
assert.Equal(t, expected_response, response)
}
func Test_parser_invokes_a_step_with_the_given_id(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
id := "7"
steps.On("invoke_step", id, mock.Anything).Return(success, "").Once()
command := []byte(`["invoke",{"id":"` + id + `","args":[]}]`)
testee.parse(command)
steps.Mock.AssertExpectations(t)
}
func Test_parser_returns_pending_when_invoking_a_pending_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("invoke_step", mock.Anything, mock.Anything).Return(pending, "").Once()
command := []byte(`["invoke",{"id":"13","args":[]}]`)
response := testee.parse(command)
expected_response := `["pending"]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_success_when_invoking_a_passing_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("invoke_step", mock.Anything, mock.Anything).Return(success, "").Once()
command := []byte(`["invoke",{"id":"24","args":[]}]`)
response := testee.parse(command)
expected_response := `["success"]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_fail_when_invoking_a_failing_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
steps.On("invoke_step", mock.Anything, mock.Anything).Return(fail, "").Once()
command := []byte(`["invoke",{"id":"35","args":[]}]`)
response := testee.parse(command)
expected_response := `["fail",{"message":""}]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_failure_message_of_failing_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
message := "failure message"
steps.On("invoke_step", mock.Anything, mock.Anything).Return(fail, message).Once()
command := []byte(`["invoke",{"id":"46","args":[]}]`)
response := testee.parse(command)
expected_response := `["fail",{"message":"` + message + `"}]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_fail_when_the_command_is_malformed_json(t *testing.T) {
testee := &wire_protocol_parser{}
command := []byte(`[}"this is not valid json"`)
response := testee.parse(command)
expected_response := `["fail",{"message":"invalid command"}]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_fail_when_the_message_is_not_wire_command(t *testing.T) {
testee := &wire_protocol_parser{}
response := testee.parse([]byte(`[123]`))
expected_response := `["fail",{"message":"invalid command"}]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_a_capturing_group_as_an_argument(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
arguments := []capturing_group{
capturing_group{value: "value", position: 5}}
steps.On("matching_step", mock.Anything).Return("47", true, arguments).Once()
command := []byte(`["step_matches",{"name_to_match":"some value"}]`)
response := testee.parse(command)
expected_response := `["success",[{"id":"47","args":[{"val":"value","pos":5}]}]]`
assert.Equal(t, expected_response, response)
}
func Test_parser_returns_all_capturing_groups_as_arguments(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
arguments := []capturing_group{
capturing_group{value: "some", position: 0},
capturing_group{value: "value", position: 5},
capturing_group{value: "match", position: 14}}
steps.On("matching_step", mock.Anything).Return("47", true, arguments).Once()
command := []byte(`["step_matches",{"name_to_match":"some value to match"}]`)
response := testee.parse(command)
expected_response := `["success",[{"id":"47","args":[{"val":"some","pos":0},{"val":"value","pos":5},{"val":"match","pos":14}]}]]`
assert.Equal(t, expected_response, response)
}
func Test_parser_passes_arguments_to_invoked_step(t *testing.T) {
steps := &steps_mock{}
testee := &wire_protocol_parser{steps}
expected_arguments := []string{"some", "value"}
steps.On("invoke_step", mock.Anything, expected_arguments).Return(success, "").Once()
command := []byte(`["invoke",{"id":"123","args":["some","value"]}]`)
testee.parse(command)
steps.Mock.AssertExpectations(t)
}