-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
assert.js
68 lines (62 loc) · 2.55 KB
/
assert.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
Chomp.registerTemplate('assert', function (task) {
let env = {};
if (typeof task.templateOptions.expectEquals === 'string')
env['EXPECT_EQUALS'] = task.templateOptions.expectEquals;
if (typeof task.templateOptions.expectMatch === 'string')
env['EXPECT_MATCH'] = task.templateOptions.expectMatch;
if (typeof task.templateOptions.expectPattern === 'string')
env['EXPECT_PATTERN'] = task.templateOptions.expectPattern;
if (task.targets.length === 0 && task.deps.length === 0)
throw new Error('Assertion tests must have a dep or target to assert.');
if (task.deps.some(dep => dep.indexOf('#') !== -1))
throw new Error('Assertion tests do not support interpolates.');
env['ASSERT_TARGET'] = task.targets[0] || task.deps[0];
if (!task.name)
throw new Error('Assertion tests must be named.');
if (task.templateOptions.taskTemplate)
task.template = task.templateOptions.taskTemplate;
task.templateOptions = task.templateOptions.taskTemplateOptions;
const name = task.name;
if (!ENV.CHOMP_EJECT) {
task.display = task.display || 'none';
delete task.name;
}
// ejection of assertions ejects assertions usage
return !ENV.CHOMP_EJECT ? [{
name,
dep: '&next',
engine: 'node',
env,
envReplace: false,
display: 'status-only',
run: `
import { strictEqual, match } from 'assert';
import { readFileSync } from 'fs';
function rnlb (source) {
source = source.replace(/\\r\\n/g, '\\n');
if (source.startsWith('\\ufeff'))
source = source.slice(1);
if (source.endsWith('\\n'))
source = source.slice(0, -1);
return source;
}
let asserted = false;
const assertSource = readFileSync(process.env.ASSERT_TARGET, 'utf8');
if (process.env.EXPECT_EQUALS !== undefined) {
asserted = true;
strictEqual(rnlb(assertSource), rnlb(process.env.EXPECT_EQUALS));
}
if (process.env.EXPECT_MATCH !== undefined) {
asserted = true;
match(assertSource, new RegExp(process.env.EXPECT_MATCH));
}
if (process.env.EXPECT_PATTERN !== undefined) {
const uniqueStr = 'VERY_UNIQUE_STRING';
asserted = true;
match(rnlb(assertSource), new RegExp('^' + rnlb(process.env.EXPECT_PATTERN).replace(/\\\\\\*/g, uniqueStr).replace(/[-[\\]{}()+?.,\\\\^$|#]/g, '\\\\$&').replace(/\\*/g, '.*').replace(new RegExp(uniqueStr, 'g'), '\\\\*') + '$'));
}
if (!asserted)
throw new Error(\`No assertions made for \${process.env.ASSERT_TARGET}\`);
`
}, task] : [task];
});