Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PRに適切なラベルを付与してください

- 冷笑のパターンを追加する場合
- [./src/lib/patterns.ts](./src/lib/patterns.ts) の `patterns` 配列に `PatternDefinition` を1つ追加してください(`id` / `strict` / `source` / `samples`)
- `samples` に書いたサンプル文字列は自動でテスト化されます([test/patterns.test.ts](./test/patterns.test.ts))。個別にテストコードを書く必要はありません
- `samples` に書いたサンプル文字列は自動でテスト化されます([test/index.test.ts](./test/index.test.ts))。個別にテストコードを書く必要はありません
- 可能であれば **ひらがな** , **カタカナ** , **半角カナ** , **小文字** も同様に含めてください
- 追加するパターンが明らかに冷笑な場合は `strict: true` に、冷笑か怪しい場合や文脈によっては冷笑ではないパターンは `strict: false` (relaxedモードでのみ検知)にしてください

Expand Down
48 changes: 0 additions & 48 deletions test/findMatches.test.ts

This file was deleted.

84 changes: 84 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, test, expect } from "vitest";
import { findAll, contains, findMatches, patterns } from "../src/index";

describe("findAll / contains", () => {
test("基本的な検出", () => {
expect(contains("うおw")).toBe(true);
expect(contains("普通の文章です")).toBe(false);
});

test("relaxedモードでのみ検知するパターン", () => {
expect(contains("いや💦")).toBe(false);
expect(contains("いや💦", { relaxed: true })).toBe(true);
});

test("(笑)は語幹込みで一致する(旧実装では語幹が欠落するバグがあった)", () => {
expect(findAll("うお(笑)")).toEqual(["うお(笑)"]);
expect(findAll("どわー(笑)")).toEqual(["どわー(笑)"]);
});

test("複数マッチ", () => {
expect(findAll("うおうおうおw、爆笑爆笑")).toEqual(["うおw", "爆笑爆笑"]);
expect(findAll("うおうおうおw、爆笑爆笑", { relaxed: true })).toEqual([
"うお",
"うお",
"うおw",
"爆笑爆笑",
]);
});

test("不正な引数はTypeErrorを投げる", () => {
// @ts-expect-error 意図的に不正な型を渡す
expect(() => findAll(123)).toThrow(TypeError);
// @ts-expect-error 意図的に不正な型を渡す
expect(() => contains("text", "invalid")).toThrow(TypeError);
});
});

describe("findMatches", () => {
test("マッチしたパターンのidと位置を返す", () => {
expect(findMatches("うおうおうおw、爆笑爆笑")).toEqual([
{ text: "うおw", index: 4, patternId: "stem-uo", strict: true },
{
text: "爆笑爆笑",
index: 8,
patternId: "repeat-bakushou",
strict: true,
},
]);
});

test("見つからなければnullを返す", () => {
expect(findMatches("普通の文章です")).toBeNull();
});

test("relaxed指定時のみrelaxed専用パターンが含まれる", () => {
expect(findMatches("いや💦")).toBeNull();
const result = findMatches("いや💦", { relaxed: true });
expect(
result?.every((m) => m.patternId === "emoji-sweat-drop" && !m.strict)
).toBe(true);
});
});

// patterns.ts に PatternDefinition を追加するだけで、そのsamplesが自動的に
// テスト対象になる(strict/relaxedの分類と、findAll側/findMatches側の両方の
// 検出経路を検証する)。
describe("パターン別サンプル", () => {
for (const p of patterns) {
describe(`[${p.id}]`, () => {
for (const sample of p.samples) {
test(`strict -> ${sample}`, () => {
expect(contains(sample)).toBe(p.strict);
});
test(`relaxed -> ${sample}`, () => {
expect(contains(sample, { relaxed: true })).toBe(true);
});
test(`findMatchesにも同じidが含まれる -> ${sample}`, () => {
const matches = findMatches(sample, { relaxed: true }) ?? [];
expect(matches.some((m) => m.patternId === p.id)).toBe(true);
});
}
});
}
});
18 changes: 0 additions & 18 deletions test/patterns.test.ts

This file was deleted.

77 changes: 0 additions & 77 deletions test/regex.test.ts

This file was deleted.

Loading