Skip to content

Commit 243bce8

Browse files
committed
Moved Gherkin JSON formatter back into Wrapper class. Added getter to allow us to spy on this in jasmine tests
1 parent 0ee152a commit 243bce8

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

lib/cucumber/cli/configuration.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ var Configuration = function(argv) {
1616
formatter = Cucumber.Listener.PrettyFormatter();
1717
break;
1818
case Configuration.JSON_FORMAT_NAME:
19-
var JSONFormatter = require('gherkin/lib/gherkin/formatter/json_formatter');
20-
var jsonFormatter = new JSONFormatter(process.stdout);
21-
formatter = Cucumber.Listener.JsonFormatterWrapper(jsonFormatter);
19+
formatter = Cucumber.Listener.JsonFormatterWrapper(process.stdout);
2220
break;
2321
case Configuration.SUMMARY_FORMAT_NAME:
2422
formatter = Cucumber.Listener.SummaryFormatter();

lib/cucumber/listener/json_formatter_wrapper.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var JsonFormatterWrapper = function(formatter) {
1+
var JsonFormatterWrapper = function(io) {
22

3-
var formatter = formatter;
3+
var JSONFormatter = require('gherkin/lib/gherkin/formatter/json_formatter');
4+
var formatter = new JSONFormatter(io);
45

56
var currentFeatureId = 'undefined';
67

@@ -10,6 +11,10 @@ var JsonFormatterWrapper = function(formatter) {
1011

1112
var parentFeatureTags;
1213

14+
self.getGherkinFormatter = function() {
15+
return formatter;
16+
}
17+
1318
self.formatStep = function formatStep(step) {
1419

1520
var stepProperties = {name: step.getName(), line: step.getLine(), keyword: step.getKeyword()};

spec/cucumber/listener/json_formatter_wrapper_spec.js

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
44
var Cucumber = requireLib('cucumber');
55
var listener, failedStepResults;
66

7-
var fakeFormatter = createSpyObj('formatter', ['step', 'uri', 'feature', 'background', 'scenario', 'result', 'match', 'eof', 'done']);
7+
// var fakeFormatter = createSpyObj('formatter', ['step', 'uri', 'feature', 'background', 'scenario', 'result', 'match', 'eof', 'done']);
88

99
beforeEach(function() {
10-
listener = Cucumber.Listener.JsonFormatterWrapper(fakeFormatter);
10+
spyOn(process.stdout, 'write'); // prevent actual output during spec execution
11+
listener = Cucumber.Listener.JsonFormatterWrapper(process.stdout);
12+
formatter = listener.getGherkinFormatter();
13+
14+
spyOn(formatter, 'uri');
15+
spyOn(formatter, 'feature');
16+
spyOn(formatter, 'step');
17+
spyOn(formatter, 'background');
18+
spyOn(formatter, 'scenario');
19+
spyOn(formatter, 'result');
20+
spyOn(formatter, 'match');
21+
spyOn(formatter, 'eof');
22+
spyOn(formatter, 'done');
23+
1124
});
1225

1326
// Handle Feature
@@ -31,8 +44,8 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
3144

3245
it("adds the feature attributes to the output", function() {
3346
listener.handleBeforeFeatureEvent(event, callback);
34-
expect(fakeFormatter.uri).toHaveBeenCalledWith('TODO');
35-
expect(fakeFormatter.feature).toHaveBeenCalledWith({id: 'A-Name',
47+
expect(formatter.uri).toHaveBeenCalledWith('TODO');
48+
expect(formatter.feature).toHaveBeenCalledWith({id: 'A-Name',
3649
name: 'A Name',
3750
description: 'A Description',
3851
line: 3,
@@ -82,7 +95,7 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
8295

8396
it("adds the background attributes to the output", function() {
8497
listener.handleBackgroundEvent(event, callback);
85-
expect(fakeFormatter.background).toHaveBeenCalledWith({name: 'A Name',
98+
expect(formatter.background).toHaveBeenCalledWith({name: 'A Name',
8699
keyword: 'Background',
87100
description: 'A Description',
88101
type: 'background',
@@ -120,7 +133,7 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
120133

121134
it("adds the scenario attributes to the output", function() {
122135
listener.handleBeforeScenarioEvent(event, callback);
123-
expect(fakeFormatter.scenario).toHaveBeenCalledWith({name: 'A Name',
136+
expect(formatter.scenario).toHaveBeenCalledWith({name: 'A Name',
124137
id: 'undefined;a-name',
125138
line: 3,
126139
keyword: 'Scenario',
@@ -145,7 +158,7 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
145158
});
146159

147160
listener.formatStep(step);
148-
expect(fakeFormatter.step).toHaveBeenCalledWith({ name : 'Step', line : 3, keyword : 'Step'});
161+
expect(formatter.step).toHaveBeenCalledWith({ name : 'Step', line : 3, keyword : 'Step'});
149162

150163
});
151164

@@ -166,7 +179,7 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
166179
});
167180

168181
listener.formatStep(step);
169-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step',
182+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step',
170183
line: 3,
171184
keyword: 'Step',
172185
doc_string: {value: 'This is a DocString', line: 3, content_type: null}
@@ -194,7 +207,7 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
194207
});
195208

196209
listener.formatStep(step);
197-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step',
210+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step',
198211
line: 3,
199212
keyword: 'Step',
200213
rows: [{line : 'TODO', cells: ['a:1', 'a:2', 'a:3'] },
@@ -271,9 +284,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
271284

272285
listener.handleStepResultEvent(fakeEvent, callback);
273286

274-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
275-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'failed'});
276-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
287+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
288+
expect(formatter.result).toHaveBeenCalledWith({status: 'failed'});
289+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
277290

278291
});
279292

@@ -302,9 +315,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
302315

303316
listener.handleStepResultEvent(fakeEvent, callback);
304317

305-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
306-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'passed'});
307-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
318+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
319+
expect(formatter.result).toHaveBeenCalledWith({status: 'passed'});
320+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
308321

309322
});
310323

@@ -333,9 +346,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
333346

334347
listener.handleStepResultEvent(fakeEvent, callback);
335348

336-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
337-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'pending', error_message: 'TODO'});
338-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
349+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
350+
expect(formatter.result).toHaveBeenCalledWith({status: 'pending', error_message: 'TODO'});
351+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
339352

340353
});
341354

@@ -364,9 +377,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
364377

365378
listener.handleStepResultEvent(fakeEvent, callback);
366379

367-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
368-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'failed'});
369-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
380+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
381+
expect(formatter.result).toHaveBeenCalledWith({status: 'failed'});
382+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
370383

371384
});
372385

@@ -395,9 +408,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
395408

396409
listener.handleStepResultEvent(fakeEvent, callback);
397410

398-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
399-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'skipped'});
400-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
411+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
412+
expect(formatter.result).toHaveBeenCalledWith({status: 'skipped'});
413+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
401414

402415
});
403416

@@ -426,9 +439,9 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
426439

427440
listener.handleStepResultEvent(fakeEvent, callback);
428441

429-
expect(fakeFormatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
430-
expect(fakeFormatter.result).toHaveBeenCalledWith({status: 'undefined'});
431-
expect(fakeFormatter.match).toHaveBeenCalledWith({location: 'TODO'});
442+
expect(formatter.step).toHaveBeenCalledWith({name: 'Step', line: 3, keyword: 'Step'});
443+
expect(formatter.result).toHaveBeenCalledWith({status: 'undefined'});
444+
expect(formatter.match).toHaveBeenCalledWith({location: 'TODO'});
432445

433446
});
434447

@@ -447,8 +460,8 @@ describe("Cucumber.Listener.JsonFormatterWrapper", function() {
447460

448461
it("finalises output", function() {
449462
listener.handleAfterFeaturesEvent(event, callback);
450-
expect(fakeFormatter.eof).toHaveBeenCalled();
451-
expect(fakeFormatter.done).toHaveBeenCalled();
463+
expect(formatter.eof).toHaveBeenCalled();
464+
expect(formatter.done).toHaveBeenCalled();
452465
});
453466

454467
it("calls back", function() {

0 commit comments

Comments
 (0)