From 0edbac615cf23f55c9517243609c45250c29f9aa Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 12 Apr 2018 23:34:46 -0400 Subject: [PATCH] add validator tests --- test/validator/index.js | 105 ++++++++++-------- .../title-no-attributes/errors-v2.json | 12 ++ .../samples/title-no-attributes/input-v2.html | 3 + 3 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 test/validator/samples/title-no-attributes/errors-v2.json create mode 100644 test/validator/samples/title-no-attributes/input-v2.html diff --git a/test/validator/index.js b/test/validator/index.js index ddfbfb8a4a..0ea1120493 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -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`) + ); } }); }); diff --git a/test/validator/samples/title-no-attributes/errors-v2.json b/test/validator/samples/title-no-attributes/errors-v2.json new file mode 100644 index 0000000000..0491303d0a --- /dev/null +++ b/test/validator/samples/title-no-attributes/errors-v2.json @@ -0,0 +1,12 @@ +[{ + "message": " cannot have attributes", + "loc": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 25 + }, + "pos": 22 +}] \ No newline at end of file diff --git a/test/validator/samples/title-no-attributes/input-v2.html b/test/validator/samples/title-no-attributes/input-v2.html new file mode 100644 index 0000000000..0e2dbefa73 --- /dev/null +++ b/test/validator/samples/title-no-attributes/input-v2.html @@ -0,0 +1,3 @@ +<svelte:head> + <title data-invalid=true>a static title + \ No newline at end of file