-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrunner.js
145 lines (144 loc) · 4.79 KB
/
runner.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
/*global module, require, Promise*/
module.exports = function Runner(stepFunc, config) {
'use strict';
var Context = require('./context'),
CountingResultListener = require('./counting-result-listener'),
RegexUtil = require('./regex-util'),
StepExecutor = require('./step-executor'),
PromisingIterator = require('./promising-iterator'),
observable = require('./observable'),
regexUtil = new RegexUtil(),
ExampleBlocks = require('./example-blocks'),
self = observable(this),
standardMatchers = [require('./matchers/table'), require('./matchers/list')],
context = new Context();
self.executeSuite = function (suite) {
var counts = new CountingResultListener(self),
executeSpecs = true,
iterator = new PromisingIterator(suite, function (spec) {
var specPromise;
if (!executeSpecs) {
return;
}
if (typeof spec.content === 'function') {
specPromise = self.execute(spec.content(), spec.name);
} else {
specPromise = self.execute(spec.content, spec.name);
}
specPromise.then(function () {
if (config && config.failFast) {
if (counts.current.error || counts.current.failed || (!config.allowSkipped && counts.current.skipped) || !counts.current.passed) {
executeSpecs = false;
}
}
});
return specPromise;
});
return new Promise(function (resolve, reject) {
iterator.iterate().then(function () {
self.dispatchEvent('suiteEnded', counts.total);
if (counts.total.failed || counts.total.error || (!config.allowSkipped && counts.total.skipped) || !counts.current.passed) {
return resolve(false);
}
resolve(true);
}, reject);
});
};
self.execute = function (inputText, exampleName) {
var blocks = new ExampleBlocks(inputText),
lineNumber = 0,
counts = new CountingResultListener(self),
sendLineEvent = function (eventName, line) {
if (!line && line !== '') {
self.dispatchEvent(eventName, lineNumber, exampleName);
} else {
self.dispatchEvent(eventName, line, lineNumber, exampleName);
}
},
processTableBlock = function (block) {
var blockLines = block.getMatchText(),
stepDefinition,
executor,
headerLine,
startNewTable = function (line) {
stepDefinition = context.getStepDefinitionForLine(line);
if (!stepDefinition) {
sendLineEvent('skippedLine', line);
} else {
headerLine = line;
sendLineEvent('tableStarted');
sendLineEvent('nonAssertionLine', line);
}
},
endCurrentTable = function () {
if (stepDefinition) {
sendLineEvent('tableEnded');
stepDefinition = false;
}
};
return new PromisingIterator(blockLines, function (line) {
lineNumber++;
if (!regexUtil.isTableItem(line)) {
endCurrentTable();
sendLineEvent('nonAssertionLine', line);
} else if (!stepDefinition) {
startNewTable(line);
} else if (regexUtil.isTableDataRow(line)) {
executor = new StepExecutor(stepDefinition, context);
return executor.executeTableRow(line, headerLine).then(function (result) {
sendLineEvent('stepResult', result);
});
} else {
sendLineEvent('nonAssertionLine', line);
}
}).iterate().then(endCurrentTable);
},
processBlock = function (block) {
var blockLines = block.getMatchText(),
blockParam = block.getAttachment(),
attachmentLines = block.getAttachmentLines(),
executor;
return new PromisingIterator(blockLines, function (line) {
var stepDefinition;
lineNumber++;
if (!regexUtil.assertionLine(line)) { //Move to block?
if (!blockParam) {
sendLineEvent('nonAssertionLine', line);
}
return;
}
stepDefinition = context.getStepDefinitionForLine(line);
if (!stepDefinition) {
sendLineEvent('skippedLine', line);
if (attachmentLines.length) {
sendLineEvent('nonAssertionLine', '');
attachmentLines.forEach(function (attachmentLine) {
lineNumber++;
sendLineEvent('nonAssertionLine', attachmentLine);
});
}
return;
}
executor = new StepExecutor(stepDefinition, context);
return executor.execute(line, blockParam).then(function (result) {
sendLineEvent('stepResult', result);
lineNumber += attachmentLines.length;
});
}).iterate();
};
self.dispatchEvent('specStarted', exampleName);
return new PromisingIterator(blocks.getBlocks(), function (block) {
if (block.isTableBlock()) {
return processTableBlock(block);
} else {
return processBlock(block);
}
}).iterate().then(function () {
self.dispatchEvent('specEnded', exampleName, counts.current);
});
};
context.exportToGlobal();
standardMatchers.concat((config && config.matchers) || []).forEach(context.addMatchers);
stepFunc.apply(context, [context]);
context.resetGlobal();
};