From a6c523343d672895a87d205ab90eca988903cfb2 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 2 Sep 2023 12:53:48 +0800 Subject: [PATCH] chore: update test --- .gitignore | 2 + .npmignore | 10 +++-- .vscode/settings.json | 22 ++++++----- test.js | 29 -------------- {testdata => test_data}/comments.golden | 0 {testdata => test_data}/comments.input | 0 {testdata => test_data}/crlf.golden | 0 {testdata => test_data}/crlf.input | 26 ++++++------- {testdata => test_data}/go2numbers.golden | 0 {testdata => test_data}/go2numbers.input | 0 {testdata => test_data}/import.golden | 0 {testdata => test_data}/import.input | 0 {testdata => test_data}/issue28082.golden | 0 {testdata => test_data}/issue28082.input | 0 {testdata => test_data}/tabs.golden | 0 {testdata => test_data}/tabs.input | 0 {testdata => test_data}/typealias.golden | 0 {testdata => test_data}/typealias.input | 0 {testdata => test_data}/typeparams.golden | 0 {testdata => test_data}/typeparams.input | 0 {testdata => test_data}/typeswitch.golden | 0 {testdata => test_data}/typeswitch.input | 0 test_deno/deno.test.ts | 33 ++++++++++++++++ test_node/test-node.mjs | 46 +++++++++++++++++++++++ 24 files changed, 113 insertions(+), 55 deletions(-) delete mode 100644 test.js rename {testdata => test_data}/comments.golden (100%) rename {testdata => test_data}/comments.input (100%) rename {testdata => test_data}/crlf.golden (100%) rename {testdata => test_data}/crlf.input (94%) rename {testdata => test_data}/go2numbers.golden (100%) rename {testdata => test_data}/go2numbers.input (100%) rename {testdata => test_data}/import.golden (100%) rename {testdata => test_data}/import.input (100%) rename {testdata => test_data}/issue28082.golden (100%) rename {testdata => test_data}/issue28082.input (100%) rename {testdata => test_data}/tabs.golden (100%) rename {testdata => test_data}/tabs.input (100%) rename {testdata => test_data}/typealias.golden (100%) rename {testdata => test_data}/typealias.input (100%) rename {testdata => test_data}/typeparams.golden (100%) rename {testdata => test_data}/typeparams.input (100%) rename {testdata => test_data}/typeswitch.golden (100%) rename {testdata => test_data}/typeswitch.input (100%) create mode 100644 test_deno/deno.test.ts create mode 100644 test_node/test-node.mjs diff --git a/.gitignore b/.gitignore index bcb0549..76dea44 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ go.work gofmt.wasm *.tgz gofmt.js + +node_modules diff --git a/.npmignore b/.npmignore index 2ee477d..0af6e7d 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,7 @@ -.github -.vscode -testdata +test_* +src +node_modules *.tgz -*.patch \ No newline at end of file +*.sh +*.patch +.* diff --git a/.vscode/settings.json b/.vscode/settings.json index 781886e..ce96875 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,14 @@ { - "go.testEnvVars": { - "GOARCH": "wasm", - "GOOS": "js" - }, - "go.toolsEnvVars": { - "GOARCH": "wasm", - "GOOS": "js" - } -} \ No newline at end of file + "go.testEnvVars": { + "GOARCH": "wasm", + "GOOS": "js" + }, + "go.toolsEnvVars": { + "GOARCH": "wasm", + "GOOS": "js" + }, + "deno.enable": true, + "deno.enablePaths": [ + "test_deno" + ] +} diff --git a/test.js b/test.js deleted file mode 100644 index a5d37ae..0000000 --- a/test.js +++ /dev/null @@ -1,29 +0,0 @@ -import assert from "node:assert/strict"; -import fs from "node:fs/promises"; -import test from "node:test"; -import init, { format } from "./gofmt.js"; - -await init(); - -const files = (await fs.readdir("testdata")) - .filter((f) => f.endsWith(".input")) - .map((f) => { - return { - input_name: f, - golden_name: f.replace(".input", ".golden"), - }; - }); - -for (const { input_name, golden_name } of files) { - test(`format ${input_name}`, { skip: input_name[0] === "." }, async () => { - const [input, expected] = await Promise.all( - [input_name, golden_name].map((f) => - fs.readFile(`testdata/${f}`, "utf-8") - ) - ); - - const actual = format(input); - - assert.equal(actual, expected); - }); -} diff --git a/testdata/comments.golden b/test_data/comments.golden similarity index 100% rename from testdata/comments.golden rename to test_data/comments.golden diff --git a/testdata/comments.input b/test_data/comments.input similarity index 100% rename from testdata/comments.input rename to test_data/comments.input diff --git a/testdata/crlf.golden b/test_data/crlf.golden similarity index 100% rename from testdata/crlf.golden rename to test_data/crlf.golden diff --git a/testdata/crlf.input b/test_data/crlf.input similarity index 94% rename from testdata/crlf.input rename to test_data/crlf.input index 3cd4934..65de9cf 100644 --- a/testdata/crlf.input +++ b/test_data/crlf.input @@ -1,13 +1,13 @@ -/* -Source containing CR/LF line endings. -The gofmt'ed output must only have LF -line endings. -Test case for issue 3961. -*/ -package main - -func main() { - // line comment - println("hello, world!") // another line comment - println() -} +/* +Source containing CR/LF line endings. +The gofmt'ed output must only have LF +line endings. +Test case for issue 3961. +*/ +package main + +func main() { + // line comment + println("hello, world!") // another line comment + println() +} diff --git a/testdata/go2numbers.golden b/test_data/go2numbers.golden similarity index 100% rename from testdata/go2numbers.golden rename to test_data/go2numbers.golden diff --git a/testdata/go2numbers.input b/test_data/go2numbers.input similarity index 100% rename from testdata/go2numbers.input rename to test_data/go2numbers.input diff --git a/testdata/import.golden b/test_data/import.golden similarity index 100% rename from testdata/import.golden rename to test_data/import.golden diff --git a/testdata/import.input b/test_data/import.input similarity index 100% rename from testdata/import.input rename to test_data/import.input diff --git a/testdata/issue28082.golden b/test_data/issue28082.golden similarity index 100% rename from testdata/issue28082.golden rename to test_data/issue28082.golden diff --git a/testdata/issue28082.input b/test_data/issue28082.input similarity index 100% rename from testdata/issue28082.input rename to test_data/issue28082.input diff --git a/testdata/tabs.golden b/test_data/tabs.golden similarity index 100% rename from testdata/tabs.golden rename to test_data/tabs.golden diff --git a/testdata/tabs.input b/test_data/tabs.input similarity index 100% rename from testdata/tabs.input rename to test_data/tabs.input diff --git a/testdata/typealias.golden b/test_data/typealias.golden similarity index 100% rename from testdata/typealias.golden rename to test_data/typealias.golden diff --git a/testdata/typealias.input b/test_data/typealias.input similarity index 100% rename from testdata/typealias.input rename to test_data/typealias.input diff --git a/testdata/typeparams.golden b/test_data/typeparams.golden similarity index 100% rename from testdata/typeparams.golden rename to test_data/typeparams.golden diff --git a/testdata/typeparams.input b/test_data/typeparams.input similarity index 100% rename from testdata/typeparams.input rename to test_data/typeparams.input diff --git a/testdata/typeswitch.golden b/test_data/typeswitch.golden similarity index 100% rename from testdata/typeswitch.golden rename to test_data/typeswitch.golden diff --git a/testdata/typeswitch.input b/test_data/typeswitch.input similarity index 100% rename from testdata/typeswitch.input rename to test_data/typeswitch.input diff --git a/test_deno/deno.test.ts b/test_deno/deno.test.ts new file mode 100644 index 0000000..1f6d358 --- /dev/null +++ b/test_deno/deno.test.ts @@ -0,0 +1,33 @@ +import init, { format } from "../gofmt.js"; + +import { assertEquals } from "https://deno.land/std@0.201.0/assert/mod.ts"; +import { walk } from "https://deno.land/std@0.201.0/fs/walk.ts"; +import { relative } from "https://deno.land/std@0.201.0/path/mod.ts"; + +await init(); + +const update = Deno.args.includes("--update"); + +const test_root = new URL("../test_data", import.meta.url); + +for await (const entry of walk(test_root, { + includeDirs: false, + exts: ["input"], +})) { + const expect_path = entry.path.replace(/input$/, "golden"); + const input = Deno.readTextFileSync(entry.path); + + const actual = format(input); + + if (update) { + Deno.writeTextFileSync(expect_path, actual); + } else { + const expected = Deno.readTextFileSync(expect_path); + + const test_name = relative(test_root.pathname, entry.path); + + Deno.test(test_name, () => { + assertEquals(actual, expected); + }); + } +} diff --git a/test_node/test-node.mjs b/test_node/test-node.mjs new file mode 100644 index 0000000..6737937 --- /dev/null +++ b/test_node/test-node.mjs @@ -0,0 +1,46 @@ +import init, { format } from "../gofmt.js"; +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs/promises"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +await init(); + +/** + * @param {string} dir + * @returns {Generator} + */ +async function* walk(dir) { + for await (const d of await fs.opendir(dir)) { + const entry = path.join(dir, d.name); + if (d.isDirectory()) yield* walk(entry); + else if (d.isFile()) yield entry; + } +} + +const test_root = fileURLToPath(new URL("../test_data", import.meta.url)); + +for await (const input_path of walk(test_root)) { + const ext = path.extname(input_path); + + switch (ext) { + case ".input": + break; + + default: + continue; + } + + const test_name = path.relative(test_root, input_path); + const [input, expected] = await Promise.all([ + fs.readFile(input_path, { encoding: "utf-8" }), + fs.readFile(input_path.replace(ext, ".golden"), { encoding: "utf-8" }), + ]); + + const actual = format(input, input_path); + + test(test_name, () => { + assert.equal(actual, expected); + }); +}