github.com/xrash/gopher-lua@v0.0.0-20160304065408-e5faab4db06a/script_test.go (about)

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