Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
packages/svelte2tsx/*.d.ts
packages/svelte2tsx/test/**
packages/svelte2tsx/test/*/samples/*/*sx
packages/language-server/test/**/*.svelte
packages/svelte-vscode/syntaxes/*.yaml
.github/**
81 changes: 81 additions & 0 deletions packages/svelte2tsx/test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const fs = require('fs');
const assert = require('assert');

function benchmark(fn) {
return -Date.now() + (fn(), Date.now());
}

function readFileSync(path) {
return fs.existsSync(path)
? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '')
: null;
}

function check_dir(path, { allowed = [], required = allowed }) {
const unchecked = new Set(required);
const unknown = [];
loop: for (const fileName of fs.readdirSync(path)) {
for (const name of unchecked) {
if ('*' === name[0] ? fileName.endsWith(name.slice(1)) : name === fileName) {
unchecked.delete(name);
continue loop;
}
}
for (const name of allowed) {
if ('*' === name[0] ? fileName.endsWith(name.slice(1)) : name === fileName) {
continue loop;
}
}
unknown.push(fileName);
}
if (unknown.length !== 0) {
after(() => {
for (const name of unknown) {
const msg = `Unexpected file ${path.split('/').slice(-1)}/${name}`;
if (process.env.CI) {
throw new Error(msg);
} else {
console.info(msg);
}
}
});
}
if (unchecked.size !== 0) {
throw new Error(
`Expected file(s) ${[...unchecked].map((str) => `"${str}"`).join(', ')} in ${path}`
);
}
}

function test_samples(dir, transform, tsx) {
for (const testName of fs.readdirSync(`${dir}/samples`)) {
const path = `${dir}/samples/${testName}`;
const expected_path = `${path}/expected.${tsx}`;
const has_expected = fs.existsSync(expected_path);
const solo = testName.endsWith('.solo');
const skip = testName.startsWith('.');
check_dir(path, {
required: ['*.svelte'],
allowed: ['expected.js', `expected.${tsx}`]
});
(skip ? it.skip : solo ? it.only : it)(testName, function () {
const fileName = fs.readdirSync(path).find((f) => f.endsWith('.svelte'));
const output = transform(readFileSync(`${path}/${fileName}`), testName, fileName);
if (!has_expected) {
after(() => {
fs.writeFileSync(expected_path, output.code);
console.info(`Generated ${testName}/expected.${tsx}`);
});
this.skip();
} else {
assert.strictEqual(output.code, readFileSync(expected_path));
}
if (fs.existsSync(`${path}/expected.js`)) {
const run = require(`${path}/expected.js`);
run(output);
}
});
}
}

module.exports = { benchmark, test_samples };
28 changes: 4 additions & 24 deletions packages/svelte2tsx/test/htmlx2jsx/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
let converter = require('../build/htmlxtojsx')
let fs = require('fs')
let assert = require('assert')
const { htmlx2jsx } = require('../build/htmlxtojsx');
const { test_samples } = require('../helpers');

describe('htmlx2jsx', () => {
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);

if (solo && process.env.CI) {
throw new Error(
`Forgot to remove '.solo' from test parser/samples/${dir}`
);
}

(solo ? it.only : it)(dir, () => {
const input = fs.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, 'utf-8').replace(/\s+$/, '').replace(/\r\n/g, "\n");
const expectedOutput = fs.readFileSync(`${__dirname}/samples/${dir}/expected.jsx`, 'utf-8').replace(/\s+$/, '').replace(/\r\n/g, "\n");

const { map, code} = converter.htmlx2jsx(input);
assert.equal(code, expectedOutput);
});
});
});
test_samples(__dirname, htmlx2jsx, 'jsx');
});
25 changes: 13 additions & 12 deletions packages/svelte2tsx/test/htmlxparser/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
let converter = require('../build/htmlxtojsx')
let fs = require('fs')
let assert = require('assert')
const { htmlx2jsx } = require('../build/htmlxtojsx');
const assert = require('assert');
const { benchmark } = require('../helpers');

describe('htmlxparser', () => {
let content = fs.readFileSync(`${__dirname}/large.svelte`, {encoding: 'utf8'});

it('parses in a reasonable time', () => {
const start = new Date();
converter.htmlx2jsx(content);
const elapsed = new Date() - start;
assert(elapsed <= 1000, `Parsing took ${elapsed} ms, which was longer than 1000ms`);
})

});
let random = '';
let str = '';
for (let i = 0; i !== 17; i++) random += Math.random().toString(26).slice(2);
for (let i = 0; i !== 1137; i++) str += `${random} - line\t${i}\n`;
const duration = benchmark(
htmlx2jsx.bind(null, `<script> ${str} </script>` + `<style> ${str} </style>`)
);
assert(duration <= 1000, `Parsing took ${duration} ms, which was longer than 1000ms`);
});
});
Loading