Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Sep 2, 2023
1 parent 57044da commit a6c5233
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ go.work
gofmt.wasm
*.tgz
gofmt.js

node_modules
10 changes: 6 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.github
.vscode
testdata
test_*
src
node_modules
*.tgz
*.patch
*.sh
*.patch
.*
22 changes: 13 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"go.testEnvVars": {
"GOARCH": "wasm",
"GOOS": "js"
},
"go.toolsEnvVars": {
"GOARCH": "wasm",
"GOOS": "js"
}
}
"go.testEnvVars": {
"GOARCH": "wasm",
"GOOS": "js"
},
"go.toolsEnvVars": {
"GOARCH": "wasm",
"GOOS": "js"
},
"deno.enable": true,
"deno.enablePaths": [
"test_deno"
]
}
29 changes: 0 additions & 29 deletions test.js

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 13 additions & 13 deletions testdata/crlf.input → test_data/crlf.input
Original file line number Diff line number Diff line change
@@ -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()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions test_deno/deno.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
46 changes: 46 additions & 0 deletions test_node/test-node.mjs
Original file line number Diff line number Diff line change
@@ -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<string>}
*/
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);
});
}

0 comments on commit a6c5233

Please sign in to comment.