auxlib_test.go 7.6 KB

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