-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
128 lines (118 loc) · 2.86 KB
/
test.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
var assert = require("assert");
var fs = require("fs");
var { parse } = require("./index");
var manual = process.argv[2];
if (manual) {
runSpecTest(manual + ".aml", true);
process.exit();
}
function runSpecTest(file, verbose = false) {
var contents = fs.readFileSync(`tests/${file}`, "utf-8");
var product = parse(contents, { verbose });
var { test, result } = product;
delete product.test
delete product.result;
result = JSON.parse(result);
console.log(`\n==== ${file} ====`);
console.log("TEST: ", test);
console.log("EXPECTED: ", JSON.stringify(result));
console.log("FOUND: ", JSON.stringify(product));
try {
assert.deepStrictEqual(result, product);
console.log(`RESULT: passed`);
passed++;
} catch (err) {
console.error(err.message)
console.log(`RESULT: failed`);
}
}
// run all archieML tests
var testFiles = fs.readdirSync("tests");
var passed = 0;
var possible = testFiles.length + 1;
for (var f of testFiles) {
runSpecTest(f);
}
// syntax extension tests
console.log(`\n==== Betty extensions ====`)
var text = fs.readFileSync("test_document.txt", "utf-8");
var parsed = parse(text, {
verbose: true,
onFieldName: t => t.toLowerCase(),
onValue: function(value) {
if (value == "true" || value == "false") {
return value == "true";
}
if (typeof value == "string" && value.match(/^\d{4}-\d{2}-\d{2}T\d{1,2}:\d{2}:\d{2}.\d+Z$/)) {
return Date.parse(value);
}
var attempt = parseFloat(value);
if (!isNaN(attempt)) return attempt;
return value;
}
});
//console.log(JSON.stringify(parsed, null, 2));
try {
assert.deepStrictEqual(parsed, {
hello: "world",
options: {
test: true,
x: false,
longer: `this is a block
It can contain markup
[and]
it won't care
this: isn't a field`,
multiline: "this is a standard multiline value",
child: {
block: true
}
},
not: "in options",
free: { form: [
{ type: "text", value: "this is a test block" },
{ type: "key", value: "value" },
{ type: "quote", value: {
text: "Correctly parses."
}}
] },
quote: {
error: "This should exit the array."
},
strings: [
"test",
"a",
"b",
"longer string goes here: the sequel"
],
list: [
{ a: 1, b: 2, c: { x: { d: 1, lengthy: "deeply nested multiline" } } },
{ a: 3 },
{ a: 4 }
],
parent: [
{
nested: [
"one",
"two"
]
}
],
named: {
sub: {
inner: {
prop: "This is a named object"
}
},
outer: "Closing only one level"
},
closing: "out of list",
timestamp: Date.parse("2020-02-10T15:00:00.000Z")
});
console.log(`RESULT: passed`)
passed++
} catch (err) {
throw err;
}
console.log(`\n==== Final result summary ====
Passed: ${passed} of ${possible}`);