1
0

issues.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. -- issue #10
  2. local function inspect(options)
  3. options = options or {}
  4. return type(options)
  5. end
  6. assert(inspect(nil) == "table")
  7. local function inspect(options)
  8. options = options or setmetatable({}, {__mode = "test"})
  9. return type(options)
  10. end
  11. assert(inspect(nil) == "table")
  12. -- issue #16
  13. local ok, msg = pcall(function()
  14. local a = {}
  15. a[nil] = 1
  16. end)
  17. assert(not ok and string.find(msg, "table index is nil", 1, true))
  18. -- issue #19
  19. local tbl = {1,2,3,4,5}
  20. assert(#tbl == 5)
  21. assert(table.remove(tbl) == 5)
  22. assert(#tbl == 4)
  23. assert(table.remove(tbl, 3) == 3)
  24. assert(#tbl == 3)
  25. -- issue #24
  26. local tbl = {string.find('hello.world', '.', 0)}
  27. assert(tbl[1] == 1 and tbl[2] == 1)
  28. assert(string.sub('hello.world', 0, 2) == "he")
  29. -- issue 33
  30. local a,b
  31. a = function ()
  32. pcall(function()
  33. end)
  34. coroutine.yield("a")
  35. return b()
  36. end
  37. b = function ()
  38. return "b"
  39. end
  40. local co = coroutine.create(a)
  41. assert(select(2, coroutine.resume(co)) == "a")
  42. assert(select(2, coroutine.resume(co)) == "b")
  43. assert(coroutine.status(co) == "dead")
  44. -- issue 37
  45. function test(a, b, c)
  46. b = b or string.format("b%s", a)
  47. c = c or string.format("c%s", a)
  48. assert(a == "test")
  49. assert(b == "btest")
  50. assert(c == "ctest")
  51. end
  52. test("test")
  53. -- issue 39
  54. assert(string.match("あいうえお", ".*あ.*") == "あいうえお")
  55. assert(string.match("あいうえお", "あいうえお") == "あいうえお")
  56. -- issue 47
  57. assert(string.gsub("A\nA", ".", "A") == "AAA")
  58. -- issue 62
  59. local function level4() error("error!") end
  60. local function level3() level4() end
  61. local function level2() level3() end
  62. local function level1() level2() end
  63. local ok, result = xpcall(level1, function(err)
  64. return debug.traceback("msg", 10)
  65. end)
  66. assert(result == [[msg
  67. stack traceback:]])
  68. ok, result = xpcall(level1, function(err)
  69. return debug.traceback("msg", 9)
  70. end)
  71. assert(result == string.gsub([[msg
  72. stack traceback:
  73. @TAB@[G]: ?]], "@TAB@", "\t"))
  74. local ok, result = xpcall(level1, function(err)
  75. return debug.traceback("msg", 0)
  76. end)
  77. assert(result == string.gsub([[msg
  78. stack traceback:
  79. @TAB@[G]: in function 'traceback'
  80. @TAB@issues.lua:87: in function <issues.lua:86>
  81. @TAB@[G]: in function 'error'
  82. @TAB@issues.lua:71: in function 'level4'
  83. @TAB@issues.lua:72: in function 'level3'
  84. @TAB@issues.lua:73: in function 'level2'
  85. @TAB@issues.lua:74: in function <issues.lua:74>
  86. @TAB@[G]: in function 'xpcall'
  87. @TAB@issues.lua:86: in main chunk
  88. @TAB@[G]: ?]], "@TAB@", "\t"))
  89. local ok, result = xpcall(level1, function(err)
  90. return debug.traceback("msg", 3)
  91. end)
  92. assert(result == string.gsub([[msg
  93. stack traceback:
  94. @TAB@issues.lua:71: in function 'level4'
  95. @TAB@issues.lua:72: in function 'level3'
  96. @TAB@issues.lua:73: in function 'level2'
  97. @TAB@issues.lua:74: in function <issues.lua:74>
  98. @TAB@[G]: in function 'xpcall'
  99. @TAB@issues.lua:103: in main chunk
  100. @TAB@[G]: ?]], "@TAB@", "\t"))
  101. -- issue 81
  102. local tbl = {
  103. [-1] = "a",
  104. [0] = "b",
  105. [1] = "c",
  106. }
  107. local a, b = next(tbl, nil)
  108. assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
  109. local a, b = next(tbl, a)
  110. assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
  111. local a, b = next(tbl, a)
  112. assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c")
  113. local a, b = next(tbl, a)
  114. assert( a == nil and b == nil)
  115. local tbl = {'a', 'b'}
  116. local a, b = next(tbl, nil)
  117. assert(a == 1 and b == "a")
  118. local a, b = next(tbl, a)
  119. assert(a == 2 and b == "b")
  120. local a, b = next(tbl, a)
  121. assert(a == nil and b == nil)
  122. -- issue 82
  123. local cr = function()
  124. return coroutine.wrap(function()
  125. coroutine.yield(1, "a")
  126. coroutine.yield(2, "b")
  127. end)
  128. end
  129. local f = cr()
  130. local a, b = f()
  131. assert(a == 1 and b == "a")
  132. local a, b = f()
  133. assert(a == 2 and b == "b")
  134. -- issue 91, 92
  135. local url = "www.aaa.bbb_abc123-321-cba_abc123"
  136. assert(string.match(url, ".-([%w-]*)[.]*") == "www")
  137. local s = "hello.world"
  138. assert(s:match("([^.]+).world") == "hello")
  139. local s = "hello-world"
  140. assert(s:match("([^-]+)-world") == "hello")
  141. -- issue 93
  142. local t = {}
  143. local ok, msg = pcall(function() t.notfound() end)
  144. assert(not ok and string.find(msg, "attempt to call a non-function object", 1, true))
  145. -- issue 150
  146. local util = {
  147. fn = function() end
  148. }
  149. local b
  150. local x = util.fn(
  151. 1,
  152. (b or {}).x)
  153. local s = [=[["a"]['b'][9] - ["a"]['b'][8] > ]=]
  154. local result = {}
  155. for i in s:gmatch([=[[[][^%s,]*[]]]=]) do
  156. table.insert(result, i)
  157. end
  158. assert(result[1] == [=[["a"]['b'][9]]=])
  159. assert(result[2] == [=[["a"]['b'][8]]=])
  160. -- issue 168
  161. local expected = 1
  162. local result = math.random(1)
  163. assert(result == expected)
  164. -- issue 202
  165. local t = {}
  166. ok, res = pcall(table.remove, t)
  167. if not ok or not res then
  168. table.insert(t, {})
  169. else
  170. assert(false)
  171. end
  172. ok, res = pcall(table.remove, t)
  173. ok, res = pcall(table.remove, t)
  174. assert(not ok or not res)
  175. -- issue 204
  176. local ok, message = pcall(nil)
  177. assert(not ok)
  178. assert(message == "attempt to call a nil value")
  179. local ok, message = pcall(1)
  180. assert(not ok)
  181. assert(message == "attempt to call a number value")
  182. ok, message = pcall(function()
  183. pcall()
  184. end)
  185. assert(not ok and string.find(message, "bad argument #1 to pcall", 1, true))
  186. -- issue 216
  187. local function bar()
  188. return "bar"
  189. end
  190. local function test(foo)
  191. local should_not_change
  192. foo = foo or bar()
  193. print(should_not_change)
  194. return should_not_change
  195. end
  196. assert(test(nil) == nil)
  197. -- issue 220
  198. function test()
  199. function f(v)
  200. return v
  201. end
  202. local tbl = {y=0}
  203. local a,b
  204. a, b = f(10), f(20)
  205. assert(tbl.y == 0)
  206. end
  207. test()
  208. -- issue 222
  209. function test()
  210. local m = {n=2}
  211. function m:f1()
  212. return self:f3() >= self.n
  213. end
  214. function m:f2()
  215. local v1, v2, v3 = m:f1()
  216. assert(v1 == true)
  217. assert(v2 == nil)
  218. assert(v3 == nil)
  219. end
  220. function m:f3()
  221. return 3
  222. end
  223. m:f2()
  224. end
  225. test()
  226. -- issue #292
  227. function test()
  228. t0 = {}
  229. t0.year = 2006
  230. t0.month = 1
  231. t0.day = 2
  232. t0.hour = 15
  233. t0.min = 4
  234. t0.sec = 5
  235. t1 = {}
  236. t1.year = "2006"
  237. t1.month = "1"
  238. t1.day = "2"
  239. t1.hour = "15"
  240. t1.min = "4"
  241. t1.sec = "5"
  242. assert(os.time(t0) == os.time(t1))
  243. t2 = {}
  244. t2.year = " 2006"--prefix blank space
  245. t2.month = "1"
  246. t2.day = "2"
  247. t2.hour = "15"
  248. t2.min = "4"
  249. t2.sec = "5"
  250. assert(os.time(t0) == os.time(t2))
  251. t3 = {}
  252. t3.year = " 0002006"--prefix blank space and 0
  253. t3.month = "1"
  254. t3.day = "2"
  255. t3.hour = "15"
  256. t3.min = "4"
  257. t3.sec = "5"
  258. assert(os.time(t1) == os.time(t3))
  259. t4 = {}
  260. t4.year = "0002006"--prefix 0
  261. t4.month = "1"
  262. t4.day = "2"
  263. t4.hour = "15"
  264. t4.min = "4"
  265. t4.sec = "5"
  266. assert(os.time(t1) == os.time(t4))
  267. t5 = {}
  268. t5.year = "0x7d6"--prefix 0x
  269. t5.month = "1"
  270. t5.day = "2"
  271. t5.hour = "15"
  272. t5.min = "4"
  273. t5.sec = "5"
  274. assert(os.time(t1) == os.time(t5))
  275. t6 = {}
  276. t6.year = "0X7d6"--prefix 0X
  277. t6.month = "1"
  278. t6.day = "2"
  279. t6.hour = "15"
  280. t6.min = "4"
  281. t6.sec = "5"
  282. assert(os.time(t1) == os.time(t6))
  283. end
  284. test()
  285. --issue #331
  286. function test()
  287. local select_a = function()
  288. return select(3, "1")
  289. end
  290. assert(true == pcall(select_a))
  291. local select_b = function()
  292. return select(0)
  293. end
  294. assert(false == pcall(select_b))
  295. local select_c = function()
  296. return select(1/9)
  297. end
  298. assert(false == pcall(select_c))
  299. local select_d = function()
  300. return select(1, "a")
  301. end
  302. assert("a" == select_d())
  303. local select_e = function()
  304. return select(3, "a", "b", "c")
  305. end
  306. assert("c" == select_e())
  307. local select_f = function()
  308. return select(0)(select(1/9))
  309. end
  310. assert(false == pcall(select_f))
  311. end
  312. test()
  313. -- issue #363
  314. -- Any expression enclosed in parentheses always results in only one value.
  315. function test()
  316. function ret2(a, b)
  317. return a, b
  318. end
  319. function enclosed_ret()
  320. return (ret2(1, 2))
  321. end
  322. local a,b = enclosed_ret()
  323. assert(a == 1 and b == nil)
  324. function enclosed_vararg_ret(...)
  325. return (...)
  326. end
  327. local a,b,c=enclosed_vararg_ret(1, 2, 3)
  328. assert(a == 1 and b == nil and c == nil)
  329. function enclosed_vararg_assign(...)
  330. local a,b,c = (...)
  331. return a,b,c
  332. end
  333. local a,b,c=enclosed_vararg_assign(1, 2, 3)
  334. assert(a == 1 and b == nil and c == nil)
  335. end
  336. test()
  337. -- issue #412
  338. -- issue #418
  339. -- Conversion from symmetric modulo is incorrect.
  340. function test()
  341. assert(-2 % -2 == 0)
  342. assert(-1 % -2 == -1)
  343. assert(0 % -2 == 0)
  344. assert(1 % -2 == -1)
  345. assert(2 % -2 == 0)
  346. assert(-2 % 2 == 0)
  347. assert(-1 % 2 == 1)
  348. assert(0 % 2 == 0)
  349. assert(1 % 2 == 1)
  350. assert(2 % 2 == 0)
  351. end
  352. test()
  353. -- issue #355
  354. function test()
  355. local x = "valid"
  356. assert(x == "valid")
  357. assert(zzz == nil)
  358. x = zzz and "not-valid" or x
  359. assert(x == "valid")
  360. end
  361. test()
  362. function test()
  363. local x = "valid"
  364. local z = nil
  365. assert(x == "valid")
  366. assert(z == nil)
  367. x = z and "not-valid" or x
  368. assert(x == "valid")
  369. end
  370. test()
  371. function test()
  372. local x = "valid"
  373. assert(x == "valid")
  374. assert(zzz == nil)
  375. x = zzz and "not-valid" or "still " .. x
  376. assert(x == "still valid")
  377. end
  378. test()
  379. -- issue #315
  380. function test()
  381. local a = {}
  382. local d = 'e'
  383. local f = 1
  384. f, a.d = f, d
  385. assert(f..", "..a.d == "1, e")
  386. end
  387. test()
  388. -- issue #423
  389. function test()
  390. local a, b, c = "1", "3", "1"
  391. a, b, c= tonumber(a), tonumber(b) or a, tonumber(c)
  392. assert(a == 1)
  393. assert(type(a) == "number")
  394. assert(b == 3)
  395. assert(type(b) == "number")
  396. assert(c == 1)
  397. assert(type(c) == "number")
  398. end
  399. test()
  400. -- issue #452
  401. function test()
  402. local ok, msg = pcall(function()
  403. local ok, msg = xpcall(function() error("fn") end, function(err) error("handler") end)
  404. assert(not ok and msg)
  405. error("expected to reach this.")
  406. end)
  407. assert(not ok)
  408. end
  409. test()
  410. -- issue #455
  411. function test()
  412. local path = "."
  413. local fd, _, code = io.open(path, "r")
  414. assert(fd ~= nil)
  415. local _, _, ecode = fd:read(1)
  416. assert(ecode == 1)
  417. end
  418. test()
  419. -- issue #459
  420. function test()
  421. local a, b = io.popen("ls", nil)
  422. assert(a)
  423. assert(b == nil)
  424. local a, b = io.popen("ls", nil, nil)
  425. assert(a)
  426. assert(b == nil)
  427. end
  428. test()