dry up classes

This commit is contained in:
Sebastian McKenzie
2014-09-29 12:23:50 +10:00
parent 53a8c34d33
commit 2e035663e1
28 changed files with 115 additions and 73 deletions

View File

@@ -1,51 +0,0 @@
var transform = require("../lib/6to5/transform");
var assert = require("assert");
suite("classes", function () {
test("no calling super properties", function () {
assert.throws(function () {
transform.test([
"class Test extends Foo {",
" constructor() {",
" super.test.whatever();",
" }",
"}"
]);
}, /cannot access super properties/);
});
test("no accessing super properties", function () {
assert.throws(function () {
transform.test([
"class Test extends Foo {",
" constructor() {",
" super.test.whatever;",
" }",
"}"
]);
}, /cannot access super properties/);
});
test("accessing super without having one", function () {
assert.throws(function () {
transform.test([
"class Test {",
" constructor() {",
" super();",
" }",
"}"
]);
}, /cannot access super as this class has none/);
});
test("defining constructor as a mutator", function () {
assert.throws(function () {
transform.test([
"class Test {",
" get constructor() {",
" }",
"}"
]);
}, /unknown kind for constructor method/);
});
});

View File

@@ -1,14 +0,0 @@
var transform = require("../lib/6to5/transform");
var assert = require("assert");
suite("errors", function () {
test("syntax", function () {
assert.throws(function () {
transform.test([
"arr.map(function () {",
" return $@!@#;",
"});"
]);
}, /Error: test: Line 2: Unexpected token ILLEGAL/);
});
});

View File

@@ -1,5 +0,0 @@
var arr = [
1,
2,
3
];

View File

@@ -0,0 +1,7 @@
(function () {
var arr = [
1,
2,
3
];
})();

View File

@@ -0,0 +1,5 @@
class Test {
constructor() {
super();
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "cannot access super as this class has none"
}

View File

@@ -0,0 +1,4 @@
class Test {
get constructor() {
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "unknown kind for constructor method"
}

View File

@@ -0,0 +1,5 @@
class Test extends Foo {
constructor() {
super.test.whatever;
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "cannot access super properties"
}

View File

@@ -0,0 +1,5 @@
class Test extends Foo {
constructor() {
super.test.whatever();
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "cannot access super properties"
}

View File

@@ -0,0 +1,5 @@
var obj = {
[foobar]() {
return "foobar"
}
};

View File

@@ -0,0 +1,6 @@
var obj = function (obj) {
obj[foobar] = function () {
return 'foobar';
};
return obj;
}({});

View File

@@ -0,0 +1,4 @@
for (let i in arr) {
const MULTIPLIER = 5;
console.log(arr[i] * MULTIPLIER);
}

View File

@@ -0,0 +1,8 @@
(function () {
for (var i in arr) {
(function () {
var MULTIPLIER = 5;
console.log(arr[i] * MULTIPLIER);
}());
}
}());

View File

@@ -0,0 +1,3 @@
const MULTIPLIER = 5;
MULTIPLIER = "overwrite";

View File

@@ -0,0 +1,3 @@
{
"throws": "MULTIPLIER is read-only"
}

View File

@@ -0,0 +1,3 @@
const MULTIPLIER = 5;
var MULTIPLIER = "overwrite";

View File

@@ -0,0 +1,3 @@
{
"throws": "MULTIPLIER is read-only"
}

View File

@@ -0,0 +1,5 @@
const MULTIPLIER = 5;
for (var i in arr) {
console.log(arr[i] * MULTIPLIER);
}

View File

@@ -0,0 +1,6 @@
(function () {
var MULTIPLIER = 5;
for (var i in arr) {
console.log(arr[i] * MULTIPLIER);
}
}());

3
test/fixtures/errors/syntax/actual.js vendored Normal file
View File

@@ -0,0 +1,3 @@
arr.map(function () {
return $@!@#;
});

View File

@@ -0,0 +1,3 @@
{
"throws": "Line 2: Unexpected token ILLEGAL"
}

View File

@@ -0,0 +1 @@
f(...[1, 2, 3]);

View File

@@ -0,0 +1 @@
f.apply(null, [1, 2, 3]);

View File

@@ -7,6 +7,14 @@ var humanise = function (val) {
return val.replace(/-/g, " ");
};
var readFile = function (filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename);
} else {
return "";
}
};
var fixturesDir = __dirname + "/fixtures";
_.each(fs.readdirSync(fixturesDir), function (suiteName) {
@@ -24,14 +32,26 @@ _.each(fs.readdirSync(fixturesDir), function (suiteName) {
test(humanise(taskName), function () {
var actualLoc = taskDir + "/actual.js";
var actual = fs.readFileSync(actualLoc, "utf8");
var expect = fs.readFileSync(taskDir + "/expected.js", "utf8");
var actual = readFile(actualLoc);
var expect = readFile(taskDir + "/expected.js");
var taskOptsLoc = taskDir + "/options.json";
var taskOpts = _.merge({ filename: actualLoc }, _.cloneDeep(suiteOpts));
if (fs.existsSync(taskOptsLoc)) _.merge(taskOpts, require(taskOptsLoc));
transform.test(actual, expect, taskOpts);
var test = function () {
transform.test(actual, expect, taskOpts);
};
var throwMsg = taskOpts.throws;
if (throwMsg) {
// internal api doesn't have this option but it's best not to pollute
// the options object with useless options
delete taskOpts.throws;
assert.throws(test, new RegExp(throwMsg));
} else {
test();
}
});
});
});