-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsl.php
330 lines (269 loc) · 7.68 KB
/
dsl.php
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
use Peridot\Plugin\GherkinPlugin;
use Peridot\Runner\Context;
function background(...$args)
{
$setup = null;
$teardown = null;
$descriptions = [];
foreach ($args as $arg) {
if (is_string($arg)) {
if (count($descriptions) === 0) {
$descriptions[] = '';
$descriptions[] = 'Background:';
}
$descriptions[] = ' ' . $arg;
} elseif (is_callable($arg)) {
if ($setup === null) {
$setup = $arg;
} elseif ($teardown === null) {
$teardown = $arg;
}
}
}
return [
'descriptions' => $descriptions,
'fn' => function () use ($setup, $teardown) {
if (isset($setup)) {
Context::getInstance()->addSetupFunction($setup);
}
if (isset($teardown)) {
Context::getInstance()->addTearDownFunction($teardown);
}
},
'setup' => $setup,
'teardown' => $teardown
];
}
function __feature($name, $pending, $focused, ...$args)
{
$fns = [];
$descriptions = [];
foreach ($args as $arg) {
if (is_string($arg)) {
$descriptions[] = $arg;
} elseif (is_callable($arg)) {
$fns[] = $arg;
} elseif (is_array($arg)) {
foreach ($arg['descriptions'] as $description) {
$descriptions[] = $description;
}
$fns[] = $arg['fn'];
}
}
$fn = function () use ($fns) {
foreach ($fns as $fn) {
if (is_callable($fn)) {
$fn();
}
}
};
$indent = ' ';
$feature = 'Feature: ' . $name;
foreach ($descriptions as $i => $description) {
if ($i === 1 && $description === '') {
continue;
}
if ($i !== 0) {
$feature .= PHP_EOL . $indent;
}
$feature .= $description;
}
Context::getInstance()->addSuite($feature, $fn, $pending, $focused);
}
function feature($name, ...$args)
{
return __feature($name, null, false, ...$args);
}
function ffeature($name, ...$args)
{
return __feature($name, null, true, ...$args);
}
function xfeature($name, ...$args)
{
return __feature($name, true, false, ...$args);
}
function __suite($title, $pending, $focused, $isolated, ...$args)
{
$background = null;
$focusedTest = false;
$pendingTest = null;
$tests = [];
$description = null;
foreach ($args as $arg) {
if (is_string($arg)) {
if ($arg === '') {
continue;
}
if (isset($description)) {
$tests[] = [
'description' => $description,
'fn' => null,
'pending' => null,
'focused' => false
];
}
$description = $arg;
} elseif (is_callable($arg)) {
if ($description === null) {
continue;
}
// now that we have a function,
// update any previous "tests" that are pending to pass
if (strpos($title, 'Scenario:') === 0) {
$testCount = count($tests);
for ($i = 0; $i < $testCount; $i++) {
if ($tests[$i]['fn'] === null) {
$tests[$i]['fn'] = function () {
// noop to make the "test" pass
};
}
}
}
$tests[] = [
'description' => $description,
'fn' => $arg,
'pending' => $pendingTest,
'focused' => $focusedTest
];
$description = null;
$pendingTest = null;
$focusedTest = false;
} elseif (is_array($arg)) {
if (count($arg) === 1) {
if ($arg[0] === 'focus') {
$focusedTest = true;
} elseif ($arg[0] === 'skip') {
$pendingTest = true;
}
} else {
foreach ($arg['descriptions'] as $i => $desc) {
if ($i <= 1) {
continue;
}
$tests[] = [
'description' => 'Background: ' . trim($desc),
'fn' => function () {
},
'pending' => null,
'focused' => false
];
}
$background = $arg;
}
}
}
if (isset($description)) {
$tests[] = [
'description' => $description,
'fn' => null,
'pending' => $pendingTest,
'focused' => $focusedTest
];
$pendingTest = null;
$focusedTest = false;
}
$fn = function () use ($background, $tests) {
foreach ($tests as $test) {
$fn = $test['fn'];
if (is_array($background)) {
$fn = function () use ($background, $fn) {
if (is_callable($background['setup'])) {
$background['setup']();
}
$exception = null;
try {
$fn();
} catch (\Throwable $e) {
$exception = $e;
}
if (is_callable($background['teardown'])) {
$background['teardown']();
}
if (isset($exception)) {
throw $exception;
}
};
}
Context::getInstance()->addTest(
$test['description'],
$fn,
$test['pending'],
$test['focused']
);
}
};
return function () use ($title, $fn, $pending, $focused, $isolated) {
$suite = Context::getInstance()->addSuite(
$title,
$fn,
$pending,
$focused
);
$suite->isolated = $isolated;
};
}
function __scenario($title, $pending, $focused, $isolated, ...$args)
{
return __suite("Scenario: $title", $pending, $focused, $isolated, ...$args);
}
function scenario($title, ...$args)
{
return __scenario($title, null, false, false, ...$args);
}
function fscenario($title, ...$args)
{
return __scenario($title, null, true, false, ...$args);
}
function xscenario($title, ...$args)
{
return __scenario($title, true, false, false, ...$args);
}
function isolatedScenario($title, ...$args)
{
return __scenario($title, null, false, true, ...$args);
}
function fisolatedScenario($title, ...$args)
{
return __scenario($title, null, true, true, ...$args);
}
function xisolatedScenario($title, ...$args)
{
return __scenario($title, true, false, true, ...$args);
}
function __stories($pending, $focused, $isolated, ...$args)
{
return __suite('Stories:', $pending, $focused, $isolated, ...$args);
}
function stories(...$args)
{
return __stories(null, false, false, ...$args);
}
function fstories(...$args)
{
return __stories(null, true, false, ...$args);
}
function xstories(...$args)
{
return __stories(true, false, false, ...$args);
}
function isolatedStories(...$args)
{
return __stories(null, false, true, ...$args);
}
function fisolatedStories(...$args)
{
return __stories(null, true, true, ...$args);
}
function xisolatedStories(...$args)
{
return __stories(true, false, true, ...$args);
}
function focusNextStory()
{
return ['focus'];
}
function skipNextStory()
{
return ['skip'];
}