add validator tests

pull/1330/head
Rich Harris 6 years ago
parent 31ee144b9c
commit 0edbac615c

@ -16,59 +16,72 @@ describe("validate", () => {
(solo ? it.only : skip ? it.skip : it)(dir, () => {
const config = loadConfig(`./validator/samples/${dir}/_config.js`);
const filename = `test/validator/samples/${dir}/input.html`;
const input = fs.readFileSync(filename, "utf-8").replace(/\s+$/, "");
const expectedWarnings = tryToLoadJson(`test/validator/samples/${dir}/warnings.json`) || [];
const expectedErrors = tryToLoadJson(`test/validator/samples/${dir}/errors.json`);
let error;
try {
const warnings = [];
const { stats } = svelte.compile(input, {
onwarn(warning) {
warnings.push({
message: warning.message,
pos: warning.pos,
loc: warning.loc,
end: warning.end,
});
},
dev: config.dev
});
assert.equal(stats.warnings.length, warnings.length);
stats.warnings.forEach((full, i) => {
const lite = warnings[i];
assert.deepEqual({
message: full.message,
pos: full.pos,
loc: full.loc,
end: full.end
}, lite);
});
function test(input, expectedWarnings, expectedErrors) {
let error;
try {
const warnings = [];
const { stats } = svelte.compile(input, {
onwarn(warning) {
warnings.push({
message: warning.message,
pos: warning.pos,
loc: warning.loc,
end: warning.end,
});
},
dev: config.dev
});
assert.equal(stats.warnings.length, warnings.length);
stats.warnings.forEach((full, i) => {
const lite = warnings[i];
assert.deepEqual({
message: full.message,
pos: full.pos,
loc: full.loc,
end: full.end
}, lite);
});
assert.deepEqual(warnings, expectedWarnings);
} catch (e) {
error = e;
}
assert.deepEqual(warnings, expectedWarnings);
} catch (e) {
error = e;
}
const expected = expectedErrors && expectedErrors[0];
const expected = expectedErrors && expectedErrors[0];
if (error || expected) {
if (error && !expected) {
throw error;
}
if (error || expected) {
if (error && !expected) {
throw error;
}
if (expected && !error) {
throw new Error(`Expected an error: ${expected.message}`);
}
if (expected && !error) {
throw new Error(`Expected an error: ${expected.message}`);
assert.equal(error.message, expected.message);
assert.deepEqual(error.loc, expected.loc);
assert.deepEqual(error.end, expected.end);
assert.equal(error.pos, expected.pos);
}
}
assert.equal(error.message, expected.message);
assert.deepEqual(error.loc, expected.loc);
assert.deepEqual(error.end, expected.end);
assert.equal(error.pos, expected.pos);
// TODO remove the v1 tests
test(
fs.readFileSync(`test/validator/samples/${dir}/input.html`, "utf-8").replace(/\s+$/, ""),
tryToLoadJson(`test/validator/samples/${dir}/warnings.json`) || [],
tryToLoadJson(`test/validator/samples/${dir}/errors.json`)
);
if (fs.existsSync(`test/validator/samples/${dir}/input-v2.html`)) {
test(
fs.readFileSync(`test/validator/samples/${dir}/input-v2.html`, "utf-8").replace(/\s+$/, ""),
tryToLoadJson(`test/validator/samples/${dir}/warnings-v2.json`) || [],
tryToLoadJson(`test/validator/samples/${dir}/errors-v2.json`)
);
}
});
});

@ -0,0 +1,12 @@
[{
"message": "<title> cannot have attributes",
"loc": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 25
},
"pos": 22
}]

@ -0,0 +1,3 @@
<svelte:head>
<title data-invalid=true>a static title</title>
</svelte:head>
Loading…
Cancel
Save