Example #1
0
 public void ImplicitBlock_Section3_3_1()
 {
     var runtime = new LuaRuntime();
     runtime.AssertedExecuteScript("a = 1; b = 'This is an implicit block.'");
     Assert.AreEqual(1, runtime["a"]);
     Assert.AreEqual("This is an implicit block.", runtime["b"]);
 }
Example #2
0
 public void ExplicitBlock_Section3_3_1()
 {
     var runtime = new LuaRuntime();
     var result = runtime.AssertedExecuteScript("local j = 100; do local j = 200; a = 1; b = 'This is an explicit block.'; return j end");
     Assert.AreEqual(1, runtime["a"]);
     Assert.AreEqual("This is an explicit block.", runtime["b"]);
     Assert.AreEqual(200, result);
 }
Example #3
0
 public void FunctionCallAsStatement_Section_3_3_6()
 {
     var runtime = new LuaRuntime();
     var printStack = new Stack<string>();
     runtime["tostring"] = (Func<object, string>) (o => o != null ? o.ToString() : "nil");
     runtime["print"] = (Action<string>) printStack.Push;
     runtime.AssertedExecuteScript(@"print('Test1!'); print('Test2!'); print(tostring(3))");
     Assert.AreEqual("3", printStack.Pop());
     Assert.AreEqual("Test2!", printStack.Pop());
     Assert.AreEqual("Test1!", printStack.Pop());
     Assert.IsEmpty(printStack);
 }
Example #4
0
 public void AssignFunctionToLocalAndCall_Section3_4_9()
 {
     var runtime = new LuaRuntime();
     var printStack = new Stack<string>();
     runtime["tostring"] = (Func<object, string>) (o => o != null ? o.ToString() : "nil");
     runtime["print"] = (Action<string>) printStack.Push;
     runtime.AssertedExecuteScript(@"local boo = print; boo('Test1!'); boo('Test2!'); boo(tostring(3))");
     Assert.AreEqual("3", printStack.Pop());
     Assert.AreEqual("Test2!", printStack.Pop());
     Assert.AreEqual("Test1!", printStack.Pop());
     Assert.IsEmpty(printStack);
 }
 public void TypeReturnsPropertStringDesignation_Section_6_1()
 {
     var runtime = new LuaRuntime();
     runtime.Environment.ImportBasicFunctions();
     runtime.AssertedExecuteScript(
         "a = type(nil)\r\nb = type('test')\r\nc = type(33)\r\nd = type(function() return 666 end)\r\ne = type(tostring)\r\nf = type(true)");
     Assert.AreEqual("nil", runtime["a"]);
     Assert.AreEqual("string", runtime["b"]);
     Assert.AreEqual("number", runtime["c"]);
     Assert.AreEqual("function", runtime["d"]);
     Assert.AreEqual("function", runtime["e"]);
     Assert.AreEqual("boolean", runtime["f"]);
 }
 public void ToNumberConvertsInAnyBase_Section_6_1()
 {
     var runtime = new LuaRuntime();
     runtime.Environment.ImportBasicFunctions();
     runtime.AssertedExecuteScript(
         "a = tonumber('C000', 16)\r\nb = tonumber('49152', 10)\r\nc = tonumber(140000, 8)\r\nd = tonumber('-128',10)\r\ne = tonumber('ffff', 16)\r\nf = tonumber('123')");
     Assert.AreEqual(49152, runtime["a"]);
     Assert.AreEqual(49152, runtime["b"]);
     Assert.AreEqual(49152, runtime["c"]);
     Assert.AreEqual(-128, runtime["d"]);
     Assert.AreEqual(65535, runtime["e"]);
     Assert.AreEqual(123, runtime["f"]);
 }
Example #7
0
        public void FunctionWithVariableArguments_3_4_10()
        {
            var runtime = new LuaRuntime();
            runtime.Environment.ImportBasicFunctions();
            var result = runtime.AssertedExecuteScript(
                @"function join(...)
                    local s = ''
                    local tbl = {args}
                    for _, v in ipairs(tbl) do
                        s = s .. tostring(v)
                    end
                    return s
                  end

                  return join('jeff', ' and ', 'deddie', ' go', ' to ', 2021, ' Midwest Road, Suite 200')");
            Assert.AreEqual("jeff and deddie go to 2021 Midwest Road, Suite 200", result);
        }
Example #8
0
 public void FunctionDefintionAndCall_Section_3_4_10_And_Section_3_4_9()
 {
     var runtime = new LuaRuntime();
     var result = runtime.AssertedExecuteScript(@"local boo = function(x, y) return x + y end; return boo(2, 4);");
     Assert.AreEqual(6, result);
 }
Example #9
0
 public void FunctionCallWithTable_Section_3_4_9()
 {
     var runtime = new LuaRuntime();
     var result = runtime.AssertedExecuteScript(@"local boo = function(t) return t.x + t.y end; return boo{ x=2, y=4};");
     Assert.AreEqual(6, result);
 }
Example #10
0
 public void ProperTailCall_Section3_4_9()
 {
     var runtime = new LuaRuntime();
     var printStack = new Stack<string>();
     runtime["tostring"] = (Func<object, string>) (o => o != null ? o.ToString() : "nil");
     runtime["print"] = (Action<string>) printStack.Push;
     runtime.AssertedExecuteScript(
         @"local up; local down; local right;
             local function left(from) print('went left from ' .. tostring(from)); return right('left') end;
             down = function(from) print('went down from ' .. tostring(from)); return 'end' end;
             up = function(from) print('went up from ' .. tostring(from)); return down('up') end;
             right = function(from) print('went right from ' .. tostring(from)); return up('right') end;
             print(tostring(left('start')))");
     Assert.AreEqual("end", printStack.Pop());
     Assert.AreEqual("went down from up", printStack.Pop());
     Assert.AreEqual("went up from right", printStack.Pop());
     Assert.AreEqual("went right from left", printStack.Pop());
     Assert.AreEqual("went left from start", printStack.Pop());
     Assert.IsEmpty(printStack);
 }
Example #11
0
        public void GeneratePrimeNumbers()
        {
            var runtime = new LuaRuntime();
            runtime.Environment.ImportBasicFunctions();
            runtime["tostring"] = (Func<object, string>) (o => o != null ? o.ToString() : "nil");
            runtime["print"] = (Action<string>) Console.WriteLine;
            runtime.AssertedExecuteScript(
                @"---Find primes smaller than this number:
                LIMIT=100

                ---Checks to see if a number is prime or not
                function isPrime(n)
                    print(tostring(n))
                    if n<=1 then return false end
                    if n==2 then return true end
                    if (n % 2 == 0 ) then return false end
                    for i=3, n / 2, 2 do
                        if (n % i == 0 ) then return false end
                    end
                    return true
                end

                primes = {}

                ---Print all the prime numbers within range
                for i=1, LIMIT do
                    if isPrime(i) then
                        print(tostring(i) .. ' is prime!')
                        print('#primes = ' .. tostring(#primes))
                        primes[#primes + 1] = i
                    end
                end");
            var primes = (Table) runtime["primes"];
            Assert.AreEqual(25, primes.Count);
        }
 public void AssertDoesNotRaiseErrorIfTrue_Section_6_1()
 {
     var runtime = new LuaRuntime();
     runtime.Environment.ImportBasicFunctions();
     runtime.AssertedExecuteScript("assert(a == nil)");
 }
Example #13
0
 public void DoubleHyphenComments_Section_3_1()
 {
     var runtime = new LuaRuntime();
     runtime.AssertedExecuteScript("-- This is a comment on a single line\r\na = 3.14 -- This is an apporximation of Pi");
     Assert.AreEqual(3.14, runtime["a"]);
 }
Example #14
0
 public void DoubleHypenCommentsWithMultiLineString_Section_3_1()
 {
     var runtime = new LuaRuntime();
     runtime.AssertedExecuteScript("a = 3.14 -- This is an apporximation of Pi\r\n--[[\r\n\t\tThis is a multiline.\r\nComment that can stretch on for a while! ]]");
     Assert.AreEqual(3.14, runtime["a"]);
 }