Debian Codegraph @

helpers_test.go

raw
1 package luar
2
3 import (
4 "runtime/debug"
5 "strconv"
6 "strings"
7 "testing"
8
9 "github.com/yuin/gopher-lua"
10 )
11
12 type StructTestPerson struct {
13 Name string
14 Age int
15 Friend *StructTestPerson
16 LastAddSum int
17 }
18
19 func (p StructTestPerson) Hello() string {
20 return "Hello, " + p.Name
21 }
22
23 func (p StructTestPerson) String() string {
24 return p.Name + " (" + strconv.Itoa(p.Age) + ")"
25 }
26
27 func (p *StructTestPerson) AddNumbers(L *LState) int {
28 sum := 0
29 for i := L.GetTop(); i >= 1; i-- {
30 sum += L.CheckInt(i)
31 }
32 L.Push(lua.LString(p.Name + " counts: " + strconv.Itoa(sum)))
33 p.LastAddSum = sum
34 return 1
35 }
36
37 func (p *StructTestPerson) IncreaseAge() {
38 p.Age++
39 }
40
41 func testReturn(t *testing.T, L *lua.LState, code string, values ...string) {
42 top := L.GetTop()
43 if err := L.DoString(code); err != nil {
44 t.Fatalf("%s\n\n%s", err, debug.Stack())
45 }
46
47 valid := true
48 newTop := L.GetTop()
49
50 if newTop-top != len(values) {
51 valid = false
52 } else {
53 for i, expect := range values {
54 // TODO: strong typing
55 val := L.Get(top + i + 1).String()
56 if val != expect {
57 valid = false
58 }
59 }
60 }
61
62 if !valid {
63 got := make([]string, newTop-top)
64 for i := 0; i < len(got); i++ {
65 got[i] = L.Get(top + i + 1).String()
66 }
67
68 t.Fatalf("bad return values: expecting %#v, got %#v\n\n%s", values, got, debug.Stack())
69 }
70
71 L.SetTop(top)
72 }
73
74 func testError(t *testing.T, L *lua.LState, code, error string) {
75 err := L.DoString(code)
76 if err == nil {
77 t.Fatalf("expecting error, got nil\n\n%s", debug.Stack())
78 }
79
80 if s := err.Error(); strings.Index(s, error) == -1 {
81 t.Fatalf("error substring '%s' not found in '%s'\n\n%s", error, s, debug.Stack())
82 }
83 }