-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (178 loc) · 4.69 KB
/
main.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
"use strict";
require('array.prototype.find');
var resolve_path = require("path").resolve;
var basename = require("path").basename;
var glob = require("glob");
var hbs = require("handlebars");
var framework_config = {};
var features = {};
var steps = {};
var current_run_context;
module.exports = {
configure: function(config) {
var rightAngleConfig = config.rightAngle || {};
config.specs = config.specs || [];
config.jasmineNodeOpts = config.jasmineNodeOpts || {};
config.specs.push(__dirname + "/run-spec.js");
if(config.jasmineNodeOpts.isVerbose === undefined) {
config.jasmineNodeOpts.isVerbose = true;
}
framework_config.pages_path = rightAngleConfig.pagesPath || './page';
framework_config.features_path = rightAngleConfig.featuresPath || './feature';
framework_config.steps_path = rightAngleConfig.stepsPath || './step';
return config;
},
run: function() {
var feature_name;
var feature;
glob.sync(resolve_path(framework_config.features_path, "*.js")).forEach (
function(feature_file) {
var feature = require(resolve_path(feature_file));
if(features[feature.name]) {
throw new Error("Feature \"" + feature.name + "\" is already defined.");
}
features[feature.name] = feature;
}
);
glob.sync(resolve_path(framework_config.steps_path, "*.js")).forEach (
function(bundle_file) {
var bundle = require(resolve_path(bundle_file));
var step_regex;
var step;
for(step_regex in bundle) {
if(steps[step_regex] !== undefined) {
throw new Error (
"Step regular expression /" + step_regex + "/ is already defined "
+ "(in bundle '" + steps[step_regex].bundle_file + "')"
);
}
step = bundle[step_regex];
step.bundle_file = basename(bundle_file);
steps[step_regex] = step;
}
}
);
for(feature_name in features) {
feature = features[feature_name];
describe (
"Feature: " + feature_name, function() {
var scenario_name;
var scenario;
var scenario_run;
var data;
if(feature.beforeAll) {
feature.beforeAll();
}
for(scenario_name in feature.scenarios) {
scenario = feature.scenarios[scenario_name];
if(typeof(scenario) === "function") {
scenario_run = scenario;
}
else {
scenario_run = scenario.run;
if(typeof(scenario.data) === 'function') {
data = scenario.data();
}
else {
data = scenario.data;
}
}
if(!Array.isArray(data)) {
data = [data];
}
data.forEach (
function(data) {
var scenario_run_name;
if(data) {
scenario_run_name = hbs.compile(scenario_name)(data);
}
else {
scenario_run_name = scenario_name;
}
current_run_context = data || {};
describe (
"Scenario: " + scenario_run_name, function() {
if(feature.beforeEach) {
feature.beforeEach.call(current_run_context);
}
scenario_run(data);
}
);
current_run_context = null;
}
);
}
}
);
}
},
loadPage: function(name) {
return require(resolve_path(framework_config.pages_path, name + ".js"));
}
};
global.given = function() {
return runStep.apply (
null, ["Given"].concat([].slice.call(arguments, 0))
);
};
global.and = function() {
return runStep.apply (
null, ["And"].concat([].slice.call(arguments, 0))
);
};
global.when = function() {
return runStep.apply (
null, ["When"].concat([].slice.call(arguments, 0))
);
};
global.then = function() {
return runStep.apply (
null, ["Then"].concat([].slice.call(arguments, 0))
);
};
function runStep(prefix, statement) {
var matching_step_keys;
var step_key;
var step_regex_result;
var extra_step_parameters = [].slice.call(arguments, 2);
var step;
var step_fn;
var jasmine_fn;
if(current_run_context) {
statement = hbs.compile(statement)(current_run_context);
}
matching_step_keys = Object.keys(steps).filter (
function(step_regex) {
var result = new RegExp("^" + step_regex + "$").exec(statement);
if(result) {
step_regex_result = result;
}
return !!result;
}
);
switch(matching_step_keys.length) {
case 0:
throw new Error("No step matching \"" + statement + "\".");
case 1:
step_key = matching_step_keys[0];
break;
default:
throw new Error (
matching_step_keys.length + " steps match ambiguous statement \"" + statement + "\"."
);
}
step = steps[step_key];
if(step.composite) {
jasmine_fn = describe;
step_fn = step.run;
}
else {
jasmine_fn = it;
step_fn = step;
}
jasmine_fn (
prefix + " " + statement, function() {
step_fn.apply(current_run_context, step_regex_result.slice(1).concat(extra_step_parameters));
}
);
}