1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package luar
import (
"testing"
"github.com/yuin/gopher-lua"
)
func Test_slice(t *testing.T) {
L := lua.NewState()
defer L.Close()
things := []string{
"cake",
"wallet",
"calendar",
"phone",
"speaker",
}
L.SetGlobal("things", New(L, things))
testReturn(t, L, `return #things, things[1], things[5]`, "5", "cake", "speaker")
if err := L.DoString(`things[1] = "cookie"`); err != nil {
t.Fatal(err)
}
if things[0] != "cookie" {
t.Fatalf(`expected things[0] = "cookie", got %s`, things[0])
}
}
func Test_slice_2(t *testing.T) {
L := lua.NewState()
defer L.Close()
items := make([]string, 0, 10)
L.SetGlobal("items", New(L, items))
testReturn(t, L, `return #items`, "0")
testReturn(t, L, `items = items + "hello" + "world"; return #items`, "2")
testReturn(t, L, `return items[1]`, "hello")
testReturn(t, L, `return items[2]`, "world")
}
func Test_slice_iterator(t *testing.T) {
L := lua.NewState()
defer L.Close()
s := []string{
"hello",
"there",
}
e := []string{}
L.SetGlobal("s", New(L, s))
L.SetGlobal("e", New(L, e))
testReturn(t, L, `local itr = s(); local a, b = itr(); local c, d = itr(); return a, b, c, d`, "1", "hello", "2", "there")
testReturn(t, L, `local itr = e(); local a, b = itr(); return a, b`, "nil", "nil")
}
|