forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.js
75 lines (62 loc) · 2.01 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as fs from 'fs';
import assert from 'assert';
import { svelte, exists, tryToLoadJson } from './helpers.js';
describe( 'validate', () => {
fs.readdirSync( 'test/validator' ).forEach( dir => {
if ( dir[0] === '.' ) return;
const solo = exists( `test/validator/${dir}/solo` );
if ( solo && process.env.CI ) {
throw new Error( 'Forgot to remove `solo: true` from test' );
}
( solo ? it.only : it )( dir, () => {
const filename = `test/validator/${dir}/input.html`;
const input = fs.readFileSync( filename, 'utf-8' ).replace( /\s+$/, '' );
try {
const parsed = svelte.parse( input );
const errors = [];
const warnings = [];
const { names } = svelte.validate( parsed, input, {
onerror ( error ) {
errors.push({
message: error.message,
pos: error.pos,
loc: error.loc
});
},
onwarn ( warning ) {
warnings.push({
message: warning.message,
pos: warning.pos,
loc: warning.loc
});
}
});
const expectedErrors = tryToLoadJson( `test/validator/${dir}/errors.json` ) || [];
const expectedWarnings = tryToLoadJson( `test/validator/${dir}/warnings.json` ) || [];
const expectedNames = tryToLoadJson( `test/validator/${dir}/names.json` );
assert.deepEqual( errors, expectedErrors );
assert.deepEqual( warnings, expectedWarnings );
if ( expectedNames ) {
assert.deepEqual( names, expectedNames );
}
} catch ( err ) {
if ( err.name !== 'ParseError' ) throw err;
try {
const expected = require( `./validator/${dir}/errors.json` )[0];
assert.equal( err.message, expected.message );
assert.deepEqual( err.loc, expected.loc );
assert.equal( err.pos, expected.pos );
} catch ( err2 ) {
throw err2.code === 'MODULE_NOT_FOUND' ? err : err2;
}
}
});
});
it( 'errors if options.name is illegal', () => {
assert.throws( () => {
svelte.compile( '<div></div>', {
name: 'not.valid'
});
}, /options\.name must be a valid identifier/ );
});
});