mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.7 KiB
105 lines
2.7 KiB
7 years ago
|
import * as fs from "fs";
|
||
6 years ago
|
import * as assert from "assert";
|
||
4 years ago
|
import { svelte, loadConfig, tryToLoadJson } from "../helpers";
|
||
8 years ago
|
|
||
6 years ago
|
describe("validate", () => {
|
||
5 years ago
|
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
||
7 years ago
|
if (dir[0] === ".") return;
|
||
8 years ago
|
|
||
7 years ago
|
// add .solo to a sample directory name to only run that test
|
||
7 years ago
|
const solo = /\.solo/.test(dir);
|
||
7 years ago
|
const skip = /\.skip/.test(dir);
|
||
8 years ago
|
|
||
7 years ago
|
if (solo && process.env.CI) {
|
||
|
throw new Error("Forgot to remove `solo: true` from test");
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
(solo ? it.only : skip ? it.skip : it)(dir, () => {
|
||
5 years ago
|
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
|
||
8 years ago
|
|
||
4 years ago
|
const input = fs.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, "utf-8").replace(/\s+$/, "").replace(/\r/g, "");
|
||
5 years ago
|
const expected_warnings = tryToLoadJson(`${__dirname}/samples/${dir}/warnings.json`) || [];
|
||
|
const expected_errors = tryToLoadJson(`${__dirname}/samples/${dir}/errors.json`);
|
||
5 years ago
|
const options = tryToLoadJson(`${__dirname}/samples/${dir}/options.json`);
|
||
6 years ago
|
|
||
|
let error;
|
||
|
|
||
|
try {
|
||
5 years ago
|
const { warnings } = svelte.compile(input, {
|
||
6 years ago
|
dev: config.dev,
|
||
6 years ago
|
legacy: config.legacy,
|
||
5 years ago
|
generate: false,
|
||
5 years ago
|
customElement: config.customElement,
|
||
4 years ago
|
...options
|
||
6 years ago
|
});
|
||
|
|
||
6 years ago
|
assert.deepEqual(warnings.map(w => ({
|
||
6 years ago
|
code: w.code,
|
||
|
message: w.message,
|
||
|
pos: w.pos,
|
||
|
start: w.start,
|
||
|
end: w.end
|
||
6 years ago
|
})), expected_warnings);
|
||
6 years ago
|
} catch (e) {
|
||
|
error = e;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
const expected = expected_errors && expected_errors[0];
|
||
6 years ago
|
|
||
6 years ago
|
if (error || expected) {
|
||
|
if (error && !expected) {
|
||
|
throw error;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
if (expected && !error) {
|
||
|
throw new Error(`Expected an error: ${expected.message}`);
|
||
7 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
try {
|
||
|
assert.equal(error.code, expected.code);
|
||
|
assert.equal(error.message, expected.message);
|
||
|
assert.deepEqual(error.start, expected.start);
|
||
|
assert.deepEqual(error.end, expected.end);
|
||
|
assert.equal(error.pos, expected.pos);
|
||
|
} catch (e) {
|
||
5 years ago
|
console.error(error); // eslint-disable-line no-console
|
||
6 years ago
|
throw e;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
7 years ago
|
it("errors if options.name is illegal", () => {
|
||
|
assert.throws(() => {
|
||
|
svelte.compile("<div></div>", {
|
||
6 years ago
|
name: "not.valid",
|
||
|
generate: false
|
||
8 years ago
|
});
|
||
7 years ago
|
}, /options\.name must be a valid identifier/);
|
||
8 years ago
|
});
|
||
7 years ago
|
|
||
7 years ago
|
it("warns if options.name is not capitalised", () => {
|
||
6 years ago
|
const { warnings } = svelte.compile("<div></div>", {
|
||
7 years ago
|
name: "lowercase",
|
||
6 years ago
|
generate: false
|
||
7 years ago
|
});
|
||
6 years ago
|
|
||
6 years ago
|
assert.deepEqual(warnings.map(w => ({
|
||
6 years ago
|
code: w.code,
|
||
|
message: w.message
|
||
|
})), [{
|
||
|
code: `options-lowercase-name`,
|
||
|
message: "options.name should be capitalised"
|
||
|
}]);
|
||
7 years ago
|
});
|
||
7 years ago
|
|
||
|
it("does not warn if options.name begins with non-alphabetic character", () => {
|
||
6 years ago
|
const { warnings } = svelte.compile("<div></div>", {
|
||
7 years ago
|
name: "_",
|
||
6 years ago
|
generate: false
|
||
7 years ago
|
});
|
||
6 years ago
|
|
||
6 years ago
|
assert.deepEqual(warnings, []);
|
||
7 years ago
|
});
|
||
8 years ago
|
});
|