| 1 |
package luar |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/yuin/gopher-lua" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
func Test_ptr(t *testing.T) { |
| 11 |
L := lua.NewState() |
| 12 |
defer L.Close() |
| 13 |
|
| 14 |
str := "hello" |
| 15 |
|
| 16 |
L.SetGlobal("ptr", New(L, &str)) |
| 17 |
|
| 18 |
testReturn(t, L, `return -ptr`, "hello") |
| 19 |
} |
| 20 |
|
| 21 |
func Test_ptr_comparison(t *testing.T) { |
| 22 |
L := lua.NewState() |
| 23 |
defer L.Close() |
| 24 |
|
| 25 |
var ptr1 *string |
| 26 |
str := "hello" |
| 27 |
|
| 28 |
L.SetGlobal("ptr1", New(L, ptr1)) |
| 29 |
L.SetGlobal("ptr2", New(L, &str)) |
| 30 |
|
| 31 |
testReturn(t, L, `return ptr1 == nil`, "true") |
| 32 |
testReturn(t, L, `return ptr2 == nil`, "false") |
| 33 |
testReturn(t, L, `return ptr1 == ptr2`, "false") |
| 34 |
} |
| 35 |
|
| 36 |
func Test_ptr_assignment(t *testing.T) { |
| 37 |
L := lua.NewState() |
| 38 |
defer L.Close() |
| 39 |
|
| 40 |
str := "hello" |
| 41 |
|
| 42 |
L.SetGlobal("str", New(L, &str)) |
| 43 |
|
| 44 |
testReturn(t, L, `return tostring(-str)`, "hello") |
| 45 |
testReturn(t, L, `str = str ^ "world"; return (string.match(tostring(str), "userdata:"))`, "userdata:") |
| 46 |
testReturn(t, L, `return tostring(-str)`, "world") |
| 47 |
} |
| 48 |
|
| 49 |
type TestPtrNested struct { |
| 50 |
*TestPtrNestedChild |
| 51 |
} |
| 52 |
|
| 53 |
type TestPtrNestedChild struct { |
| 54 |
Value *string |
| 55 |
StructTestPerson |
| 56 |
} |
| 57 |
|
| 58 |
func Test_ptr_nested(t *testing.T) { |
| 59 |
L := lua.NewState() |
| 60 |
defer L.Close() |
| 61 |
|
| 62 |
a := TestPtrNested{ |
| 63 |
TestPtrNestedChild: &TestPtrNestedChild{ |
| 64 |
StructTestPerson: StructTestPerson{ |
| 65 |
Name: "Tim", |
| 66 |
}, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
L.SetGlobal("a", New(L, &a)) |
| 71 |
L.SetGlobal("str_ptr", NewType(L, "")) |
| 72 |
|
| 73 |
testReturn(t, L, `return a.Value == nil`, "true") |
| 74 |
testReturn(t, L, `a.Value = str_ptr(); _ = a.Value ^ "hello"`) |
| 75 |
testReturn(t, L, `return a.Value == nil`, "false") |
| 76 |
testReturn(t, L, `return -a.Value`, "hello") |
| 77 |
testReturn(t, L, `return a.Name`, "Tim") |
| 78 |
} |
| 79 |
|
| 80 |
func Test_ptr_assignstruct(t *testing.T) { |
| 81 |
L := lua.NewState() |
| 82 |
defer L.Close() |
| 83 |
|
| 84 |
a := &StructTestPerson{ |
| 85 |
Name: "tim", |
| 86 |
} |
| 87 |
b := &StructTestPerson{ |
| 88 |
Name: "bob", |
| 89 |
} |
| 90 |
|
| 91 |
L.SetGlobal("a", New(L, a)) |
| 92 |
L.SetGlobal("b", New(L, b)) |
| 93 |
|
| 94 |
testReturn(t, L, `return a.Name`, "tim") |
| 95 |
testReturn(t, L, `_ = a ^ -b`) |
| 96 |
testReturn(t, L, `return a.Name`, "bob") |
| 97 |
} |
| 98 |
|
| 99 |
type TestStringType string |
| 100 |
|
| 101 |
func (s *TestStringType) ToUpper() { |
| 102 |
*s = TestStringType(strings.ToUpper(string(*s))) |
| 103 |
} |
| 104 |
|
| 105 |
func Test_ptr_nested2(t *testing.T) { |
| 106 |
L := lua.NewState() |
| 107 |
defer L.Close() |
| 108 |
|
| 109 |
a := [...]StructTestPerson{ |
| 110 |
{Name: "Tim"}, |
| 111 |
} |
| 112 |
s := []StructTestPerson{ |
| 113 |
{Name: "Tim", Age: 32}, |
| 114 |
} |
| 115 |
|
| 116 |
str := TestStringType("Hello World") |
| 117 |
|
| 118 |
L.SetGlobal("a", New(L, &a)) |
| 119 |
L.SetGlobal("s", New(L, s)) |
| 120 |
L.SetGlobal("p", New(L, &s[0])) |
| 121 |
L.SetGlobal("str", New(L, &str)) |
| 122 |
|
| 123 |
testReturn(t, L, `return a[1]:AddNumbers(1, 2, 3, 4, 5)`, "Tim counts: 15") |
| 124 |
testReturn(t, L, `return s[1]:AddNumbers(1, 2, 3, 4)`, "Tim counts: 10") |
| 125 |
testReturn(t, L, `return s[1].LastAddSum`, "10") |
| 126 |
testReturn(t, L, `return p:AddNumbers(1, 2, 3, 4, 5)`, "Tim counts: 15") |
| 127 |
testReturn(t, L, `return p.LastAddSum`, "15") |
| 128 |
|
| 129 |
testReturn(t, L, `return p.Age`, "32") |
| 130 |
testReturn(t, L, `p:IncreaseAge(); return p.Age`, "33") |
| 131 |
|
| 132 |
testReturn(t, L, `return -str`, "Hello World") |
| 133 |
testReturn(t, L, `str:ToUpper(); return -str`, "HELLO WORLD") |
| 134 |
} |