Restructure types; Add a use-case specific options parser; Allow (re)creation of specific parser fixtures

This commit is contained in:
dcodeIO
2018-07-03 03:06:01 +02:00
parent 82da2d1f6d
commit 5ca5df3dc7
29 changed files with 486 additions and 279 deletions

View File

@ -49,7 +49,11 @@ To (re-)create all fixtures:
$>npm run test:parser -- --create
```
Note that the parser suite currently can't recreate just a specific fixture.
To (re-)create a specific fixture only:
```
$> npm run test:parser -- testNameWithoutTs --create
```
Compiler
--------

View File

@ -1,5 +1,8 @@
const asc = require("../dist/asc.js");
if (typeof asc.definitionFiles.assembly !== "string") throw Error("missing bundled assembly.d.ts");
if (typeof asc.definitionFiles.portable !== "string") throw Error("missing bundled portable.d.ts");
const stdout = asc.createMemoryStream();
const stderr = asc.createMemoryStream();
const files = { "module.ts": `import "allocator/arena";` };
@ -70,7 +73,7 @@ process.stdout.write(stderr.toString());
console.log("\n# asc.compileString");
const output = asc.compileString(`import "allocator/arena";`, { optimize: 2 });
const output = asc.compileString(`import "allocator/arena";`, { optimizeLevel: 2 });
console.log(">>> .stdout >>>");
process.stdout.write(output.stdout.toString());
console.log(">>> .stderr >>>");

View File

@ -1,24 +1,37 @@
const fs = require("fs");
const path = require("path");
const os = require("os");
const colors = require("../cli/util/colors");
const glob = require("glob");
const minimist = require("minimist");
const colorsUtil = require("../cli/util/colors");
const optionsUtil = require("../cli/util/options");
const diff = require("./util/diff");
const asc = require("../cli/asc.js");
const args = minimist(process.argv.slice(2), {
boolean: [ "create", "help" ],
alias: { h: "help" }
});
const config = {
"create": {
"description": [
"Recreates the fixture for the specified test(s)",
"or all the fixtures if no specific test is given."
],
"type": "b"
},
"help": {
"description": "Prints this message and exits.",
"type": "b",
"alias": "h"
}
};
const opts = optionsUtil.parse(process.argv.slice(2),config);
const args = opts.options;
const argv = opts.arguments;
if (args.help) {
console.log([
"Usage: npm run test:compiler -- [test1, test2 ...] [--create]",
colorsUtil.white("SYNTAX"),
" " + colorsUtil.cyan("npm run test:compiler --") + " [test1, test2 ...] [options]",
"",
"Runs all tests if no tests have been specified.",
"Recreates affected fixtures if --create is specified."
colorsUtil.white("OPTIONS"),
optionsUtil.help(config)
].join(os.EOL) + os.EOL);
process.exit(0);
}
@ -29,13 +42,13 @@ var failedTests = [];
const basedir = path.join(__dirname, "compiler");
// Get a list of all tests
var tests = glob.sync("**/!(_)*.ts", { cwd: basedir });
var tests = glob.sync("**/!(_*).ts", { cwd: basedir });
// Run specific tests only if arguments are provided
if (args._.length) {
tests = tests.filter(filename => args._.indexOf(filename.replace(/\.ts$/, "")) >= 0);
if (argv.length) {
tests = tests.filter(filename => argv.indexOf(filename.replace(/\.ts$/, "")) >= 0);
if (!tests.length) {
console.error("No matching tests: " + args._.join(" "));
console.error("No matching tests: " + argv.join(" "));
process.exit(1);
}
}
@ -54,7 +67,7 @@ function getExpectedErrors(filePath) {
// TODO: asc's callback is synchronous here. This might change.
tests.forEach(filename => {
console.log(colors.white("Testing compiler/" + filename) + "\n");
console.log(colorsUtil.white("Testing compiler/" + filename) + "\n");
const expectedErrors = getExpectedErrors(path.join(basedir, filename));
const basename = filename.replace(/\.ts$/, "");
@ -85,13 +98,13 @@ tests.forEach(filename => {
for (const expectedError of expectedErrors) {
if (!stderrString.includes(expectedError)) {
console.log(`Expected error "${expectedError}" was not in the error output.`);
console.log("- " + colors.red("error check ERROR"));
console.log("- " + colorsUtil.red("error check ERROR"));
failedTests.push(basename);
console.log();
return;
}
}
console.log("- " + colors.green("error check OK"));
console.log("- " + colorsUtil.green("error check OK"));
++successes;
console.log();
return;
@ -102,16 +115,16 @@ tests.forEach(filename => {
var actual = stdout.toString().replace(/\r\n/g, "\n");
if (args.create) {
fs.writeFileSync(path.join(basedir, basename + ".untouched.wat"), actual, { encoding: "utf8" });
console.log("- " + colors.yellow("Created fixture"));
console.log("- " + colorsUtil.yellow("Created fixture"));
} else {
let expected = fs.readFileSync(path.join(basedir, basename + ".untouched.wat"), { encoding: "utf8" }).replace(/\r\n/g, "\n");
let diffs = diff(basename + ".untouched.wat", expected, actual);
if (diffs !== null) {
console.log(diffs);
console.log("- " + colors.red("diff ERROR"));
console.log("- " + colorsUtil.red("diff ERROR"));
failed = true;
} else
console.log("- " + colors.green("diff OK"));
console.log("- " + colorsUtil.green("diff OK"));
}
console.log();
@ -178,9 +191,9 @@ tests.forEach(filename => {
},
});
});
console.log("- " + colors.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
console.log("- " + colorsUtil.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
} catch (e) {
console.log("- " + colors.red("instantiate ERROR: ") + e.stack);
console.log("- " + colorsUtil.red("instantiate ERROR: ") + e.stack);
failed = true;
}
@ -193,6 +206,6 @@ tests.forEach(filename => {
if (failedTests.length) {
process.exitCode = 1;
console.log(colors.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
console.log(colorsUtil.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
} else
console.log("[ " + colors.white("SUCCESS") + " ]");
console.log("[ " + colorsUtil.white("SUCCESS") + " ]");

View File

@ -1,6 +1,6 @@
var binaryen = global.Binaryen = require("../lib/binaryen");
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
require("ts-node").register({ project: require("path").join(__dirname, "..", "src", "tsconfig.json") });
require("../src/glue/js");
var mod = new binaryen.Module();

View File

@ -1,51 +1,91 @@
var fs = require("fs");
var path = require("path");
var colors = require("../cli/util/colors");
var glob = require("glob");
var diff = require("./util/diff");
const fs = require("fs");
const path = require("path");
const os = require("os");
const glob = require("glob");
const colorsUtil = require("../cli/util/colors");
const optionsUtil = require("../cli/util/options");
const diff = require("./util/diff");
const config = {
"create": {
"description": [
"Recreates the fixture for the specified test(s)",
"or all the fixtures if no specific test is given."
],
"type": "b"
},
"help": {
"description": "Prints this message and exits.",
"type": "b",
"alias": "h"
}
};
const opts = optionsUtil.parse(process.argv.slice(2), config);
const args = opts.options;
const argv = opts.arguments;
if (args.help) {
console.log([
colorsUtil.white("SYNTAX"),
" " + colorsUtil.cyan("npm run test:parser --") + " [test1, test2 ...] [options]",
"",
colorsUtil.white("OPTIONS"),
optionsUtil.help(config)
].join(os.EOL) + os.EOL);
process.exit(0);
}
const basedir = path.join(__dirname, "parser");
// Get a list of all tests
var tests = glob.sync("**/!(_*).ts", { cwd: basedir });
// Run specific tests only if arguments are provided
if (argv.length) {
tests = tests.filter(filename => argv.indexOf(filename.replace(/\.ts$/, "")) >= 0);
if (!tests.length) {
console.error("No matching tests: " + argv.join(" "));
process.exit(1);
}
}
require("ts-node").register({
project: require("path").join(__dirname, "..", "src", "tsconfig.json"),
files: [ // see: https://github.com/TypeStrong/ts-node/issues/620
path.join(__dirname, "..", "std", "portable.d.ts"),
path.join(__dirname, "..", "src", "glue", "binaryen.d.ts")
]
project: path.join(__dirname, "..", "src", "tsconfig.json"),
cache: false // FIXME: for some reason, if both asc and the parser tests use caching, the one
// invoked later cannot find some definition files.
});
require("../src/glue/js");
var Parser = require("../src/parser").Parser;
var ASTBuilder = require("../src/extra/ast").ASTBuilder;
var isCreate = process.argv[2] === "--create";
var filter = process.argv.length > 2 && !isCreate ? "*" + process.argv[2] + "*.ts" : "**.ts";
var failures = 0;
glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts"))
return;
tests.forEach(filename => {
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts")) return;
console.log(colors.white("Testing parser/" + filename));
console.log(colorsUtil.white("Testing parser/" + filename));
var failed = false;
var parser = new Parser();
var sourceText = fs.readFileSync(__dirname + "/parser/" + filename, { encoding: "utf8" }).replace(/\r?\n/g, "\n");
var sourceText = fs.readFileSync(basedir + "/" + filename, { encoding: "utf8" }).replace(/\r?\n/g, "\n");
parser.parseFile(sourceText, filename, true);
var serializedSourceText = ASTBuilder.build(parser.program.sources[0]);
var actual = serializedSourceText + parser.diagnostics.map(diagnostic => "// " + diagnostic +"\n").join("");
var fixture = filename + ".fixture.ts";
if (isCreate) {
fs.writeFileSync(__dirname + "/parser/" + fixture, actual, { encoding: "utf8" });
if (args.create) {
fs.writeFileSync(basedir + "/" + fixture, actual, { encoding: "utf8" });
console.log("Created\n");
} else {
var expected = fs.readFileSync(__dirname + "/parser/" + fixture, { encoding: "utf8" }).replace(/\r\n/g, "\n");
var expected = fs.readFileSync(basedir + "/" + fixture, { encoding: "utf8" }).replace(/\r\n/g, "\n");
var diffs = diff("parser/" + fixture, expected, actual);
if (diffs !== null) {
failed = true;
console.log(diffs);
console.log(colors.red("diff ERROR"));
console.log(colorsUtil.red("diff ERROR"));
} else {
console.log(colors.green("diff OK"));
console.log(colorsUtil.green("diff OK"));
}
}
@ -56,6 +96,6 @@ glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
if (failures) {
process.exitCode = 1;
console.log(colors.red("ERROR: ") + failures + " parser tests failed");
console.log(colorsUtil.red("ERROR: ") + failures + " parser tests failed");
} else
console.log("[ " + colors.white("SUCCESS") + " ]");
console.log("[ " + colorsUtil.white("SUCCESS") + " ]");