/
cucumber_world.js
120 lines (99 loc) · 4.04 KB
/
cucumber_world.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
var CucumberWorld = function() {
this.touchedSteps = [];
this.featureSource = "";
this.stepDefinitions = "";
this.runOutput = "";
this.runSucceeded = false;
CucumberWorld.mostRecentInstance = this;
};
var proto = CucumberWorld.prototype;
proto.runFeature = function runFeature(callback) {
var supportCode;
var supportCodeSource = "supportCode = function() {\n var Given = When = Then = this.defineStep;\n" +
" var Before = this.Before, After = this.After;\n" +
this.stepDefinitions + "};\n";
var world = this;
eval(supportCodeSource);
this.runFeatureWithSupportCodeSource(supportCode, callback);
}
proto.runFeatureWithSupportCodeSource = function runFeatureWithSupportCodeSource(supportCode, callback) {
var world = this;
var Cucumber = require('../../lib/cucumber');
var cucumber = Cucumber(this.featureSource, supportCode);
var formatter = Cucumber.Listener.ProgressFormatter({logToConsole: false});
cucumber.attachListener(formatter);
cucumber.start(function(succeeded) {
world.runSucceeded = succeeded;
world.runOutput = formatter.getLogs();
Cucumber.Debug.notice(world.runOutput, 'cucumber output', 5);
callback();
});
}
proto.touchStep = function touchStep(string) {
this.touchedSteps.push(string);
}
proto.isStepTouched = function isStepTouched(pattern) {
return (this.touchedSteps.indexOf(pattern) >= 0);
}
proto.assertPassedFeature = function assertPassedFeature() {
this.assertNoPartialOutput("failed", this.runOutput);
this.assertSuccess();
}
proto.assertPassedScenario = function assertPassedScenario() {
this.assertPartialOutput("1 scenario (1 passed)", this.runOutput);
this.assertSuccess();
}
proto.assertFailedScenario = function assertFailedScenario() {
this.assertPartialOutput("1 scenario (1 failed)", this.runOutput);
this.assertFailure();
}
proto.assertPendingScenario = function assertPendingScenario() {
this.assertPartialOutput("1 scenario (1 pending)", this.runOutput);
this.assertSuccess();
}
proto.assertUndefinedScenario = function assertUndefinedScenario() {
this.assertPartialOutput("1 scenario (1 undefined)", this.runOutput);
this.assertSuccess();
}
proto.assertScenarioReportedAsFailing = function assertScenarioReportedAsFailing(scenarioName) {
this.assertPartialOutput("# Scenario: " + scenarioName, this.runOutput);
this.assertFailure();
}
proto.assertScenarioNotReportedAsFailing = function assertScenarioNotReportedAsFailing(scenarioName) {
this.assertNoPartialOutput("# Scenario: " + scenarioName, this.runOutput);
}
proto.assertPassedStep = function assertPassedStep(stepName) {
if (!this.isStepTouched(stepName))
throw(new Error("Expected step \"" + stepName + "\" to have passed."));
}
proto.assertSkippedStep = function assertSkippedStep(stepName) {
if (this.isStepTouched(stepName))
throw(new Error("Expected step \"" + stepName + "\" to have been skipped."));
}
proto.assertSuccess = function assertSuccess() {
if (!this.runSucceeded)
throw(new Error("Expected Cucumber to succeed but it failed."));
}
proto.assertFailure = function assertFailure() {
if (this.runSucceeded)
throw(new Error("Expected Cucumber to fail but it succeeded."));
}
proto.assertFailureMessage = function assertFailureMessage(message) {
this.assertPartialOutput(message, this.runOutput);
this.assertFailure();
}
proto.assertPartialOutput = function assertPartialOutput(expected, actual) {
if (actual.indexOf(expected) < 0)
throw(new Error("Expected:\n\"" + actual + "\"\nto match:\n\"" + expected + "\""));
}
proto.assertNoPartialOutput = function assertNoPartialOutput(expected, actual) {
if (actual.indexOf(expected) >= 0)
throw(new Error("Expected:\n\"" + actual + "\"\nnot to match:\n\"" + expected + "\""));
}
proto.assertEqual = function assertRawDataTable(expected, actual) {
var expectedJSON = JSON.stringify(expected);
var actualJSON = JSON.stringify(actual);
if (actualJSON != expectedJSON)
throw(new Error("Expected:\n\"" + actualJSON + "\"\nto match:\n\"" + expectedJSON + "\""));
}
module.exports = CucumberWorld;