diff --git a/.gitignore b/.gitignore index 441b859..bd9469a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ /docs/.vitepress/dist /docs/.vitepress/cache + +/tests/e2e/*/tmp diff --git a/tests/e2e/assertFailed/as-test.config.js b/tests/e2e/assertFailed/as-test.config.js new file mode 100644 index 0000000..48c78b0 --- /dev/null +++ b/tests/e2e/assertFailed/as-test.config.js @@ -0,0 +1,19 @@ +import path from "node:path"; + +const __dirname = path.dirname(new URL(import.meta.url).pathname); + +export default { + include: [__dirname], + imports(runtime) { + return { + env: { + log: (msg) => { + runtime.framework.log(runtime.exports.__getString(msg)); + }, + }, + }; + }, + temp: path.join(__dirname, "tmp"), + output: path.join(__dirname, "tmp"), + mode: [], +}; diff --git a/tests/e2e/assertFailed/env.ts b/tests/e2e/assertFailed/env.ts new file mode 100644 index 0000000..b8c4440 --- /dev/null +++ b/tests/e2e/assertFailed/env.ts @@ -0,0 +1 @@ +export declare function log(msg: string): void; diff --git a/tests/e2e/assertFailed/source.test.ts b/tests/e2e/assertFailed/source.test.ts new file mode 100644 index 0000000..52cc3ae --- /dev/null +++ b/tests/e2e/assertFailed/source.test.ts @@ -0,0 +1,12 @@ +import { test, expect } from "../../../assembly"; +import { log } from "./env"; + +test("failed test", () => { + log("This is a log message for the failed test."); + assert(false, "This is AS assert failure"); +}); + +test("succeed test", () => { + log("This is a log message for the succeed test."); + expect(1 + 1).equal(2); +}); diff --git a/tests/e2e/assertFailed/stdout.txt b/tests/e2e/assertFailed/stdout.txt new file mode 100644 index 0000000..0adbf7c --- /dev/null +++ b/tests/e2e/assertFailed/stdout.txt @@ -0,0 +1,3 @@ +code analysis: OK +compile testcases: OK +instrument: OK diff --git a/tests/e2e/assertFailed/tsconfig.json b/tests/e2e/assertFailed/tsconfig.json new file mode 100644 index 0000000..798b474 --- /dev/null +++ b/tests/e2e/assertFailed/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "assemblyscript/std/assembly.json", + "include": ["./**/*.ts"] +} diff --git a/tests/e2e/printLogInFailedInfo/as-test.config.js b/tests/e2e/printLogInFailedInfo/as-test.config.js index 2a55a40..6a4e6f8 100644 --- a/tests/e2e/printLogInFailedInfo/as-test.config.js +++ b/tests/e2e/printLogInFailedInfo/as-test.config.js @@ -1,10 +1,12 @@ import os from "node:os"; import path from "node:path"; +import { URL } from "node:url"; const tmpFolder = path.join(os.tmpdir(), "as-test-e2e"); +const __dirname = path.dirname(new URL(import.meta.url).pathname); export default { - include: ["tests/e2e/printLogInFailedInfo"], + include: [__dirname], imports(runtime) { return { env: { @@ -14,7 +16,7 @@ export default { }, }; }, - temp: tmpFolder, - output: tmpFolder, + temp: path.join(__dirname, "tmp"), + output: path.join(__dirname, "tmp"), mode: [], }; diff --git a/tests/e2e/printLogInFailedInfo/stdout.txt b/tests/e2e/printLogInFailedInfo/stdout.txt new file mode 100644 index 0000000..34bd3c2 --- /dev/null +++ b/tests/e2e/printLogInFailedInfo/stdout.txt @@ -0,0 +1,11 @@ +code analysis: OK +compile testcases: OK +instrument: OK +execute testcases: OK + +test case: 1/2 (success/total) + +Error Message: + failed test: + tests/e2e/printLogInFailedInfo/source.test.ts:6:2 value: 2 expect: = 3 +This is a log message for the failed test. diff --git a/tests/e2e/run.js b/tests/e2e/run.js index dea54df..0dc5ae1 100644 --- a/tests/e2e/run.js +++ b/tests/e2e/run.js @@ -2,41 +2,58 @@ import assert from "node:assert"; import { exec } from "node:child_process"; import { diffChars } from "diff"; import chalk from "chalk"; +import { argv } from "node:process"; +import { readFileSync } from "node:fs"; -function assertStringEq(s1, s2) { - const parts = diffChars(s1, s2); - const diffs = []; - let hasDiff = false; - for (const part of parts) { - if (part.added) { - hasDiff = true; - diffs.push(chalk.bgGreen(part.value)); - } else if (part.removed) { - hasDiff = true; - diffs.push(chalk.bgRed(part.value)); - } else { - diffs.push(part.value); - } - } - assert(!hasDiff, diffs.join("")); +function getDiff(s1, s2) { + const handleEscape = (c) => { + if (c === "\n") return "\n'\\n'"; + return c; + }; + return diffChars(s1, s2) + .map((part) => { + if (part.added) { + return chalk.bgGreen(handleEscape(part.value)); + } else if (part.removed) { + return chalk.bgRed(handleEscape(part.value)); + } else { + return part.value; + } + }) + .join(""); } -console.log("Running e2e test: printLogInFailedInfo"); -exec("node ./bin/as-test.js --config tests/e2e/printLogInFailedInfo/as-test.config.js", (error, stdout, stderr) => { - assert(error.code === 255); - const expectStdOut = ` -code analysis: OK -compile testcases: OK -instrument: OK -execute testcases: OK +function isEnabled(name) { + const enabledTests = argv.slice(2); + if (enabledTests.length === 0) { + return true; // Run all tests by default + } + return enabledTests.includes(name); +} -test case: 1/2 (success/total) +function runEndToEndTest(name, handle) { + if (isEnabled(name)) { + console.log(`Running e2e test: ${name}`); + exec(`node ./bin/as-test.js --config tests/e2e/${name}/as-test.config.js`, (error, stdout, stderr) => { + // standard check + const expectStdOut = readFileSync(`tests/e2e/${name}/stdout.txt`, "utf-8"); + if (expectStdOut !== stdout) { + console.log(`=========STDOUT ${name}=========`); + console.log(getDiff(expectStdOut, stdout)); + console.log(`=========STDERR ${name}=========`); + console.log(stderr); + process.exit(1); + } + // customize check + handle(error, stdout, stderr); + }); + } +} -Error Message: - failed test: - tests/e2e/printLogInFailedInfo/source.test.ts:6:2 value: 2 expect: = 3 -This is a log message for the failed test. -`.trimStart(); +runEndToEndTest("printLogInFailedInfo", (error, stdout, stderr) => { + assert(error.code === 255); +}); - assertStringEq(expectStdOut, stdout); +runEndToEndTest("assertFailed", (error, stdout, stderr) => { + assert(error.code === 255); });