Skip to content

Commit

Permalink
Prepare tests for running in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomek Wiszniewski committed Sep 11, 2015
1 parent 3a7ad06 commit 2d733ee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
"eslint": "1.3.1",
"eslint-config-airbnb": "0.0.8",
"eslint-plugin-react": "3.3.1",
"isomorphic-ensure": "1.0.1",
"istanbul": "0.3.19",
"js-yaml": "3.4.2",
"jsdom": "6.3.0",
"nodangel": "1.3.8",
"opn-cli": "1.0.0",
"ord": "0.1.1",
"parametric-svg-spec": "git://github.com/parametric-svg/spec.git#5bda58c",
"raw-loader": "0.5.1",
"tap-spec": "2.2.2",
"tape-catch": "1.0.4",
"tosource": "1.0.0",
Expand Down
133 changes: 77 additions & 56 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,99 @@ import parse from './module';

const test = require('tape-catch');
const {safeLoad: yaml} = require('js-yaml');
// const {readFileSync, readdirSync} = require('fs');
const {readFileSync} = require('fs');
const {resolve} = require('path');
const ord = require('ord');
const tosource = require('tosource');
const {jsdom} = require('jsdom');
const arrayFrom = require('array-from');
const {DOMParser} = require('xmldom');
const {DOMParser: XmldomParser} = require('xmldom');

const specDirectory = resolve(__dirname,
'node_modules/parametric-svg-spec/specs'
);

// const specs = readdirSync(specDirectory)
const specs = ['usage-html5.yaml', 'usage-xml.yaml']
.map((filename) => yaml(readFileSync(
resolve(specDirectory, filename)
)));

specs.forEach(({name, tests}) => tests.forEach((
{description, ast, document, mode}
) => {
test(`${name}: ${description}`, (is) => {
const rootElement = (mode === 'html' ?
jsdom(document).defaultView.document.body.parentNode :
new DOMParser().parseFromString(document).documentElement
);

const {attributes} = parse(rootElement);
if (typeof require.ensure !== 'function') require.ensure =
require('isomorphic-ensure')({
loaders: {
raw: require('raw-loader'),
},
dirname: __dirname,
});

is.equal(
attributes.size,
ast.length,
'the AST has the right number of attributes'
);
const specPaths = [
'usage-html5',
'usage-xml',
].map((spec) => `raw!./node_modules/parametric-svg-spec/specs/${spec}.yaml`);

ast.forEach((expected, index) => {
const n = index + 1;
const nth = `${n}${ord(n)}`;
const actual = arrayFrom(attributes)[index];
require.ensure(specPaths, (require) => {
const specs = specPaths.map((path) => yaml(require(path)));

is.deepEqual(
actual.address,
expected.address,
`the \`address\` matches in the ${nth} parametric attribute`
specs.forEach((
{name, tests}
) => tests.forEach((
{description, ast, document, mode}
) => {
test(`${name}: ${description}`, (is) => {
const inBrowser = typeof window !== 'undefined' && window.DOMParser;
const htmlMode = mode === 'html';
const rootElement = (
(inBrowser && htmlMode &&
(new window.DOMParser()).parseFromString(document, 'text/html')
.documentElement
) ||
(inBrowser && !htmlMode &&
(new window.DOMParser()).parseFromString(document, 'application/xml')
.documentElement
) ||
(!inBrowser && htmlMode &&
jsdom(document).defaultView.document.body.parentNode
) ||
(!inBrowser && !htmlMode &&
(new XmldomParser()).parseFromString(document, 'application/xml')
.documentElement
)
);

const {attributes} = parse(rootElement);

is.equal(
actual.name,
expected.name,
`the \`name\` matches in the ${nth} parametric attribute`
attributes.size,
ast.length,
'the AST has the right number of attributes'
);

is.deepEqual(
actual.dependencies,
expected.dependencies,
`the \`dependencies\` match in the ${nth} parametric attribute`
);
ast.forEach((expected, index) => {
const n = index + 1;
const nth = `${n}${ord(n)}`;
const actual = arrayFrom(attributes)[index];

expected.relation.forEach(({input, output: expectedOutput}) => {
const inputArguments = input.map(tosource).join(', ');
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.relation(...input),
expectedOutput,
`the \`relation\` in the ${nth} parametric attribute returns ` +
'the expected value when called with the arguments ' +
`\`(${inputArguments})\`.`
actual.dependencies,
expected.dependencies,
`the \`dependencies\` match in the ${nth} parametric attribute`
);

expected.relation.forEach(({input, output: expectedOutput}) => {
const inputArguments = input.map(tosource).join(', ');

is.deepEqual(
actual.relation(...input),
expectedOutput,
`the \`relation\` in the ${nth} parametric attribute returns ` +
'the expected value when called with the arguments ' +
`\`(${inputArguments})\`.`
);
});
});
});

is.end();
});
}));
is.end();
});
}));
});

0 comments on commit 2d733ee

Please sign in to comment.