forked from cucumber/cucumber-js
/
step_definition_snippet_builder_spec.js
295 lines (245 loc) · 12 KB
/
step_definition_snippet_builder_spec.js
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require('../../support/spec_helper');
describe("Cucumber.SupportCode.StepDefinitionSnippetBuilder", function() {
var Cucumber = requireLib('cucumber');
var StepDefinitionSnippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder;
var snippetBuilder, step;
beforeEach(function() {
step = createSpy("step");
syntax = createSpyWithStubs("step syntax", {
getStepDefinitionStart : 'this.',
getStepDefinitionInner1 : '(',
getStepDefinitionInner2 : ', function(',
getStepDefinitionEnd : ") {\n // express the regexp above with the code you wish you had\n callback.pending();\n});\n",
getContextStepDefinitionFunctionName : 'Given',
getEventStepDefinitionFunctionName : 'When',
getOutcomeStepDefinitionFunctionName : 'Then',
getNumberMatchingGroup : '(\\d+)',
getQuotedStringMatchingGroup : '"([^"]*)"',
getFunctionParameterSeparator : ', ',
getStepDefinitionDocString : 'string',
getStepDefinitionDataTable : 'table',
getStepDefinitionCallback : 'callback',
getPatternStart : '/^',
getPatternEnd : '$/'
});
snippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder(step, syntax);
});
describe("buildSnippet()", function() {
var functionName, pattern, parameters;
beforeEach(function() {
functionName = "defineSomeStep";
pattern = "/^some step pattern$/";
parameters = "some, parameters, and, the, callback";
spyOn(snippetBuilder, 'buildStepDefinitionFunctionName').andReturn(functionName);
spyOn(snippetBuilder, 'buildStepDefinitionPattern').andReturn(pattern);
spyOn(snippetBuilder, 'buildStepDefinitionParameters').andReturn(parameters);
});
it("builds the step definition's function name", function() {
snippetBuilder.buildSnippet();
expect(snippetBuilder.buildStepDefinitionFunctionName).toHaveBeenCalled();
});
it("builds the step definition's pattern", function() {
snippetBuilder.buildSnippet();
expect(snippetBuilder.buildStepDefinitionPattern).toHaveBeenCalled();
});
it("builds the step definition's parameters", function() {
snippetBuilder.buildSnippet();
expect(snippetBuilder.buildStepDefinitionParameters).toHaveBeenCalled();
});
it("returns the snippet", function() {
var actualSnippet = snippetBuilder.buildSnippet();
var expectedSnippet =
"this." +
functionName +
"(" +
pattern +
", function(" +
parameters +
") {\n // express the regexp above with the code you wish you had\n callback.pending();\n});\n";
expect(actualSnippet).toBe(expectedSnippet);
});
});
describe("buildStepDefinitionFunctionName()", function() {
var stepType;
beforeEach(function() {
spyOnStub(step, 'isOutcomeStep');
spyOnStub(step, 'isEventStep');
});
it("checks whether the step is a context step", function() {
snippetBuilder.buildStepDefinitionFunctionName();
expect(step.isOutcomeStep).toHaveBeenCalled();
});
describe("when the step is a context step", function() {
beforeEach(function() {
step.isOutcomeStep.andReturn(true);
});
it("returns the outcome step definition function name", function() {
expect(snippetBuilder.buildStepDefinitionFunctionName()).toBe("Then");
});
});
describe("when the step is not a context step", function() {
beforeEach(function() {
step.isOutcomeStep.andReturn(false);
});
it("checks whether the step is an event step", function() {
snippetBuilder.buildStepDefinitionFunctionName();
expect(step.isEventStep).toHaveBeenCalled();
});
describe("when the step is an event step", function() {
beforeEach(function() {
step.isEventStep.andReturn(true);
});
it("returns the event step definition function name", function() {
expect(snippetBuilder.buildStepDefinitionFunctionName()).toBe("When");
});
});
describe("when the step is not an event step", function() {
beforeEach(function() {
step.isEventStep.andReturn(false);
});
it("returns the context step definition function name", function() {
expect(snippetBuilder.buildStepDefinitionFunctionName()).toBe("Given");
});
});
});
});
describe("buildStepDefinitionPattern()", function() {
var stepName, escapedStepName, parameterizedStepName;
beforeEach(function() {
parameterizedStepName = "step name-" + (Math.random()*10);
escapedStepName = createSpy("escaped step name");
stepName = createSpy("step name");
spyOnStub(step, 'getName').andReturn(stepName);
spyOnStub(Cucumber.Util.RegExp, 'escapeString').andReturn(escapedStepName);
spyOn(snippetBuilder, 'parameterizeStepName').andReturn(parameterizedStepName);
});
it("gets the step name", function() {
snippetBuilder.buildStepDefinitionPattern();
expect(step.getName).toHaveBeenCalled();
});
it("escapes the step name for use as a regexp", function() {
snippetBuilder.buildStepDefinitionPattern();
expect(Cucumber.Util.RegExp.escapeString).toHaveBeenCalledWith(stepName);
});
it("parameterizes the step name", function() {
snippetBuilder.buildStepDefinitionPattern();
expect(snippetBuilder.parameterizeStepName).toHaveBeenCalledWith(escapedStepName);
});
it("returns the step name within a full line-matching regexp", function() {
var pattern = snippetBuilder.buildStepDefinitionPattern();
expect(pattern).toBe('/^' + parameterizedStepName + '$/');
});
});
describe("parameterizeStepName()", function() {
var stepName, parameterizedNumbersStepName, parameterizedStepName;
beforeEach(function() {
parameterizedStepName = createSpy("parameterized step name");
parameterizedNumbersStepName = createSpyWithStubs("step name with parameterized numbers", {replace: parameterizedStepName});
stepName = createSpyWithStubs("step name", {replace: parameterizedNumbersStepName});
});
it("replaces numbers with matching groups", function() {
snippetBuilder.parameterizeStepName(stepName);
expect(stepName.replace).toHaveBeenCalled();
expect(stepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/\d+/gi, 1);
expect(stepName.replace).toHaveBeenCalledWithValueAsNthParameter('(\\d+)', 2);
});
it("replaces quoted strings with matching groups", function() {
snippetBuilder.parameterizeStepName(stepName);
expect(parameterizedNumbersStepName.replace).toHaveBeenCalled();
expect(parameterizedNumbersStepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/"[^"]*"/gi, 1);
expect(parameterizedNumbersStepName.replace).toHaveBeenCalledWithValueAsNthParameter('"([^"]*)"', 2);
});
it("returns the parameterized step name", function() {
expect(snippetBuilder.parameterizeStepName(stepName)).toBe(parameterizedStepName);
});
});
describe("buildStepDefinitionParameters()", function() {
var patternMatchingGroupParameters;
beforeEach(function() {
patternMatchingGroupParameters = ['some', 'stepdef', 'parameters'];
spyOn(snippetBuilder, 'getStepDefinitionPatternMatchingGroupParameters').andReturn(patternMatchingGroupParameters);
spyOnStub(step, 'hasDocString');
spyOnStub(step, 'hasDataTable');
});
it("gets the step definition pattern matching group parameters", function() {
snippetBuilder.buildStepDefinitionParameters();
expect(snippetBuilder.getStepDefinitionPatternMatchingGroupParameters).toHaveBeenCalled();
});
it("checks whether the step has a doc string attached or not", function() {
snippetBuilder.buildStepDefinitionParameters();
expect(step.hasDocString).toHaveBeenCalled();
});
it("checks whether the step has a data table attached or not", function() {
snippetBuilder.buildStepDefinitionParameters();
expect(step.hasDataTable).toHaveBeenCalled();
});
it("returns the parameters and a callback joined", function() {
var parameters = patternMatchingGroupParameters.concat(['callback']).join(', ');
expect(snippetBuilder.buildStepDefinitionParameters()).toBe(parameters);
});
describe("when there is a doc string", function() {
it("returns the parameters, an additional 'string' parameter and a callback joined", function() {
step.hasDocString.andReturn(true);
var parameters = patternMatchingGroupParameters.concat(['string', 'callback']).join(', ');
expect(snippetBuilder.buildStepDefinitionParameters()).toBe(parameters);
});
});
describe("when there is a data table", function() {
it("returns the parameters, an additional 'table' parameter and a callback joined", function() {
step.hasDataTable.andReturn(true);
var parameters = patternMatchingGroupParameters.concat(['table', 'callback']).join(', ');
expect(snippetBuilder.buildStepDefinitionParameters()).toBe(parameters);
});
});
});
describe("getStepDefinitionPatternMatchingGroupParameters()", function() {
beforeEach(function() {
spyOn(snippetBuilder, 'countStepDefinitionPatternMatchingGroups');
});
it("gets the number of step definition pattern matching groups", function() {
snippetBuilder.getStepDefinitionPatternMatchingGroupParameters();
expect(snippetBuilder.countStepDefinitionPatternMatchingGroups).toHaveBeenCalled();
});
it("returns an empty array when there are no matching groups", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups.andReturn(0);
var parameters = snippetBuilder.getStepDefinitionPatternMatchingGroupParameters();
expect(parameters).toEqual([]);
});
it("returns one parameter when there is one matching group", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups.andReturn(1);
var parameters = snippetBuilder.getStepDefinitionPatternMatchingGroupParameters();
expect(parameters).toEqual(['arg1']);
});
it("returns two joined parameters when there are two matching groups", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups.andReturn(2);
var parameters = snippetBuilder.getStepDefinitionPatternMatchingGroupParameters();
expect(parameters).toEqual(['arg1', 'arg2']);
});
});
describe("countStepDefinitionPatternMatchingGroups()", function() {
var stepDefinitionPattern, numberCount, stringCount, count;
beforeEach(function() {
numberCount = Math.floor(Math.random() * 10)
stringCount = Math.floor(Math.random() * 10)
count = numberCount + stringCount
stepDefinitionPattern = createSpy("step definition pattern");
spyOn(snippetBuilder, 'buildStepDefinitionPattern').andReturn(stepDefinitionPattern);
spyOn(Cucumber.Util.String, 'count').andReturnSeveral([numberCount, stringCount]);
});
it("builds the step definition pattern", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups();
expect(snippetBuilder.buildStepDefinitionPattern).toHaveBeenCalled();
});
it("counts the number matching groups inside the pattern", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups();
expect(Cucumber.Util.String.count).toHaveBeenCalledWith(stepDefinitionPattern, '(\\d+)');
});
it("counts the quoted string matching groups inside the pattern", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups();
expect(Cucumber.Util.String.count).toHaveBeenCalledWith(stepDefinitionPattern, '"([^"]*)"');
});
it("returns the sum of both counts", function() {
expect(snippetBuilder.countStepDefinitionPatternMatchingGroups()).toBe(count);
});
});
});