More control over encoding of test files · lua/lua@f4211a5 · GitHub
Skip to content

Commit f4211a5

Browse files
committed
More control over encoding of test files
The few UTF-8 test files are commented as such, and there is only one non UTF-8 test file (to test non UTF-8 sources).
1 parent 1b3f507 commit f4211a5

6 files changed

Lines changed: 49 additions & 28 deletions

File tree

‎testes/db.lua‎

Lines changed: 3 additions & 3 deletions

‎testes/files.lua‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
9292

9393
assert(io.output():seek("set") == 0)
9494

95-
assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
96-
assert(io.write('çfourth_line'))
95+
assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
96+
assert(io.write('Xfourth_line'))
9797
io.output(io.stdout)
9898
collectgarbage() -- file should be closed by GC
9999
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
@@ -300,14 +300,14 @@ do -- test error returns
300300
end
301301
checkerr("invalid format", io.read, "x")
302302
assert(io.read(0) == "") -- not eof
303-
assert(io.read(5, 'l') == '"álo"')
303+
assert(io.read(5, 'l') == '"alo"')
304304
assert(io.read(0) == "")
305305
assert(io.read() == "second line")
306306
local x = io.input():seek()
307307
assert(io.read() == "third line ")
308308
assert(io.input():seek("set", x))
309309
assert(io.read('L') == "third line \n")
310-
assert(io.read(1) == "ç")
310+
assert(io.read(1) == "X")
311311
assert(io.read(string.len"fourth_line") == "fourth_line")
312312
assert(io.input():seek("cur", -string.len"fourth_line"))
313313
assert(io.read() == "fourth_line")

‎testes/pm.lua‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- $Id: testes/pm.lua $
22
-- See Copyright Notice in file all.lua
33

4+
-- UTF-8 file
5+
6+
47
print('testing pattern matching')
58

69
local function checkerror (msg, f, ...)
@@ -50,6 +53,19 @@ assert(f('aLo_ALO', '%a*') == 'aLo')
5053

5154
assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu")
5255

56+
57+
-- Adapt a pattern to UTF-8
58+
local function PU (p)
59+
-- break '?' into each individual byte of a character
60+
p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c)
61+
return string.gsub(c, ".", "%0?")
62+
end)
63+
-- change '.' to utf-8 character patterns
64+
p = string.gsub(p, "%.", utf8.charpattern)
65+
return p
66+
end
67+
68+
5369
assert(f('aaab', 'a*') == 'aaa');
5470
assert(f('aaa', '^.*$') == 'aaa');
5571
assert(f('aaa', 'b*') == '');
@@ -73,16 +89,16 @@ assert(f('aaa', '^.-$') == 'aaa')
7389
assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
7490
assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
7591
assert(f('alo xo', '.o$') == 'xo')
76-
assert(f(' \n isto é assim', '%S%S*') == 'isto')
77-
assert(f(' \n isto é assim', '%S*$') == 'assim')
78-
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
92+
assert(f(' \n isto é assim', '%S%S*') == 'isto')
93+
assert(f(' \n isto é assim', '%S*$') == 'assim')
94+
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
7995
assert(f('um caracter ? extra', '[^%sa-z]') == '?')
8096
assert(f('', 'a?') == '')
81-
assert(f('á', 'á?') == 'á')
82-
assert(f('ábl', 'á?b?l?') == 'ábl')
83-
assert(f(' ábl', 'á?b?l?') == '')
97+
assert(f('á', PU'á?') == 'á')
98+
assert(f('ábl', PU'á?b?l?') == 'ábl')
99+
assert(f(' ábl', PU'á?b?l?') == '')
84100
assert(f('aa', '^aa?a?a') == 'aa')
85-
assert(f(']]]áb', '[^]]') == 'á')
101+
assert(f(']]]áb', '[^]]+') == 'áb')
86102
assert(f("0alo alo", "%x*") == "0a")
87103
assert(f("alo alo", "%C+") == "alo alo")
88104
print('+')
@@ -136,28 +152,28 @@ assert(string.match("alo xyzK", "(%w+)K") == "xyz")
136152
assert(string.match("254 K", "(%d*)K") == "")
137153
assert(string.match("alo ", "(%w*)$") == "")
138154
assert(not string.match("alo ", "(%w+)$"))
139-
assert(string.find("(álo)", "%(á") == 1)
140-
local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$")
141-
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
155+
assert(string.find("(álo)", "%(á") == 1)
156+
local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$")
157+
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
142158
a, b, c, d = string.match('0123456789', '(.+(.?)())')
143159
assert(a == '0123456789' and b == '' and c == 11 and d == nil)
144160
print('+')
145161

146-
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
147-
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
162+
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
163+
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
148164
assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
149165
assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
150-
local t = "abç d"
151-
a, b = string.gsub(t, '(.)', '%1@')
152-
assert('@'..a == string.gsub(t, '', '@') and b == 5)
153-
a, b = string.gsub('abçd', '(.)', '%0@', 2)
154-
assert(a == 'a@b@çd' and b == 2)
166+
local t = "abç d"
167+
a, b = string.gsub(t, PU'(.)', '%1@')
168+
assert(a == "a@b@ç@ @d@" and b == 5)
169+
a, b = string.gsub('abçd', PU'(.)', '%0@', 2)
170+
assert(a == 'a@b@çd' and b == 2)
155171
assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
156172
assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
157173
"xyz=abc-abc=xyz")
158174
assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
159175
assert(string.gsub("abc", "%w+", "%0%1") == "abcabc")
160-
assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú')
176+
assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú')
161177
assert(string.gsub('', '^', 'r') == 'r')
162178
assert(string.gsub('', '$', 'r') == 'r')
163179
print('+')
@@ -188,8 +204,8 @@ do
188204
end
189205

190206
function f(a,b) return string.gsub(a,'.',b) end
191-
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
192-
"trocar tudo em bbbbb é alalalalalal")
207+
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
208+
"trocar tudo em bbbbb é alalalalalal")
193209

194210
local function dostring (s) return load(s, "")() or "" end
195211
assert(string.gsub("alo $a='x'$ novamente $return a$",

‎testes/sort.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ timesort(a, limit, function(x,y) return nil end, "equal")
289289

290290
for i,v in pairs(a) do assert(v == false) end
291291

292-
AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
292+
AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
293293
table.sort(AA)
294294
check(AA)
295295

‎testes/strings.lua‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- $Id: testes/strings.lua $
22
-- See Copyright Notice in file all.lua
33

4+
-- ISO Latin encoding
5+
6+
47
print('testing strings and string library')
58

69
local maxi <const> = math.maxinteger

‎testes/utf8.lua‎

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)