Commit 5fc72461 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Mono-style format.

parent a0e1671c
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace LuaInterfaceTest.Mock namespace LuaInterfaceTest.Mock
{ {
public class Entity public class Entity
{ {
public event EventHandler<EventArgs> Clicked; public event EventHandler<EventArgs> Clicked;
protected virtual void OnEntityClicked(EventArgs e) protected virtual void OnEntityClicked (EventArgs e)
{ {
EventHandler<EventArgs> handler = Clicked; EventHandler<EventArgs> handler = Clicked;
if (handler != null) if (handler != null) {
{ // Use the () operator to raise the event.
// Use the () operator to raise the event. handler (this, e);
handler(this, e); }
} }
}
public Entity() public Entity ()
{ {
} }
public void Click() public void Click ()
{ {
OnEntityClicked(new EventArgs()); OnEntityClicked (new EventArgs ());
} }
} }
} }
...@@ -10,1746 +10,1756 @@ using LuaInterface.Exceptions; ...@@ -10,1746 +10,1756 @@ using LuaInterface.Exceptions;
namespace LuaInterfaceTest namespace LuaInterfaceTest
{ {
[TestFixture] [TestFixture]
public class LuaTests public class LuaTests
{ {
/* /*
* Tests capturing an exception * Tests capturing an exception
*/ */
[Test] [Test]
public void ThrowException () public void ThrowException ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("err,errMsg=pcall(test.exceptionMethod,test)"); lua.DoString ("err,errMsg=pcall(test.exceptionMethod,test)");
bool err = (bool) lua ["err"]; bool err = (bool)lua ["err"];
Exception errMsg = (Exception) lua ["errMsg"]; Exception errMsg = (Exception)lua ["errMsg"];
Assert.False (err); Assert.False (err);
Assert.NotNull (errMsg.InnerException); Assert.NotNull (errMsg.InnerException);
Assert.AreEqual ("exception test", errMsg.InnerException.Message); Assert.AreEqual ("exception test", errMsg.InnerException.Message);
} }
} }
/* /*
* Tests capturing an exception * Tests capturing an exception
*/ */
[Test] [Test]
public void ThrowUncaughtException () public void ThrowUncaughtException ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
try { try {
lua.DoString ("test:exceptionMethod()"); lua.DoString ("test:exceptionMethod()");
//failed //failed
Assert.True (false); Assert.True (false);
} catch (Exception e) { } catch (Exception) {
//passed //passed
Assert.True (true); Assert.True (true);
} }
} }
} }
/* /*
* Tests nullable fields * Tests nullable fields
*/ */
[Test] [Test]
public void TestNullable () public void TestNullable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("val=test.NullableBool"); lua.DoString ("val=test.NullableBool");
Assert.Null ( (object) lua ["val"]); Assert.Null ((object)lua ["val"]);
lua.DoString ("test.NullableBool = true"); lua.DoString ("test.NullableBool = true");
lua.DoString ("val=test.NullableBool"); lua.DoString ("val=test.NullableBool");
Assert.True ( (bool) lua ["val"]); Assert.True ((bool)lua ["val"]);
} }
} }
/* /*
* Tests structure assignment * Tests structure assignment
*/ */
[Test] [Test]
public void TestStructs () public void TestStructs ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("TestStruct=luanet.import_type('LuaInterfaceTest.Mock.TestStruct')"); lua.DoString ("TestStruct=luanet.import_type('LuaInterfaceTest.Mock.TestStruct')");
lua.DoString ("struct=TestStruct(2)"); lua.DoString ("struct=TestStruct(2)");
lua.DoString ("test.Struct = struct"); lua.DoString ("test.Struct = struct");
lua.DoString ("val=test.Struct.val"); lua.DoString ("val=test.Struct.val");
Assert.AreEqual (2.0d, (double) lua ["val"]); Assert.AreEqual (2.0d, (double)lua ["val"]);
} }
} }
[Test] [Test]
public void TestMethodOverloads () public void TestMethodOverloads ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("test:MethodOverload()"); lua.DoString ("test:MethodOverload()");
lua.DoString ("test:MethodOverload(test)"); lua.DoString ("test:MethodOverload(test)");
lua.DoString ("test:MethodOverload(1,1,1)"); lua.DoString ("test:MethodOverload(1,1,1)");
lua.DoString ("test:MethodOverload(2,2,i)\r\nprint(i)"); lua.DoString ("test:MethodOverload(2,2,i)\r\nprint(i)");
} }
} }
[Test] [Test]
public void TestDispose () public void TestDispose ()
{ {
System.GC.Collect (); System.GC.Collect ();
long startingMem = System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64; long startingMem = System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64;
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
_Calc (lua, i); _Calc (lua, i);
} }
} }
//TODO: make this test assert so that it is useful //TODO: make this test assert so that it is useful
Console.WriteLine ("Was using " + startingMem / 1024 / 1024 + "MB, now using: " + System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64 / 1024 / 1024 + "MB"); Console.WriteLine ("Was using " + startingMem / 1024 / 1024 + "MB, now using: " + System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64 / 1024 / 1024 + "MB");
} }
private void _Calc (Lua lua, int i) private void _Calc (Lua lua, int i)
{ {
lua.DoString ( lua.DoString (
"sqrt = math.sqrt;" + "sqrt = math.sqrt;" +
"sqr = function(x) return math.pow(x,2); end;" + "sqr = function(x) return math.pow(x,2); end;" +
"log = math.log;" + "log = math.log;" +
"log10 = math.log10;" + "log10 = math.log10;" +
"exp = math.exp;" + "exp = math.exp;" +
"sin = math.sin;" + "sin = math.sin;" +
"cos = math.cos;" + "cos = math.cos;" +
"tan = math.tan;" + "tan = math.tan;" +
"abs = math.abs;" "abs = math.abs;"
); );
lua.DoString ("function calcVP(a,b) return a+b end"); lua.DoString ("function calcVP(a,b) return a+b end");
LuaFunction lf = lua.GetFunction ("calcVP"); LuaFunction lf = lua.GetFunction ("calcVP");
Object[] ret = lf.Call (i, 20); lf.Call (i, 20);
} }
[Test] [Test]
public void TestThreading () public void TestThreading ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
object lua_locker = new object (); object lua_locker = new object ();
DoWorkClass doWork = new DoWorkClass (); DoWorkClass doWork = new DoWorkClass ();
lua.RegisterFunction ("dowork", doWork, typeof (DoWorkClass).GetMethod ("DoWork") ); lua.RegisterFunction ("dowork", doWork, typeof(DoWorkClass).GetMethod ("DoWork"));
bool failureDetected = false; bool failureDetected = false;
int completed = 0; int completed = 0;
int iterations = 10; int iterations = 10;
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
ThreadPool.QueueUserWorkItem (new WaitCallback (delegate (object o) { ThreadPool.QueueUserWorkItem (new WaitCallback (delegate (object o) {
try { try {
lock (lua_locker) { lock (lua_locker) {
lua.DoString ("dowork()"); lua.DoString ("dowork()");
} }
} catch (Exception e) { } catch (Exception e) {
Console.Write (e); Console.Write (e);
failureDetected = true; failureDetected = true;
} }
completed++; completed++;
}) ); }));
} }
while (completed < iterations && !failureDetected) while (completed < iterations && !failureDetected)
Thread.Sleep (50); Thread.Sleep (50);
Assert.False (failureDetected); Assert.False (failureDetected);
} }
} }
[Test] [Test]
public void TestPrivateMethod () public void TestPrivateMethod ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
try { try {
lua.DoString ("test:_PrivateMethod()"); lua.DoString ("test:_PrivateMethod()");
} catch { } catch {
Assert.True (true); Assert.True (true);
return; return;
} }
Assert.True (false); Assert.True (false);
} }
} }
/* /*
* Tests functions * Tests functions
*/ */
[Test] [Test]
public void TestFunctions () public void TestFunctions ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.RegisterFunction ("p", null, typeof (System.Console).GetMethod ("WriteLine", new Type [] { typeof (String) }) ); lua.RegisterFunction ("p", null, typeof(System.Console).GetMethod ("WriteLine", new Type [] { typeof(String) }));
/// Lua command that works (prints to console) /// Lua command that works (prints to console)
lua.DoString ("p('Foo')"); lua.DoString ("p('Foo')");
/// Yet this works... /// Yet this works...
lua.DoString ("string.gsub('some string', '(%w+)', function(s) p(s) end)"); lua.DoString ("string.gsub('some string', '(%w+)', function(s) p(s) end)");
/// This fails if you don't fix Lua5.1 lstrlib.c/add_value to treat LUA_TUSERDATA the same as LUA_FUNCTION /// This fails if you don't fix Lua5.1 lstrlib.c/add_value to treat LUA_TUSERDATA the same as LUA_FUNCTION
lua.DoString ("string.gsub('some string', '(%w+)', p)"); lua.DoString ("string.gsub('some string', '(%w+)', p)");
} }
} }
/* /*
* Tests making an object from a Lua table and calling one of * Tests making an object from a Lua table and calling one of
* methods the table overrides. * methods the table overrides.
*/ */
[Test] [Test]
public void LuaTableOverridedMethod () public void LuaTableOverridedMethod ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test={}"); lua.DoString ("test={}");
lua.DoString ("function test:overridableMethod(x,y) return x*y; end"); lua.DoString ("function test:overridableMethod(x,y) return x*y; end");
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("a=TestClass.callOverridable(test,2,3)"); lua.DoString ("a=TestClass.callOverridable(test,2,3)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
lua.DoString ("luanet.free_object(test)"); lua.DoString ("luanet.free_object(test)");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
} }
} }
/* /*
* Tests making an object from a Lua table and calling a method * Tests making an object from a Lua table and calling a method
* the table does not override. * the table does not override.
*/ */
[Test] [Test]
public void LuaTableInheritedMethod () public void LuaTableInheritedMethod ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test={}"); lua.DoString ("test={}");
lua.DoString ("function test:overridableMethod(x,y) return x*y; end"); lua.DoString ("function test:overridableMethod(x,y) return x*y; end");
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test:setVal(3)"); lua.DoString ("test:setVal(3)");
lua.DoString ("a=test.testval"); lua.DoString ("a=test.testval");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
lua.DoString ("luanet.free_object(test)"); lua.DoString ("luanet.free_object(test)");
Assert.AreEqual (3, a); Assert.AreEqual (3, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/// <summary> /// <summary>
/// Basic multiply method which expects 2 floats /// Basic multiply method which expects 2 floats
/// </summary> /// </summary>
/// <param name="val"></param> /// <param name="val"></param>
/// <param name="val2"></param> /// <param name="val2"></param>
/// <returns></returns> /// <returns></returns>
private float _TestException (float val, float val2) private float _TestException (float val, float val2)
{ {
return val * val2; return val * val2;
} }
class LuaEventArgsHandler : LuaInterface.Method.LuaDelegate class LuaEventArgsHandler : LuaInterface.Method.LuaDelegate
{ {
void CallFunction (object sender, EventArgs eventArgs) void CallFunction (object sender, EventArgs eventArgs)
{ {
object [] args = new object [] {sender, eventArgs }; object [] args = new object [] {sender, eventArgs };
object [] inArgs = new object [] { sender, eventArgs }; object [] inArgs = new object [] { sender, eventArgs };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
base.callFunction (args, inArgs, outArgs); base.callFunction (args, inArgs, outArgs);
} }
} }
[Test] [Test]
public void TestEventException () public void TestEventException ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
//Register a C# function //Register a C# function
MethodInfo testException = this.GetType ().GetMethod ("_TestException", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, new Type [] { MethodInfo testException = this.GetType ().GetMethod ("_TestException", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, new Type [] {
typeof (float), typeof(float),
typeof (float) typeof(float)
}, null); }, null);
lua.RegisterFunction ("Multiply", this, testException); lua.RegisterFunction ("Multiply", this, testException);
lua.RegisterLuaDelegateType (typeof (EventHandler<EventArgs>), typeof (LuaEventArgsHandler) ); lua.RegisterLuaDelegateType (typeof(EventHandler<EventArgs>), typeof(LuaEventArgsHandler));
//create the lua event handler code for the entity //create the lua event handler code for the entity
//includes the bad code! //includes the bad code!
lua.DoString ("function OnClick(sender, eventArgs)\r\n" + lua.DoString ("function OnClick(sender, eventArgs)\r\n" +
"--Multiply expects 2 floats, but instead receives 2 strings\r\n" + "--Multiply expects 2 floats, but instead receives 2 strings\r\n" +
"Multiply(asd, we)\r\n" + "Multiply(asd, we)\r\n" +
"end"); "end");
//create the lua event handler code for the entity //create the lua event handler code for the entity
//good code //good code
//lua.DoString("function OnClick(sender, eventArgs)\r\n" + //lua.DoString("function OnClick(sender, eventArgs)\r\n" +
// "--Multiply expects 2 floats\r\n" + // "--Multiply expects 2 floats\r\n" +
// "Multiply(2, 50)\r\n" + // "Multiply(2, 50)\r\n" +
// "end"); // "end");
//Create the event handler script //Create the event handler script
lua.DoString ("function SubscribeEntity(e)\r\ne.Clicked:Add(OnClick)\r\nend"); lua.DoString ("function SubscribeEntity(e)\r\ne.Clicked:Add(OnClick)\r\nend");
//Create the entity object //Create the entity object
Entity entity = new Entity (); Entity entity = new Entity ();
//Register the entity object with the event handler inside lua //Register the entity object with the event handler inside lua
LuaFunction lf = lua.GetFunction ("SubscribeEntity"); LuaFunction lf = lua.GetFunction ("SubscribeEntity");
lf.Call (new object [1] { entity }); lf.Call (new object [1] { entity });
try { try {
//Cause the event to be fired //Cause the event to be fired
entity.Click (); entity.Click ();
//failed //failed
Assert.True (false); Assert.True (false);
} catch (LuaException e) { } catch (LuaException) {
//passed //passed
Assert.True (true); Assert.True (true);
} }
} }
} }
[Test] [Test]
public void TestExceptionWithChunkOverload () public void TestExceptionWithChunkOverload ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
try { try {
lua.DoString ("thiswillthrowanerror", "MyChunk"); lua.DoString ("thiswillthrowanerror", "MyChunk");
} catch (Exception e) { } catch (Exception e) {
Assert.True (e.Message.StartsWith ("[string \"MyChunk\"]") ); Assert.True (e.Message.StartsWith ("[string \"MyChunk\"]"));
} }
} }
} }
[Test] [Test]
public void TestGenerics () public void TestGenerics ()
{ {
//Im not sure support for generic classes is possible to implement, see: http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx //Im not sure support for generic classes is possible to implement, see: http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx
//specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked" //specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked"
//TestClassGeneric<string> genericClass = new TestClassGeneric<string>(); //TestClassGeneric<string> genericClass = new TestClassGeneric<string>();
//lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod")); //lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod"));
//lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod")); //lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod"));
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod (); TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod ();
lua.RegisterFunction ("genericMethod2", classWithGenericMethod, typeof (TestClassWithGenericMethod).GetMethod ("GenericMethod") ); lua.RegisterFunction ("genericMethod2", classWithGenericMethod, typeof(TestClassWithGenericMethod).GetMethod ("GenericMethod"));
try { try {
lua.DoString ("genericMethod2(100)"); lua.DoString ("genericMethod2(100)");
} catch { } catch {
} }
Assert.True (classWithGenericMethod.GenericMethodSuccess); Assert.True (classWithGenericMethod.GenericMethodSuccess);
Assert.True (classWithGenericMethod.Validate<double> (100) ); //note the gotcha: numbers are all being passed to generic methods as doubles Assert.True (classWithGenericMethod.Validate<double> (100)); //note the gotcha: numbers are all being passed to generic methods as doubles
try { try {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass(56)"); lua.DoString ("test=TestClass(56)");
lua.DoString ("genericMethod2(test)"); lua.DoString ("genericMethod2(test)");
} catch { } catch {
} }
Assert.True (classWithGenericMethod.GenericMethodSuccess); Assert.True (classWithGenericMethod.GenericMethodSuccess);
Assert.AreEqual (56, (classWithGenericMethod.PassedValue as TestClass).val); Assert.AreEqual (56, (classWithGenericMethod.PassedValue as TestClass).val);
} }
} }
[Test] [Test]
public void RegisterFunctionStressTest () public void RegisterFunctionStressTest ()
{ {
LuaFunction fc = null; const int Count = 200; // it seems to work with 41
const int Count = 200; // it seems to work with 41 using (Lua lua = new Lua ()) {
using (Lua lua = new Lua () ) { MyClass t = new MyClass ();
MyClass t = new MyClass ();
for (int i = 1; i < Count - 1; ++i) {
for (int i = 1; i < Count - 1; ++i) { lua.RegisterFunction ("func" + i, t, typeof(MyClass).GetMethod ("Func1"));
fc = lua.RegisterFunction ("func" + i, t, typeof (MyClass).GetMethod ("Func1") ); }
}
lua.RegisterFunction ("func" + (Count - 1), t, typeof(MyClass).GetMethod ("Func1"));
fc = lua.RegisterFunction ("func" + (Count - 1), t, typeof (MyClass).GetMethod ("Func1") ); lua.DoString ("print(func1())");
lua.DoString ("print(func1())"); }
} }
}
[Test]
[Test] public void TestMultipleOutParameters ()
public void TestMultipleOutParameters () {
{ using (Lua lua = new Lua ()) {
using (Lua lua = new Lua () ) { TestClass t1 = new TestClass ();
TestClass t1 = new TestClass (); lua ["netobj"] = t1;
lua ["netobj"] = t1; lua.DoString ("a,b,c=netobj:outValMutiple(2)");
lua.DoString ("a,b,c=netobj:outValMutiple(2)"); int a = (int)lua.GetNumber ("a");
int a = (int) lua.GetNumber ("a"); string b = (string)lua.GetString ("b");
string b = (string) lua.GetString ("b"); string c = (string)lua.GetString ("c");
string c = (string) lua.GetString ("c"); Assert.AreEqual (2, a);
Assert.AreEqual (2, a); Assert.NotNull (b);
Assert.NotNull (b); Assert.NotNull (c);
Assert.NotNull (c); }
} }
}
[Test]
[Test] public void TestLoadStringLeak ()
public void TestLoadStringLeak () {
{ //Test to prevent stack overflow
//Test to prevent stack overflow //See: http://code.google.com/p/luainterface/issues/detail?id=5
//See: http://code.google.com/p/luainterface/issues/detail?id=5 //number of iterations to test
//number of iterations to test int count = 1000;
int count = 1000; using (Lua lua = new Lua ()) {
using (Lua lua = new Lua () ) { for (int i = 0; i < count; i++) {
for (int i = 0; i < count; i++) { lua.LoadString ("abc = 'def'", string.Empty);
lua.LoadString ("abc = 'def'", string.Empty); }
} }
} //any thrown exceptions cause the test run to fail
//any thrown exceptions cause the test run to fail }
}
[Test]
[Test] public void TestLoadFileLeak ()
public void TestLoadFileLeak () {
{ //Test to prevent stack overflow
//Test to prevent stack overflow //See: http://code.google.com/p/luainterface/issues/detail?id=5
//See: http://code.google.com/p/luainterface/issues/detail?id=5 //number of iterations to test
//number of iterations to test int count = 1000;
int count = 1000; using (Lua lua = new Lua ()) {
using (Lua lua = new Lua () ) { for (int i = 0; i < count; i++) {
for (int i = 0; i < count; i++) { lua.LoadFile (Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "test.lua");
lua.LoadFile (Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "test.lua"); }
} }
} //any thrown exceptions cause the test run to fail
//any thrown exceptions cause the test run to fail }
}
[Test]
[Test] public void TestRegisterFunction ()
public void TestRegisterFunction () {
{ using (Lua lua = new Lua ()) {
using (Lua lua = new Lua () ) { lua.RegisterFunction ("func1", null, typeof(TestClass2).GetMethod ("func"));
lua.RegisterFunction ("func1", null, typeof (TestClass2).GetMethod ("func") ); object[] vals1 = lua.GetFunction ("func1").Call (2, 3);
object[] vals1 = lua.GetFunction ("func1").Call (2, 3); Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]));
Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]) ); TestClass2 obj = new TestClass2 ();
TestClass2 obj = new TestClass2 (); lua.RegisterFunction ("func2", obj, typeof(TestClass2).GetMethod ("funcInstance"));
lua.RegisterFunction ("func2", obj, typeof (TestClass2).GetMethod ("funcInstance") ); vals1 = lua.GetFunction ("func2").Call (2, 3);
vals1 = lua.GetFunction ("func2").Call (2, 3); Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]));
Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]) ); }
} }
}
/*
/*
* Tests if DoString is correctly returning values * Tests if DoString is correctly returning values
*/ */
[Test] [Test]
public void DoString () public void DoString ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
object[] res = lua.DoString ("a=2\nreturn a,3"); object[] res = lua.DoString ("a=2\nreturn a,3");
//Console.WriteLine("a="+res[0]+", b="+res[1]); //Console.WriteLine("a="+res[0]+", b="+res[1]);
Assert.AreEqual (res [0], 2d); Assert.AreEqual (res [0], 2d);
Assert.AreEqual (res [1], 3d); Assert.AreEqual (res [1], 3d);
} }
} }
/* /*
* Tests getting of global numeric variables * Tests getting of global numeric variables
*/ */
[Test] [Test]
public void GetGlobalNumber () public void GetGlobalNumber ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=2"); lua.DoString ("a=2");
double num = lua.GetNumber ("a"); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num); //Console.WriteLine("a="+num);
Assert.AreEqual (num, 2d); Assert.AreEqual (num, 2d);
} }
} }
/* /*
* Tests setting of global numeric variables * Tests setting of global numeric variables
*/ */
[Test] [Test]
public void SetGlobalNumber () public void SetGlobalNumber ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=2"); lua.DoString ("a=2");
lua ["a"] = 3; lua ["a"] = 3;
double num = lua.GetNumber ("a"); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num); //Console.WriteLine("a="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests getting of numeric variables from tables * Tests getting of numeric variables from tables
* by specifying variable path * by specifying variable path
*/ */
[Test] [Test]
public void GetNumberInTable () public void GetNumberInTable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
double num = lua.GetNumber ("a.b.c"); double num = lua.GetNumber ("a.b.c");
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 2d); Assert.AreEqual (num, 2d);
} }
} }
/* /*
* Tests setting of numeric variables from tables * Tests setting of numeric variables from tables
* by specifying variable path * by specifying variable path
*/ */
[Test] [Test]
public void SetNumberInTable () public void SetNumberInTable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
lua ["a.b.c"] = 3; lua ["a.b.c"] = 3;
double num = lua.GetNumber ("a.b.c"); double num = lua.GetNumber ("a.b.c");
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests getting of global string variables * Tests getting of global string variables
*/ */
[Test] [Test]
public void GetGlobalString () public void GetGlobalString ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=\"test\""); lua.DoString ("a=\"test\"");
string str = lua.GetString ("a"); string str = lua.GetString ("a");
//Console.WriteLine("a="+str); //Console.WriteLine("a="+str);
Assert.AreEqual (str, "test"); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests setting of global string variables * Tests setting of global string variables
*/ */
[Test] [Test]
public void SetGlobalString () public void SetGlobalString ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=\"test\""); lua.DoString ("a=\"test\"");
lua ["a"] = "new test"; lua ["a"] = "new test";
string str = lua.GetString ("a"); string str = lua.GetString ("a");
//Console.WriteLine("a="+str); //Console.WriteLine("a="+str);
Assert.AreEqual (str, "new test"); Assert.AreEqual (str, "new test");
} }
} }
/* /*
* Tests getting of string variables from tables * Tests getting of string variables from tables
* by specifying variable path * by specifying variable path
*/ */
[Test] [Test]
public void GetStringInTable () public void GetStringInTable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
string str = lua.GetString ("a.b.c"); string str = lua.GetString ("a.b.c");
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "test"); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests setting of string variables from tables * Tests setting of string variables from tables
* by specifying variable path * by specifying variable path
*/ */
[Test] [Test]
public void SetStringInTable () public void SetStringInTable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
lua ["a.b.c"] = "new test"; lua ["a.b.c"] = "new test";
string str = lua.GetString ("a.b.c"); string str = lua.GetString ("a.b.c");
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "new test"); Assert.AreEqual (str, "new test");
} }
} }
/* /*
* Tests getting and setting of global table variables * Tests getting and setting of global table variables
*/ */
[Test] [Test]
public void GetAndSetTable () public void GetAndSetTable ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}\nb={c=3}"); lua.DoString ("a={b={c=2}}\nb={c=3}");
LuaTable tab = lua.GetTable ("b"); LuaTable tab = lua.GetTable ("b");
lua ["a.b"] = tab; lua ["a.b"] = tab;
double num = lua.GetNumber ("a.b.c"); double num = lua.GetNumber ("a.b.c");
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests getting of numeric field of a table * Tests getting of numeric field of a table
*/ */
[Test] [Test]
public void GetTableNumericField1 () public void GetTableNumericField1 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
LuaTable tab = lua.GetTable ("a.b"); LuaTable tab = lua.GetTable ("a.b");
double num = (double) tab ["c"]; double num = (double)tab ["c"];
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 2d); Assert.AreEqual (num, 2d);
} }
} }
/* /*
* Tests getting of numeric field of a table * Tests getting of numeric field of a table
* (the field is inside a subtable) * (the field is inside a subtable)
*/ */
[Test] [Test]
public void GetTableNumericField2 () public void GetTableNumericField2 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
LuaTable tab = lua.GetTable ("a"); LuaTable tab = lua.GetTable ("a");
double num = (double) tab ["b.c"]; double num = (double)tab ["b.c"];
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 2d); Assert.AreEqual (num, 2d);
} }
} }
/* /*
* Tests setting of numeric field of a table * Tests setting of numeric field of a table
*/ */
[Test] [Test]
public void SetTableNumericField1 () public void SetTableNumericField1 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
LuaTable tab = lua.GetTable ("a.b"); LuaTable tab = lua.GetTable ("a.b");
tab ["c"] = 3; tab ["c"] = 3;
double num = lua.GetNumber ("a.b.c"); double num = lua.GetNumber ("a.b.c");
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests setting of numeric field of a table * Tests setting of numeric field of a table
* (the field is inside a subtable) * (the field is inside a subtable)
*/ */
[Test] [Test]
public void SetTableNumericField2 () public void SetTableNumericField2 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=2}}");
LuaTable tab = lua.GetTable ("a"); LuaTable tab = lua.GetTable ("a");
tab ["b.c"] = 3; tab ["b.c"] = 3;
double num = lua.GetNumber ("a.b.c"); double num = lua.GetNumber ("a.b.c");
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests getting of string field of a table * Tests getting of string field of a table
*/ */
[Test] [Test]
public void GetTableStringField1 () public void GetTableStringField1 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a.b"); LuaTable tab = lua.GetTable ("a.b");
string str = (string) tab ["c"]; string str = (string)tab ["c"];
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "test"); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests getting of string field of a table * Tests getting of string field of a table
* (the field is inside a subtable) * (the field is inside a subtable)
*/ */
[Test] [Test]
public void GetTableStringField2 () public void GetTableStringField2 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a"); LuaTable tab = lua.GetTable ("a");
string str = (string) tab ["b.c"]; string str = (string)tab ["b.c"];
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "test"); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests setting of string field of a table * Tests setting of string field of a table
*/ */
[Test] [Test]
public void SetTableStringField1 () public void SetTableStringField1 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a.b"); LuaTable tab = lua.GetTable ("a.b");
tab ["c"] = "new test"; tab ["c"] = "new test";
string str = lua.GetString ("a.b.c"); string str = lua.GetString ("a.b.c");
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "new test"); Assert.AreEqual (str, "new test");
} }
} }
/* /*
* Tests setting of string field of a table * Tests setting of string field of a table
* (the field is inside a subtable) * (the field is inside a subtable)
*/ */
[Test] [Test]
public void SetTableStringField2 () public void SetTableStringField2 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a"); LuaTable tab = lua.GetTable ("a");
tab ["b.c"] = "new test"; tab ["b.c"] = "new test";
string str = lua.GetString ("a.b.c"); string str = lua.GetString ("a.b.c");
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (str, "new test"); Assert.AreEqual (str, "new test");
} }
} }
/* /*
* Tests calling of a global function with zero arguments * Tests calling of a global function with zero arguments
*/ */
[Test] [Test]
public void CallGlobalFunctionNoArgs () public void CallGlobalFunctionNoArgs ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=2\nfunction f()\na=3\nend"); lua.DoString ("a=2\nfunction f()\na=3\nend");
lua.GetFunction ("f").Call (); lua.GetFunction ("f").Call ();
double num = lua.GetNumber ("a"); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num); //Console.WriteLine("a="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests calling of a global function with one argument * Tests calling of a global function with one argument
*/ */
[Test] [Test]
public void CallGlobalFunctionOneArg () public void CallGlobalFunctionOneArg ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=2\nfunction f(x)\na=a+x\nend"); lua.DoString ("a=2\nfunction f(x)\na=a+x\nend");
lua.GetFunction ("f").Call (1); lua.GetFunction ("f").Call (1);
double num = lua.GetNumber ("a"); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num); //Console.WriteLine("a="+num);
Assert.AreEqual (num, 3d); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests calling of a global function with two arguments * Tests calling of a global function with two arguments
*/ */
[Test] [Test]
public void CallGlobalFunctionTwoArgs () public void CallGlobalFunctionTwoArgs ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a=2\nfunction f(x,y)\na=x+y\nend"); lua.DoString ("a=2\nfunction f(x,y)\na=x+y\nend");
lua.GetFunction ("f").Call (1, 3); lua.GetFunction ("f").Call (1, 3);
double num = lua.GetNumber ("a"); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num); //Console.WriteLine("a="+num);
Assert.AreEqual (num, 4d); Assert.AreEqual (num, 4d);
} }
} }
/* /*
* Tests calling of a global function that returns one value * Tests calling of a global function that returns one value
*/ */
[Test] [Test]
public void CallGlobalFunctionOneReturn () public void CallGlobalFunctionOneReturn ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("function f(x)\nreturn x+2\nend"); lua.DoString ("function f(x)\nreturn x+2\nend");
object[] ret = lua.GetFunction ("f").Call (3); object[] ret = lua.GetFunction ("f").Call (3);
//Console.WriteLine("ret="+ret[0]); //Console.WriteLine("ret="+ret[0]);
Assert.AreEqual (1, ret.Length); Assert.AreEqual (1, ret.Length);
Assert.AreEqual (5, (double) ret [0]); Assert.AreEqual (5, (double)ret [0]);
} }
} }
/* /*
* Tests calling of a global function that returns two values * Tests calling of a global function that returns two values
*/ */
[Test] [Test]
public void CallGlobalFunctionTwoReturns () public void CallGlobalFunctionTwoReturns ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("function f(x,y)\nreturn x,x+y\nend"); lua.DoString ("function f(x,y)\nreturn x,x+y\nend");
object[] ret = lua.GetFunction ("f").Call (3, 2); object[] ret = lua.GetFunction ("f").Call (3, 2);
//Console.WriteLine("ret="+ret[0]+","+ret[1]); //Console.WriteLine("ret="+ret[0]+","+ret[1]);
Assert.AreEqual (2, ret.Length); Assert.AreEqual (2, ret.Length);
Assert.AreEqual (3, (double) ret [0]); Assert.AreEqual (3, (double)ret [0]);
Assert.AreEqual (5, (double) ret [1]); Assert.AreEqual (5, (double)ret [1]);
} }
} }
/* /*
* Tests calling of a function inside a table * Tests calling of a function inside a table
*/ */
[Test] [Test]
public void CallTableFunctionTwoReturns () public void CallTableFunctionTwoReturns ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={}\nfunction a.f(x,y)\nreturn x,x+y\nend"); lua.DoString ("a={}\nfunction a.f(x,y)\nreturn x,x+y\nend");
object[] ret = lua.GetFunction ("a.f").Call (3, 2); object[] ret = lua.GetFunction ("a.f").Call (3, 2);
//Console.WriteLine("ret="+ret[0]+","+ret[1]); //Console.WriteLine("ret="+ret[0]+","+ret[1]);
Assert.AreEqual (2, ret.Length); Assert.AreEqual (2, ret.Length);
Assert.AreEqual (3, (double) ret [0]); Assert.AreEqual (3, (double)ret [0]);
Assert.AreEqual (5, (double) ret [1]); Assert.AreEqual (5, (double)ret [1]);
} }
} }
/* /*
* Tests setting of a global variable to a CLR object value * Tests setting of a global variable to a CLR object value
*/ */
[Test] [Test]
public void SetGlobalObject () public void SetGlobalObject ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
t1.testval = 4; t1.testval = 4;
lua ["netobj"] = t1; lua ["netobj"] = t1;
object o = lua ["netobj"]; object o = lua ["netobj"];
Assert.True (o is TestClass); Assert.True (o is TestClass);
TestClass t2 = (TestClass) lua ["netobj"]; TestClass t2 = (TestClass)lua ["netobj"];
Assert.AreEqual (t2.testval, 4); Assert.AreEqual (t2.testval, 4);
Assert.True (t1 == t2); Assert.True (t1 == t2);
} }
} }
///* ///*
// * Tests if CLR object is being correctly collected by Lua // * Tests if CLR object is being correctly collected by Lua
// */ // */
//[Test] //[Test]
//public void GarbageCollection() //public void GarbageCollection()
//{ //{
// using (Lua lua = new Lua()) // using (Lua lua = new Lua())
// { // {
// TestClass t1 = new TestClass(); // TestClass t1 = new TestClass();
// t1.testval = 4; // t1.testval = 4;
// lua["netobj"] = t1; // lua["netobj"] = t1;
// TestClass t2 = (TestClass)lua["netobj"]; // TestClass t2 = (TestClass)lua["netobj"];
// Assert.True(lua[0] != null); // Assert.True(lua[0] != null);
// lua.DoString("netobj=nil;collectgarbage();"); // lua.DoString("netobj=nil;collectgarbage();");
// Assert.True(lua.translator.objects[0] == null); // Assert.True(lua.translator.objects[0] == null);
// } // }
//} //}
/* /*
* Tests setting of a table field to a CLR object value * Tests setting of a table field to a CLR object value
*/ */
[Test] [Test]
public void SetTableObjectField1 () public void SetTableObjectField1 ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a.b"); LuaTable tab = lua.GetTable ("a.b");
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
t1.testval = 4; t1.testval = 4;
tab ["c"] = t1; tab ["c"] = t1;
TestClass t2 = (TestClass) lua ["a.b.c"]; TestClass t2 = (TestClass)lua ["a.b.c"];
//Console.WriteLine("a.b.c="+t2.testval); //Console.WriteLine("a.b.c="+t2.testval);
Assert.AreEqual (t2.testval, 4); Assert.AreEqual (t2.testval, 4);
Assert.True (t1 == t2); Assert.True (t1 == t2);
} }
} }
/* /*
* Tests reading and writing of an object's field * Tests reading and writing of an object's field
*/ */
[Test] [Test]
public void AccessObjectField () public void AccessObjectField ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
t1.val = 4; t1.val = 4;
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("var=netobj.val"); lua.DoString ("var=netobj.val");
double var = (double) lua ["var"]; double var = (double)lua ["var"];
//Console.WriteLine("value from Lua="+var); //Console.WriteLine("value from Lua="+var);
Assert.AreEqual (4, var); Assert.AreEqual (4, var);
lua.DoString ("netobj.val=3"); lua.DoString ("netobj.val=3");
Assert.AreEqual (3, t1.val); Assert.AreEqual (3, t1.val);
//Console.WriteLine("new val (from Lua)="+t1.val); //Console.WriteLine("new val (from Lua)="+t1.val);
} }
} }
/* /*
* Tests reading and writing of an object's non-indexed * Tests reading and writing of an object's non-indexed
* property * property
*/ */
[Test] [Test]
public void AccessObjectProperty () public void AccessObjectProperty ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
t1.testval = 4; t1.testval = 4;
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("var=netobj.testval"); lua.DoString ("var=netobj.testval");
double var = (double) lua ["var"]; double var = (double)lua ["var"];
//Console.WriteLine("value from Lua="+var); //Console.WriteLine("value from Lua="+var);
Assert.AreEqual (4, var); Assert.AreEqual (4, var);
lua.DoString ("netobj.testval=3"); lua.DoString ("netobj.testval=3");
Assert.AreEqual (3, t1.testval); Assert.AreEqual (3, t1.testval);
//Console.WriteLine("new val (from Lua)="+t1.testval); //Console.WriteLine("new val (from Lua)="+t1.testval);
} }
} }
/* /*
* Tests calling of an object's method with no overloads * Tests calling of an object's method with no overloads
*/ */
[Test] [Test]
public void CallObjectMethod () public void CallObjectMethod ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
t1.testval = 4; t1.testval = 4;
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("netobj:setVal(3)"); lua.DoString ("netobj:setVal(3)");
Assert.AreEqual (3, t1.testval); Assert.AreEqual (3, t1.testval);
//Console.WriteLine("new val(from C#)="+t1.testval); //Console.WriteLine("new val(from C#)="+t1.testval);
lua.DoString ("val=netobj:getVal()"); lua.DoString ("val=netobj:getVal()");
int val = (int) lua.GetNumber ("val"); int val = (int)lua.GetNumber ("val");
Assert.AreEqual (3, val); Assert.AreEqual (3, val);
//Console.WriteLine("new val(from Lua)="+val); //Console.WriteLine("new val(from Lua)="+val);
} }
} }
/* /*
* Tests calling of an object's method with overloading * Tests calling of an object's method with overloading
*/ */
[Test] [Test]
public void CallObjectMethodByType () public void CallObjectMethodByType ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("netobj:setVal('str')"); lua.DoString ("netobj:setVal('str')");
Assert.AreEqual ("str", t1.getStrVal () ); Assert.AreEqual ("str", t1.getStrVal ());
//Console.WriteLine("new val(from C#)="+t1.getStrVal()); //Console.WriteLine("new val(from C#)="+t1.getStrVal());
} }
} }
/* /*
* Tests calling of an object's method with no overloading * Tests calling of an object's method with no overloading
* and out parameters * and out parameters
*/ */
[Test] [Test]
public void CallObjectMethodOutParam () public void CallObjectMethodOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("a,b=netobj:outVal()"); lua.DoString ("a,b=netobj:outVal()");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
int b = (int) lua.GetNumber ("b"); int b = (int)lua.GetNumber ("b");
Assert.AreEqual (3, a); Assert.AreEqual (3, a);
Assert.AreEqual (5, b); Assert.AreEqual (5, b);
//Console.WriteLine("function returned (from lua)="+a+","+b); //Console.WriteLine("function returned (from lua)="+a+","+b);
} }
} }
/* /*
* Tests calling of an object's method with overloading and * Tests calling of an object's method with overloading and
* out params * out params
*/ */
[Test] [Test]
public void CallObjectMethodOverloadedOutParam () public void CallObjectMethodOverloadedOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("a,b=netobj:outVal(2)"); lua.DoString ("a,b=netobj:outVal(2)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
int b = (int) lua.GetNumber ("b"); int b = (int)lua.GetNumber ("b");
Assert.AreEqual (2, a); Assert.AreEqual (2, a);
Assert.AreEqual (5, b); Assert.AreEqual (5, b);
//Console.WriteLine("function returned (from lua)="+a+","+b); //Console.WriteLine("function returned (from lua)="+a+","+b);
} }
} }
/* /*
* Tests calling of an object's method with ref params * Tests calling of an object's method with ref params
*/ */
[Test] [Test]
public void CallObjectMethodByRefParam () public void CallObjectMethodByRefParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("a,b=netobj:outVal(2,3)"); lua.DoString ("a,b=netobj:outVal(2,3)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
int b = (int) lua.GetNumber ("b"); int b = (int)lua.GetNumber ("b");
Assert.AreEqual (2, a); Assert.AreEqual (2, a);
Assert.AreEqual (5, b); Assert.AreEqual (5, b);
//Console.WriteLine("function returned (from lua)="+a+","+b); //Console.WriteLine("function returned (from lua)="+a+","+b);
} }
} }
/* /*
* Tests calling of two versions of an object's method that have * Tests calling of two versions of an object's method that have
* the same name and signature but implement different interfaces * the same name and signature but implement different interfaces
*/ */
[Test] [Test]
public void CallObjectMethodDistinctInterfaces () public void CallObjectMethodDistinctInterfaces ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
TestClass t1 = new TestClass (); TestClass t1 = new TestClass ();
lua ["netobj"] = t1; lua ["netobj"] = t1;
lua.DoString ("a=netobj:foo()"); lua.DoString ("a=netobj:foo()");
lua.DoString ("b=netobj['LuaInterfaceTest.Mock.IFoo1.foo']"); lua.DoString ("b=netobj['LuaInterfaceTest.Mock.IFoo1.foo']");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
int b = (int) lua.GetNumber ("b"); int b = (int)lua.GetNumber ("b");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
Assert.AreEqual (1, b); Assert.AreEqual (1, b);
//Console.WriteLine("function returned (from lua)="+a+","+b); //Console.WriteLine("function returned (from lua)="+a+","+b);
} }
} }
/* /*
* Tests instantiating an object with no-argument constructor * Tests instantiating an object with no-argument constructor
*/ */
[Test] [Test]
public void CreateNetObjectNoArgsCons () public void CreateNetObjectNoArgsCons ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("test:setVal(3)"); lua.DoString ("test:setVal(3)");
object[] res = lua.DoString ("return test"); object[] res = lua.DoString ("return test");
TestClass test = (TestClass) res [0]; TestClass test = (TestClass)res [0];
//Console.WriteLine("returned: "+test.testval); //Console.WriteLine("returned: "+test.testval);
Assert.AreEqual (3, test.testval); Assert.AreEqual (3, test.testval);
} }
} }
/* /*
* Tests instantiating an object with one-argument constructor * Tests instantiating an object with one-argument constructor
*/ */
[Test] [Test]
public void CreateNetObjectOneArgCons () public void CreateNetObjectOneArgCons ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
lua.DoString ("test=TestClass(3)"); lua.DoString ("test=TestClass(3)");
object[] res = lua.DoString ("return test"); object[] res = lua.DoString ("return test");
TestClass test = (TestClass) res [0]; TestClass test = (TestClass)res [0];
//Console.WriteLine("returned: "+test.testval); //Console.WriteLine("returned: "+test.testval);
Assert.AreEqual (3, test.testval); Assert.AreEqual (3, test.testval);
} }
} }
/* /*
* Tests instantiating an object with overloaded constructor * Tests instantiating an object with overloaded constructor
*/ */
[Test] [Test]
public void CreateNetObjectOverloadedCons () public void CreateNetObjectOverloadedCons ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
lua.DoString ("test=TestClass('str')"); lua.DoString ("test=TestClass('str')");
object[] res = lua.DoString ("return test"); object[] res = lua.DoString ("return test");
TestClass test = (TestClass) res [0]; TestClass test = (TestClass)res [0];
//Console.WriteLine("returned: "+test.getStrVal()); //Console.WriteLine("returned: "+test.getStrVal());
Assert.AreEqual ("str", test.getStrVal () ); Assert.AreEqual ("str", test.getStrVal ());
} }
} }
/* /*
* Tests getting item of a CLR array * Tests getting item of a CLR array
*/ */
[Test] [Test]
public void ReadArrayField () public void ReadArrayField ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
string[] arr = new string [] { "str1", "str2", "str3" }; string[] arr = new string [] { "str1", "str2", "str3" };
lua ["netobj"] = arr; lua ["netobj"] = arr;
lua.DoString ("val=netobj[1]"); lua.DoString ("val=netobj[1]");
string val = lua.GetString ("val"); string val = lua.GetString ("val");
Assert.AreEqual ("str2", val); Assert.AreEqual ("str2", val);
//Console.WriteLine("new val(from array to Lua)="+val); //Console.WriteLine("new val(from array to Lua)="+val);
} }
} }
/*G /*G
* Tests setting item of a CLR array * Tests setting item of a CLR array
*/ */
[Test] [Test]
public void WriteArrayField () public void WriteArrayField ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
string[] arr = new string [] { "str1", "str2", "str3" }; string[] arr = new string [] { "str1", "str2", "str3" };
lua ["netobj"] = arr; lua ["netobj"] = arr;
lua.DoString ("netobj[1]='test'"); lua.DoString ("netobj[1]='test'");
Assert.AreEqual ("test", arr [1]); Assert.AreEqual ("test", arr [1]);
//Console.WriteLine("new val(from Lua to array)="+arr[1]); //Console.WriteLine("new val(from Lua to array)="+arr[1]);
} }
} }
/* /*
* Tests creating a new CLR array * Tests creating a new CLR array
*/ */
[Test] [Test]
public void CreateArray () public void CreateArray ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
lua.DoString ("arr=TestClass[3]"); lua.DoString ("arr=TestClass[3]");
lua.DoString ("for i=0,2 do arr[i]=TestClass(i+1) end"); lua.DoString ("for i=0,2 do arr[i]=TestClass(i+1) end");
TestClass[] arr = (TestClass[]) lua ["arr"]; TestClass[] arr = (TestClass[])lua ["arr"];
Assert.AreEqual (arr [1].testval, 2); Assert.AreEqual (arr [1].testval, 2);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with value-type arguments * with value-type arguments
*/ */
[Test] [Test]
public void LuaDelegateValueTypes () public void LuaDelegateValueTypes ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate1), typeof (LuaTestDelegate1Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate1), typeof(LuaTestDelegate1Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x,y) return x+y; end"); lua.DoString ("function func(x,y) return x+y; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate1(func)"); lua.DoString ("a=test:callDelegate1(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with value-type arguments and out params * with value-type arguments and out params
*/ */
[Test] [Test]
public void LuaDelegateValueTypesOutParam () public void LuaDelegateValueTypesOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate2), typeof (LuaTestDelegate2Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate2), typeof(LuaTestDelegate2Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x) return x,x*2; end"); lua.DoString ("function func(x) return x,x*2; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate2(func)"); lua.DoString ("a=test:callDelegate2(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with value-type arguments and ref params * with value-type arguments and ref params
*/ */
[Test] [Test]
public void LuaDelegateValueTypesByRefParam () public void LuaDelegateValueTypesByRefParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate3), typeof (LuaTestDelegate3Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate3), typeof(LuaTestDelegate3Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x,y) return x+y; end"); lua.DoString ("function func(x,y) return x+y; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate3(func)"); lua.DoString ("a=test:callDelegate3(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with value-type arguments that returns a reference type * with value-type arguments that returns a reference type
*/ */
[Test] [Test]
public void LuaDelegateValueTypesReturnReferenceType () public void LuaDelegateValueTypesReturnReferenceType ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate4), typeof (LuaTestDelegate4Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate4), typeof(LuaTestDelegate4Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x,y) return TestClass(x+y); end"); lua.DoString ("function func(x,y) return TestClass(x+y); end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate4(func)"); lua.DoString ("a=test:callDelegate4(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with reference type arguments * with reference type arguments
*/ */
[Test] [Test]
public void LuaDelegateReferenceTypes () public void LuaDelegateReferenceTypes ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate5), typeof (LuaTestDelegate5Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate5), typeof(LuaTestDelegate5Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x,y) return x.testval+y.testval; end"); lua.DoString ("function func(x,y) return x.testval+y.testval; end");
lua.DoString ("a=test:callDelegate5(func)"); lua.DoString ("a=test:callDelegate5(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with reference type arguments and an out param * with reference type arguments and an out param
*/ */
[Test] [Test]
public void LuaDelegateReferenceTypesOutParam () public void LuaDelegateReferenceTypesOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate6), typeof (LuaTestDelegate6Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate6), typeof(LuaTestDelegate6Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x) return x,TestClass(x*2); end"); lua.DoString ("function func(x) return x,TestClass(x*2); end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate6(func)"); lua.DoString ("a=test:callDelegate6(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua function to a delegate * Tests passing a Lua function to a delegate
* with reference type arguments and a ref param * with reference type arguments and a ref param
*/ */
[Test] [Test]
public void LuaDelegateReferenceTypesByRefParam () public void LuaDelegateReferenceTypesByRefParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaDelegateType (typeof (TestDelegate7), typeof (LuaTestDelegate7Handler) ); lua.RegisterLuaDelegateType (typeof(TestDelegate7), typeof(LuaTestDelegate7Handler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x,y) return TestClass(x+y.testval); end"); lua.DoString ("function func(x,y) return TestClass(x+y.testval); end");
lua.DoString ("a=test:callDelegate7(func)"); lua.DoString ("a=test:callDelegate7(func)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("delegate returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with value-type params * calling one of its methods with value-type params
*/ */
[Test] [Test]
public void LuaInterfaceAAValueTypes () public void LuaInterfaceAAValueTypes ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaClassType (typeof (ITest), typeof (LuaITestClassHandler) ); lua.RegisterLuaClassType (typeof(ITest), typeof(LuaITestClassHandler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test1(x,y) return x+y; end"); lua.DoString ("function itest:test1(x,y) return x+y; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface1(itest)"); lua.DoString ("a=test:callInterface1(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with value-type params * calling one of its methods with value-type params
* and an out param * and an out param
*/ */
[Test] [Test]
public void LuaInterfaceValueTypesOutParam () public void LuaInterfaceValueTypesOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test2(x) return x,x*2; end"); lua.DoString ("function itest:test2(x) return x,x*2; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface2(itest)"); lua.DoString ("a=test:callInterface2(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with value-type params * calling one of its methods with value-type params
* and a ref param * and a ref param
*/ */
[Test] [Test]
public void LuaInterfaceValueTypesByRefParam () public void LuaInterfaceValueTypesByRefParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test3(x,y) return x+y; end"); lua.DoString ("function itest:test3(x,y) return x+y; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface3(itest)"); lua.DoString ("a=test:callInterface3(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with value-type params * calling one of its methods with value-type params
* returning a reference type param * returning a reference type param
*/ */
[Test] [Test]
public void LuaInterfaceValueTypesReturnReferenceType () public void LuaInterfaceValueTypesReturnReferenceType ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test4(x,y) return TestClass(x+y); end"); lua.DoString ("function itest:test4(x,y) return TestClass(x+y); end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface4(itest)"); lua.DoString ("a=test:callInterface4(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with reference type params * calling one of its methods with reference type params
*/ */
[Test] [Test]
public void LuaInterfaceReferenceTypes () public void LuaInterfaceReferenceTypes ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test5(x,y) return x.testval+y.testval; end"); lua.DoString ("function itest:test5(x,y) return x.testval+y.testval; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface5(itest)"); lua.DoString ("a=test:callInterface5(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with reference type params * calling one of its methods with reference type params
* and an out param * and an out param
*/ */
[Test] [Test]
public void LuaInterfaceReferenceTypesOutParam () public void LuaInterfaceReferenceTypesOutParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test6(x) return x,TestClass(x*2); end"); lua.DoString ("function itest:test6(x) return x,TestClass(x*2); end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callInterface6(itest)"); lua.DoString ("a=test:callInterface6(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* calling one of its methods with reference type params * calling one of its methods with reference type params
* and a ref param * and a ref param
*/ */
[Test] [Test]
public void LuaInterfaceReferenceTypesByRefParam () public void LuaInterfaceReferenceTypesByRefParam ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:test7(x,y) return TestClass(x+y.testval); end"); lua.DoString ("function itest:test7(x,y) return TestClass(x+y.testval); end");
lua.DoString ("a=test:callInterface7(itest)"); lua.DoString ("a=test:callInterface7(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (5, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
#region LUA_BOILERPLATE_CLASS #region LUA_BOILERPLATE_CLASS
/*** This class is used to bind the .NET world with the Lua world, this boilerplate code is pratically the same, get values call Lua function return value back, /*** This class is used to bind the .NET world with the Lua world, this boilerplate code is pratically the same, get values call Lua function return value back,
* this class is usually dynamic generated using System.Reflection.Emit, but this will not work on iOS. */ * this class is usually dynamic generated using System.Reflection.Emit, but this will not work on iOS. */
class LuaTestClassHandler: TestClass, ILuaGeneratedType class LuaTestClassHandler: TestClass, ILuaGeneratedType
{ {
public LuaTable __luaInterface_luaTable; public LuaTable __luaInterface_luaTable;
public Type[][] __luaInterface_returnTypes; public Type[][] __luaInterface_returnTypes;
public LuaTestClassHandler (LuaTable luaTable, Type[][] returnTypes) public LuaTestClassHandler (LuaTable luaTable, Type[][] returnTypes)
{ {
__luaInterface_luaTable = luaTable; __luaInterface_luaTable = luaTable;
__luaInterface_returnTypes = returnTypes; __luaInterface_returnTypes = returnTypes;
} }
public LuaTable __luaInterface_getLuaTable () public LuaTable __luaInterface_getLuaTable ()
{ {
return __luaInterface_luaTable; return __luaInterface_luaTable;
} }
public override int overridableMethod(int x, int y) public override int overridableMethod (int x, int y)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
x, x,
y y
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
x, x,
y y
}; };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [0]; Type [] returnTypes = __luaInterface_returnTypes [0];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "overridableMethod"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "overridableMethod");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
return (int) ret; return (int)ret;
} }
} }
class LuaITestClassHandler : ILuaGeneratedType, ITest
class LuaITestClassHandler : ILuaGeneratedType, ITest {
{ public LuaTable __luaInterface_luaTable;
public LuaTable __luaInterface_luaTable; public Type[][] __luaInterface_returnTypes;
public Type[][] __luaInterface_returnTypes;
public LuaITestClassHandler (LuaTable luaTable, Type[][] returnTypes)
public LuaITestClassHandler (LuaTable luaTable, Type[][] returnTypes) {
{ __luaInterface_luaTable = luaTable;
__luaInterface_luaTable = luaTable; __luaInterface_returnTypes = returnTypes;
__luaInterface_returnTypes = returnTypes; }
}
public LuaTable __luaInterface_getLuaTable ()
public LuaTable __luaInterface_getLuaTable () {
{ return __luaInterface_luaTable;
return __luaInterface_luaTable; }
}
public int intProp {
public int intProp get {
{ object [] args = new object [] { __luaInterface_luaTable };
get { object [] inArgs = new object [] { __luaInterface_luaTable };
object [] args = new object [] { __luaInterface_luaTable }; int [] outArgs = new int [] { };
object [] inArgs = new object [] { __luaInterface_luaTable }; Type [] returnTypes = __luaInterface_returnTypes [0];
int [] outArgs = new int [] { }; LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_intProp");
Type [] returnTypes = __luaInterface_returnTypes [0]; object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_intProp"); return (int)ret;
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); }
return (int) ret; set {
} set { int i = value;
int i = value; object [] args = new object [] {
object [] args = new object [] { __luaInterface_luaTable , i}; __luaInterface_luaTable ,
object [] inArgs = new object [] { __luaInterface_luaTable, i }; i
int [] outArgs = new int [] { }; };
Type [] returnTypes = __luaInterface_returnTypes [1]; object [] inArgs = new object [] {
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_intProp"); __luaInterface_luaTable,
LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); i
} };
} int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [1];
public TestClass refProp LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_intProp");
{ LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
get { }
object [] args = new object [] { __luaInterface_luaTable }; }
object [] inArgs = new object [] { __luaInterface_luaTable };
int [] outArgs = new int [] { }; public TestClass refProp {
Type [] returnTypes = __luaInterface_returnTypes [2]; get {
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_refProp"); object [] args = new object [] { __luaInterface_luaTable };
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object [] inArgs = new object [] { __luaInterface_luaTable };
return (TestClass) ret; int [] outArgs = new int [] { };
} set { Type [] returnTypes = __luaInterface_returnTypes [2];
TestClass test = value; LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_refProp");
object [] args = new object [] { __luaInterface_luaTable , test}; object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
object [] inArgs = new object [] { __luaInterface_luaTable, test }; return (TestClass)ret;
int [] outArgs = new int [] { }; }
Type [] returnTypes = __luaInterface_returnTypes [3]; set {
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_refProp"); TestClass test = value;
LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object [] args = new object [] {
} __luaInterface_luaTable ,
} test
};
public int test1 (int a, int b) object [] inArgs = new object [] {
{ __luaInterface_luaTable,
object [] args = new object [] { test
};
int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [3];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_refProp");
LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
}
}
public int test1 (int a, int b)
{
object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [4]; Type [] returnTypes = __luaInterface_returnTypes [4];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test1"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test1");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
return (int) ret; return (int)ret;
} }
public int test2 (int a, out int b) public int test2 (int a, out int b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
0 0
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a a
}; };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
Type [] returnTypes = __luaInterface_returnTypes [5]; Type [] returnTypes = __luaInterface_returnTypes [5];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test2"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test2");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
b = (int)args [1]; b = (int)args [1];
return (int) ret; return (int)ret;
} }
public void test3 (int a, ref int b) public void test3 (int a, ref int b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
Type [] returnTypes = __luaInterface_returnTypes [6]; Type [] returnTypes = __luaInterface_returnTypes [6];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test3"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test3");
LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
b = (int)args [1]; b = (int)args [1];
} }
public TestClass test4 (int a, int b) public TestClass test4 (int a, int b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [7]; Type [] returnTypes = __luaInterface_returnTypes [7];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test4"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test4");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
return (TestClass) ret; return (TestClass)ret;
} }
public int test5 (TestClass a, TestClass b) public int test5 (TestClass a, TestClass b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
Type [] returnTypes = __luaInterface_returnTypes [8]; Type [] returnTypes = __luaInterface_returnTypes [8];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test5"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test5");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
return (int) ret; return (int)ret;
} }
public int test6 (int a, out TestClass b) public int test6 (int a, out TestClass b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
null null
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
}; };
int [] outArgs = new int [] { 1}; int [] outArgs = new int [] { 1};
Type [] returnTypes = __luaInterface_returnTypes [9]; Type [] returnTypes = __luaInterface_returnTypes [9];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test6"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test6");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
b = (TestClass)args [1]; b = (TestClass)args [1];
return (int) ret; return (int)ret;
} }
public void test7 (int a, ref TestClass b) public void test7 (int a, ref TestClass b)
{ {
object [] args = new object [] { object [] args = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
object [] inArgs = new object [] { object [] inArgs = new object [] {
__luaInterface_luaTable, __luaInterface_luaTable,
a, a,
b b
}; };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
Type [] returnTypes = __luaInterface_returnTypes [10]; Type [] returnTypes = __luaInterface_returnTypes [10];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test7"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test7");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
b = (TestClass)args [1]; b = (TestClass)args [1];
} }
} }
#endregion #endregion
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* accessing one of its value-type properties * accessing one of its value-type properties
*/ */
[Test] [Test]
public void LuaInterfaceValueProperty () public void LuaInterfaceValueProperty ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:get_intProp() return itest.int_prop; end"); lua.DoString ("function itest:get_intProp() return itest.int_prop; end");
lua.DoString ("function itest:set_intProp(val) itest.int_prop=val; end"); lua.DoString ("function itest:set_intProp(val) itest.int_prop=val; end");
lua.DoString ("a=test:callInterface8(itest)"); lua.DoString ("a=test:callInterface8(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (3, a); Assert.AreEqual (3, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests passing a Lua table as an interface and * Tests passing a Lua table as an interface and
* accessing one of its reference type properties * accessing one of its reference type properties
*/ */
[Test] [Test]
public void LuaInterfaceReferenceProperty () public void LuaInterfaceReferenceProperty ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("itest={}");
lua.DoString ("function itest:get_refProp() return TestClass(itest.int_prop); end"); lua.DoString ("function itest:get_refProp() return TestClass(itest.int_prop); end");
lua.DoString ("function itest:set_refProp(val) itest.int_prop=val.testval; end"); lua.DoString ("function itest:set_refProp(val) itest.int_prop=val.testval; end");
lua.DoString ("a=test:callInterface9(itest)"); lua.DoString ("a=test:callInterface9(itest)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
Assert.AreEqual (3, a); Assert.AreEqual (3, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests making an object from a Lua table and calling the base * Tests making an object from a Lua table and calling the base
* class version of one of the methods the table overrides. * class version of one of the methods the table overrides.
*/ */
[Test] [Test]
public void LuaTableBaseMethod () public void LuaTableBaseMethod ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.RegisterLuaClassType (typeof (TestClass), typeof (LuaTestClassHandler)); lua.RegisterLuaClassType (typeof(TestClass), typeof(LuaTestClassHandler));
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test={}"); lua.DoString ("test={}");
lua.DoString ("function test:overridableMethod(x,y) print(self[base]); return 6 end"); lua.DoString ("function test:overridableMethod(x,y) print(self[base]); return 6 end");
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("a=TestClass.callOverridable(test,2,3)"); lua.DoString ("a=TestClass.callOverridable(test,2,3)");
int a = (int) lua.GetNumber ("a"); int a = (int)lua.GetNumber ("a");
lua.DoString ("luanet.free_object(test)"); lua.DoString ("luanet.free_object(test)");
Assert.AreEqual (6, a); Assert.AreEqual (6, a);
// lua.DoString("luanet.load_assembly('LuaInterfaceTest')"); // lua.DoString("luanet.load_assembly('LuaInterfaceTest')");
// lua.DoString("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); // lua.DoString("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
// lua.DoString("test={}"); // lua.DoString("test={}");
// //
// lua.DoString("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); // lua.DoString("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
// lua.DoString ("function test.overridableMethod(test,x,y) return 2*test.base.overridableMethod(test,x,y); end"); // lua.DoString ("function test.overridableMethod(test,x,y) return 2*test.base.overridableMethod(test,x,y); end");
// lua.DoString("a=TestClass.callOverridable(test,2,3)"); // lua.DoString("a=TestClass.callOverridable(test,2,3)");
// int a = (int)lua.GetNumber("a"); // int a = (int)lua.GetNumber("a");
// lua.DoString("luanet.free_object(test)"); // lua.DoString("luanet.free_object(test)");
// Assert.AreEqual(10, a); // Assert.AreEqual(10, a);
//Console.WriteLine("interface returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/* /*
* Tests getting an object's method by its signature * Tests getting an object's method by its signature
* (from object) * (from object)
*/ */
[Test] [Test]
public void GetMethodBySignatureFromObj () public void GetMethodBySignatureFromObj ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("setMethod=luanet.get_method_bysig(test,'setVal','System.String')"); lua.DoString ("setMethod=luanet.get_method_bysig(test,'setVal','System.String')");
lua.DoString ("setMethod('test')"); lua.DoString ("setMethod('test')");
TestClass test = (TestClass) lua ["test"]; TestClass test = (TestClass)lua ["test"];
Assert.AreEqual ("test", test.getStrVal () ); Assert.AreEqual ("test", test.getStrVal ());
//Console.WriteLine("interface returned: "+test.getStrVal()); //Console.WriteLine("interface returned: "+test.getStrVal());
} }
} }
/* /*
* Tests getting an object's method by its signature * Tests getting an object's method by its signature
* (from type) * (from type)
*/ */
[Test] [Test]
public void GetMethodBySignatureFromType () public void GetMethodBySignatureFromType ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("setMethod=luanet.get_method_bysig(TestClass,'setVal','System.String')"); lua.DoString ("setMethod=luanet.get_method_bysig(TestClass,'setVal','System.String')");
lua.DoString ("setMethod(test,'test')"); lua.DoString ("setMethod(test,'test')");
TestClass test = (TestClass) lua ["test"]; TestClass test = (TestClass)lua ["test"];
Assert.AreEqual ("test", test.getStrVal () ); Assert.AreEqual ("test", test.getStrVal ());
//Console.WriteLine("interface returned: "+test.getStrVal()); //Console.WriteLine("interface returned: "+test.getStrVal());
} }
} }
/* /*
* Tests getting a type's method by its signature * Tests getting a type's method by its signature
*/ */
[Test] [Test]
public void GetStaticMethodBySignature () public void GetStaticMethodBySignature ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("make_method=luanet.get_method_bysig(TestClass,'makeFromString','System.String')"); lua.DoString ("make_method=luanet.get_method_bysig(TestClass,'makeFromString','System.String')");
lua.DoString ("test=make_method('test')"); lua.DoString ("test=make_method('test')");
TestClass test = (TestClass) lua ["test"]; TestClass test = (TestClass)lua ["test"];
Assert.AreEqual ("test", test.getStrVal () ); Assert.AreEqual ("test", test.getStrVal ());
//Console.WriteLine("interface returned: "+test.getStrVal()); //Console.WriteLine("interface returned: "+test.getStrVal());
} }
} }
/* /*
* Tests getting an object's constructor by its signature * Tests getting an object's constructor by its signature
*/ */
[Test] [Test]
public void GetConstructorBySignature () public void GetConstructorBySignature ()
{ {
using (Lua lua = new Lua () ) { using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test_cons=luanet.get_constructor_bysig(TestClass,'System.String')"); lua.DoString ("test_cons=luanet.get_constructor_bysig(TestClass,'System.String')");
lua.DoString ("test=test_cons('test')"); lua.DoString ("test=test_cons('test')");
TestClass test = (TestClass) lua ["test"]; TestClass test = (TestClass)lua ["test"];
Assert.AreEqual ("test", test.getStrVal () ); Assert.AreEqual ("test", test.getStrVal ());
//Console.WriteLine("interface returned: "+test.getStrVal()); //Console.WriteLine("interface returned: "+test.getStrVal());
} }
} }
} }
} }
...@@ -2,23 +2,28 @@ ...@@ -2,23 +2,28 @@
namespace LuaInterfaceTest.Mock namespace LuaInterfaceTest.Mock
{ {
using System; using System;
using LuaInterface; using LuaInterface;
using System.Threading; using System.Threading;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
/* /*
* Delegates used for testing Lua function -> delegate translation * Delegates used for testing Lua function -> delegate translation
*/ */
public delegate int TestDelegate1(int a, int b); public delegate int TestDelegate1 (int a, int b);
public delegate int TestDelegate2(int a, out int b);
public delegate void TestDelegate3(int a, ref int b); public delegate int TestDelegate2 (int a, out int b);
public delegate TestClass TestDelegate4(int a, int b);
public delegate int TestDelegate5(TestClass a, TestClass b); public delegate void TestDelegate3 (int a, ref int b);
public delegate int TestDelegate6(int a, out TestClass b);
public delegate void TestDelegate7(int a, ref TestClass b); public delegate TestClass TestDelegate4 (int a, int b);
public delegate int TestDelegate5 (TestClass a, TestClass b);
public delegate int TestDelegate6 (int a, out TestClass b);
public delegate void TestDelegate7 (int a, ref TestClass b);
/* Delegate Lua-handlers */ /* Delegate Lua-handlers */
...@@ -32,7 +37,7 @@ namespace LuaInterfaceTest.Mock ...@@ -32,7 +37,7 @@ namespace LuaInterfaceTest.Mock
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (int) ret; return (int)ret;
} }
} }
...@@ -47,7 +52,7 @@ namespace LuaInterfaceTest.Mock ...@@ -47,7 +52,7 @@ namespace LuaInterfaceTest.Mock
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
b = (int)args [1]; b = (int)args [1];
return (int) ret; return (int)ret;
} }
} }
...@@ -75,7 +80,7 @@ namespace LuaInterfaceTest.Mock ...@@ -75,7 +80,7 @@ namespace LuaInterfaceTest.Mock
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (TestClass) ret; return (TestClass)ret;
} }
} }
...@@ -89,7 +94,7 @@ namespace LuaInterfaceTest.Mock ...@@ -89,7 +94,7 @@ namespace LuaInterfaceTest.Mock
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (int) ret; return (int)ret;
} }
} }
...@@ -104,7 +109,7 @@ namespace LuaInterfaceTest.Mock ...@@ -104,7 +109,7 @@ namespace LuaInterfaceTest.Mock
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
b = (TestClass)args [1]; b = (TestClass)args [1];
return (int) ret; return (int)ret;
} }
} }
...@@ -123,404 +128,433 @@ namespace LuaInterfaceTest.Mock ...@@ -123,404 +128,433 @@ namespace LuaInterfaceTest.Mock
} }
/* /*
* Interface used for testing Lua table -> interface translation * Interface used for testing Lua table -> interface translation
*/ */
public interface ITest public interface ITest
{ {
int intProp int intProp {
{ get;
get; set;
set; }
}
TestClass refProp TestClass refProp {
{ get;
get; set;
set; }
}
int test1(int a, int b); int test1 (int a, int b);
int test2(int a, out int b);
void test3(int a, ref int b); int test2 (int a, out int b);
TestClass test4(int a, int b);
int test5(TestClass a, TestClass b); void test3 (int a, ref int b);
int test6(int a, out TestClass b);
void test7(int a, ref TestClass b); TestClass test4 (int a, int b);
}
int test5 (TestClass a, TestClass b);
public interface IFoo1
{ int test6 (int a, out TestClass b);
int foo();
} void test7 (int a, ref TestClass b);
}
public interface IFoo2
{ public interface IFoo1
int foo(); {
} int foo ();
}
class MyClass
{ public interface IFoo2
public int Func1() { return 1; } {
} int foo ();
}
/// <summary>
/// Use to test threading class MyClass
/// </summary> {
class DoWorkClass public int Func1 ()
{ {
return 1;
public void DoWork() }
{ }
//simulate work by sleeping /// <summary>
//Console.WriteLine("Started to do work on thread: " + Thread.CurrentThread.ManagedThreadId); /// Use to test threading
Thread.Sleep(new Random().Next(0, 1000)); /// </summary>
//Console.WriteLine("Finished work on thread: " + Thread.CurrentThread.ManagedThreadId); class DoWorkClass
} {
}
public void DoWork ()
/// <summary> {
/// test structure passing
/// </summary> //simulate work by sleeping
public struct TestStruct //Console.WriteLine("Started to do work on thread: " + Thread.CurrentThread.ManagedThreadId);
{ Thread.Sleep (new Random ().Next (0, 1000));
public TestStruct(float val) //Console.WriteLine("Finished work on thread: " + Thread.CurrentThread.ManagedThreadId);
{ }
v = val; }
}
/// <summary>
public float v; /// test structure passing
/// </summary>
public float val public struct TestStruct
{ {
get { return v; } public TestStruct (float val)
set { v = value; } {
} v = val;
} }
/// <summary> public float v;
/// Generic class with generic and non-generic methods
/// </summary> public float val {
/// <typeparam name="T"></typeparam> get { return v; }
public class TestClassGeneric<T> set { v = value; }
{ }
private object _PassedValue; }
private bool _RegularMethodSuccess; /// <summary>
/// Generic class with generic and non-generic methods
public bool RegularMethodSuccess /// </summary>
{ /// <typeparam name="T"></typeparam>
get { return _RegularMethodSuccess; } public class TestClassGeneric<T>
} {
private object _PassedValue;
private bool _GenericMethodSuccess; private bool _RegularMethodSuccess;
public bool GenericMethodSuccess public bool RegularMethodSuccess {
{ get { return _RegularMethodSuccess; }
get { return _GenericMethodSuccess; } }
}
private bool _GenericMethodSuccess;
public void GenericMethod(T value)
{ public bool GenericMethodSuccess {
_PassedValue = value; get { return _GenericMethodSuccess; }
_GenericMethodSuccess = true; }
}
public void GenericMethod (T value)
public void RegularMethod() {
{ _PassedValue = value;
_RegularMethodSuccess = true; _GenericMethodSuccess = true;
} }
/// <summary> public void RegularMethod ()
/// Returns true if the generic method was successfully passed a matching value {
/// </summary> _RegularMethodSuccess = true;
/// <param name="value"></param> }
/// <returns></returns>
public bool Validate(T value) /// <summary>
{ /// Returns true if the generic method was successfully passed a matching value
return value.Equals(_PassedValue); /// </summary>
} /// <param name="value"></param>
} /// <returns></returns>
public bool Validate (T value)
/// <summary> {
/// Normal class containing a generic method return value.Equals (_PassedValue);
/// </summary> }
public class TestClassWithGenericMethod }
{
private object _PassedValue; /// <summary>
/// Normal class containing a generic method
public object PassedValue /// </summary>
{ public class TestClassWithGenericMethod
get { return _PassedValue; } {
} private object _PassedValue;
private bool _GenericMethodSuccess; public object PassedValue {
get { return _PassedValue; }
public bool GenericMethodSuccess }
{
get { return _GenericMethodSuccess; } private bool _GenericMethodSuccess;
}
public bool GenericMethodSuccess {
public void GenericMethod<T>(T value) get { return _GenericMethodSuccess; }
{ }
_PassedValue = value;
_GenericMethodSuccess = true; public void GenericMethod<T> (T value)
} {
_PassedValue = value;
internal bool Validate<T>(T value) _GenericMethodSuccess = true;
{ }
return value.Equals(_PassedValue);
} internal bool Validate<T> (T value)
} {
return value.Equals (_PassedValue);
public class TestClass2 }
{ }
public static int func(int x, int y)
{ public class TestClass2
return x + y; {
} public static int func (int x, int y)
public int funcInstance(int x, int y) {
{ return x + y;
return x + y; }
}
} public int funcInstance (int x, int y)
{
/* return x + y;
}
}
/*
* Sample class used in several test cases to check if * Sample class used in several test cases to check if
* Lua scripts are accessing objects correctly * Lua scripts are accessing objects correctly
*/ */
public class TestClass : IFoo1, IFoo2 public class TestClass : IFoo1, IFoo2
{ {
public int val; public int val;
private string strVal; private string strVal;
public TestClass()
{ public TestClass ()
val = 0; {
} val = 0;
public TestClass(int val) }
{
this.val = val; public TestClass (int val)
} {
public TestClass(string val) this.val = val;
{ }
this.strVal = val;
} public TestClass (string val)
public static TestClass makeFromString(String str) {
{ this.strVal = val;
return new TestClass(str); }
}
public static TestClass makeFromString (String str)
bool? nb2 = null; {
return new TestClass (str);
public bool? NullableBool }
{
get { return nb2; } bool? nb2 = null;
set { nb2 = value; }
} public bool? NullableBool {
get { return nb2; }
set { nb2 = value; }
TestStruct s = new TestStruct(); }
public TestStruct Struct TestStruct s = new TestStruct ();
{
get { return s; } public TestStruct Struct {
set { s = (TestStruct)value; } get { return s; }
} set { s = (TestStruct)value; }
}
public int testval
{ public int testval {
get get {
{ return this.val;
return this.val; }
} set {
set this.val = value;
{ }
this.val = value; }
}
} public int this [int index] {
public int this[int index] get { return 1; }
{ set { }
get { return 1; } }
set { }
} public int this [string index] {
public int this[string index] get { return 1; }
{ set { }
get { return 1; } }
set { }
} public int sum (int x, int y)
public int sum(int x, int y) {
{ return x + y;
return x + y; }
}
public void setVal(int newVal) public void setVal (int newVal)
{ {
val = newVal; val = newVal;
} }
public void setVal(string newVal)
{ public void setVal (string newVal)
strVal = newVal; {
} strVal = newVal;
public int getVal() }
{
return val; public int getVal ()
} {
public string getStrVal() return val;
{ }
return strVal;
} public string getStrVal ()
public int outVal(out int val) {
{ return strVal;
val = 5; }
return 3;
} public int outVal (out int val)
public int outVal(out int val, int val2) {
{ val = 5;
val = 5; return 3;
return val2; }
}
public int outVal(int val, ref int val2) public int outVal (out int val, int val2)
{ {
val2 = val + val2; val = 5;
return val; return val2;
} }
public int outValMutiple(int arg, out string arg2, out string arg3) public int outVal (int val, ref int val2)
{ {
arg2 = Guid.NewGuid().ToString(); val2 = val + val2;
arg3 = Guid.NewGuid().ToString(); return val;
}
return arg;
} public int outValMutiple (int arg, out string arg2, out string arg3)
{
public int callDelegate1(TestDelegate1 del) arg2 = Guid.NewGuid ().ToString ();
{ arg3 = Guid.NewGuid ().ToString ();
return del(2, 3);
} return arg;
public int callDelegate2(TestDelegate2 del) }
{
int a = 3; public int callDelegate1 (TestDelegate1 del)
int b = del(2, out a); {
return a + b; return del (2, 3);
} }
public int callDelegate3(TestDelegate3 del)
{ public int callDelegate2 (TestDelegate2 del)
int a = 3; {
del(2, ref a); int a = 3;
//Console.WriteLine(a); int b = del (2, out a);
return a; return a + b;
} }
public int callDelegate4(TestDelegate4 del)
{ public int callDelegate3 (TestDelegate3 del)
return del(2, 3).testval; {
} int a = 3;
public int callDelegate5(TestDelegate5 del) del (2, ref a);
{ //Console.WriteLine(a);
return del(new TestClass(2), new TestClass(3)); return a;
} }
public int callDelegate6(TestDelegate6 del)
{ public int callDelegate4 (TestDelegate4 del)
TestClass test = new TestClass(); {
int a = del(2, out test); return del (2, 3).testval;
return a + test.testval; }
}
public int callDelegate7(TestDelegate7 del) public int callDelegate5 (TestDelegate5 del)
{ {
TestClass test = new TestClass(3); return del (new TestClass (2), new TestClass (3));
del(2, ref test); }
return test.testval;
} public int callDelegate6 (TestDelegate6 del)
public int callInterface1(ITest itest) {
{ TestClass test = new TestClass ();
return itest.test1(2, 3); int a = del (2, out test);
} return a + test.testval;
public int callInterface2(ITest itest) }
{
int a = 3; public int callDelegate7 (TestDelegate7 del)
int b = itest.test2(2, out a); {
return a + b; TestClass test = new TestClass (3);
} del (2, ref test);
public int callInterface3(ITest itest) return test.testval;
{ }
int a = 3;
itest.test3(2, ref a); public int callInterface1 (ITest itest)
//Console.WriteLine(a); {
return a; return itest.test1 (2, 3);
} }
public int callInterface4(ITest itest)
{ public int callInterface2 (ITest itest)
return itest.test4(2, 3).testval; {
} int a = 3;
public int callInterface5(ITest itest) int b = itest.test2 (2, out a);
{ return a + b;
return itest.test5(new TestClass(2), new TestClass(3)); }
}
public int callInterface6(ITest itest) public int callInterface3 (ITest itest)
{ {
TestClass test = new TestClass(); int a = 3;
int a = itest.test6(2, out test); itest.test3 (2, ref a);
return a + test.testval; //Console.WriteLine(a);
} return a;
public int callInterface7(ITest itest) }
{
TestClass test = new TestClass(3); public int callInterface4 (ITest itest)
itest.test7(2, ref test); {
return test.testval; return itest.test4 (2, 3).testval;
} }
public int callInterface8(ITest itest)
{ public int callInterface5 (ITest itest)
itest.intProp = 3; {
return itest.intProp; return itest.test5 (new TestClass (2), new TestClass (3));
} }
public int callInterface9(ITest itest)
{ public int callInterface6 (ITest itest)
itest.refProp = new TestClass(3); {
return itest.refProp.testval; TestClass test = new TestClass ();
} int a = itest.test6 (2, out test);
public void exceptionMethod() return a + test.testval;
{ }
throw new Exception("exception test");
} public int callInterface7 (ITest itest)
public virtual int overridableMethod(int x, int y) {
{ TestClass test = new TestClass (3);
return x + y; itest.test7 (2, ref test);
} return test.testval;
public static int callOverridable(TestClass test, int x, int y) }
{
return test.overridableMethod(x, y); public int callInterface8 (ITest itest)
} {
int IFoo1.foo() itest.intProp = 3;
{ return itest.intProp;
return 3; }
}
public int foo() public int callInterface9 (ITest itest)
{ {
return 5; itest.refProp = new TestClass (3);
} return itest.refProp.testval;
}
private void _PrivateMethod()
{ public void exceptionMethod ()
Console.WriteLine("Private method called"); {
} throw new Exception ("exception test");
}
public void MethodOverload()
{ public virtual int overridableMethod (int x, int y)
Console.WriteLine("Method with no params"); {
} return x + y;
}
public void MethodOverload(TestClass testClass) public static int callOverridable (TestClass test, int x, int y)
{ {
Console.WriteLine("Method with testclass param"); return test.overridableMethod (x, y);
} }
public void MethodOverload(int i, int j, int k) int IFoo1.foo ()
{ {
Console.WriteLine("Overload without out param: " + i + ", " + j + ", " + k); return 3;
} }
public void MethodOverload(int i, int j, out int k) public int foo ()
{ {
k = 5; return 5;
Console.WriteLine("Overload with out param" + i + ", " + j); }
}
} private void _PrivateMethod ()
{
Console.WriteLine ("Private method called");
}
public void MethodOverload ()
{
Console.WriteLine ("Method with no params");
}
public void MethodOverload (TestClass testClass)
{
Console.WriteLine ("Method with testclass param");
}
public void MethodOverload (int i, int j, int k)
{
Console.WriteLine ("Overload without out param: " + i + ", " + j + ", " + k);
}
public void MethodOverload (int i, int j, out int k)
{
k = 5;
Console.WriteLine ("Overload with out param" + i + ", " + j);
}
}
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment