Skip to content

Commit

Permalink
test: enable tests for js parser implementation
Browse files Browse the repository at this point in the history
Serde tests were only launched using the default parser implementation
(which should be the native one while developing locally, and is always
the native one on CI since it fails the build if it doesn't succeed
building the addon).  This commit makes the tests run with both
implementations, the default and the fallback JavaScript-only one.

PR-URL: #258
Reviewed-By: Dmytro Nechai <nechaido@gmail.com>
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
  • Loading branch information
aqrln authored and belochub committed Jan 22, 2018
1 parent cc1847f commit 0597b58
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 34 deletions.
31 changes: 23 additions & 8 deletions test/node/serde.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = require('tap').test;

const jstp = require('../..');
const jsParser = require('../../lib/serde-fallback');

const testCases = require('../fixtures/serde-test-cases');

Expand All @@ -14,15 +15,29 @@ testCases.serde.concat(testCases.serialization).forEach((testCase) => {
});

testCases.serde.concat(testCases.deserialization).forEach((testCase) => {
test(`must deserialize ${testCase.name}`, (test) => {
test.strictSame(jstp.parse(testCase.serialized), testCase.value);
test.end();
});
const runTest = (parserName, parser) => {
test(
`must deserialize ${testCase.name} using ${parserName} parser`,
(test) => {
test.strictSame(parser.parse(testCase.serialized), testCase.value);
test.end();
}
);
};
runTest('native', jstp);
runTest('js', jsParser);
});

testCases.invalid.forEach((testCase) => {
test(`must not allow ${testCase.name}`, (test) => {
test.throws(() => jstp.parse(testCase.value));
test.end();
});
const runTest = (parserName, parser) => {
test(
`must not allow ${testCase.name} using ${parserName} parser`,
(test) => {
test.throws(() => parser.parse(testCase.value));
test.end();
}
);
};
runTest('native', jstp);
runTest('js', jsParser);
});
54 changes: 32 additions & 22 deletions test/todo/json5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const tap = require('tap');
const jstp = require('../..');
const jsParser = require('../../lib/serde-fallback');

const supportedByUs = {
arrays: [
Expand Down Expand Up @@ -46,29 +47,38 @@ testCases.forEach((testCase) => {
const testPath = path.join(testCase.path, filename);
const file = fs.readFileSync(testPath, 'utf8');

test.test(testName, (test) => {
switch (ext) {
case '.json':
test.strictSame(jstp.parse(file), JSON.parse(file));
break;
case '.json5':
test.strictSame(jstp.parse(file), extendedEval(file));
break;
case '.js': {
const supportedTests = supportedByUs[testCase.name];
if (supportedTests && supportedTests.includes(testName)) {
test.strictSame(jstp.parse(file), extendedEval(file));
} else {
test.throws(() => jstp.parse(file));
}
break;
const testCases = {
json(test, parser) {
test.strictSame(parser.parse(file), JSON.parse(file));
},

json5(test, parser) {
test.strictSame(parser.parse(file), extendedEval(file));
},

js(test, parser) {
const supportedTests = supportedByUs[testCase.name];
if (supportedTests && supportedTests.includes(testName)) {
test.strictSame(parser.parse(file), extendedEval(file));
} else {
test.throws(() => parser.parse(file));
}
case '.txt':
test.throws(() => jstp.parse(file));
break;
}
test.end();
});
},

txt(test, parser) {
test.throws(() => parser.parse(file));
},
};

const runTest = (parserName, parser) => {
test.test(`${testName} (${parserName} parser)`, (test) => {
testCases[ext.slice(1)](test, parser);
test.end();
});
};

runTest('native', jstp);
runTest('js', jsParser);
});
test.end();
});
Expand Down
16 changes: 12 additions & 4 deletions test/todo/serde.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
'use strict';

const jstp = require('../..');
const jsParser = require('../../lib/serde-fallback');

const test = require('tap').test;
const testCases = require('../fixtures/todo/serde');

testCases.deserialization.forEach((testCase) => {
test(`must deserialize ${testCase.name}`, (test) => {
test.strictSame(jstp.parse(testCase.serialized), testCase.value);
test.end();
});
const runTest = (parserName, parser) => {
test(
`must deserialize ${testCase.name} using ${parserName} parser`,
(test) => {
test.strictSame(parser.parse(testCase.serialized), testCase.value);
test.end();
}
);
};
runTest('native', jstp);
runTest('js', jsParser);
});

testCases.serialization.forEach((testCase) => {
Expand Down

0 comments on commit 0597b58

Please sign in to comment.