Use prettier (#600)
This commit is contained in:
@@ -2,53 +2,59 @@ import test from "ava";
|
||||
import { multiple as getFixtures } from "babel-helper-fixtures";
|
||||
|
||||
export function runFixtureTests(fixturesPath, parseFunction) {
|
||||
var fixtures = getFixtures(fixturesPath);
|
||||
const 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 : task.options.only ? test.only : test;
|
||||
Object.keys(fixtures).forEach(function(name) {
|
||||
fixtures[name].forEach(function(testSuite) {
|
||||
testSuite.tests.forEach(function(task) {
|
||||
const testFn = task.disabled
|
||||
? test.skip
|
||||
: task.options.only ? test.only : test;
|
||||
|
||||
testFn(name + "/" + testSuite.title + "/" + task.title, function (t) {
|
||||
try {
|
||||
runTest(task, parseFunction);
|
||||
t.pass();
|
||||
} catch (err) {
|
||||
const message = name + "/" + task.actual.filename + ": " + err.message;
|
||||
t.fail(message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
|
||||
var fixtures = getFixtures(fixturesPath);
|
||||
|
||||
Object.keys(fixtures).forEach(function (name) {
|
||||
fixtures[name].forEach(function (testSuite) {
|
||||
testSuite.tests.forEach(function (task) {
|
||||
if (!task.options.throws) return;
|
||||
|
||||
task.options.plugins = task.options.plugins || [];
|
||||
task.options.plugins.push("estree");
|
||||
|
||||
var testFn = task.disabled ? test.skip : task.options.only ? test.only : test;
|
||||
|
||||
testFn(name + "/" + testSuite.title + "/" + task.title, function (t) {
|
||||
testFn(name + "/" + testSuite.title + "/" + task.title, function(t) {
|
||||
try {
|
||||
runTest(task, parseFunction);
|
||||
t.pass();
|
||||
} catch (err) {
|
||||
const message = name + "/" + task.actual.filename + ": " + err.message;
|
||||
const message =
|
||||
name + "/" + task.actual.filename + ": " + err.message;
|
||||
t.fail(message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
|
||||
const fixtures = getFixtures(fixturesPath);
|
||||
|
||||
Object.keys(fixtures).forEach(function(name) {
|
||||
fixtures[name].forEach(function(testSuite) {
|
||||
testSuite.tests.forEach(function(task) {
|
||||
if (!task.options.throws) return;
|
||||
|
||||
task.options.plugins = task.options.plugins || [];
|
||||
task.options.plugins.push("estree");
|
||||
|
||||
const testFn = task.disabled
|
||||
? test.skip
|
||||
: task.options.only ? test.only : test;
|
||||
|
||||
testFn(name + "/" + testSuite.title + "/" + task.title, function(t) {
|
||||
try {
|
||||
runTest(task, parseFunction);
|
||||
t.pass();
|
||||
} catch (err) {
|
||||
const message =
|
||||
name + "/" + task.actual.filename + ": " + err.message;
|
||||
t.fail(message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function save(test, ast) {
|
||||
// Ensure that RegExp are serialized as strings
|
||||
@@ -59,20 +65,28 @@ function save(test, ast) {
|
||||
}
|
||||
|
||||
function runTest(test, parseFunction) {
|
||||
var opts = test.options;
|
||||
const opts = test.options;
|
||||
|
||||
if (opts.throws && test.expect.code) {
|
||||
throw new Error("File expected.json exists although options specify throws. Remove expected.json.");
|
||||
throw new Error(
|
||||
"File expected.json exists although options specify throws. Remove expected.json.",
|
||||
);
|
||||
}
|
||||
|
||||
let ast;
|
||||
|
||||
try {
|
||||
var ast = parseFunction(test.actual.code, opts);
|
||||
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;
|
||||
err.message =
|
||||
"Expected error message: " +
|
||||
opts.throws +
|
||||
". Got error message: " +
|
||||
err.message;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -88,9 +102,11 @@ function runTest(test, parseFunction) {
|
||||
}
|
||||
|
||||
if (opts.throws) {
|
||||
throw new Error("Expected error message: " + opts.throws + ". But parsing succeeded.");
|
||||
throw new Error(
|
||||
"Expected error message: " + opts.throws + ". But parsing succeeded.",
|
||||
);
|
||||
} else {
|
||||
var mis = misMatch(JSON.parse(test.expect.code), ast);
|
||||
const mis = misMatch(JSON.parse(test.expect.code), ast);
|
||||
|
||||
if (mis) {
|
||||
throw new Error(mis);
|
||||
@@ -113,25 +129,27 @@ function addPath(str, pt) {
|
||||
|
||||
function misMatch(exp, act) {
|
||||
if (exp instanceof RegExp || act instanceof RegExp) {
|
||||
var left = ppJSON(exp), right = ppJSON(act);
|
||||
const left = ppJSON(exp);
|
||||
const right = ppJSON(act);
|
||||
if (left !== right) return left + " !== " + right;
|
||||
} else if (Array.isArray(exp)) {
|
||||
if (!Array.isArray(act)) 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 (act.length != exp.length)
|
||||
return "array length mismatch " + exp.length + " != " + act.length;
|
||||
for (let i = 0; i < act.length; ++i) {
|
||||
const mis = misMatch(exp[i], act[i]);
|
||||
if (mis) return addPath(mis, i);
|
||||
}
|
||||
} else if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
|
||||
} else if (!exp || !act || typeof exp != "object" || typeof act != "object") {
|
||||
if (exp !== act && typeof exp != "function")
|
||||
return ppJSON(exp) + " !== " + ppJSON(act);
|
||||
} else {
|
||||
for (var prop in exp) {
|
||||
var mis = misMatch(exp[prop], act[prop]);
|
||||
} else {
|
||||
for (const prop in exp) {
|
||||
const mis = misMatch(exp[prop], act[prop]);
|
||||
if (mis) return addPath(mis, prop);
|
||||
}
|
||||
|
||||
for (var prop in act) {
|
||||
for (const prop in act) {
|
||||
if (typeof act[prop] === "function") {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user