forked from nayish/regex-workshop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
43 lines (31 loc) · 1.22 KB
/
test.ts
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
const answers = require('./solution');
const {steps} = require(`./test-config.json`);
const currentStep = getCurrentStep();
const numberOfSteps = steps.length;
if (currentStep !== undefined && (+currentStep < 1 || +currentStep > numberOfSteps || Number.isNaN(+currentStep))) {
throw `There is no step ${currentStep} (try steps 1-${numberOfSteps})`;
}
const stepsConfig = (currentStep) ? [currentStep] : new Array(numberOfSteps).fill(1).map((_,i) => `${i+1}`);
describe.each(stepsConfig)(`Regex Step %s`, (currentTest) => {
const config = steps[+currentTest - 1];
const answer = answers[`answer${currentTest}`]
if (!answer) {
it(`should have answer for step ${currentTest} (define answer${currentTest} in ./solution.ts)`, function () {
expect(answer).toBeDefined();
});
return;
}
it.each(config.pass.map(JSON.stringify))(`should ${config.title} (%s)`, (text) => {
expect(JSON.parse(text)).toMatch(answer);
});
it.each(config.fail.map(JSON.stringify))(`should not ${config.title} (%s)`, (text) => {
expect(JSON.parse(text)).not.toMatch(answer);
});
});
function getCurrentStep() {
try{
return JSON.parse(process.env.npm_config_argv).remain[0];
} catch {
return process.argv[4];
}
}