-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfilename_test.go
142 lines (125 loc) · 3.7 KB
/
filename_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package upload
import (
"testing"
"unicode"
"golang.org/x/text/unicode/norm"
. "github.com/smartystreets/goconvey/convey"
)
func TestInAlphabet(t *testing.T) {
Convey("InAlphabet", t, FailureContinues, func() {
Convey("handles Latin-1 input correctly", FailureContinues, func() {
samples := []struct {
input string
returned bool
}{
// ASCII
{"file.name", true},
{"the space", true},
{"line\nbreak", false},
{"the\tTAB", false},
{"Samba?", false},
{"not print\x0e.", false}, {"fancier not print\u000e.", false},
{"a null\x00.", false},
{"form feed\x0c", false},
// now comes Latin-1
{"start \xb0", false}, {"end \xdf", false}, // obsolete blocks, like in old terminal programs
{"stray box \xfe", false},
}
for i, tuple := range samples {
tuple.returned = InAlphabet(samples[i].input, nil, nil)
So(tuple, ShouldResemble, samples[i])
}
})
Convey("accepts correct UTF-8 input", FailureContinues, func() {
samples := []struct {
input string
returned bool
}{
{"J. Edgar", true},
{"keyboard → „typewriters’ keylayout“ ≠ »DIN T2 you ought better buy«", true},
{"Döner macht schöner.", true},
{"GENUẞMITTEL Kaufläche häufig Dzerba", true}, // ligatures (capital ß after 1900 for historic documents)
{"フ\u30d7", true}, {"プ\u30d5\u309a", true},
}
for i, tuple := range samples {
tuple.returned = InAlphabet(samples[i].input, nil, nil)
So(tuple, ShouldResemble, samples[i])
}
})
Convey("rejects undesired runes", FailureContinues, func() {
samples := []struct {
input string
returned bool
}{
{"form\xfffeed", false}, {"feed\u000cform", false},
{"IND\u0084", false}, {"NEL\u0085", false},
{"line\u2028", false}, {"paragraph\u2029", false},
}
for i, tuple := range samples {
tuple.returned = InAlphabet(samples[i].input, nil, nil)
So(tuple, ShouldResemble, samples[i])
}
})
Convey("allows to restrict acceptable rune ranges", FailureContinues, func() {
azOnly := unicode.RangeTable{
R16: []unicode.Range16{
{0x0061, 0x007a, 1}, // a-z
},
LatinOffset: 1,
}
samples := []struct {
input string
restrict []*unicode.RangeTable
returned bool
}{
{"az", []*unicode.RangeTable{&azOnly}, true},
{"äz", []*unicode.RangeTable{&azOnly}, false},
}
for i, tuple := range samples {
tuple.returned = InAlphabet(samples[i].input, samples[i].restrict, nil)
So(tuple, ShouldResemble, samples[i])
}
})
Convey("enforces inputs that are normalized under a Form", FailureContinues, func() {
samples := []struct {
input string
form norm.Form
returned bool
}{
{"säet", norm.NFC, true},
{"säet", norm.NFD, false},
}
for i, tuple := range samples {
tuple.returned = InAlphabet(samples[i].input, nil, &samples[i].form)
So(tuple, ShouldResemble, samples[i])
}
})
})
}
func TestParseUnicodeBlockList(t *testing.T) {
Convey("ParseUnicodeBlockList works", t, FailureContinues, func() {
samples := []struct {
input string
table *unicode.RangeTable
err error
}{
{`x0000-x007F x0100-x017F x2152-x217F:2 xf0000-xf0010 // don't use this`, &unicode.RangeTable{
R16: []unicode.Range16{
{0x0000, 0x007f, 1},
{0x0100, 0x017f, 1},
{0x2152, 0x217f, 2},
},
R32: []unicode.Range32{
{Lo: 0xf0000, Hi: 0xf0010, Stride: 1},
},
LatinOffset: 1,
}, nil},
}
for i, tuple := range samples {
tuple.table, tuple.err = ParseUnicodeBlockList(samples[i].input)
So(tuple, ShouldResemble, samples[i])
}
})
}