Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize tests for expected errors in samples #869

Merged
merged 41 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
09ba333
init
pushkine Jan 18, 2021
91e7009
Merge remote-tracking branch 'upstream/master'
pushkine Feb 14, 2021
5924cf3
Merge remote-tracking branch 'upstream/master'
pushkine Feb 22, 2021
8d23f64
init
pushkine Feb 25, 2021
1d4a307
indent using spaces
pushkine Feb 25, 2021
2a62d01
end with breakline
pushkine Feb 25, 2021
bd8c1bf
yarn lock
pushkine Feb 25, 2021
d245308
Merge remote-tracking branch 'upstream/master'
pushkine Mar 8, 2021
3860741
init
pushkine Mar 8, 2021
dc56d37
fix
pushkine Mar 8, 2021
3732855
esm
pushkine Mar 8, 2021
6b3cd1a
fix
pushkine Mar 9, 2021
2868216
Merge branch 'typescript-tests-2' into typescript-tests
pushkine Mar 9, 2021
fa4862e
Update yarn.lock
pushkine Mar 9, 2021
51ceb0c
Merge remote-tracking branch 'upstream/master'
pushkine Mar 9, 2021
1eece76
Merge remote-tracking branch 'upstream/master' into typescript-tests
pushkine Mar 9, 2021
349f64d
Update rollup.config.test.js
pushkine Mar 9, 2021
6a52674
Update rollup.config.test.js
pushkine Mar 9, 2021
7196cc7
Update rollup.config.test.js
pushkine Mar 9, 2021
74c6c42
update
pushkine Mar 9, 2021
56f883b
Merge branch 'test-build-dts' into typescript-tests
pushkine Mar 9, 2021
2a6e1c0
Update package.json
pushkine Mar 9, 2021
ea48a96
Update index.ts
pushkine Mar 9, 2021
3233908
Update helpers.ts
pushkine Mar 9, 2021
abacc06
init
pushkine Mar 9, 2021
047d94f
Update helpers.ts
pushkine Mar 9, 2021
8db7186
Merge branch 'master' into share-sample-logic
pushkine Mar 9, 2021
0f42fb2
Merge remote-tracking branch 'upstream/master'
pushkine Mar 9, 2021
72ca6bb
Merge branch 'master' into share-sample-logic
pushkine Mar 9, 2021
397c56d
Revert "Merge branch 'master' into share-sample-logic"
pushkine Mar 9, 2021
93e794f
update
pushkine Mar 9, 2021
afdb5f0
Update helpers.ts
pushkine Mar 9, 2021
5027748
Update helpers.ts
pushkine Mar 9, 2021
68fa7c2
Update index.ts
pushkine Mar 9, 2021
e22abb8
Update helpers.ts
pushkine Mar 9, 2021
7ba07be
Merge branch 'master' into typescript-tests
pushkine Mar 9, 2021
6fe74c8
Merge branch 'share-sample-logic' into typescript-tests
pushkine Mar 9, 2021
4f32a8f
Merge remote-tracking branch 'upstream/master' into normalize-test-er…
pushkine Mar 10, 2021
a31f0f2
Update helpers.ts
pushkine Mar 10, 2021
3d7facd
inline
pushkine Mar 10, 2021
9730c5e
code style
dummdidumm Mar 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions packages/svelte2tsx/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function benchmark(fn: () => void) {
return -Date.now() + (fn(), Date.now());
}

export function readFileSync(path: string) {
function readFileSync(path: string) {
return fs.existsSync(path)
? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '')
: null;
Expand Down Expand Up @@ -122,35 +122,54 @@ type TransformSampleFn = (
}
) => ReturnType<typeof htmlx2jsx | typeof svelte2tsx>;

export function test_samples(dir: string, transform: TransformSampleFn, tsx: 'jsx' | 'tsx') {
export function test_samples(dir: string, transform: TransformSampleFn, jsx: 'jsx' | 'tsx') {
for (const sample of each_sample(dir)) {
const svelteFile = sample.folder.find((f) => f.endsWith('.svelte'));

sample.check_dir({
required: ['*.svelte'],
allowed: ['expected.js', `expected.${tsx}`, 'test.js']
allowed: ['expected.js', `expected.${jsx}`, 'expected.error.json']
});

const shouldGenerateExpected = !sample.has(`expected.${tsx}`);
const shouldGenerateExpected = !sample.has(`expected.${jsx}`);
const shouldGenerateError = sample.get('expected.error.json') === '';

sample.it(function () {
if (sample.has('test.js')) {
sample.eval('test.js');
return;
}
const input = sample.get(svelteFile);
const config = {
fileName: svelteFile,
sampleName: sample.name,
emitOnTemplateError: false
};

if (sample.has('expected.error.json')) {
let hadError = false;
try {
transform(input, config);
} catch (error) {
hadError = true;
if (shouldGenerateError) {
sample.generate(
'expected.error.json',
JSON.stringify(error, null, 4) + '\n'
);
} else {
assert.deepEqual(
JSON.parse(sample.get('expected.error.json')),
JSON.parse(JSON.stringify(error))
);
}
}
config.emitOnTemplateError = true;
assert(hadError, `Expected a template error but got none`);
}

const output = transform(input, config);

if (shouldGenerateExpected) {
sample.generate(`expected.${tsx}`, output.code);
sample.generate(`expected.${jsx}`, output.code);
} else {
assert.strictEqual(output.code, sample.get(`expected.${tsx}`));
assert.strictEqual(output.code, sample.get(`expected.${jsx}`));
}

if (sample.has('expected.js')) {
Expand All @@ -165,13 +184,3 @@ export function* each_sample(dir: string) {
yield new Sample(dir, name);
}
}

/**
*
* @param {string} dirPath
*/
export function get_input_content(dirPath) {
const filename = fs.readdirSync(dirPath).find((f) => f.endsWith('.svelte'));
const content = readFileSync(`${dirPath}/${filename}`);
return { filename, content };
}
8 changes: 7 additions & 1 deletion packages/svelte2tsx/test/htmlx2jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ import { htmlx2jsx } from '../build/htmlxtojsx';
import { test_samples } from '../helpers';

describe('htmlx2jsx', () => {
test_samples(__dirname, htmlx2jsx, 'jsx');
test_samples(
__dirname,
(input, { emitOnTemplateError }) => {
return htmlx2jsx(input, { emitOnTemplateError });
},
'jsx'
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ParseError",
"code": "parse-error",
"start": {
"line": 1,
"column": 8,
"character": 8
},
"end": {
"line": 1,
"column": 8,
"character": 8
},
"pos": 8,
"frame": "1: {abc. }\n ^\n2: {abc?. }\n3: {abc ?}"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ParseError",
"code": "parse-error",
"start": {
"line": 1,
"column": 4,
"character": 4
},
"end": {
"line": 1,
"column": 4,
"character": 4
},
"pos": 4,
"frame": "1: {a?.}\n ^"
}

This file was deleted.