Skip to content

Commit 3f09e81

Browse files
committed
[test] file generate test
1 parent cba4d7b commit 3f09e81

File tree

4 files changed

+82
-27
lines changed

4 files changed

+82
-27
lines changed

Diff for: lib/command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ModelMessage = { model: string; message: string };
5151
* @param input {string} : ユーザーの入力
5252
* @return {string} モデル名(@に続く文字列)
5353
*/
54-
const extractAtModel = (input: string): ModelMessage => {
54+
export const extractAtModel = (input: string): ModelMessage => {
5555
const match = input.match(/^@[^\s\n\t]+/);
5656
const model = match ? match[0].substring(1) : "";
5757
// matchでマッチした@modelName を削除したinput を割り当てる

Diff for: lib/params.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function parseArgs(): Params {
7171

7272
/** 引数となる配列の中から、
7373
* すべての-f フラグの次の引数を配列に格納する。
74-
* @params {string[]} - コマンドライン引数。通常Deno.args
74+
* @param {string[]} args - コマンドライン引数。通常Deno.args
7575
* @returns {string[]} - ファイルパスの配列
7676
* @throws - No file specified after -f option
7777
* @throws - Invalid file path after -f

Diff for: test/command_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ Deno.test("SlashCommand constructor", () => {
1717
});
1818

1919
Deno.test("ユーザーの入力が@から始まると、@に続くモデル名を返す", () => {
20-
const testCases: [string, string | undefined][] = [
20+
const testCases: [string, string][] = [
2121
["@modelName arg1 arg2", "modelName"], // 行頭に@が入るとモデル名を返す
22-
[" @modelName arg1 arg2", undefined], // 行頭にスペースが入ると@コマンドではない
23-
["plain text", undefined],
22+
[" @modelName arg1 arg2", ""], // 行頭にスペースが入ると@コマンドではない
23+
["plain text", ""],
2424
];
2525

2626
for (const [args, expected] of testCases) {

Diff for: test/file_test.ts

+77-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,82 @@
11
// deno test --allow-read --allow-write
22
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3-
import { parseFileContent } from "../lib/file.ts";
4-
5-
Deno.test("Read file contents test", async () => {
6-
const filePath = "./file1.txt";
7-
const expectedContent = "This is the content of file1.";
8-
9-
try {
10-
await Deno.writeTextFile(filePath, expectedContent);
11-
const fileContent = await parseFileContent(filePath);
12-
13-
assertEquals(
14-
fileContent.toString(),
15-
"```" + filePath + "\n" + expectedContent + "\n```",
16-
);
17-
} catch (error) {
18-
console.error("Error during test:", error);
19-
throw error;
20-
} finally { // Clean up test files
3+
import { exists } from "jsr:@std/fs/exists";
4+
import { expect } from "jsr:@std/expect";
5+
import {
6+
afterAll,
7+
beforeAll,
8+
describe,
9+
it,
10+
} from "https://deno.land/std/testing/bdd.ts";
11+
12+
import { filesGenerator, parseFileContent } from "../lib/file.ts";
13+
14+
describe("create test directory", async () => {
15+
const tempDir = new URL("./temp/", import.meta.url);
16+
beforeAll(async () => {
17+
// 絶対パスで一時ディレクトリを作成
18+
if (!await exists(tempDir)) {
19+
await Deno.mkdir(tempDir, { recursive: true });
20+
}
21+
const testFiles = ["file.txt", "dir", "test.js", "test.ts"];
22+
23+
// 一時ファイルを作成
24+
for (const file of testFiles) {
25+
const filePath = new URL(file, tempDir);
26+
await Deno.writeTextFile(filePath, "");
27+
}
28+
});
29+
30+
afterAll(() => {
31+
Deno.removeSync(tempDir, { recursive: true });
32+
});
33+
34+
it("Read file contents test", async () => {
35+
const filePath = "./file1.txt";
36+
const expectedContent = "This is the content of file1.";
37+
2138
try {
22-
await Deno.remove(filePath);
23-
} catch (cleanupError) {
24-
console.error("Error cleaning up test files:", cleanupError);
39+
await Deno.writeTextFile(filePath, expectedContent);
40+
const fileContent = await parseFileContent(filePath);
41+
42+
assertEquals(
43+
fileContent.toString(),
44+
"```" + filePath + "\n" + expectedContent + "\n```",
45+
);
46+
} catch (error) {
47+
console.error("Error during test:", error);
48+
throw error;
49+
} finally { // Clean up test files
50+
try {
51+
await Deno.remove(filePath);
52+
} catch (cleanupError) {
53+
console.error("Error cleaning up test files:", cleanupError);
54+
}
2555
}
26-
}
56+
});
57+
58+
it("非Globパターンはそのまま生成される", async () => {
59+
const patterns = ["test.js", "dir"];
60+
const generator = filesGenerator(patterns);
61+
const result: string[] = [];
62+
63+
for await (const file of generator) {
64+
result.push(file);
65+
}
66+
67+
assertEquals(result, ["test.js", "dir"]);
68+
});
69+
70+
it("Globパターンが正しく展開される", async () => {
71+
const patterns = ["./test/**/*.txt", "./test/**/*.js"];
72+
const generator = filesGenerator(patterns);
73+
const result: string[] = [];
74+
75+
for await (const file of generator) {
76+
result.push(file);
77+
}
78+
79+
expect(result).toContain(Deno.cwd() + "/test/temp/file.txt");
80+
expect(result).toContain(Deno.cwd() + "/test/temp/test.js");
81+
});
2782
});

0 commit comments

Comments
 (0)