auxlib_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package lua
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. func TestCheckInt(t *testing.T) {
  7. L := NewState()
  8. defer L.Close()
  9. errorIfGFuncNotFail(t, L, func(L *LState) int {
  10. L.Push(LNumber(10))
  11. errorIfNotEqual(t, 10, L.CheckInt(2))
  12. L.Push(LString("aaa"))
  13. L.CheckInt(3)
  14. return 0
  15. }, "number expected, got string")
  16. }
  17. func TestCheckInt64(t *testing.T) {
  18. L := NewState()
  19. defer L.Close()
  20. errorIfGFuncNotFail(t, L, func(L *LState) int {
  21. L.Push(LNumber(10))
  22. errorIfNotEqual(t, int64(10), L.CheckInt64(2))
  23. L.Push(LString("aaa"))
  24. L.CheckInt64(3)
  25. return 0
  26. }, "number expected, got string")
  27. }
  28. func TestCheckNumber(t *testing.T) {
  29. L := NewState()
  30. defer L.Close()
  31. errorIfGFuncNotFail(t, L, func(L *LState) int {
  32. L.Push(LNumber(10))
  33. errorIfNotEqual(t, LNumber(10), L.CheckNumber(2))
  34. L.Push(LString("11"))
  35. errorIfNotEqual(t, LNumber(11), L.CheckNumber(3))
  36. L.Push(LString("aaa"))
  37. L.CheckNumber(4)
  38. return 0
  39. }, "number expected, got string")
  40. }
  41. func TestCheckString(t *testing.T) {
  42. L := NewState()
  43. defer L.Close()
  44. errorIfGFuncNotFail(t, L, func(L *LState) int {
  45. L.Push(LString("aaa"))
  46. errorIfNotEqual(t, "aaa", L.CheckString(2))
  47. L.Push(LNumber(10))
  48. errorIfNotEqual(t, "10", L.CheckString(3))
  49. L.Push(L.NewTable())
  50. L.CheckString(4)
  51. return 0
  52. }, "string expected, got table")
  53. }
  54. func TestCheckBool(t *testing.T) {
  55. L := NewState()
  56. defer L.Close()
  57. errorIfGFuncNotFail(t, L, func(L *LState) int {
  58. L.Push(LTrue)
  59. errorIfNotEqual(t, true, L.CheckBool(2))
  60. L.Push(LNumber(10))
  61. L.CheckBool(3)
  62. return 0
  63. }, "boolean expected, got number")
  64. }
  65. func TestCheckTable(t *testing.T) {
  66. L := NewState()
  67. defer L.Close()
  68. errorIfGFuncNotFail(t, L, func(L *LState) int {
  69. tbl := L.NewTable()
  70. L.Push(tbl)
  71. errorIfNotEqual(t, tbl, L.CheckTable(2))
  72. L.Push(LNumber(10))
  73. L.CheckTable(3)
  74. return 0
  75. }, "table expected, got number")
  76. }
  77. func TestCheckFunction(t *testing.T) {
  78. L := NewState()
  79. defer L.Close()
  80. errorIfGFuncNotFail(t, L, func(L *LState) int {
  81. fn := L.NewFunction(func(l *LState) int { return 0 })
  82. L.Push(fn)
  83. errorIfNotEqual(t, fn, L.CheckFunction(2))
  84. L.Push(LNumber(10))
  85. L.CheckFunction(3)
  86. return 0
  87. }, "function expected, got number")
  88. }
  89. func TestCheckUserData(t *testing.T) {
  90. L := NewState()
  91. defer L.Close()
  92. errorIfGFuncNotFail(t, L, func(L *LState) int {
  93. ud := L.NewUserData()
  94. L.Push(ud)
  95. errorIfNotEqual(t, ud, L.CheckUserData(2))
  96. L.Push(LNumber(10))
  97. L.CheckUserData(3)
  98. return 0
  99. }, "userdata expected, got number")
  100. }
  101. func TestCheckThread(t *testing.T) {
  102. L := NewState()
  103. defer L.Close()
  104. errorIfGFuncNotFail(t, L, func(L *LState) int {
  105. th, _ := L.NewThread()
  106. L.Push(th)
  107. errorIfNotEqual(t, th, L.CheckThread(2))
  108. L.Push(LNumber(10))
  109. L.CheckThread(3)
  110. return 0
  111. }, "thread expected, got number")
  112. }
  113. func TestCheckChannel(t *testing.T) {
  114. L := NewState()
  115. defer L.Close()
  116. errorIfGFuncNotFail(t, L, func(L *LState) int {
  117. ch := make(chan LValue)
  118. L.Push(LChannel(ch))
  119. errorIfNotEqual(t, ch, L.CheckChannel(2))
  120. L.Push(LString("aaa"))
  121. L.CheckChannel(3)
  122. return 0
  123. }, "channel expected, got string")
  124. }
  125. func TestCheckType(t *testing.T) {
  126. L := NewState()
  127. defer L.Close()
  128. errorIfGFuncNotFail(t, L, func(L *LState) int {
  129. L.Push(LNumber(10))
  130. L.CheckType(2, LTNumber)
  131. L.CheckType(2, LTString)
  132. return 0
  133. }, "string expected, got number")
  134. }
  135. func TestCheckTypes(t *testing.T) {
  136. L := NewState()
  137. defer L.Close()
  138. errorIfGFuncNotFail(t, L, func(L *LState) int {
  139. L.Push(LNumber(10))
  140. L.CheckTypes(2, LTString, LTBool, LTNumber)
  141. L.CheckTypes(2, LTString, LTBool)
  142. return 0
  143. }, "string or boolean expected, got number")
  144. }
  145. func TestCheckOption(t *testing.T) {
  146. opts := []string{
  147. "opt1",
  148. "opt2",
  149. "opt3",
  150. }
  151. L := NewState()
  152. defer L.Close()
  153. errorIfGFuncNotFail(t, L, func(L *LState) int {
  154. L.Push(LString("opt1"))
  155. errorIfNotEqual(t, 0, L.CheckOption(2, opts))
  156. L.Push(LString("opt5"))
  157. L.CheckOption(3, opts)
  158. return 0
  159. }, "invalid option: opt5 \\(must be one of opt1,opt2,opt3\\)")
  160. }
  161. func TestOptInt(t *testing.T) {
  162. L := NewState()
  163. defer L.Close()
  164. errorIfGFuncNotFail(t, L, func(L *LState) int {
  165. errorIfNotEqual(t, 99, L.OptInt(1, 99))
  166. L.Push(LNumber(10))
  167. errorIfNotEqual(t, 10, L.OptInt(2, 99))
  168. L.Push(LString("aaa"))
  169. L.OptInt(3, 99)
  170. return 0
  171. }, "number expected, got string")
  172. }
  173. func TestOptInt64(t *testing.T) {
  174. L := NewState()
  175. defer L.Close()
  176. errorIfGFuncNotFail(t, L, func(L *LState) int {
  177. errorIfNotEqual(t, int64(99), L.OptInt64(1, int64(99)))
  178. L.Push(LNumber(10))
  179. errorIfNotEqual(t, int64(10), L.OptInt64(2, int64(99)))
  180. L.Push(LString("aaa"))
  181. L.OptInt64(3, int64(99))
  182. return 0
  183. }, "number expected, got string")
  184. }
  185. func TestOptNumber(t *testing.T) {
  186. L := NewState()
  187. defer L.Close()
  188. errorIfGFuncNotFail(t, L, func(L *LState) int {
  189. errorIfNotEqual(t, LNumber(99), L.OptNumber(1, LNumber(99)))
  190. L.Push(LNumber(10))
  191. errorIfNotEqual(t, LNumber(10), L.OptNumber(2, LNumber(99)))
  192. L.Push(LString("aaa"))
  193. L.OptNumber(3, LNumber(99))
  194. return 0
  195. }, "number expected, got string")
  196. }
  197. func TestOptString(t *testing.T) {
  198. L := NewState()
  199. defer L.Close()
  200. errorIfGFuncNotFail(t, L, func(L *LState) int {
  201. errorIfNotEqual(t, "bbb", L.OptString(1, "bbb"))
  202. L.Push(LString("aaa"))
  203. errorIfNotEqual(t, "aaa", L.OptString(2, "bbb"))
  204. L.Push(LNumber(10))
  205. L.OptString(3, "bbb")
  206. return 0
  207. }, "string expected, got number")
  208. }
  209. func TestOptBool(t *testing.T) {
  210. L := NewState()
  211. defer L.Close()
  212. errorIfGFuncNotFail(t, L, func(L *LState) int {
  213. errorIfNotEqual(t, true, L.OptBool(1, true))
  214. L.Push(LTrue)
  215. errorIfNotEqual(t, true, L.OptBool(2, false))
  216. L.Push(LNumber(10))
  217. L.OptBool(3, false)
  218. return 0
  219. }, "boolean expected, got number")
  220. }
  221. func TestOptTable(t *testing.T) {
  222. L := NewState()
  223. defer L.Close()
  224. errorIfGFuncNotFail(t, L, func(L *LState) int {
  225. deftbl := L.NewTable()
  226. errorIfNotEqual(t, deftbl, L.OptTable(1, deftbl))
  227. tbl := L.NewTable()
  228. L.Push(tbl)
  229. errorIfNotEqual(t, tbl, L.OptTable(2, deftbl))
  230. L.Push(LNumber(10))
  231. L.OptTable(3, deftbl)
  232. return 0
  233. }, "table expected, got number")
  234. }
  235. func TestOptFunction(t *testing.T) {
  236. L := NewState()
  237. defer L.Close()
  238. errorIfGFuncNotFail(t, L, func(L *LState) int {
  239. deffn := L.NewFunction(func(l *LState) int { return 0 })
  240. errorIfNotEqual(t, deffn, L.OptFunction(1, deffn))
  241. fn := L.NewFunction(func(l *LState) int { return 0 })
  242. L.Push(fn)
  243. errorIfNotEqual(t, fn, L.OptFunction(2, deffn))
  244. L.Push(LNumber(10))
  245. L.OptFunction(3, deffn)
  246. return 0
  247. }, "function expected, got number")
  248. }
  249. func TestOptUserData(t *testing.T) {
  250. L := NewState()
  251. defer L.Close()
  252. errorIfGFuncNotFail(t, L, func(L *LState) int {
  253. defud := L.NewUserData()
  254. errorIfNotEqual(t, defud, L.OptUserData(1, defud))
  255. ud := L.NewUserData()
  256. L.Push(ud)
  257. errorIfNotEqual(t, ud, L.OptUserData(2, defud))
  258. L.Push(LNumber(10))
  259. L.OptUserData(3, defud)
  260. return 0
  261. }, "userdata expected, got number")
  262. }
  263. func TestOptChannel(t *testing.T) {
  264. L := NewState()
  265. defer L.Close()
  266. errorIfGFuncNotFail(t, L, func(L *LState) int {
  267. defch := make(chan LValue)
  268. errorIfNotEqual(t, defch, L.OptChannel(1, defch))
  269. ch := make(chan LValue)
  270. L.Push(LChannel(ch))
  271. errorIfNotEqual(t, ch, L.OptChannel(2, defch))
  272. L.Push(LString("aaa"))
  273. L.OptChannel(3, defch)
  274. return 0
  275. }, "channel expected, got string")
  276. }
  277. func TestLoadFileForShebang(t *testing.T) {
  278. tmpFile, err := os.CreateTemp("", "")
  279. errorIfNotNil(t, err)
  280. err = os.WriteFile(tmpFile.Name(), []byte(`#!/path/to/lua
  281. print("hello")
  282. `), 0644)
  283. errorIfNotNil(t, err)
  284. defer func() {
  285. tmpFile.Close()
  286. os.Remove(tmpFile.Name())
  287. }()
  288. L := NewState()
  289. defer L.Close()
  290. _, err = L.LoadFile(tmpFile.Name())
  291. errorIfNotNil(t, err)
  292. }
  293. func TestLoadFileForEmptyFile(t *testing.T) {
  294. tmpFile, err := os.CreateTemp("", "")
  295. errorIfNotNil(t, err)
  296. defer func() {
  297. tmpFile.Close()
  298. os.Remove(tmpFile.Name())
  299. }()
  300. L := NewState()
  301. defer L.Close()
  302. _, err = L.LoadFile(tmpFile.Name())
  303. errorIfNotNil(t, err)
  304. }