-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregularuse_test.go
312 lines (302 loc) · 7.32 KB
/
regularuse_test.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package kanji
import (
"bufio"
"os"
"strconv"
"strings"
"testing"
)
func TestIs_Golden(t *testing.T) {
f, err := os.Open("./testdata/golden_jyouyou_H22-11-30.csv")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer f.Close()
s := bufio.NewScanner(f)
var line int
for s.Scan() {
line++
txt := s.Text()
if txt == "" {
t.Errorf("invalid golden data, line=%d, %s", line, txt)
continue
}
ary := strings.Split(txt, "\t")
if len(ary) == 0 {
t.Errorf("invalid golden data, line=%d, %s", line, txt)
continue
}
target := []rune(ary[0])
for i, v := range target {
if v == '[' || v == ']' || v == '(' || v == ')' { // 餅[餅](餠)
continue
}
if !IsRegularUse(v) {
t.Errorf("line=%d, want IsRegularUse(%s)=true, got false", line, string(v))
}
if i == 0 {
if !IsStandardRegularUse(v) {
t.Errorf("line=%d, want IsStandardRegularUse(%s)=true, got false", line, string(v))
}
} else if target[i-1] == '(' {
if !IsOldFormRegularUse(v) {
t.Errorf("line=%d, want IsOldFormRegularUse(%s)=true, got false", line, string(v))
}
} else if target[i-1] == '[' {
if !IsTolerableRegularUse(v) {
t.Errorf("line=%d, want IsTolerableRegularUse(%s)=true, got false", line, string(v))
}
}
}
}
if err := s.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestIsOldFormRegularUse(t *testing.T) {
f, err := os.Open("./testdata/golden_old-new.txt")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer f.Close()
s := bufio.NewScanner(f)
var line int
for s.Scan() {
l := s.Text()
line++
if strings.HasPrefix(l, "!") {
continue
}
a := strings.Split(l, " ")
if len(a) != 4 {
t.Errorf("invalid golden file, line=%d, %s", line, l)
continue
}
code, err := strconv.ParseInt(a[0], 16, strconv.IntSize)
if err != nil {
t.Errorf("invalid golden file, line=%d, parse int error %v, %s", line, err, l)
continue
}
r := rune(code)
if !IsOldFormRegularUse(r) {
t.Errorf("expected IsOldFormRegularUse(%c [%X])=true, but false. line=%d", r, r, line)
}
}
if err := s.Err(); err != nil {
t.Errorf("unexpected error, %v", err)
}
}
func TestIsNotRegularUse(t *testing.T) {
tests := []struct {
name string
args string
want bool
}{
{
name: "OK",
args: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
want: false,
},
{
name: "NG",
args: "勺錘銑脹匁",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, v := range tt.args {
if got := IsNotRegularUse(v); got != tt.want {
t.Errorf("IsNotRegularUse(%c) = %v, want %v", v, got, tt.want)
}
}
})
}
}
func TestIsRegularUse(t *testing.T) {
tests := []struct {
name string
args string
want bool
}{
{
name: "regular-use kanji characters",
args: "常用漢字挨曖宛嵐畏萎椅彙茨",
want: true,
},
{
name: "not kanji characters",
args: "ひらがなカタカナ123😀",
want: false,
},
{
name: "old form or variant kanji characters",
args: "亞惡壓餌遡雙壯遜膽",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, v := range tt.args {
if got := IsRegularUse(v); got != tt.want {
t.Errorf("IsRegularUse(%c) = %v, want %v", v, got, tt.want)
}
}
})
}
}
func TestReplaceNotRegularUseAll(t *testing.T) {
type args struct {
s string
replacement string
}
tests := []struct {
name string
args args
want string
}{
{
name: "no matching",
args: args{
s: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
replacement: "■",
},
want: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
},
{
name: "replace",
args: args{
s: "夜明け間際の𠮷野屋では",
replacement: "■",
},
want: "夜明け間際の■野屋では",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReplaceNotRegularUseAll(tt.args.s, tt.args.replacement); got != tt.want {
t.Errorf("ReplaceNotRegularUseAll() = %v, want %v", got, tt.want)
}
})
}
}
func TestRegularUseDiscriminator_IsNotRegularUse(t *testing.T) {
type fields struct {
allow []rune
disallow []rune
}
tests := []struct {
name string
fields fields
args string
want bool
}{
{
name: "OK",
fields: fields{},
args: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
want: false,
},
{
name: "OK with allow",
fields: fields{
allow: []rune{'勺', '錘', '銑', '脹', '匁'},
},
args: "勺錘銑脹匁",
want: false,
},
{
name: "NG",
fields: fields{},
args: "勺錘銑脹匁",
want: true,
},
{
name: "NG with disallow",
fields: fields{
disallow: []rune{'漢', '字', '以', '外', 'の', 'ひ', 'ら', 'が', 'な', 'や', 'カ', 'タ', 'カ', 'ナ', 'や', '😀', 'な', 'ど', 'も', 'O', 'K', 'と', 'し', 'て', 'い', 'ま', 'す', '!'},
},
args: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &RegularUseDiscriminator{
allow: tt.fields.allow,
disallow: tt.fields.disallow,
}
for _, v := range tt.args {
if got := d.IsNotRegularUse(v); got != tt.want {
t.Errorf("IsNotRegularUse(%c) = %v, want %v", v, got, tt.want)
}
}
})
}
}
func TestRegularUseDiscriminator_ReplaceNotRegularUseAll(t *testing.T) {
type fields struct {
allow []rune
disallow []rune
}
type args struct {
s string
replacement string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "no matching",
fields: fields{},
args: args{
s: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
replacement: "■",
},
want: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
},
{
name: "replace",
fields: fields{
disallow: []rune{'漢', '😀'},
},
args: args{
s: "漢字以外のひらがなやカタカナや😀などもOKとしています!",
replacement: "■",
},
want: "■字以外のひらがなやカタカナや■などもOKとしています!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &RegularUseDiscriminator{
allow: tt.fields.allow,
disallow: tt.fields.disallow,
}
if got := d.ReplaceNotRegularUseAll(tt.args.s, tt.args.replacement); got != tt.want {
t.Errorf("ReplaceNotRegularUseAll() = %v, want %v", got, tt.want)
}
})
}
}
func TestAllow(t *testing.T) {
d := NewRegularUseDiscriminator(Allow('𠮷'))
for _, v := range "夜明け間際の𠮷野屋" {
if got, want := d.IsNotRegularUse(v), false; got != want {
t.Errorf("d.IsNotRegularUse(%c) = %v, want %v", v, got, want)
}
}
}
func TestDisallow(t *testing.T) {
d := NewRegularUseDiscriminator(Disallow([]rune{
'虞', '且', '遵', '朕', '但', '附', '又',
}...))
for _, v := range "虞且遵朕但附又" {
if got, want := d.IsNotRegularUse(v), true; got != want {
t.Errorf("d.IsNotRegularUse(%c) = %v, want %v", v, got, want)
}
}
}