-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua-utf8-test.lua
121 lines (105 loc) · 2.49 KB
/
lua-utf8-test.lua
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
local luaunit = require("luaunit")
TestLuaUtf8 = {
utf8 = require("lua-utf8")
}
function TestLuaUtf8:testUtf8Validate()
local cases = {
{
value = "",
valid = true
},
{
value = "¢€𤭢",
valid = true
},
{
value = "Pay in €. Thanks.",
valid = true
},
{
value = "lua程序\129\001设计",
valid = false,
invalid_pos = 10
},
}
for _, case in ipairs(cases) do
local valid, invalid_pos = self.utf8.validate(case.value)
luaunit.assertEquals(valid, case.valid)
if case.invalid_pos then
luaunit.assertEquals(invalid_pos, case.invalid_pos)
end
end
end
function TestLuaUtf8:testUtf8Len()
local cases = {
{
value = "",
expect = 0
},
{
value = "lua程序设计",
expect = 7
},
{
value = "¢€𤭢",
expect = 3
},
{
value = "lua程序\129\001设计",
expect = 5,
invalid_pos = 10
},
}
for _, case in ipairs(cases) do
local len, invalid_pos = self.utf8.len(case.value)
luaunit.assertEquals(len, case.expect)
if case.invalid_pos then
luaunit.assertEquals(invalid_pos, case.invalid_pos)
end
end
end
function TestLuaUtf8:testUtf8Reverse()
local cases = {
{
value = "a",
expect = "a"
},
{
value = "lua",
expect = "aul"
},
{
value = "lua程序设计",
expect = "计设序程aul"
}
}
for _, case in ipairs(cases) do
local s = self.utf8.reverse(case.value)
luaunit.assertEquals(s, case.expect)
end
end
function TestLuaUtf8:testUtf8Sub()
local cases = {
{
value = "lua程序设计",
start_pos = 1,
end_pos = 5,
expect = "lua程序"
},
{
value = "lua程序设计",
start_pos = 3,
expect = "a程序设计"
},
{
value = "lua程序设计",
start_pos = 20,
expect = ""
},
}
for _, case in ipairs(cases) do
local s = self.utf8.sub(case.value, case.start_pos, case.end_pos)
luaunit.assertEquals(s, case.expect)
end
end
os.exit(luaunit.run())