1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { assert, beforeEach, describe, test } from 'vitest';
import {
normalizeString,
initIntlString,
normalizeStringWithHiragana,
compareStringEquals,
compareStringIncludes,
} from '@/utility/intl-string.js';
// 共通のテストを実行するヘルパー関数
const runCommonTests = (normalizeFn: (str: string) => string) => {
test('全角英数字が半角の小文字になる', () => {
// ローマ字にならないようにする
const input = 'B123';
const expected = 'b123';
assert.strictEqual(normalizeFn(input), expected);
});
test('濁点・半濁点が正しく結合される', () => {
const input = 'か\u3099';
const expected = 'が';
assert.strictEqual(normalizeFn(input), expected);
});
test('小文字に揃う', () => {
// ローマ字にならないようにする
const input = 'tSt';
const expected = 'tst';
assert.strictEqual(normalizeFn(input), expected);
});
test('文字列の前後の空白が削除される', () => {
const input = ' tst ';
const expected = 'tst';
assert.strictEqual(normalizeFn(input), expected);
});
};
describe('normalize string', () => {
runCommonTests(normalizeString);
test('異体字の正規化 (ligature)', () => {
const input = 'fi';
const expected = 'fi';
assert.strictEqual(normalizeString(input), expected);
});
test('半角カタカナは全角に変換される', () => {
const input = 'カタカナ';
const expected = 'カタカナ';
assert.strictEqual(normalizeString(input), expected);
});
});
// normalizeStringWithHiraganaのテスト
describe('normalize string with hiragana', () => {
beforeEach(async () => {
await initIntlString(true);
});
// 共通テスト
describe('共通のnormalizeStringテスト', () => {
runCommonTests(normalizeStringWithHiragana);
});
test('半角カタカナがひらがなに変換される', () => {
const input = 'カタカナ';
const expected = 'かたかな';
assert.strictEqual(normalizeStringWithHiragana(input), expected);
});
// normalizeStringWithHiragana特有のテスト
test('カタカナがひらがなに変換される・伸ばし棒はハイフンに変換される', () => {
const input = 'カタカナひーらがーな';
const expected = 'かたかなひ-らが-な';
assert.strictEqual(normalizeStringWithHiragana(input), expected);
});
test('ローマ字がひらがなに変換される', () => {
const input = 'ro-majimohiragananinarimasu';
const expected = 'ろ-まじもひらがなになります';
assert.strictEqual(normalizeStringWithHiragana(input), expected);
});
});
describe('compareStringEquals', () => {
beforeEach(async () => {
await initIntlString(true);
});
test('完全一致ならtrue', () => {
assert.isTrue(compareStringEquals('テスト', 'テスト'));
});
test('大文字・小文字の違いを無視', () => {
assert.isTrue(compareStringEquals('TeSt', 'test'));
});
test('全角・半角の違いを無視', () => {
assert.isTrue(compareStringEquals('ABC', 'abc'));
});
test('カタカナとひらがなの違いを無視', () => {
assert.isTrue(compareStringEquals('カタカナ', 'かたかな'));
});
test('ローマ字をひらがなと比較可能', () => {
assert.isTrue(compareStringEquals('hiragana', 'ひらがな'));
});
test('異なる文字列はfalse', () => {
assert.isFalse(compareStringEquals('テスト', 'サンプル'));
});
});
describe('compareStringIncludes', () => {
test('部分一致ならtrue', () => {
assert.isTrue(compareStringIncludes('これはテストです', 'テスト'));
});
test('大文字・小文字の違いを無視', () => {
assert.isTrue(compareStringIncludes('This is a Test', 'test'));
});
test('全角・半角の違いを無視', () => {
assert.isTrue(compareStringIncludes('ABCDE', 'abc'));
});
test('カタカナとひらがなの違いを無視', () => {
assert.isTrue(compareStringIncludes('カタカナのテスト', 'かたかな'));
});
test('ローマ字をひらがなと比較可能', () => {
assert.isTrue(compareStringIncludes('これはhiraganaのテスト', 'ひらがな'));
});
test('異なる文字列はfalse', () => {
assert.isFalse(compareStringIncludes('これはテストです', 'サンプル'));
});
});
|