|
1 | 1 | package mapper |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
@@ -129,3 +130,168 @@ func TestToGFunction(t *testing.T) { |
129 | 130 | ) |
130 | 131 | } |
131 | 132 | } |
| 133 | + |
| 134 | +func TestToGFunctionInteroperability(t *testing.T) { |
| 135 | + type testCase struct { |
| 136 | + name string |
| 137 | + code string |
| 138 | + args map[string][]interface{} |
| 139 | + exports map[string]func(*testing.T, func(...interface{})) interface{} |
| 140 | + err error |
| 141 | + } |
| 142 | + |
| 143 | + type state struct { |
| 144 | + called bool |
| 145 | + args []interface{} |
| 146 | + } |
| 147 | + |
| 148 | + var ( |
| 149 | + samples = []testCase{ |
| 150 | + { |
| 151 | + name: "single return value", |
| 152 | + code: `local m = require("module"); m.fn1(m.fn2())`, |
| 153 | + args: map[string][]interface{}{ |
| 154 | + "fn1": []interface{}{1}, |
| 155 | + "fn2": nil, |
| 156 | + }, |
| 157 | + exports: map[string]func(*testing.T, func(...interface{})) interface{}{ |
| 158 | + "fn1": func(t *testing.T, callback func(...interface{})) interface{} { |
| 159 | + return func(x int) { |
| 160 | + callback(x) |
| 161 | + } |
| 162 | + }, |
| 163 | + "fn2": func(t *testing.T, callback func(...interface{})) interface{} { |
| 164 | + return func() int { |
| 165 | + callback() |
| 166 | + return 1 |
| 167 | + } |
| 168 | + }, |
| 169 | + }, |
| 170 | + err: nil, |
| 171 | + }, |
| 172 | + // FIXME: This one is broken, we should not zip out args with in args automagicaly |
| 173 | + // { |
| 174 | + // name: "multiple return value", |
| 175 | + // code: `local m = require("module"); m.fn1(m.fn2(), m.fn3())`, |
| 176 | + // args: map[string][]interface{}{ |
| 177 | + // "fn1": []interface{}{1, "hello", 1, "hello"}, |
| 178 | + // "fn2": nil, |
| 179 | + // }, |
| 180 | + // exports: map[string]func(*testing.T, func(...interface{})) interface{}{ |
| 181 | + // "fn1": func(t *testing.T, callback func(...interface{})) interface{} { |
| 182 | + // return func(x int, y string, xx int, yy string) { |
| 183 | + // callback(x, y, xx, yy) |
| 184 | + // } |
| 185 | + // }, |
| 186 | + // "fn2": func(t *testing.T, callback func(...interface{})) interface{} { |
| 187 | + // return func() (int, string) { |
| 188 | + // callback() |
| 189 | + // return 1, "hello" |
| 190 | + // } |
| 191 | + // }, |
| 192 | + // "fn3": func(t *testing.T, callback func(...interface{})) interface{} { |
| 193 | + // return func() (int, string) { |
| 194 | + // callback() |
| 195 | + // return 1, "hello" |
| 196 | + // } |
| 197 | + // }, |
| 198 | + // }, |
| 199 | + // err: nil, |
| 200 | + // }, |
| 201 | + { |
| 202 | + name: "errors", |
| 203 | + code: `local m = require("module"); m.fn1(m.fn2())`, |
| 204 | + args: map[string][]interface{}{ |
| 205 | + "fn1": []interface{}{ |
| 206 | + errors.New("we bring you an interface{} so you could interface{} while interface{}"), |
| 207 | + }, |
| 208 | + "fn2": nil, |
| 209 | + }, |
| 210 | + exports: map[string]func(*testing.T, func(...interface{})) interface{}{ |
| 211 | + "fn1": func(t *testing.T, callback func(...interface{})) interface{} { |
| 212 | + return func(x error) { |
| 213 | + callback(x) |
| 214 | + } |
| 215 | + }, |
| 216 | + "fn2": func(t *testing.T, callback func(...interface{})) interface{} { |
| 217 | + return func() error { |
| 218 | + callback() |
| 219 | + return errors.New("we bring you an interface{} so you could interface{} while interface{}") |
| 220 | + } |
| 221 | + }, |
| 222 | + }, |
| 223 | + err: nil, |
| 224 | + }, |
| 225 | + } |
| 226 | + ) |
| 227 | + |
| 228 | + for _, sample := range samples { |
| 229 | + t.Run( |
| 230 | + sample.name, |
| 231 | + func(t *testing.T) { |
| 232 | + var ( |
| 233 | + l = lua.NewState() |
| 234 | + s = map[string]*state{} |
| 235 | + loader func(l *lua.LState) int |
| 236 | + err error |
| 237 | + ) |
| 238 | + |
| 239 | + loader = func(l *lua.LState) int { |
| 240 | + var ( |
| 241 | + exports = map[string]lua.LGFunction{} |
| 242 | + fn lua.LGFunction |
| 243 | + module *lua.LTable |
| 244 | + err error |
| 245 | + ) |
| 246 | + |
| 247 | + for k, v := range sample.exports { |
| 248 | + fn, err = ToGFunction( |
| 249 | + v( |
| 250 | + t, |
| 251 | + func(name string) func(...interface{}) { |
| 252 | + return func(args ...interface{}) { |
| 253 | + s[name] = &state{ |
| 254 | + called: true, |
| 255 | + args: args, |
| 256 | + } |
| 257 | + } |
| 258 | + }(k), |
| 259 | + ), |
| 260 | + ) |
| 261 | + if err != nil { |
| 262 | + t.Error(err) |
| 263 | + return 0 |
| 264 | + } |
| 265 | + |
| 266 | + exports[k] = fn |
| 267 | + } |
| 268 | + |
| 269 | + module = l.SetFuncs(l.NewTable(), exports) |
| 270 | + |
| 271 | + l.Push(module) |
| 272 | + return 1 |
| 273 | + } |
| 274 | + |
| 275 | + l.PreloadModule("module", loader) |
| 276 | + |
| 277 | + for n := 0; n < 1; n++ { |
| 278 | + for _, v := range s { |
| 279 | + v.called = false |
| 280 | + v.args = []interface{}{} |
| 281 | + } |
| 282 | + |
| 283 | + err = l.DoString(sample.code) |
| 284 | + if err != nil { |
| 285 | + t.Error(err) |
| 286 | + return |
| 287 | + } |
| 288 | + |
| 289 | + for k, v := range s { |
| 290 | + assert.Equal(t, true, v.called) |
| 291 | + assert.Equal(t, sample.args[k], v.args) |
| 292 | + } |
| 293 | + } |
| 294 | + }, |
| 295 | + ) |
| 296 | + } |
| 297 | +} |
0 commit comments