Beispiel #1
0
        public void TestAtPanic()
        {
            LuaState L = null;
            using (L = new LuaState())
            {
                Assert.Throws<ArgumentNullException>(() => L.AtPanic(null));

                // Default panic
                L.PushCClosure(l => {
                    L.PushString("Test panic");
                    L.Error();
                    return 0;
                }, 0);
                Exception ex = Assert.Throws<LuaAtPanicException>(() => L.Call(0, 0));
                Assert.Equal("Test panic", ex.Message);

                // user panic
                L.PushCClosure(l => {
                    L.PushString("Test panic");
                    L.Error();
                    return 0;
                }, 0);
                var old = L.AtPanic(l => {
                    throw new ApplicationException(String.Format("Panic !! => {0}", l.ToString(-1)));
                });
                ex = Assert.Throws<ApplicationException>(() => L.Call(0, 0));
                Assert.Equal("Panic !! => Test panic", ex.Message);

                // Restore
                L.AtPanic(old);

                // Restore the default
                Assert.True(L.RestoreOriginalAtPanic());
                Assert.False(L.RestoreOriginalAtPanic());
            }
        }