|
| 1 | +const fs = require('fs'); |
| 2 | +const assert = require('assert'); |
| 3 | + |
| 4 | +function benchmark(fn) { |
| 5 | + return -Date.now() + (fn(), Date.now()); |
| 6 | +} |
| 7 | + |
| 8 | +function readFileSync(path) { |
| 9 | + return fs.existsSync(path) |
| 10 | + ? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '') |
| 11 | + : null; |
| 12 | +} |
| 13 | + |
| 14 | +function check_dir(path, { allowed = [], required = allowed }) { |
| 15 | + const unchecked = new Set(required); |
| 16 | + const unknown = []; |
| 17 | + loop: for (const fileName of fs.readdirSync(path)) { |
| 18 | + for (const name of unchecked) { |
| 19 | + if ('*' === name[0] ? fileName.endsWith(name.slice(1)) : name === fileName) { |
| 20 | + unchecked.delete(name); |
| 21 | + continue loop; |
| 22 | + } |
| 23 | + } |
| 24 | + for (const name of allowed) { |
| 25 | + if ('*' === name[0] ? fileName.endsWith(name.slice(1)) : name === fileName) { |
| 26 | + continue loop; |
| 27 | + } |
| 28 | + } |
| 29 | + unknown.push(fileName); |
| 30 | + } |
| 31 | + if (unknown.length !== 0) { |
| 32 | + after(() => { |
| 33 | + for (const name of unknown) { |
| 34 | + const msg = `Unexpected file ${path.split('/').slice(-1)}/${name}`; |
| 35 | + if (process.env.CI) { |
| 36 | + throw new Error(msg); |
| 37 | + } else { |
| 38 | + console.info(msg); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + if (unchecked.size !== 0) { |
| 44 | + throw new Error( |
| 45 | + `Expected file(s) ${[...unchecked].map((str) => `"${str}"`).join(', ')} in ${path}` |
| 46 | + ); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function test_samples(dir, transform, tsx) { |
| 51 | + for (const testName of fs.readdirSync(`${dir}/samples`)) { |
| 52 | + const path = `${dir}/samples/${testName}`; |
| 53 | + const expected_path = `${path}/expected.${tsx}`; |
| 54 | + const has_expected = fs.existsSync(expected_path); |
| 55 | + const solo = testName.endsWith('.solo'); |
| 56 | + const skip = testName.startsWith('.'); |
| 57 | + check_dir(path, { |
| 58 | + required: ['*.svelte'], |
| 59 | + allowed: ['expected.js', `expected.${tsx}`] |
| 60 | + }); |
| 61 | + (skip ? it.skip : solo ? it.only : it)(testName, function () { |
| 62 | + const fileName = fs.readdirSync(path).find((f) => f.endsWith('.svelte')); |
| 63 | + const output = transform(readFileSync(`${path}/${fileName}`), testName, fileName); |
| 64 | + if (!has_expected) { |
| 65 | + after(() => { |
| 66 | + fs.writeFileSync(expected_path, output.code); |
| 67 | + console.info(`Generated ${testName}/expected.${tsx}`); |
| 68 | + }); |
| 69 | + this.skip(); |
| 70 | + } else { |
| 71 | + assert.strictEqual(output.code, readFileSync(expected_path)); |
| 72 | + } |
| 73 | + if (fs.existsSync(`${path}/expected.js`)) { |
| 74 | + const run = require(`${path}/expected.js`); |
| 75 | + run(output); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = { benchmark, test_samples }; |
0 commit comments