MCPcopy Create free account
hub / github.com/yuin/gopher-lua / TestPCall

Function TestPCall

state_test.go:279–327  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

277}
278
279func TestPCall(t *testing.T) {
280 L := NewState()
281 defer L.Close()
282 L.Register("f1", func(L *LState) int {
283 panic("panic!")
284 return 0
285 })
286 errorIfScriptNotFail(t, L, `f1()`, "panic!")
287 L.Push(L.GetGlobal("f1"))
288 err := L.PCall(0, 0, L.NewFunction(func(L *LState) int {
289 L.Push(LString("by handler"))
290 return 1
291 }))
292 errorIfFalse(t, strings.Contains(err.Error(), "by handler"), "")
293
294 L.Push(L.GetGlobal("f1"))
295 err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
296 L.RaiseError("error!")
297 return 1
298 }))
299 errorIfFalse(t, strings.Contains(err.Error(), "error!"), "")
300
301 L.Push(L.GetGlobal("f1"))
302 err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
303 panic("panicc!")
304 return 1
305 }))
306 errorIfFalse(t, strings.Contains(err.Error(), "panicc!"), "")
307
308 // Issue #452, expected to be revert back to previous call stack after any error.
309 currentFrame, currentTop, currentSp := L.currentFrame, L.GetTop(), L.stack.Sp()
310 L.Push(L.GetGlobal("f1"))
311 err = L.PCall(0, 0, nil)
312 errorIfFalse(t, err != nil, "")
313 errorIfFalse(t, L.currentFrame == currentFrame, "")
314 errorIfFalse(t, L.GetTop() == currentTop, "")
315 errorIfFalse(t, L.stack.Sp() == currentSp, "")
316
317 currentFrame, currentTop, currentSp = L.currentFrame, L.GetTop(), L.stack.Sp()
318 L.Push(L.GetGlobal("f1"))
319 err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
320 L.RaiseError("error!")
321 return 1
322 }))
323 errorIfFalse(t, err != nil, "")
324 errorIfFalse(t, L.currentFrame == currentFrame, "")
325 errorIfFalse(t, L.GetTop() == currentTop, "")
326 errorIfFalse(t, L.stack.Sp() == currentSp, "")
327}
328
329func TestCoroutineApi1(t *testing.T) {
330 L := NewState()

Callers

nothing calls this directly

Calls 14

errorIfScriptNotFailFunction · 0.85
LStringTypeAlias · 0.85
errorIfFalseFunction · 0.85
NewStateFunction · 0.70
PushMethod · 0.65
ErrorMethod · 0.65
SpMethod · 0.65
CloseMethod · 0.45
RegisterMethod · 0.45
GetGlobalMethod · 0.45
PCallMethod · 0.45
NewFunctionMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…