-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
142 lines (123 loc) · 4.05 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import parse from './module';
const test = require('tape-catch');
const {safeLoad: yaml} = require('js-yaml');
const ord = require('ord');
const tosource = require('tosource');
const {jsdom} = require('jsdom');
const arrayFrom = require('array-from');
const {DOMParser: XmldomParser} = require('xmldom');
const wrap = (element) => (
`<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:parametric="//parametric-svg.js.org/v1"
>${element}</svg>
`
);
if (typeof require.ensure !== 'function') require.ensure =
require('isomorphic-ensure')({
loaders: {
raw: require('raw-loader'),
},
dirname: __dirname,
});
require.ensure([
'raw!./node_modules/parametric-svg-spec/specs/usage-html5.yaml',
'raw!./node_modules/parametric-svg-spec/specs/usage-xml.yaml',
'raw!./node_modules/parametric-svg-spec/specs/parametric-attributes.yaml',
'raw!./node_modules/parametric-svg-spec/specs/syntax-operators.yaml',
'raw!./node_modules/parametric-svg-spec/specs/syntax-types.yaml',
'raw!./node_modules/parametric-svg-spec/specs/syntax-variables.yaml',
// NOTE: These paths have to be hard-coded in stone – otherwise webpack
// gets confused. Remember to keep them in sync with the `require`
// calls below.
], (require) => {
const specs = [
require('raw!./node_modules/parametric-svg-spec/specs/usage-html5.yaml'),
require('raw!./node_modules/parametric-svg-spec/specs/usage-xml.yaml'),
require(
'raw!./node_modules/parametric-svg-spec/specs/parametric-attributes.yaml'
),
require(
'raw!./node_modules/parametric-svg-spec/specs/syntax-operators.yaml'
),
require(
'raw!./node_modules/parametric-svg-spec/specs/syntax-types.yaml'
),
require(
'raw!./node_modules/parametric-svg-spec/specs/syntax-variables.yaml'
),
// NOTE: See above.
].map(yaml);
specs.forEach((
{name, tests, mode}
) => tests.forEach((
{description, ast, original}
) => {
test(`${name}: ${description}`, (is) => {
const inBrowser = typeof window !== 'undefined' && window.DOMParser;
const htmlMode = mode === 'HTML5 document';
const xmlMode = mode === 'XML document';
const elementMode = mode === 'Element';
if (!htmlMode && !xmlMode && !elementMode) throw new Error(
'Unknown test mode'
);
const documentSource = (elementMode ?
wrap(original) :
original
);
const contentType = (htmlMode ?
'text/html' :
'application/xml'
);
const Parser = (inBrowser ?
window.DOMParser :
XmldomParser
);
const document = (!inBrowser && htmlMode ?
jsdom(documentSource).defaultView.document :
(new Parser()).parseFromString(documentSource, contentType)
).documentElement;
const rootElement = (elementMode ?
document.firstChild :
document
);
const {attributes} = parse(rootElement);
is.equal(
attributes.size,
ast.length,
'the AST has the right number of attributes'
);
ast.forEach((expected, index) => {
const n = index + 1;
const nth = `${n}-${ord(n)}`;
const actual = arrayFrom(attributes)[index];
is.deepEqual(
actual.address,
expected.address,
`the \`address\` matches in the ${nth} parametric attribute`
);
is.equal(
actual.name,
expected.name,
`the \`name\` matches in the ${nth} parametric attribute`
);
is.deepEqual(
actual.dependencies,
expected.dependencies,
`the \`dependencies\` match in the ${nth} parametric attribute`
);
expected.relation.forEach(({input, output: expectedOutput}) => {
is.deepEqual(
actual.relation(input),
expectedOutput,
`the \`relation\` in the ${nth} parametric attribute returns ` +
'the expected value when called with the arguments ' +
`\`${tosource(input)}\`.`
);
});
});
is.end();
});
}));
});