auxlib_test.go 7.8 KB

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