-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·78 lines (65 loc) · 1.94 KB
/
Copy pathtest.js
File metadata and controls
executable file
·78 lines (65 loc) · 1.94 KB
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
#!/usr/bin/env node
import * as epilog from '@epilog/epilog';
import assert from 'node:assert';
import glob from 'glob';
import fs from 'fs/promises';
import path from 'path';
import test from 'node:test';
import YAML from 'yaml';
const __dirname = new URL('.', import.meta.url).pathname;
let testPaths = [];
glob(__dirname + '**/*.yml', (err, paths) => {
if (err) {
console.error(err);
return;
}
paths.forEach(path => {
Promise.all([
loadRules(path),
fs.readFile(path, { encoding: 'utf-8' }).then(parseTests)
]).then(([rules, t]) => {
t.context.rules = rules;
runTests(t.tests, t.context);
});
});
});
function loadRules(testPath) {
return Promise.all([
fs.readFile(path.join(testPath, '../../core.epilog'), { encoding: 'utf-8' }),
fs.readFile(path.join(testPath, '../rules.epilog'), { encoding: 'utf-8' })
]).then(([core, rules]) => {
const ruleset = [];
epilog.definerules(ruleset, epilog.readdata(core));
epilog.definemorerules(ruleset, epilog.readdata(rules));
return ruleset;
});
}
function parseTests(sourceText) {
const docs = YAML.parseAllDocuments(sourceText).map(d => { return d.toJS(); });
const result = { context: {}, tests: [] };
if (docs[0].commonSetup) {
result.context.commonSetup = docs[0].commonSetup;
result.tests = docs.slice(1);
} else {
result.tests = docs;
}
return result;
}
function runTests(tests, context) {
tests.forEach(testCase => {
test(testCase.label, () => {
const dataset = [];
if (context.commonSetup) {
epilog.definefacts(dataset, epilog.readdata(context.commonSetup));
}
epilog.definemorefacts(dataset, epilog.readdata(testCase.setup));
testCase.check.split('\n').filter(Boolean).forEach(check => {
const result = epilog.compfindp(
epilog.read(check),
dataset,
context.rules);
assert.ok(result, `failed: ${check}`);
});
});
});
}