Skip to content

Commit

Permalink
refactor: bun test file
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 27, 2024
1 parent 875fbba commit df480d8
Showing 1 changed file with 16 additions and 34 deletions.
50 changes: 16 additions & 34 deletions test_bun/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
import { Glob } from "bun";
import { expect, test } from "bun:test";
import fs from "node:fs/promises";
import path from "node:path";
import init, { format } from "..";

await init();

async function* walk(dir: string): AsyncGenerator<string> {
for await (const d of await fs.readdir(dir)) {
const entry = path.join(dir, d);
const stat = await fs.stat(entry);

if (stat.isDirectory()) {
yield* walk(entry);
}

if (stat.isFile()) {
yield entry;
}
}
}

const test_root = Bun.fileURLToPath(new URL("../test_data", import.meta.url));
const glob = new Glob("**/*.input");

for await (const input_path of walk(test_root)) {
const ext = path.extname(input_path);

switch (ext) {
case ".input":
break;
for await (const input_path of glob.scan(test_root)) {
if (path.basename(input_path).startsWith(".")) {
continue;
}

default:
continue;
}
const full_path = path.join(test_root, input_path);
const expect_path = full_path.replace(".input", ".expect");

const test_name = path.relative(test_root, input_path);
const [input, expected] = await Promise.all([
Bun.file(input_path).text(),
Bun.file(input_path.replace(ext, ".expect")).text(),
]);
const [input, expected] = await Promise.all([
Bun.file(full_path).text(),
Bun.file(expect_path).text(),
]);

test(test_name, () => {
const actual = format(input);
expect(actual).toBe(expected);
});
test(input_path, () => {
const actual = format(input, input_path);
expect(actual).toBe(expected);
});
}

0 comments on commit df480d8

Please sign in to comment.