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.
52 lines
1.7 KiB
52 lines
1.7 KiB
6 years ago
|
import * as assert from 'assert';
|
||
|
import * as fs from 'fs';
|
||
4 years ago
|
import { svelte, tryToLoadJson, shouldUpdateExpected } from '../helpers';
|
||
8 years ago
|
|
||
6 years ago
|
describe('parse', () => {
|
||
5 years ago
|
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
||
7 years ago
|
if (dir[0] === '.') return;
|
||
8 years ago
|
|
||
8 years ago
|
// add .solo to a sample directory name to only run that test
|
||
7 years ago
|
const solo = /\.solo$/.test(dir);
|
||
8 years ago
|
|
||
7 years ago
|
if (solo && process.env.CI) {
|
||
|
throw new Error(
|
||
|
`Forgot to remove '.solo' from test parser/samples/${dir}`
|
||
|
);
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
const skip = !fs.existsSync(`${__dirname}/samples/${dir}/input.svelte`);
|
||
|
|
||
|
(skip ? it.skip : solo ? it.only : it)(dir, () => {
|
||
5 years ago
|
const options = tryToLoadJson(`${__dirname}/samples/${dir}/options.json`) || {};
|
||
7 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 expectedOutput = tryToLoadJson(`${__dirname}/samples/${dir}/output.json`);
|
||
|
const expectedError = tryToLoadJson(`${__dirname}/samples/${dir}/error.json`);
|
||
6 years ago
|
|
||
|
try {
|
||
6 years ago
|
const { ast } = svelte.compile(input, Object.assign(options, {
|
||
|
generate: false
|
||
|
}));
|
||
6 years ago
|
|
||
5 years ago
|
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.json`, JSON.stringify(ast, null, '\t'));
|
||
6 years ago
|
|
||
6 years ago
|
assert.deepEqual(ast.html, expectedOutput.html);
|
||
|
assert.deepEqual(ast.css, expectedOutput.css);
|
||
6 years ago
|
assert.deepEqual(ast.instance, expectedOutput.instance);
|
||
|
assert.deepEqual(ast.module, expectedOutput.module);
|
||
6 years ago
|
} catch (err) {
|
||
|
if (err.name !== 'ParseError') throw err;
|
||
|
if (!expectedError) throw err;
|
||
4 years ago
|
const { code, message, pos, start } = err;
|
||
8 years ago
|
try {
|
||
4 years ago
|
assert.deepEqual({ code, message, pos, start }, expectedError);
|
||
6 years ago
|
} catch (err2) {
|
||
|
const e = err2.code === 'MODULE_NOT_FOUND' ? err : err2;
|
||
|
throw e;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
});
|