forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
135 lines (115 loc) · 3.47 KB
/
index.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as fs from 'fs';
import * as assert from 'assert';
import { svelte, loadConfig, tryToLoadJson } from '../helpers';
describe('validate', () => {
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
if (dir[0] === '.') return;
// add .solo to a sample directory name to only run that test
const solo = /\.solo/.test(dir);
const skip = /\.skip/.test(dir);
if (solo && process.env.CI) {
throw new Error('Forgot to remove `solo: true` from test');
}
(solo ? it.only : skip ? it.skip : it)(dir, () => {
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
const input = fs.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, 'utf-8').replace(/\s+$/, '').replace(/\r/g, '');
const expected_warnings = tryToLoadJson(`${__dirname}/samples/${dir}/warnings.json`) || [];
const expected_errors = tryToLoadJson(`${__dirname}/samples/${dir}/errors.json`);
const options = tryToLoadJson(`${__dirname}/samples/${dir}/options.json`);
let error;
try {
const { warnings } = svelte.compile(input, {
dev: config.dev,
legacy: config.legacy,
generate: false,
customElement: config.customElement,
...options
});
assert.deepEqual(warnings.map(w => ({
code: w.code,
message: w.message,
pos: w.pos,
start: w.start,
end: w.end
})), expected_warnings);
} catch (e) {
error = e;
}
const expected = expected_errors && expected_errors[0];
if (error || expected) {
if (error && !expected) {
throw error;
}
if (expected && !error) {
throw new Error(`Expected an error: ${expected.message}`);
}
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) {
console.error(error); // eslint-disable-line no-console
throw e;
}
}
});
});
it('errors if options.name is illegal', () => {
assert.throws(() => {
svelte.compile('<div></div>', {
name: 'not.valid',
generate: false
});
}, /options\.name must be a valid identifier/);
});
it('warns if options.name is not capitalised', () => {
const { warnings } = svelte.compile('<div></div>', {
name: 'lowercase',
generate: false
});
assert.deepEqual(warnings.map(w => ({
code: w.code,
message: w.message
})), [{
code: 'options-lowercase-name',
message: 'options.name should be capitalised'
}]);
});
it('does not warn if options.name begins with non-alphabetic character', () => {
const { warnings } = svelte.compile('<div></div>', {
name: '_',
generate: false
});
assert.deepEqual(warnings, []);
});
it('errors if namespace is provided but unrecognised', () => {
assert.throws(() => {
svelte.compile('<div></div>', {
name: 'test',
namespace: 'svefefe'
});
}, /Invalid namespace 'svefefe'/);
});
it('errors with a hint if namespace is provided but unrecognised but close', () => {
assert.throws(() => {
svelte.compile('<div></div>', {
name: 'test',
namespace: 'foriegn'
});
}, /Invalid namespace 'foriegn' \(did you mean 'foreign'\?\)/);
});
it('does not throw error if \'this\' is bound for foreign element', () => {
assert.doesNotThrow(() => {
svelte.compile(`
<script>
let whatever;
</script>
<div bind:this={whatever} />`, {
name: 'test',
namespace: 'foreign'
});
});
});
});