Exemple #1
0
 static LuaScriptException GetPCallException(object[] results)
 {
     Assert.AreEqual(2, results.Length);
     Assert.AreEqual(false, results[0]);
     UAssert.IsInstanceOf <LuaScriptException>(results[1]);
     return((LuaScriptException)results[1]);
 }
Exemple #2
0
        static T GetPCallError <T>(object[] results)
        {
            var err = GetPCallError(results);

            UAssert.IsInstanceOf <T>(err);
            return((T)err);
        }
        [TestMethod] public void Delegate()
        {
            using (var lua = new Lua())
            {
                int a = 0;
                lua["a"] = new Action(() => ++ a);

                try
                {
                    lua.DoString("a()");
                    Assert.Fail("Update this unit test if support for calling delegates directly has been added.");
                }
                catch (LuaScriptException ex)
                {
                    Assert.AreEqual(@"[string ""a()""]:1: attempt to call global 'a' (a userdata value)", ex.Message);
                }
                Assert.AreEqual(0, a);

                lua.DoString("a:Invoke()");
                Assert.AreEqual(1, a);

                int b = 0;
                lua["b"] = new Action <Func <int, int> >(f => b = f(b));

                lua.DoString("b:Invoke(function(current) return current + 1 end)");
                Assert.AreEqual(1, b);

                try
                {
                    lua.DoString("b:Invoke(function(current) return {} end)");
                    Assert.Fail("Can't convert table to int.");
                }
                catch (LuaScriptException ex_outer)
                {
                    UAssert.IsInstanceOf <NullReferenceException>(ex_outer.InnerException);
                    var ex = (NullReferenceException)ex_outer.InnerException;
                    Assert.AreEqual("LuaInterface_generatedcode", ex.Source);
                    Assert.AreEqual("LuaGeneratedClass", ex.TargetSite.DeclaringType.FullName.Substring(0, "LuaGeneratedClass".Length));
                    Assert.AreEqual("Int32 CallFunction(Int32)", ex.TargetSite.ToString());
                    // todo: improve generated code to throw a proper exception and update this test
                }
            }
        }
Exemple #4
0
 [TestMethod] public void Reflection()
 {
     using (var lua = new Lua())
     {
         // with access to the object returned by GetType() it is possible to use MethodInfo.Invoke on arbitrary methods
         // therefore, SecureLuaFunctions blacklists all types in the System.Reflection namespace
         // blacklisted types are converted to strings (ToString) and the string is pushed instead.
         lua["object"] = new object();
         lua.DoString(@"t = object:GetType()");
         string[] expressions =
         {
             "t.Assembly",
             "t.Module",
             "t:GetMethod('ToString')",
             "t:GetConstructors()",                     // this evaluates to the string "System.Reflection.ConstructorInfo[]"
             "t:GetMethod('ReferenceEquals')",          // note that ReferenceEquals is a static method
         };
         foreach (var expr in expressions)
         {
             var result = lua.Eval(expr);
             UAssert.IsInstanceOf <string>(result, expr);
         }
     }
 }