-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_parser_test.go
197 lines (173 loc) · 4.86 KB
/
script_parser_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main_test
import (
"runtime"
"errors"
. "github.com/1egoman/slick"
"github.com/1egoman/slick/frontend"
"github.com/yuin/gopher-lua"
"testing"
)
func TestEmitEventNoError(t *testing.T) {
state := NewInitialStateMode("chat")
state.EventActions = append(state.EventActions, EventAction{
Type: EVENT_CONNECTION_CHANGE,
Handler: func(state *State, metadata *map[string]string) error {
return nil
},
})
err := EmitEvent(state, EVENT_CONNECTION_CHANGE, map[string]string{})
if err != nil {
t.Errorf("Error was returned when emitting event: %s", err)
}
}
func TestEmitEventError(t *testing.T) {
state := NewInitialStateMode("chat")
state.EventActions = append(state.EventActions, EventAction{
Type: EVENT_CONNECTION_CHANGE,
Handler: func(state *State, metadata *map[string]string) error {
return errors.New("my error")
},
})
err := EmitEvent(state, EVENT_CONNECTION_CHANGE, map[string]string{})
if err.Error() != "my error" {
t.Errorf("Error 'my error' was not returned when emitting event: %s", err)
}
}
var noopFunction lua.LFunction = lua.LFunction{}
var scriptGlobals map[string][]interface{} = map[string][]interface{}{
"print": []interface{}{"foo"},
"error": []interface{}{"bar"},
"clear": []interface{}{},
"keymap": []interface{}{"k", noopFunction},
"command": []interface{}{"CmdName", "desc", "[one] <two>", noopFunction},
"getenv": []interface{}{"FOO"},
"shell": []interface{}{"date"},
"sendmessage": []interface{}{"foo"},
"getclip": []interface{}{},
"setclip": []interface{}{},
"onevent": []interface{}{"connectionchange", noopFunction},
}
func TestScriptEnvironmentConstruction(t *testing.T) {
state := NewInitialStateMode("chat")
term := frontend.NewTerminalDisplay(nil)
L := lua.NewState()
defer L.Close()
AddSlickStandardLib(L, state, term)
for global, _ := range scriptGlobals {
if L.GetGlobal(global) == nil {
t.Errorf("Global `%s` isn't defined", global)
}
}
}
// func TestScriptSendMessage(t *testing.T) {
// for global, value := range scriptGlobals {
// state := NewInitialStateMode("chat")
// state.Connections = []gateway.Connection{
// gatewaySlack.New("token"),
// }
// state.ActiveConnection().SetSelectedChannel(&gateway.Channel{Id: "channel-id"})
// term := frontend.NewTerminalDisplay(nil)
//
// L := lua.NewState()
// defer L.Close()
//
// AddSlickStandardLib(L, state, term)
//
// args := lua.NewState()
// defer args.Close()
// for _, item := range value {
// switch item.(type) {
// case string:
// args.Push(lua.LString(item.(string)))
// case lua.LFunction:
// args.Push(L.NewFunction(func(L *lua.LState) int { return 0 }))
// }
// }
//
// fn := *(L.GetGlobal(global).(*lua.LFunction))
// fn.GFunction(args)
// err := args.Get(-1)
//
// if err != lua.LNil {
// t.Errorf("Error sending message with `sendmessage`: %s", err)
// }
// }
// }
// A utility function used below in `TestStructToTable`
func assertLuaEqual(t *testing.T, a lua.LValue, b lua.LValue) {
_, file, line, _ := runtime.Caller(1)
if a.String() != b.String() {
t.Errorf("Assertion failed %s:%d: %s != %s", file, line, a, b)
}
}
func TestStructToTable(t *testing.T) {
type MyStruct struct {
Foo int
Bar string
Baz bool
Quux int
Hello []int
}
instance := MyStruct{
Foo: 5,
Bar: "hello world",
Baz: false,
/* no Quux key */
Hello: []int{1, 2, 3}, // A complex key that can't be converted
}
L := lua.NewState()
defer L.Close()
table := StructToTable(L, instance)
assertLuaEqual(t, table.RawGet(lua.LString("Foo")), lua.LNumber(5))
assertLuaEqual(t, table.RawGet(lua.LString("Bar")), lua.LString("hello world"))
assertLuaEqual(t, table.RawGet(lua.LString("Baz")), lua.LBool(false))
// An unset key
assertLuaEqual(t, table.RawGet(lua.LString("Quux")), lua.LNumber(0))
// A complex value like a slice or map
assertLuaEqual(t, table.RawGet(lua.LString("Hello")), lua.LNil)
}
func TestStructToTableRecursiveStruct(t *testing.T) {
type Foo struct {
Hello string
}
type MyStruct struct {
Foo Foo
Bar *Foo
}
instance := MyStruct{
Foo: Foo{Hello: "world"},
Bar: &Foo{Hello: "Bob"},
}
L := lua.NewState()
defer L.Close()
table := StructToTable(L, instance)
assertLuaEqual(
t,
table.RawGet(lua.LString("Foo")).(*lua.LTable).RawGet(lua.LString("Hello")),
lua.LString("world"),
)
assertLuaEqual(
t,
table.RawGet(lua.LString("Bar")).(*lua.LTable).RawGet(lua.LString("Hello")),
lua.LString("Bob"),
)
assertLuaEqual(
t,
table.RawGet(lua.LString("Baz")),
lua.LNil,
)
}
// Ensure that a nil pointer is properly skipped over
func TestStructToTableNilPointer(t *testing.T) {
type Foo struct {
Hello string
}
type MyStruct struct {
Foo *Foo
}
instance := MyStruct{ Foo: nil }
L := lua.NewState()
defer L.Close()
table := StructToTable(L, instance)
assertLuaEqual(t, table.RawGet(lua.LString("Foo")), lua.LNil)
}