github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/object/reflect/reflect_test.go (about)

     1  package reflect_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hirochachacha/plua/compiler"
     8  	"github.com/hirochachacha/plua/object"
     9  	"github.com/hirochachacha/plua/object/reflect"
    10  	"github.com/hirochachacha/plua/runtime"
    11  	"github.com/hirochachacha/plua/stdlib"
    12  )
    13  
    14  type myint int
    15  
    16  func (i myint) Hello() string { return "hello" }
    17  
    18  type myArray [4]int
    19  
    20  func (m *myArray) Hello() string { return "hello" }
    21  
    22  var testCases = []struct {
    23  	Map  map[string]interface{}
    24  	Code string
    25  }{
    26  	// builtins
    27  	{
    28  		map[string]interface{}{"i": 10},
    29  		`assert(type(i) == "number" and i == 10)`,
    30  	},
    31  	{
    32  		map[string]interface{}{"i": int8(10)},
    33  		`assert(type(i) == "number" and i == 10)`,
    34  	},
    35  	{
    36  		map[string]interface{}{"i": int32(10)},
    37  		`assert(type(i) == "number" and i == 10)`,
    38  	},
    39  	{
    40  		map[string]interface{}{"b": false},
    41  		`assert(type(b) == "boolean" and b == false)`,
    42  	},
    43  	{
    44  		map[string]interface{}{"s": "test"},
    45  		`assert(type(s) == "string" and s == "test")`,
    46  	},
    47  
    48  	// uint
    49  	{
    50  		map[string]interface{}{
    51  			"u1": uint(10),
    52  			"u2": uint(10),
    53  			"u3": uint(15),
    54  		},
    55  		`
    56  		assert(type(u1) == "userdata")
    57  		-- assert(u1 == 10)
    58  		assert(u1 == u2)
    59  		assert(u1+5 == u3)
    60  		`,
    61  	},
    62  
    63  	// array
    64  	{
    65  		map[string]interface{}{
    66  			"a1": [5]int{1, 2, 3, 4, 5},
    67  			"a2": [5]int{1, 2, 3, 4, 5},
    68  		},
    69  		`
    70  		assert(type(a1) == "userdata")
    71  		assert(a1 == a2)
    72  		assert(a1[1] == 1)
    73  		assert(a1["test"] == nil)
    74  		`,
    75  	},
    76  
    77  	// slice
    78  	{
    79  		map[string]interface{}{
    80  			"a": []int{1, 2, 3, 4, 5},
    81  		},
    82  		`
    83  		assert(type(a) == "userdata")
    84  		assert(a[1] == 1)
    85  		assert(a["test"] == nil)
    86  		a[1] = 6
    87  		assert(a[1] == 6)
    88  		`,
    89  	},
    90  
    91  	// alias types
    92  	{
    93  		map[string]interface{}{
    94  			"i": myint(10),
    95  		},
    96  		`
    97  		assert(type(i) == "userdata")
    98  		assert(i:Hello() == "hello")
    99  		`,
   100  	},
   101  	{
   102  		map[string]interface{}{
   103  			"a": myArray{1, 2, 3},
   104  		},
   105  		`
   106  		assert(type(a) == "userdata")
   107  		assert(a:Hello() == "hello")
   108  		`,
   109  	},
   110  }
   111  
   112  func TestReflect(t *testing.T) {
   113  	c := compiler.NewCompiler()
   114  
   115  	for i, test := range testCases {
   116  		proto, err := c.Compile(strings.NewReader(test.Code), "=test_code", 0)
   117  		if err != nil {
   118  			t.Fatalf("%d: %v", i+1, err)
   119  		}
   120  
   121  		p := runtime.NewProcess()
   122  
   123  		p.Require("", stdlib.Open)
   124  
   125  		g := p.Globals()
   126  		for k, v := range test.Map {
   127  			g.Set(object.String(k), reflect.ValueOf(v))
   128  		}
   129  
   130  		_, err = p.Exec(proto)
   131  		if err != nil {
   132  			t.Errorf("%d: %v", i+1, err)
   133  		}
   134  	}
   135  }