0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00
svelte/test/validator/index.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-06-03 04:04:36 +02:00
import * as fs from "fs";
import assert from "assert";
import { svelte, tryToLoadJson } from "../helpers.js";
2017-06-03 04:04:36 +02:00
describe("validate", () => {
fs.readdirSync("test/validator/samples").forEach(dir => {
if (dir[0] === ".") return;
// 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);
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, () => {
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+$/, "");
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;
try {
const warnings = [];
svelte.compile(input, {
2017-06-03 04:04:36 +02:00
onwarn(warning) {
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];
2017-07-29 23:47:10 +02:00
if (error || expected) {
if (error && !expected) {
throw error;
}
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-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
});
});