-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathissues.lua
511 lines (446 loc) · 10.1 KB
/
issues.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
-- issue #10
local function inspect(options)
options = options or {}
return type(options)
end
assert(inspect(nil) == "table")
local function inspect(options)
options = options or setmetatable({}, {__mode = "test"})
return type(options)
end
assert(inspect(nil) == "table")
-- issue #16
local ok, msg = pcall(function()
local a = {}
a[nil] = 1
end)
assert(not ok and string.find(msg, "table index is nil", 1, true))
-- issue #19
local tbl = {1,2,3,4,5}
assert(#tbl == 5)
assert(table.remove(tbl) == 5)
assert(#tbl == 4)
assert(table.remove(tbl, 3) == 3)
assert(#tbl == 3)
-- issue #24
local tbl = {string.find('hello.world', '.', 0)}
assert(tbl[1] == 1 and tbl[2] == 1)
assert(string.sub('hello.world', 0, 2) == "he")
-- issue 33
local a,b
a = function ()
pcall(function()
end)
coroutine.yield("a")
return b()
end
b = function ()
return "b"
end
local co = coroutine.create(a)
assert(select(2, coroutine.resume(co)) == "a")
assert(select(2, coroutine.resume(co)) == "b")
assert(coroutine.status(co) == "dead")
-- issue 37
function test(a, b, c)
b = b or string.format("b%s", a)
c = c or string.format("c%s", a)
assert(a == "test")
assert(b == "btest")
assert(c == "ctest")
end
test("test")
-- issue 39
assert(string.match("あいうえお", ".*あ.*") == "あいうえお")
assert(string.match("あいうえお", "あいうえお") == "あいうえお")
-- issue 47
assert(string.gsub("A\nA", ".", "A") == "AAA")
-- issue 62
local function level4() error("error!") end
local function level3() level4() end
local function level2() level3() end
local function level1() level2() end
local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 10)
end)
assert(result == [[msg
stack traceback:]])
ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 9)
end)
assert(result == string.gsub([[msg
stack traceback:
@TAB@[G]: ?]], "@TAB@", "\t"))
local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 0)
end)
assert(result == string.gsub([[msg
stack traceback:
@TAB@[G]: in function 'traceback'
@[email protected]:87: in function <issues.lua:86>
@TAB@[G]: in function 'error'
@[email protected]:71: in function 'level4'
@[email protected]:72: in function 'level3'
@[email protected]:73: in function 'level2'
@[email protected]:74: in function <issues.lua:74>
@TAB@[G]: in function 'xpcall'
@[email protected]:86: in main chunk
@TAB@[G]: ?]], "@TAB@", "\t"))
local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 3)
end)
assert(result == string.gsub([[msg
stack traceback:
@[email protected]:71: in function 'level4'
@[email protected]:72: in function 'level3'
@[email protected]:73: in function 'level2'
@[email protected]:74: in function <issues.lua:74>
@TAB@[G]: in function 'xpcall'
@[email protected]:103: in main chunk
@TAB@[G]: ?]], "@TAB@", "\t"))
-- issue 81
local tbl = {
[-1] = "a",
[0] = "b",
[1] = "c",
}
local a, b = next(tbl, nil)
assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
local a, b = next(tbl, a)
assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
local a, b = next(tbl, a)
assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
local a, b = next(tbl, a)
assert( a == nil and b == nil)
local tbl = {'a', 'b'}
local a, b = next(tbl, nil)
assert(a == 1 and b == "a")
local a, b = next(tbl, a)
assert(a == 2 and b == "b")
local a, b = next(tbl, a)
assert(a == nil and b == nil)
-- issue 82
local cr = function()
return coroutine.wrap(function()
coroutine.yield(1, "a")
coroutine.yield(2, "b")
end)
end
local f = cr()
local a, b = f()
assert(a == 1 and b == "a")
local a, b = f()
assert(a == 2 and b == "b")
-- issue 91, 92
local url = "www.aaa.bbb_abc123-321-cba_abc123"
assert(string.match(url, ".-([%w-]*)[.]*") == "www")
local s = "hello.world"
assert(s:match("([^.]+).world") == "hello")
local s = "hello-world"
assert(s:match("([^-]+)-world") == "hello")
-- issue 93
local t = {}
local ok, msg = pcall(function() t.notfound() end)
assert(not ok and string.find(msg, "attempt to call a non-function object", 1, true))
-- issue 150
local util = {
fn = function() end
}
local b
local x = util.fn(
1,
(b or {}).x)
local s = [=[["a"]['b'][9] - ["a"]['b'][8] > ]=]
local result = {}
for i in s:gmatch([=[[[][^%s,]*[]]]=]) do
table.insert(result, i)
end
assert(result[1] == [=[["a"]['b'][9]]=])
assert(result[2] == [=[["a"]['b'][8]]=])
-- issue 168
local expected = 1
local result = math.random(1)
assert(result == expected)
-- issue 202
local t = {}
ok, res = pcall(table.remove, t)
if not ok or not res then
table.insert(t, {})
else
assert(false)
end
ok, res = pcall(table.remove, t)
ok, res = pcall(table.remove, t)
assert(not ok or not res)
-- issue 204
local ok, message = pcall(nil)
assert(not ok)
assert(message == "attempt to call a nil value")
local ok, message = pcall(1)
assert(not ok)
assert(message == "attempt to call a number value")
ok, message = pcall(function()
pcall()
end)
assert(not ok and string.find(message, "bad argument #1 to pcall", 1, true))
-- issue 216
local function bar()
return "bar"
end
local function test(foo)
local should_not_change
foo = foo or bar()
print(should_not_change)
return should_not_change
end
assert(test(nil) == nil)
-- issue 220
function test()
function f(v)
return v
end
local tbl = {y=0}
local a,b
a, b = f(10), f(20)
assert(tbl.y == 0)
end
test()
-- issue 222
function test()
local m = {n=2}
function m:f1()
return self:f3() >= self.n
end
function m:f2()
local v1, v2, v3 = m:f1()
assert(v1 == true)
assert(v2 == nil)
assert(v3 == nil)
end
function m:f3()
return 3
end
m:f2()
end
test()
-- issue #292
function test()
t0 = {}
t0.year = 2006
t0.month = 1
t0.day = 2
t0.hour = 15
t0.min = 4
t0.sec = 5
t1 = {}
t1.year = "2006"
t1.month = "1"
t1.day = "2"
t1.hour = "15"
t1.min = "4"
t1.sec = "5"
assert(os.time(t0) == os.time(t1))
t2 = {}
t2.year = " 2006"--prefix blank space
t2.month = "1"
t2.day = "2"
t2.hour = "15"
t2.min = "4"
t2.sec = "5"
assert(os.time(t0) == os.time(t2))
t3 = {}
t3.year = " 0002006"--prefix blank space and 0
t3.month = "1"
t3.day = "2"
t3.hour = "15"
t3.min = "4"
t3.sec = "5"
assert(os.time(t1) == os.time(t3))
t4 = {}
t4.year = "0002006"--prefix 0
t4.month = "1"
t4.day = "2"
t4.hour = "15"
t4.min = "4"
t4.sec = "5"
assert(os.time(t1) == os.time(t4))
t5 = {}
t5.year = "0x7d6"--prefix 0x
t5.month = "1"
t5.day = "2"
t5.hour = "15"
t5.min = "4"
t5.sec = "5"
assert(os.time(t1) == os.time(t5))
t6 = {}
t6.year = "0X7d6"--prefix 0X
t6.month = "1"
t6.day = "2"
t6.hour = "15"
t6.min = "4"
t6.sec = "5"
assert(os.time(t1) == os.time(t6))
end
test()
--issue #331
function test()
local select_a = function()
return select(3, "1")
end
assert(true == pcall(select_a))
local select_b = function()
return select(0)
end
assert(false == pcall(select_b))
local select_c = function()
return select(1/9)
end
assert(false == pcall(select_c))
local select_d = function()
return select(1, "a")
end
assert("a" == select_d())
local select_e = function()
return select(3, "a", "b", "c")
end
assert("c" == select_e())
local select_f = function()
return select(0)(select(1/9))
end
assert(false == pcall(select_f))
end
test()
-- issue #363
-- Any expression enclosed in parentheses always results in only one value.
function test()
function ret2(a, b)
return a, b
end
function enclosed_ret()
return (ret2(1, 2))
end
local a,b = enclosed_ret()
assert(a == 1 and b == nil)
function enclosed_vararg_ret(...)
return (...)
end
local a,b,c=enclosed_vararg_ret(1, 2, 3)
assert(a == 1 and b == nil and c == nil)
function enclosed_vararg_assign(...)
local a,b,c = (...)
return a,b,c
end
local a,b,c=enclosed_vararg_assign(1, 2, 3)
assert(a == 1 and b == nil and c == nil)
end
test()
-- issue #412
-- issue #418
-- Conversion from symmetric modulo is incorrect.
function test()
assert(-2 % -2 == 0)
assert(-1 % -2 == -1)
assert(0 % -2 == 0)
assert(1 % -2 == -1)
assert(2 % -2 == 0)
assert(-2 % 2 == 0)
assert(-1 % 2 == 1)
assert(0 % 2 == 0)
assert(1 % 2 == 1)
assert(2 % 2 == 0)
end
test()
-- issue #355
function test()
local x = "valid"
assert(x == "valid")
assert(zzz == nil)
x = zzz and "not-valid" or x
assert(x == "valid")
end
test()
function test()
local x = "valid"
local z = nil
assert(x == "valid")
assert(z == nil)
x = z and "not-valid" or x
assert(x == "valid")
end
test()
function test()
local x = "valid"
assert(x == "valid")
assert(zzz == nil)
x = zzz and "not-valid" or "still " .. x
assert(x == "still valid")
end
test()
-- issue #315
function test()
local a = {}
local d = 'e'
local f = 1
f, a.d = f, d
assert(f..", "..a.d == "1, e")
end
test()
-- issue #423
function test()
local a, b, c = "1", "3", "1"
a, b, c= tonumber(a), tonumber(b) or a, tonumber(c)
assert(a == 1)
assert(type(a) == "number")
assert(b == 3)
assert(type(b) == "number")
assert(c == 1)
assert(type(c) == "number")
end
test()
-- issue #452
function test()
local ok, msg = pcall(function()
local ok, msg = xpcall(function() error("fn") end, function(err) error("handler") end)
assert(not ok and msg)
error("expected to reach this.")
end)
assert(not ok)
end
test()
-- issue #455
function test()
local path = "."
local fd, _, code = io.open(path, "r")
assert(fd ~= nil)
local _, _, ecode = fd:read(1)
assert(ecode == 1)
end
test()
-- issue #459
function test()
local a, b = io.popen("ls", nil)
assert(a)
assert(b == nil)
local a, b = io.popen("ls", nil, nil)
assert(a)
assert(b == nil)
end
test()
-- issue #514
function test()
local tbl = {foo = 42, bar = "baz"}
local iter = function(s,k)
k,_ = next(tbl, k)
return k
end
local seen1, seen2 = {}, {}
for item in iter do
seen1[item] = true
end
for item in iter do
seen2[item] = true
end
assert(seen1.foo and seen1.bar)
assert(seen2.foo and seen2.bar)
end
test()