-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
124 lines (109 loc) · 3.41 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
import patch from './module';
const parse = require('parametric-svg-parse');
const test = require('tape-catch');
const {safeLoad: yaml} = require('js-yaml');
const {jsdom} = require('jsdom');
const {DOMParser: XmldomParser} = require('xmldom');
const asObject = require('as/object');
const asArray = require('as/array');
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-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-variables.yaml'
),
// NOTE: See above.
].map(yaml);
specs.forEach(({
name,
tests,
mode,
}) => tests.forEach(({
description,
original,
example_data: data,
example_output: expected,
}) => {
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 contentType = (htmlMode ?
'text/html' :
'application/xml'
);
const Parser = (inBrowser ?
window.DOMParser :
XmldomParser
);
const toString = (
(htmlMode &&
((node) => node.outerHTML)
) ||
(inBrowser &&
((node) => (new window.XMLSerializer()).serializeToString(node))
) ||
String
);
const rootElements = asObject(asArray({
original,
expected,
}).map(({key, value}) => {
const documentSource = (elementMode ?
wrap(value) :
value
);
const document = (!inBrowser && htmlMode ?
jsdom(documentSource).defaultView.document :
(new Parser()).parseFromString(documentSource, contentType)
).documentElement;
return {key, value: (elementMode ?
document.firstChild :
document
)};
}));
const ast = parse(rootElements.original);
patch(rootElements.original, ast, data);
is.equal(
toString(rootElements.original),
toString(rootElements.expected),
'expected output matches'
);
is.end();
});
}));
});