github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/metatable_test.go (about)

     1  package luar
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dfcfw/lua"
     7  )
     8  
     9  func Test_metatable(t *testing.T) {
    10  	L := lua.NewState()
    11  	defer L.Close()
    12  
    13  	tbl := []struct {
    14  		Value    interface{}
    15  		CustomMT bool
    16  	}{
    17  		{"hello", false},
    18  		{123, false},
    19  		{1.23, false},
    20  		{nil, false},
    21  		{struct{}{}, true},
    22  		{&struct{}{}, true},
    23  		{[]string{}, true},
    24  		{make(chan string), true},
    25  		{(*string)(nil), true},
    26  		{func() {}, false},
    27  		{map[string]int{}, true},
    28  	}
    29  
    30  	for _, v := range tbl {
    31  		mt := MT(L, v.Value)
    32  		if v.CustomMT && mt == nil {
    33  			t.Fatalf("expected to have custom MT for %#v\n", v.Value)
    34  		} else if !v.CustomMT && mt != nil {
    35  			t.Fatalf("unexpected custom MT for %#v\n", v.Value)
    36  		}
    37  	}
    38  }