Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            try
            {
                TestRemoting( );

                using (Lua state = new Lua(  ))
                {
                    LuaFunction print = state["print"] as LuaFunction;

                    LuaTable table = state.CreateTable();
                    table.SetValue("test", "value");
                    state["incomingTable"] = table;

                    state.DoFile("test.lua");
                    var f = new Function();
                    state["TestClr"] = f;

                    LuaFunction f1 = state["AFunction"] as LuaFunction;

                    state.DoString("AFunction = nil");

                    print.Call("AFunction returned: " + f1.Call(  )[0]);
                    f1.Dispose(  );

                    LuaFunction f2 = state["BFunction"] as LuaFunction;
                    f2.Call(  );
                    f2.Dispose(  );

                    LuaTable sillytable = state["SillyTable"] as LuaTable;

                    string str = sillytable["aaa"] as string;

                    print.Call(str);

                    sillytable["aaa"] = 9001;

                    foreach (var val in sillytable)
                    {
                        Console.WriteLine("{0} = {1}", val.Key, val.Value);
                    }

                    print.Call(state["SillyTable", "aaa"]);

                    sillytable.Dispose(  );

                    state.CreateTable("table");
                    print.Call(state["table"] as LuaTable);

                    print.Dispose(  );
                    GC.KeepAlive(f);
                }
            }
            catch (LuaException e)
            {
                Console.WriteLine("Fail: " + e.Message);
            }
        }
Ejemplo n.º 2
0
        public void CallDynamic_NoCallMetamethod_ThrowsBindingException()
        {
            using (var lua = new Lua()) {
                var     metatable = lua.CreateTable();
                dynamic table     = lua.CreateTable();
                table.Metatable = metatable;

                Assert.Throws <RuntimeBinderException>(() => table());
            }
        }
Ejemplo n.º 3
0
        public void UnaryOpDynamic_NoMetamethod_ThrowsBindingException()
        {
            using (var lua = new Lua()) {
                var     metatable = lua.CreateTable();
                dynamic table     = lua.CreateTable();
                table.Metatable = metatable;
                table.n         = 1;

                Assert.Throws <RuntimeBinderException>(() => - table);
            }
        }
Ejemplo n.º 4
0
        public void BinaryOpDynamic_NoMetatable_ThrowsBindingException()
        {
            using (var lua = new Lua()) {
                dynamic table1 = lua.CreateTable();
                dynamic table2 = lua.CreateTable();
                table1.n = 1;
                table2.n = 100;

                Assert.Throws <RuntimeBinderException>(() => table1 + table2);
            }
        }
Ejemplo n.º 5
0
        public void UnaryOpDynamic()
        {
            using (var lua = new Lua()) {
                var metatable = lua.CreateTable();
                metatable["__unm"] = lua.DoString("return function(o1) return -o1.n end")[0];
                dynamic table = lua.CreateTable();
                table.Metatable = metatable;
                table.n         = 1;

                Assert.Equal(-1L, -table);
            }
        }
Ejemplo n.º 6
0
        public void CallDynamic()
        {
            using (var lua = new Lua()) {
                var metatable = lua.CreateTable();
                metatable["__call"] = lua.DoString("return function(o) return 6 end")[0];
                dynamic table = lua.CreateTable();
                table.Metatable = metatable;

                var results = table();

                Assert.Single(results);
                Assert.Equal(6L, results[0]);
            }
        }
Ejemplo n.º 7
0
        public void BinaryOpDynamic()
        {
            using (var lua = new Lua()) {
                var metatable = lua.CreateTable();
                metatable["__add"] = lua.DoString("return function(o1, o2) return o1.n + o2.n end")[0];
                dynamic table1 = lua.CreateTable();
                dynamic table2 = lua.CreateTable();
                table1.Metatable = metatable;
                table2.Metatable = metatable;
                table1.n         = 1;
                table2.n         = 100;

                Assert.Equal(101L, table1 + table2);
            }
        }
Ejemplo n.º 8
0
        public void Test()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();
                table["apple"] = 15;
                table["bird"]  = 678;
                table["couch"] = -156;
                table["deli"]  = -667;

                void RemoveNegative(IDictionary <object, object> dict)
                {
                    var negativeKeys = dict.Where(kvp => (long)kvp.Value < 0).Select(kvp => kvp.Key).ToList();

                    foreach (var negativeKey in negativeKeys)
                    {
                        Assert.True(dict.Remove(negativeKey));
                    }
                }

                long SumValues(IDictionary <object, object> dict) => dict.Values.Sum(v => (long)v);

                Assert.Equal(-130, SumValues(table));

                RemoveNegative(table);

                Assert.Equal(2, table.Count);
                foreach (var kvp in table)
                {
                    var value = (long)kvp.Value;
                    Assert.True(value > 0);
                }
                Assert.False(table.ContainsKey("couch"));
                Assert.False(table.ContainsKey("deli"));
            }
        }
Ejemplo n.º 9
0
        public void Add_NullValue_ThrowsArgumentNullException()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Throws <ArgumentNullException>(() => table.Add("test", null));
            }
        }
Ejemplo n.º 10
0
        public void Values_Nothing()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Empty(table.Values);
            }
        }
Ejemplo n.º 11
0
        public void CallDynamic_NoMetatable_ThrowsBindingException()
        {
            using (var lua = new Lua()) {
                dynamic table = lua.CreateTable();

                Assert.Throws <RuntimeBinderException>(() => table());
            }
        }
Ejemplo n.º 12
0
        public void ICollectionIsReadOnly()
        {
            using (var lua = new Lua()) {
                ICollection <KeyValuePair <object, object> > table = lua.CreateTable();

                Assert.False(table.IsReadOnly);
            }
        }
Ejemplo n.º 13
0
        public void GetMetatable_None()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Null(table.Metatable);
            }
        }
Ejemplo n.º 14
0
        public void GetNull()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Null(table[null]);
            }
        }
Ejemplo n.º 15
0
        public void Values_Remove_ThrowsNotSupportedException()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Throws <NotSupportedException>(() => table.Values.Remove(0));
            }
        }
Ejemplo n.º 16
0
        public void Values_Contains_ValueDoesntExist()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.False(table.Values.Contains(0));
            }
        }
Ejemplo n.º 17
0
        public void Values_IsReadOnly()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.True(table.Values.IsReadOnly);
            }
        }
Ejemplo n.º 18
0
        public void Keys_Clear_ThrowsNotSupportedException()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Throws <NotSupportedException>(() => table.Keys.Clear());
            }
        }
Ejemplo n.º 19
0
        public void Keys_Contains_KeyDoesntExist()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.False(table.Keys.Contains("test"));
            }
        }
Ejemplo n.º 20
0
        public void Set_NullKey_ThrowsArgumentNullException()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Throws <ArgumentNullException>(() => table[null] = 0);
            }
        }
Ejemplo n.º 21
0
        public void CreateTable_Disposed_ThrowsObjectDisposedException()
        {
            var lua = new Lua();

            lua.Dispose();

            Assert.Throws <ObjectDisposedException>(() => lua.CreateTable());
        }
Ejemplo n.º 22
0
        public void ICollectionAdd_NullValue_ThrowsArgumentNullException()
        {
            using (var lua = new Lua()) {
                ICollection <KeyValuePair <object, object> > table = lua.CreateTable();

                Assert.Throws <ArgumentNullException>(() => table.Add(new KeyValuePair <object, object>("test", null)));
            }
        }
Ejemplo n.º 23
0
        public void ICollectionContains_KvpDoesntExist(object key, object value)
        {
            using (var lua = new Lua()) {
                ICollection <KeyValuePair <object, object> > table = lua.CreateTable();

                Assert.False(table.Contains(new KeyValuePair <object, object>(key, value)));
            }
        }
Ejemplo n.º 24
0
        public void TryGetValue_KeyDoesntExist(object key)
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.False(table.TryGetValue(key, out _));
            }
        }
Ejemplo n.º 25
0
        public void Remove_KeyDoesntExist(object key)
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.False(table.Remove(key));
            }
        }
Ejemplo n.º 26
0
        public void GetEnumerator_Nothing()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.Empty(table);
            }
        }
Ejemplo n.º 27
0
        public void ContainsKey_KeyDoesntExist(object key)
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();

                Assert.False(table.ContainsKey(key));
            }
        }
Ejemplo n.º 28
0
        public void LotsOfReferencesCleanedUp(int n)
        {
            using (var lua = new Lua()) {
                var function = lua.CreateFunction("x = 0 + 0");

                for (var i = 0; i < n; ++i)
                {
                    lua.CreateTable();
                    lua.CreateTable();
                    lua.CreateTable();

                    for (var j = 0; j < 1000; ++j)
                    {
                        function.Call();
                    }
                }
            }
        }
Ejemplo n.º 29
0
        public void Add_KeyExists_ThrowsArgumentException()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();
                table["test"] = 0;

                Assert.Throws <ArgumentException>(() => table.Add("test", 0));
            }
        }
Ejemplo n.º 30
0
        public void ContainsKey_KeyExists()
        {
            using (var lua = new Lua()) {
                var table = lua.CreateTable();
                table["test"] = 0;

                Assert.True(table.ContainsKey("test"));
            }
        }