Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Prepare tests for multiple fixture runners. (#240)
Browse files Browse the repository at this point in the history
* Prepare tests for multiple fixture runners.

* Replace ES2015
  • Loading branch information
danez committed Dec 8, 2016
1 parent 30545e8 commit 375838d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 98 deletions.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flowtype": "^2.20.0",
"flow-bin": "^0.35.0",
"lodash": "^4.15.0",
"nyc": "^10.0.0",
"rimraf": "^2.5.4",
"rollup": "^0.36.3",
Expand All @@ -48,7 +47,7 @@
"prepublish": "cross-env BABEL_ENV=production npm run build",
"preversion": "npm run test && npm run changelog",
"test": "npm run lint && npm run flow && npm run build -- -m && npm run test-only",
"test-only": "ava test",
"test-only": "ava",
"test-ci": "nyc npm run test-only",
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'",
"watch": "npm run clean && cross-env BABEL_ENV=watch babel src --out-dir lib --watch"
Expand All @@ -61,6 +60,15 @@
"sourceMap": false,
"instrument": false
},
"ava": {
"files": [
"test/*.js"
],
"source": [
"src/**/*.js",
"bin/**/*.js"
]
},
"greenkeeper": {
"ignore": [
"cross-env"
Expand Down
100 changes: 4 additions & 96 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,5 @@
var getFixtures = require("babel-helper-fixtures").multiple;
var parse = require("../lib").parse;
var test = require("ava");
var _ = require("lodash");
import path from "path";
import runFixtureTests from "./utils/runFixtureTests";
import { parse } from "../lib";

var fixtures = getFixtures(__dirname + "/fixtures");

_.each(fixtures, function (suites, name) {
_.each(suites, function (testSuite) {
_.each(testSuite.tests, function (task) {
test(name + "/" + testSuite.title + "/" + task.title, !task.disabled && function () {
try {
return runTest(task);
} catch (err) {
err.message = task.actual.loc + ": " + err.message;
throw err;
}
});
});
});
});

function save(test, ast) {
delete ast.tokens;
if (!ast.comments.length) delete ast.comments;
require("fs").writeFileSync(test.expect.loc, JSON.stringify(ast, null, " "));
}

function runTest(test) {
var opts = test.options;
opts.locations = true;
opts.ranges = true;

try {
var ast = parse(test.actual.code, opts);
} catch (err) {
if (opts.throws) {
if (err.message === opts.throws) {
return;
} else {
err.message = "Expected error message: " + opts.throws + ". Got error message: " + err.message;
throw err;
}
}

throw err;
}

if (!test.expect.code && !opts.throws && !process.env.CI) {
test.expect.loc += "on";
return save(test, ast);
}

if (opts.throws) {
throw new Error("Expected error message: " + opts.throws + ". But parsing succeeded.");
} else {
var mis = misMatch(JSON.parse(test.expect.code), ast);
if (mis) {
//save(test, ast);
throw new Error(mis);
}
}
}

function ppJSON(v) {
return v instanceof RegExp ? v.toString() : JSON.stringify(v, null, 2);
}

function addPath(str, pt) {
if (str.charAt(str.length - 1) == ")") {
return str.slice(0, str.length - 1) + "/" + pt + ")";
} else {
return str + " (" + pt + ")";
}
}

function misMatch(exp, act) {
if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
if (exp !== act && typeof exp != "function")
return ppJSON(exp) + " !== " + ppJSON(act);
} else if (exp instanceof RegExp || act instanceof RegExp) {
var left = ppJSON(exp), right = ppJSON(act);
if (left !== right) return left + " !== " + right;
} else if (exp.splice) {
if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act);
if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length;
for (var i = 0; i < act.length; ++i) {
var mis = misMatch(exp[i], act[i]);
if (mis) return addPath(mis, i);
}
} else {
for (var prop in exp) {
var mis = misMatch(exp[prop], act[prop]);
if (mis) return addPath(mis, prop);
}
}
}
runFixtureTests(path.join(__dirname, "fixtures"), parse);
99 changes: 99 additions & 0 deletions test/utils/runFixtureTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var test = require("ava");
var getFixtures = require("babel-helper-fixtures").multiple;

module.exports = function runFixtureTests(fixturesPath, parseFunction) {
var fixtures = getFixtures(fixturesPath);

Object.keys(fixtures).forEach(function (name) {
fixtures[name].forEach(function (testSuite) {
testSuite.tests.forEach(function (task) {
var testFn = task.disabled ? test.skip : test;

testFn(name + "/" + testSuite.title + "/" + task.title, function () {
try {
return runTest(task, parseFunction);
} catch (err) {
err.message = task.actual.loc + ": " + err.message;
throw err;
}
});
});
});
});
};

function save(test, ast) {
delete ast.tokens;
if (!ast.comments.length) delete ast.comments;
require("fs").writeFileSync(test.expect.loc, JSON.stringify(ast, null, " "));
}

function runTest(test, parseFunction) {
var opts = test.options;
opts.locations = true;
opts.ranges = true;

try {
var ast = parseFunction(test.actual.code, opts);
} catch (err) {
if (opts.throws) {
if (err.message === opts.throws) {
return;
} else {
err.message = "Expected error message: " + opts.throws + ". Got error message: " + err.message;
throw err;
}
}

throw err;
}

if (!test.expect.code && !opts.throws && !process.env.CI) {
test.expect.loc += "on";
return save(test, ast);
}

if (opts.throws) {
throw new Error("Expected error message: " + opts.throws + ". But parsing succeeded.");
} else {
var mis = misMatch(JSON.parse(test.expect.code), ast);
if (mis) {
//save(test, ast);
throw new Error(mis);
}
}
}

function ppJSON(v) {
return v instanceof RegExp ? v.toString() : JSON.stringify(v, null, 2);
}

function addPath(str, pt) {
if (str.charAt(str.length - 1) == ")") {
return str.slice(0, str.length - 1) + "/" + pt + ")";
} else {
return str + " (" + pt + ")";
}
}

function misMatch(exp, act) {
if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
if (exp !== act && typeof exp != "function")
return ppJSON(exp) + " !== " + ppJSON(act);
} else if (exp instanceof RegExp || act instanceof RegExp) {
var left = ppJSON(exp), right = ppJSON(act);
if (left !== right) return left + " !== " + right;
} else if (exp.splice) {
if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act);
if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length;
for (var i = 0; i < act.length; ++i) {
var mis = misMatch(exp[i], act[i]);
if (mis) return addPath(mis, i);
}
} else {
for (var prop in exp) {
var mis = misMatch(exp[prop], act[prop]);
if (mis) return addPath(mis, prop);
}
}
}

0 comments on commit 375838d

Please sign in to comment.