change behaviour of tests and browser testing/build
This commit is contained in:
7
test/_browser.js
Normal file
7
test/_browser.js
Normal file
@@ -0,0 +1,7 @@
|
||||
if (process.browser) {
|
||||
require("../lib/6to5/browser");
|
||||
require("./generation");
|
||||
require("./transformation");
|
||||
require("./traverse");
|
||||
require("./util");
|
||||
}
|
||||
@@ -1,56 +1,32 @@
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var fixturesDir = __dirname + "/fixtures/transformation";
|
||||
|
||||
var humanise = function (val) {
|
||||
return val.replace(/-/g, " ");
|
||||
};
|
||||
|
||||
var readFile = function (filename) {
|
||||
if (fs.existsSync(filename)) {
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
return fs.readFileSync(filename, "utf8").trim();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
exports.runTransformationTests = function (suites, transform, assert) {
|
||||
_.each(suites, function (testSuite) {
|
||||
suite("transformation/" + testSuite.title, function () {
|
||||
_.each(testSuite.tests, function (task) {
|
||||
test(task.title, function () {
|
||||
var run = function () {
|
||||
transform.test(task, assert);
|
||||
};
|
||||
exports.get = function (entryName) {
|
||||
if (exports.cache[entryName]) return exports.cache[entryName];
|
||||
|
||||
var throwMsg = task.options.throws;
|
||||
if (throwMsg) {
|
||||
// internal api doesn't have this option but it's best not to pollute
|
||||
// the options object with useless options
|
||||
delete task.options.throws;
|
||||
|
||||
assert.throws(run, new RegExp(throwMsg));
|
||||
} else {
|
||||
run();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
exports.getTransformationTests = function () {
|
||||
var suites = [];
|
||||
var entryLoc = __dirname + "/fixtures/" + entryName;
|
||||
|
||||
_.each(fs.readdirSync(fixturesDir), function (suiteName) {
|
||||
_.each(fs.readdirSync(entryLoc), function (suiteName) {
|
||||
if (suiteName[0] === ".") return;
|
||||
|
||||
var suite = {
|
||||
options: {},
|
||||
tests: [],
|
||||
title: humanise(suiteName),
|
||||
filename: fixturesDir + "/" + suiteName
|
||||
filename: entryLoc + "/" + suiteName
|
||||
};
|
||||
suites.push(suite);
|
||||
|
||||
@@ -87,10 +63,12 @@ exports.getTransformationTests = function () {
|
||||
filename: execLocAlias,
|
||||
},
|
||||
actual: {
|
||||
loc: actualLoc,
|
||||
code: readFile(actualLoc),
|
||||
filename: actualLocAlias,
|
||||
},
|
||||
expect: {
|
||||
loc: expectLoc,
|
||||
code: readFile(expectLoc),
|
||||
filename: expectLocAlias
|
||||
}
|
||||
@@ -112,5 +90,15 @@ exports.getTransformationTests = function () {
|
||||
});
|
||||
});
|
||||
|
||||
return suites;
|
||||
return exports.cache[entryName] = suites;
|
||||
};
|
||||
|
||||
try {
|
||||
exports.cache = require("../tests.json");
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
|
||||
var cache = exports.cache = {};
|
||||
cache.transformation = exports.get("transformation");
|
||||
cache.generation = exports.get("generation");
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script src="../node_modules/proclaim/lib/proclaim.js"></script>
|
||||
<script src="../node_modules/lodash/lodash.js"></script>
|
||||
<script src="../node_modules/mocha/mocha.js"></script>
|
||||
<script src="../dist/6to5.js"></script>
|
||||
<script>mocha.setup("tdd");</script>
|
||||
<script src="../dist/6to5-test.js"></script>
|
||||
<script>
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
var generate = require("../lib/6to5/generator");
|
||||
var traverse = require("../lib/6to5/traverse");
|
||||
var assert = require("assert");
|
||||
var helper = require("./_helper");
|
||||
var util = require("../lib/6to5/util");
|
||||
var chai = require("chai");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var fixturesLoc = __dirname + "/fixtures/generation";
|
||||
|
||||
suite("generation", function () {
|
||||
test("completeness", function () {
|
||||
_.each(traverse.VISITOR_KEYS, function (keys, type) {
|
||||
@@ -19,33 +17,19 @@ suite("generation", function () {
|
||||
assert.ok(traverse.VISITOR_KEYS[type], type + " should not exist");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
_.each(fs.readdirSync(fixturesLoc), function (suiteName) {
|
||||
if (suiteName[0] === ".") return;
|
||||
_.each(helper.get("generation"), function (testSuite) {
|
||||
suite("generation/" + testSuite.title, function () {
|
||||
_.each(testSuite.tests, function (task) {
|
||||
test(task.title, function () {
|
||||
var expect = task.expect;
|
||||
var actual = task.actual;
|
||||
|
||||
var suiteLoc = fixturesLoc + "/" + suiteName;
|
||||
var actualAst = util.parseNoProperties(actual.loc, actual.code);
|
||||
var actualCode = generate(actual, actualAst).code;
|
||||
|
||||
suite(suiteName, function () {
|
||||
_.each(fs.readdirSync(suiteLoc), function (testName) {
|
||||
if (testName[0] === ".") return;
|
||||
|
||||
var testLoc = suiteLoc + "/" + testName;
|
||||
|
||||
test(testName, function () {
|
||||
var expectedLoc = testLoc + "/expected.js";
|
||||
var actualLoc = testLoc + "/actual.js";
|
||||
|
||||
var expected = fs.readFileSync(expectedLoc, "utf8");
|
||||
var actual = fs.readFileSync(actualLoc, "utf8");
|
||||
|
||||
var actualAst = util.parseNoProperties(actualLoc, actual);
|
||||
actual = generate(actual, actualAst).code;
|
||||
actualAst = util.parseNoProperties(actualLoc, actual);
|
||||
|
||||
var expectedAst = util.parseNoProperties(expectedLoc, expected);
|
||||
|
||||
chai.expect(actualAst).to.deep.equal(expectedAst);
|
||||
});
|
||||
chai.expect(actualCode).to.equal(expect.code, actual.loc + " !== " + expect.loc);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,84 @@
|
||||
var transform = require("../lib/6to5/transform");
|
||||
var sourceMap = require("source-map");
|
||||
var helper = require("./_helper");
|
||||
var assert = require("assert");
|
||||
var chai = require("chai");
|
||||
var _ = require("lodash");
|
||||
|
||||
helper.runTransformationTests(helper.getTransformationTests(), transform, assert);
|
||||
var run = function (task) {
|
||||
var actual = task.actual;
|
||||
var expect = task.expect;
|
||||
var opts = task.options;
|
||||
var exec = task.exec;
|
||||
|
||||
var getOpts = function (filename) {
|
||||
return _.merge({
|
||||
whtiespace: true,
|
||||
filename: filename
|
||||
}, opts);
|
||||
};
|
||||
|
||||
var execCode = exec.code;
|
||||
var result;
|
||||
|
||||
if (execCode) {
|
||||
result = transform(execCode, getOpts(exec.filename));
|
||||
execCode = result.code;
|
||||
|
||||
require("../polyfill");
|
||||
|
||||
try {
|
||||
var fn = new Function("assert", execCode);
|
||||
fn(assert);
|
||||
} catch (err) {
|
||||
err.message += util.codeFrame(execCode);
|
||||
throw err;
|
||||
}
|
||||
} else {
|
||||
var actualCode = actual.code;
|
||||
var expectCode = expect.code;
|
||||
|
||||
result = transform(actualCode, getOpts(actual.filename));
|
||||
actualCode = result.code;
|
||||
|
||||
chai.expect(actualCode).to.be.equal(expectCode, actual.loc + " !== " + expect.loc);
|
||||
}
|
||||
|
||||
if (task.sourceMap) {
|
||||
chai.expect(result.map).to.deep.equal(task.sourceMap);
|
||||
}
|
||||
|
||||
if (task.sourceMappings) {
|
||||
var consumer = new sourceMap.SourceMapConsumer(result.map);
|
||||
|
||||
_.each(task.sourceMappings, function (mapping, i) {
|
||||
var expect = mapping.original;
|
||||
|
||||
var actual = consumer.originalPositionFor(mapping.generated);
|
||||
chai.expect({ line: actual.line, column: actual.column }).to.deep.equal(expect);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
_.each(helper.get("transformation"), function (testSuite) {
|
||||
suite("transformation/" + testSuite.title, function () {
|
||||
_.each(testSuite.tests, function (task) {
|
||||
test(task.title, function () {
|
||||
var runTask = function () {
|
||||
run(task);
|
||||
};
|
||||
|
||||
var throwMsg = task.options.throws;
|
||||
if (throwMsg) {
|
||||
// internal api doesn't have this option but it's best not to pollute
|
||||
// the options object with useless options
|
||||
delete task.options.throws;
|
||||
|
||||
assert.throws(runTask, new RegExp(throwMsg));
|
||||
} else {
|
||||
runTask();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -116,28 +116,6 @@ suite("traverse", function () {
|
||||
assert.equal(ast2.body[1].expression.left.object, replacement);
|
||||
});
|
||||
|
||||
test("traverse delete", function () {
|
||||
var ast2 = _.cloneDeep(ast);
|
||||
|
||||
traverse(ast2, function (node) {
|
||||
if (node.type === "VariableDeclaration") return traverse.Delete;
|
||||
});
|
||||
|
||||
assert.deepEqual(ast2, {
|
||||
type: "Program",
|
||||
body: [body[1]]
|
||||
});
|
||||
});
|
||||
|
||||
test("traverse delete required", function () {
|
||||
assert.throws(function () {
|
||||
var ast2 = _.cloneDeep(ast);
|
||||
traverse(ast2, function (node) {
|
||||
if (node.type === "ThisExpression") return traverse.Delete;
|
||||
});
|
||||
}, /trying to delete property object from MemberExpression but can't because it's required/);
|
||||
});
|
||||
|
||||
test("hasType", function () {
|
||||
assert.ok(traverse.hasType(ast, "ThisExpression"));
|
||||
assert.ok(traverse.hasType(ast, "Program"));
|
||||
@@ -147,12 +125,4 @@ suite("traverse", function () {
|
||||
|
||||
assert.ok(!traverse.hasType(ast, "ArrowFunctionExpression"));
|
||||
});
|
||||
|
||||
test("isPattern");
|
||||
|
||||
test("isFunction", function () {
|
||||
//assert.ok(traverse.isFunction(b.arrowFunctionExpression());
|
||||
//assert.ok(traverse.isFunction(b.functionExpression()));
|
||||
//assert.ok(traverse.isFunction(b.functionDeclaration()));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user