2017-06-03 04:04:36 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import assert from "assert";
|
|
|
|
import { svelte, tryToLoadJson } from "../helpers.js";
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
describe("validate", () => {
|
|
|
|
fs.readdirSync("test/validator/samples").forEach(dir => {
|
|
|
|
if (dir[0] === ".") return;
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-10 20:20:50 +02:00
|
|
|
// add .solo to a sample directory name to only run that test
|
2017-06-03 04:04:36 +02:00
|
|
|
const solo = /\.solo/.test(dir);
|
2017-08-27 17:22:02 +02:00
|
|
|
const skip = /\.skip/.test(dir);
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (solo && process.env.CI) {
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
2017-01-04 20:07:05 +01:00
|
|
|
}
|
|
|
|
|
2017-08-27 17:22:02 +02:00
|
|
|
(solo ? it.only : skip ? it.skip : it)(dir, () => {
|
2017-03-07 20:10:28 +01:00
|
|
|
const filename = `test/validator/samples/${dir}/input.html`;
|
2017-06-03 04:04:36 +02:00
|
|
|
const input = fs.readFileSync(filename, "utf-8").replace(/\s+$/, "");
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-07-29 23:47:10 +02:00
|
|
|
const expectedWarnings = tryToLoadJson(`test/validator/samples/${dir}/warnings.json`) || [];
|
|
|
|
const expectedErrors = tryToLoadJson(`test/validator/samples/${dir}/errors.json`);
|
|
|
|
let error;
|
|
|
|
|
2016-12-08 11:48:54 +01:00
|
|
|
try {
|
|
|
|
const warnings = [];
|
|
|
|
|
2017-07-08 00:46:16 +02:00
|
|
|
svelte.compile(input, {
|
2017-06-03 04:04:36 +02:00
|
|
|
onwarn(warning) {
|
2016-12-08 11:48:54 +01:00
|
|
|
warnings.push({
|
|
|
|
message: warning.message,
|
|
|
|
pos: warning.pos,
|
|
|
|
loc: warning.loc
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
assert.deepEqual(warnings, expectedWarnings);
|
2017-07-29 23:47:10 +02:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expected = expectedErrors && expectedErrors[0];
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-07-29 23:47:10 +02:00
|
|
|
if (error || expected) {
|
|
|
|
if (error && !expected) {
|
|
|
|
throw error;
|
2016-12-08 11:48:54 +01:00
|
|
|
}
|
2017-07-29 23:47:10 +02:00
|
|
|
|
|
|
|
if (expected && !error) {
|
|
|
|
throw new Error(`Expected an error: ${expected.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(error.message, expected.message);
|
|
|
|
assert.deepEqual(error.loc, expected.loc);
|
|
|
|
assert.equal(error.pos, expected.pos);
|
2016-12-08 11:48:54 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-12-17 22:55:40 +01:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
it("errors if options.name is illegal", () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
svelte.compile("<div></div>", {
|
|
|
|
name: "not.valid"
|
2016-12-17 22:55:40 +01:00
|
|
|
});
|
2017-06-03 04:04:36 +02:00
|
|
|
}, /options\.name must be a valid identifier/);
|
2016-12-17 22:55:40 +01:00
|
|
|
});
|
2017-04-11 00:56:55 +02:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
it("warns if options.name is not capitalised", () => {
|
2017-04-11 00:56:55 +02:00
|
|
|
const warnings = [];
|
2017-06-03 04:04:36 +02:00
|
|
|
svelte.compile("<div></div>", {
|
|
|
|
name: "lowercase",
|
|
|
|
onwarn(warning) {
|
2017-04-11 00:56:55 +02:00
|
|
|
warnings.push({
|
|
|
|
message: warning.message,
|
|
|
|
pos: warning.pos,
|
|
|
|
loc: warning.loc
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-06-03 04:04:36 +02:00
|
|
|
assert.deepEqual(warnings, [
|
|
|
|
{
|
|
|
|
message: "options.name should be capitalised",
|
|
|
|
pos: undefined,
|
|
|
|
loc: undefined
|
|
|
|
}
|
|
|
|
]);
|
2017-04-11 00:56:55 +02:00
|
|
|
});
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|