script_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package lua
  2. import (
  3. "fmt"
  4. "github.com/yuin/gopher-lua/parse"
  5. "os"
  6. "testing"
  7. )
  8. const maxMemory = 40
  9. var gluaTests []string = []string{
  10. "base.lua",
  11. "coroutine.lua",
  12. "db.lua",
  13. "issues.lua",
  14. "os.lua",
  15. "table.lua",
  16. "vm.lua",
  17. "math.lua",
  18. "strings.lua",
  19. }
  20. var luaTests []string = []string{
  21. "attrib.lua",
  22. "calls.lua",
  23. "closure.lua",
  24. "constructs.lua",
  25. "events.lua",
  26. "literals.lua",
  27. "locals.lua",
  28. "math.lua",
  29. "sort.lua",
  30. "strings.lua",
  31. "vararg.lua",
  32. "pm.lua",
  33. "files.lua",
  34. }
  35. func testScriptCompile(t *testing.T, script string) {
  36. file, err := os.Open(script)
  37. if err != nil {
  38. t.Fatal(err)
  39. return
  40. }
  41. chunk, err2 := parse.Parse(file, script)
  42. if err2 != nil {
  43. t.Fatal(err2)
  44. return
  45. }
  46. parse.Dump(chunk)
  47. proto, err3 := Compile(chunk, script)
  48. if err3 != nil {
  49. t.Fatal(err3)
  50. return
  51. }
  52. proto.String()
  53. }
  54. func testScriptDir(t *testing.T, tests []string, directory string) {
  55. if err := os.Chdir(directory); err != nil {
  56. t.Error(err)
  57. }
  58. defer os.Chdir("..")
  59. for _, script := range tests {
  60. fmt.Printf("testing %s/%s\n", directory, script)
  61. testScriptCompile(t, script)
  62. L := NewState(Options{
  63. RegistrySize: 1024 * 20,
  64. CallStackSize: 1024,
  65. })
  66. L.SetMx(maxMemory)
  67. if err := L.DoFile(script); err != nil {
  68. t.Error(err)
  69. }
  70. L.Close()
  71. }
  72. }
  73. func TestGlua(t *testing.T) {
  74. testScriptDir(t, gluaTests, "_glua-tests")
  75. }
  76. func TestLua(t *testing.T) {
  77. testScriptDir(t, luaTests, "_lua5.1-tests")
  78. }