2022-12-20 03:14:34 +02:00
|
|
|
|
import { readFile } from 'node:fs/promises';
|
2023-11-14 20:32:00 +01:00
|
|
|
|
import { readdirSync } from 'node:fs';
|
2023-02-16 18:14:05 -08:00
|
|
|
|
import { exec, jcoPath } from './helpers.js';
|
2022-12-20 03:14:34 +02:00
|
|
|
|
import { strictEqual } from 'node:assert';
|
2023-10-13 15:51:04 -07:00
|
|
|
|
import { componentNew, componentEmbed, transpile } from '@bytecodealliance/jco';
|
|
|
|
|
|
import { ok } from 'node:assert';
|
2022-12-20 03:14:34 +02:00
|
|
|
|
|
2023-10-12 16:26:11 -07:00
|
|
|
|
const eslintPath = `node_modules/eslint/bin/eslint.js`;
|
2023-08-18 17:35:55 -07:00
|
|
|
|
|
2022-12-20 03:14:34 +02:00
|
|
|
|
export async function readFlags (fixture) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
var source = await readFile(fixture, 'utf8');
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
if (e && e.code === 'ENOENT')
|
|
|
|
|
|
return [];
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const firstLine = source.split('\n')[0];
|
|
|
|
|
|
if (firstLine.startsWith('// Flags:'))
|
|
|
|
|
|
return firstLine.slice(9).trim().split(' ');
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function codegenTest (fixtures) {
|
|
|
|
|
|
suite(`Transpiler codegen`, () => {
|
|
|
|
|
|
for (const fixture of fixtures) {
|
2024-11-21 14:43:10 -08:00
|
|
|
|
const testName = fixture.replace(/(\.component)?\.(wasm|wat)$/, '');
|
2022-12-22 01:38:35 +02:00
|
|
|
|
|
2024-11-21 14:43:10 -08:00
|
|
|
|
test(`${testName} transpile`, async () => {
|
|
|
|
|
|
const flags = await readFlags(`test/runtime/${testName}.ts`);
|
|
|
|
|
|
var { stderr } = await exec(jcoPath, 'transpile', `test/fixtures/components/${fixture}`, '--name', testName, ...flags, '-o', `test/output/${testName}`);
|
|
|
|
|
|
strictEqual(stderr, '');
|
|
|
|
|
|
});
|
2023-11-14 20:32:00 +01:00
|
|
|
|
|
2024-11-21 14:43:10 -08:00
|
|
|
|
test(`${testName} lint`, async () => {
|
|
|
|
|
|
const flags = await readFlags(`test/runtime/${testName}.ts`);
|
2023-11-14 20:32:00 +01:00
|
|
|
|
|
2024-11-21 14:43:10 -08:00
|
|
|
|
if (flags.includes('--js'))
|
|
|
|
|
|
return;
|
2023-11-14 20:32:00 +01:00
|
|
|
|
|
2024-11-21 14:43:10 -08:00
|
|
|
|
var { stderr } = await exec(eslintPath, `test/output/${testName}/${testName}.js`, '-c', 'test/eslintrc.cjs');
|
|
|
|
|
|
strictEqual(stderr, '');
|
|
|
|
|
|
});
|
2022-12-20 03:14:34 +02:00
|
|
|
|
}
|
2023-03-23 15:33:09 -07:00
|
|
|
|
});
|
2023-10-13 15:51:04 -07:00
|
|
|
|
|
|
|
|
|
|
suite(`Naming`, () => {
|
|
|
|
|
|
test(`Resource deduping`, async () => {
|
|
|
|
|
|
const component = await componentNew(await componentEmbed({
|
|
|
|
|
|
witSource: await readFile(`test/fixtures/wits/resource-naming/resource-naming.wit`, 'utf8'),
|
|
|
|
|
|
dummy: true,
|
|
|
|
|
|
metadata: [['language', [['javascript', '']]], ['processed-by', [['dummy-gen', 'test']]]]
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
const { files } = await transpile(component, { name: 'resource-naming' });
|
|
|
|
|
|
|
|
|
|
|
|
const bindingsSource = new TextDecoder().decode(files['resource-naming.js']);
|
|
|
|
|
|
|
2024-01-15 10:01:41 -08:00
|
|
|
|
ok(bindingsSource.includes('class Thing$1{'));
|
2023-10-13 15:51:04 -07:00
|
|
|
|
ok(bindingsSource.includes('Thing: Thing$1'));
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2022-12-20 03:14:34 +02:00
|
|
|
|
}
|