|
| 1 | +/** |
| 2 | + * Vitest runner for onetrueawk spec tests |
| 3 | + * |
| 4 | + * This runs the imported spec tests from the onetrueawk project against just-bash's awk. |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from "node:fs"; |
| 8 | +import * as path from "node:path"; |
| 9 | +import { describe, expect, it } from "vitest"; |
| 10 | +import { parseAwkTestFile } from "./parser.js"; |
| 11 | +import { formatError, runAwkTestCase } from "./runner.js"; |
| 12 | +import { getSkipReason, isFileSkipped } from "./skips.js"; |
| 13 | + |
| 14 | +const CASES_DIR = path.join(__dirname, "cases"); |
| 15 | + |
| 16 | +// Get all T.* test files (the systematic test scripts) |
| 17 | +// Filter: starts with T., no file extension (no second dot) |
| 18 | +const ALL_TEST_FILES = fs |
| 19 | + .readdirSync(CASES_DIR) |
| 20 | + .filter((f) => f.startsWith("T.") && f.indexOf(".", 2) === -1) |
| 21 | + .sort(); |
| 22 | + |
| 23 | +// Filter out completely skipped files |
| 24 | +const TEST_FILES = ALL_TEST_FILES.filter((f) => !isFileSkipped(f)); |
| 25 | + |
| 26 | +/** |
| 27 | + * Truncate program for test name display |
| 28 | + */ |
| 29 | +function truncateProgram(program: string, maxLen = 50): string { |
| 30 | + const normalized = program |
| 31 | + .split("\n") |
| 32 | + .map((l) => l.trim()) |
| 33 | + .filter((l) => l) |
| 34 | + .join(" "); |
| 35 | + |
| 36 | + if (normalized.length <= maxLen) { |
| 37 | + return normalized; |
| 38 | + } |
| 39 | + return `${normalized.slice(0, maxLen - 3)}...`; |
| 40 | +} |
| 41 | + |
| 42 | +describe("OneTrue AWK Spec Tests", () => { |
| 43 | + for (const fileName of TEST_FILES) { |
| 44 | + const filePath = path.join(CASES_DIR, fileName); |
| 45 | + |
| 46 | + describe(fileName, () => { |
| 47 | + // Parse the test file |
| 48 | + const content = fs.readFileSync(filePath, "utf-8"); |
| 49 | + const parsed = parseAwkTestFile(content, filePath); |
| 50 | + |
| 51 | + // Skip files with no parseable tests |
| 52 | + if (parsed.testCases.length === 0) { |
| 53 | + it.skip("No parseable tests", () => {}); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + for (const testCase of parsed.testCases) { |
| 58 | + // Check for individual test skip (pass program for pattern matching) |
| 59 | + const skipReason = getSkipReason( |
| 60 | + fileName, |
| 61 | + testCase.name, |
| 62 | + testCase.program, |
| 63 | + ); |
| 64 | + if (skipReason) { |
| 65 | + testCase.skip = skipReason; |
| 66 | + } |
| 67 | + |
| 68 | + const programPreview = truncateProgram(testCase.program); |
| 69 | + const testName = `[L${testCase.lineNumber}] ${testCase.name}: ${programPreview}`; |
| 70 | + |
| 71 | + it(testName, async () => { |
| 72 | + const result = await runAwkTestCase(testCase); |
| 73 | + |
| 74 | + if (result.skipped) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (!result.passed) { |
| 79 | + expect.fail(formatError(result)); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | +}); |
0 commit comments