github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/base/base_test.go (about)

     1  package base_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hirochachacha/plua/compiler"
    10  	"github.com/hirochachacha/plua/object"
    11  	"github.com/hirochachacha/plua/runtime"
    12  	"github.com/hirochachacha/plua/stdlib/base"
    13  	"github.com/hirochachacha/plua/stdlib/math"
    14  )
    15  
    16  type execCase struct {
    17  	Code      string
    18  	Rets      []object.Value
    19  	ErrString string
    20  }
    21  
    22  var testAsserts = []execCase{
    23  	{
    24  		`return assert("hello", "world")`,
    25  		[]object.Value{object.String("hello"), object.String("world")},
    26  		"",
    27  	},
    28  	{
    29  		`assert(nil)`,
    30  		nil,
    31  		"assertion failed!",
    32  	},
    33  	{
    34  		`assert(false)`,
    35  		nil,
    36  		"assertion failed!",
    37  	},
    38  	{
    39  		`assert(false, "hello")`,
    40  		nil,
    41  		"hello",
    42  	},
    43  	{
    44  		`assert()`,
    45  		nil,
    46  		"got no value",
    47  	},
    48  	{
    49  		`function x() assert() end; x()`,
    50  		nil,
    51  		"got no value",
    52  	},
    53  	{
    54  		`t = {x = assert}; t.x()`,
    55  		nil,
    56  		"got no value",
    57  	},
    58  
    59  	{
    60  		`assert()`,
    61  		nil,
    62  		"bad argument #1 to 'assert'",
    63  	},
    64  	{
    65  		`function x() assert() end; x()`,
    66  		nil,
    67  		"bad argument #1 to 'assert'",
    68  	},
    69  	{
    70  		`x = assert; x()`,
    71  		nil,
    72  		"bad argument #1 to 'x'",
    73  	},
    74  	{
    75  		`t = {x = assert}; t.x()`,
    76  		nil,
    77  		"bad argument #1 to 'x'",
    78  	},
    79  }
    80  
    81  func TestAssert(t *testing.T) {
    82  	testExecCases(t, "test_assert", testAsserts)
    83  }
    84  
    85  var testErrors = []execCase{
    86  	{
    87  		`
    88  
    89  		error("test_message")
    90  		`,
    91  		nil,
    92  		`runtime: test_error:3: test_message`,
    93  	},
    94  	{
    95  		`
    96  		function x()
    97  		  error("test_message")
    98  		end
    99  
   100  		x()
   101  		`,
   102  		nil,
   103  		`runtime: test_error:3: test_message`,
   104  	},
   105  }
   106  
   107  func TestError(t *testing.T) {
   108  	testExecCases(t, "test_error", testErrors)
   109  }
   110  
   111  var testCollectGarbages = []execCase{
   112  	{
   113  		`return collectgarbage()`,
   114  		[]object.Value{object.Integer(0)},
   115  		"",
   116  	},
   117  	{
   118  		`return collectgarbage("collect")`,
   119  		[]object.Value{object.Integer(0)},
   120  		"",
   121  	},
   122  	{
   123  		`n = collectgarbage("count"); return type(n)`,
   124  		[]object.Value{object.String("number")},
   125  		"",
   126  	},
   127  	{
   128  		`return collectgarbage("isrunning")`,
   129  		[]object.Value{object.True},
   130  		"",
   131  	},
   132  	{
   133  		`collectgarbage("stop")`,
   134  		nil,
   135  		"not implemented",
   136  	},
   137  	{
   138  		`collectgarbage("restart")`,
   139  		nil,
   140  		"not implemented",
   141  	},
   142  	{
   143  		`collectgarbage("step")`,
   144  		nil,
   145  		"not implemented",
   146  	},
   147  	{
   148  		`collectgarbage("setpause")`,
   149  		nil,
   150  		"not implemented",
   151  	},
   152  	{
   153  		`collectgarbage("setstepmul")`,
   154  		nil,
   155  		"not implemented",
   156  	},
   157  	{
   158  		`collectgarbage("testtesttest")`,
   159  		nil,
   160  		"invalid option",
   161  	},
   162  }
   163  
   164  func TestCollectGarbage(t *testing.T) {
   165  	testExecCases(t, "test_collectgarbage", testCollectGarbages)
   166  }
   167  
   168  var testDoFiles = []execCase{
   169  	{
   170  		`return dofile("testdata/do/do.lua")`,
   171  		[]object.Value{object.True},
   172  		"",
   173  	},
   174  	// {
   175  	// `assert(dofile("testdata/do/notexist"))`,
   176  	// nil,
   177  	// "no such file",
   178  	// },
   179  	{
   180  		`assert(dofile("testdata/do/not.lua"))`,
   181  		nil,
   182  		"expected",
   183  	},
   184  	{
   185  		`dofile("testdata/do/do_err.lua")`,
   186  		nil,
   187  		"true",
   188  	},
   189  }
   190  
   191  func TestDoFile(t *testing.T) {
   192  	testExecCases(t, "test_dofile", testDoFiles)
   193  }
   194  
   195  func testExecCases(t *testing.T, testname string, tests []execCase) {
   196  	c := compiler.NewCompiler()
   197  
   198  	for _, test := range tests {
   199  		proto, err := c.Compile(strings.NewReader(test.Code), "="+testname, 0)
   200  		if err != nil {
   201  			t.Fatal(err)
   202  		}
   203  
   204  		p := runtime.NewProcess()
   205  
   206  		p.Require("_G", base.Open)
   207  
   208  		rets, err := p.Exec(proto)
   209  		if err != nil {
   210  			if test.ErrString == "" {
   211  				t.Fatal(err)
   212  			}
   213  			if !strings.Contains(err.Error(), test.ErrString) {
   214  				t.Errorf("code: `%s`, err: %v, expected: %v\n", test.Code, err, test.ErrString)
   215  			}
   216  		}
   217  		if !reflect.DeepEqual(rets, test.Rets) {
   218  			t.Errorf("code: `%s`, rets: %v, expected: %v\n", test.Code, rets, test.Rets)
   219  		}
   220  	}
   221  }
   222  
   223  func TestBase(t *testing.T) {
   224  	c := compiler.NewCompiler()
   225  
   226  	matches, err := filepath.Glob("testdata/*.lua")
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	for _, fname := range matches {
   232  		proto, err := c.CompileFile(fname, 0)
   233  		if err != nil {
   234  			t.Fatal(err)
   235  		}
   236  
   237  		p := runtime.NewProcess()
   238  
   239  		p.Require("_G", base.Open)
   240  		p.Require("math", math.Open)
   241  
   242  		_, err = p.Exec(proto)
   243  		if err != nil {
   244  			t.Error(err)
   245  		}
   246  	}
   247  }