Commit d69cf176 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Added boilerplate class to fix the tests.

parent 279f84a8
...@@ -45,6 +45,21 @@ Global ...@@ -45,6 +45,21 @@ Global
EndGlobalSection EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = LuaInterfaceTest\LuaInterfaceTest.csproj StartupItem = LuaInterfaceTest\LuaInterfaceTest.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = null
$1.scope = text/x-csharp
$0.CSharpFormattingPolicy = $2
$2.AfterDelegateDeclarationParameterComma = True
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
$0.TextStylePolicy = $3
$3.FileWidth = 120
$3.TabsToSpaces = True
$3.inheritsSet = Mono
$3.inheritsScope = text/plain
$3.scope = text/plain
description = LuaInterface description = LuaInterface
version = 2.x version = 2.x
EndGlobalSection EndGlobalSection
......
using System; using System;
using System.Text; using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using LuaInterfaceTest.Mock; using LuaInterfaceTest.Mock;
using System.Reflection; using System.Reflection;
...@@ -11,1638 +10,1707 @@ using LuaInterface.Exceptions; ...@@ -11,1638 +10,1707 @@ 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]
*/ public void ThrowUncaughtException ()
[Test] {
public void ThrowUncaughtException () using (Lua lua = new Lua () ) {
{ lua.DoString ("luanet.load_assembly('mscorlib')");
using (Lua lua = new Lua ()) { lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); try {
lua.DoString ("test:exceptionMethod()");
try { //failed
lua.DoString ("test:exceptionMethod()"); Assert.True (false);
} catch (Exception e) {
//failed //passed
Assert.True (false); Assert.True (true);
} }
catch (Exception e) { }
//passed }
Assert.True (true);
}
} /*
} * Tests nullable fields
*/
[Test]
/* public void TestNullable ()
* Tests nullable fields {
*/ using (Lua lua = new Lua () ) {
[Test] lua.DoString ("luanet.load_assembly('mscorlib')");
public void TestNullable () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("val=test.NullableBool");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); Assert.Null ( (object) lua ["val"]);
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("test.NullableBool = true");
lua.DoString ("test=TestClass()"); lua.DoString ("val=test.NullableBool");
Assert.True ( (bool) lua ["val"]);
lua.DoString ("val=test.NullableBool"); }
}
Assert.Null ((object)lua ["val"]);
lua.DoString ("test.NullableBool = true"); /*
lua.DoString ("val=test.NullableBool"); * Tests structure assignment
Assert.True ((bool)lua ["val"]); */
} [Test]
} public void TestStructs ()
{
using (Lua lua = new Lua () ) {
/* lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
* Tests structure assignment lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
*/ lua.DoString ("test=TestClass()");
[Test] lua.DoString ("TestStruct=luanet.import_type('LuaInterfaceTest.Mock.TestStruct')");
public void TestStructs () lua.DoString ("struct=TestStruct(2)");
{ lua.DoString ("test.Struct = struct");
using (Lua lua = new Lua ()) { lua.DoString ("val=test.Struct.val");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); Assert.AreEqual (2.0d, (double) lua ["val"]);
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); }
lua.DoString ("test=TestClass()"); }
lua.DoString ("TestStruct=luanet.import_type('LuaInterfaceTest.Mock.TestStruct')");
[Test]
lua.DoString ("struct=TestStruct(2)"); public void TestMethodOverloads ()
lua.DoString ("test.Struct = struct"); {
lua.DoString ("val=test.Struct.val"); using (Lua lua = new Lua () ) {
Assert.AreEqual (2.0d, (double)lua ["val"]); lua.DoString ("luanet.load_assembly('mscorlib')");
} lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
} lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()");
[Test] lua.DoString ("test:MethodOverload()");
public void TestMethodOverloads () lua.DoString ("test:MethodOverload(test)");
{ lua.DoString ("test:MethodOverload(1,1,1)");
using (Lua lua = new Lua ()) { lua.DoString ("test:MethodOverload(2,2,i)\r\nprint(i)");
lua.DoString ("luanet.load_assembly('mscorlib')"); }
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); }
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); [Test]
lua.DoString ("test:MethodOverload()"); public void TestDispose ()
lua.DoString ("test:MethodOverload(test)"); {
lua.DoString ("test:MethodOverload(1,1,1)"); System.GC.Collect ();
lua.DoString ("test:MethodOverload(2,2,i)\r\nprint(i)"); long startingMem = System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64;
}
} for (int i = 0; i < 100; i++) {
using (Lua lua = new Lua () ) {
[Test] _Calc (lua, i);
public void TestDispose () }
{ }
System.GC.Collect ();
long startingMem = System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64; //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");
for (int i = 0; i < 100; i++) { }
using (Lua lua = new Lua ()) {
_Calc (lua, i); private void _Calc (Lua lua, int i)
} {
} lua.DoString (
"sqrt = math.sqrt;" +
//TODO: make this test assert so that it is useful "sqr = function(x) return math.pow(x,2); end;" +
"log = math.log;" +
Console.WriteLine ("Was using " + startingMem / 1024 / 1024 + "MB, now using: " + System.Diagnostics.Process.GetCurrentProcess ().WorkingSet64 / 1024 / 1024 + "MB"); "log10 = math.log10;" +
} "exp = math.exp;" +
"sin = math.sin;" +
private void _Calc (Lua lua, int i) "cos = math.cos;" +
{ "tan = math.tan;" +
lua.DoString ( "abs = math.abs;"
"sqrt = math.sqrt;" + );
"sqr = function(x) return math.pow(x,2); end;" + lua.DoString ("function calcVP(a,b) return a+b end");
"log = math.log;" + LuaFunction lf = lua.GetFunction ("calcVP");
"log10 = math.log10;" + Object[] ret = lf.Call (i, 20);
"exp = math.exp;" + }
"sin = math.sin;" +
"cos = math.cos;" + [Test]
"tan = math.tan;" + public void TestThreading ()
"abs = math.abs;" {
); using (Lua lua = new Lua () ) {
object lua_locker = new object ();
lua.DoString ("function calcVP(a,b) return a+b end"); DoWorkClass doWork = new DoWorkClass ();
lua.RegisterFunction ("dowork", doWork, typeof (DoWorkClass).GetMethod ("DoWork") );
LuaFunction lf = lua.GetFunction ("calcVP"); bool failureDetected = false;
Object[] ret = lf.Call (i, 20); int completed = 0;
} int iterations = 10;
[Test] for (int i = 0; i < iterations; i++) {
public void TestThreading () ThreadPool.QueueUserWorkItem (new WaitCallback (delegate (object o) {
{ try {
using (Lua lua = new Lua ()) { lock (lua_locker) {
object lua_locker = new object (); lua.DoString ("dowork()");
DoWorkClass doWork = new DoWorkClass (); }
lua.RegisterFunction ("dowork", doWork, typeof (DoWorkClass).GetMethod ("DoWork")); } catch (Exception e) {
Console.Write (e);
bool failureDetected = false; failureDetected = true;
int completed = 0; }
int iterations = 10;
completed++;
for (int i = 0; i < iterations; i++) { }) );
ThreadPool.QueueUserWorkItem (new WaitCallback (delegate (object o) }
{
try { while (completed < iterations && !failureDetected)
lock (lua_locker) { Thread.Sleep (50);
lua.DoString ("dowork()");
} Assert.False (failureDetected);
} }
catch (Exception e) { }
Console.Write (e);
failureDetected = true; [Test]
} public void TestPrivateMethod ()
completed++; {
})); using (Lua lua = new Lua () ) {
} lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
while (completed < iterations && !failureDetected) lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
Thread.Sleep (50); lua.DoString ("test=TestClass()");
Assert.False (failureDetected); try {
} lua.DoString ("test:_PrivateMethod()");
} } catch {
Assert.True (true);
[Test] return;
public void TestPrivateMethod () }
{
using (Lua lua = new Lua ()) { Assert.True (false);
lua.DoString ("luanet.load_assembly('mscorlib')"); }
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); }
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); /*
* Tests functions
try { */
lua.DoString ("test:_PrivateMethod()"); [Test]
} public void TestFunctions ()
catch { {
Assert.True (true); using (Lua lua = new Lua () ) {
return; lua.DoString ("luanet.load_assembly('mscorlib')");
} lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.RegisterFunction ("p", null, typeof (System.Console).GetMethod ("WriteLine", new Type [] { typeof (String) }) );
Assert.True (false); /// Lua command that works (prints to console)
} lua.DoString ("p('Foo')");
} /// Yet this works...
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
* Tests functions lua.DoString ("string.gsub('some string', '(%w+)', p)");
*/ }
[Test] }
public void TestFunctions ()
{
using (Lua lua = new Lua ()) { /*
lua.DoString ("luanet.load_assembly('mscorlib')"); * Tests making an object from a Lua table and calling one of
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); * methods the table overrides.
lua.RegisterFunction ("p", null, typeof (System.Console).GetMethod ("WriteLine", new Type [] { typeof (String) })); */
[Test]
/// Lua command that works (prints to console) public void LuaTableOverridedMethod ()
lua.DoString ("p('Foo')"); {
using (Lua lua = new Lua () ) {
/// Yet this works... lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("string.gsub('some string', '(%w+)', function(s) p(s) end)"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test={}");
/// This fails if you don't fix Lua5.1 lstrlib.c/add_value to treat LUA_TUSERDATA the same as LUA_FUNCTION lua.DoString ("function test:overridableMethod(x,y) return x*y; end");
lua.DoString ("string.gsub('some string', '(%w+)', p)"); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
} lua.DoString ("a=TestClass.callOverridable(test,2,3)");
} int a = (int) lua.GetNumber ("a");
lua.DoString ("luanet.free_object(test)");
Assert.AreEqual (6, a);
/* }
* Tests making an object from a Lua table and calling one of }
* methods the table overrides.
*/
[Test] /*
public void LuaTableOverridedMethod () * Tests making an object from a Lua table and calling a method
{ * the table does not override.
using (Lua lua = new Lua ()) { */
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); [Test]
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); public void LuaTableInheritedMethod ()
lua.DoString ("test={}"); {
lua.DoString ("function test:overridableMethod(x,y) return x*y; end"); using (Lua lua = new Lua () ) {
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("a=TestClass.callOverridable(test,2,3)"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
int a = (int)lua.GetNumber ("a"); lua.DoString ("test={}");
lua.DoString ("luanet.free_object(test)"); lua.DoString ("function test:overridableMethod(x,y) return x*y; end");
Assert.AreEqual (6, a); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
} lua.DoString ("test:setVal(3)");
} lua.DoString ("a=test.testval");
int a = (int) lua.GetNumber ("a");
lua.DoString ("luanet.free_object(test)");
/* Assert.AreEqual (3, a);
* Tests making an object from a Lua table and calling a method //Console.WriteLine("interface returned: "+a);
* the table does not override. }
*/ }
[Test]
public void LuaTableInheritedMethod ()
{ /// <summary>
using (Lua lua = new Lua ()) { /// Basic multiply method which expects 2 floats
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); /// </summary>
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); /// <param name="val"></param>
lua.DoString ("test={}"); /// <param name="val2"></param>
lua.DoString ("function test:overridableMethod(x,y) return x*y; end"); /// <returns></returns>
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); private float _TestException (float val, float val2)
lua.DoString ("test:setVal(3)"); {
lua.DoString ("a=test.testval"); return val * val2;
int a = (int)lua.GetNumber ("a"); }
lua.DoString ("luanet.free_object(test)");
class LuaEventArgsHandler : LuaInterface.Method.LuaDelegate
Assert.AreEqual (3, a); {
void CallFunction (object sender, EventArgs eventArgs)
//Console.WriteLine("interface returned: "+a); {
} object [] args = new object [] {sender, eventArgs };
} object [] inArgs = new object [] { sender, eventArgs };
int [] outArgs = new int [] { };
base.callFunction (args, inArgs, outArgs);
/// <summary> }
/// Basic multiply method which expects 2 floats }
/// </summary>
/// <param name="val"></param> [Test]
/// <param name="val2"></param> public void TestEventException ()
/// <returns></returns> {
private float _TestException (float val, float val2) using (Lua lua = new Lua () ) {
{ //Register a C# function
return val * val2; MethodInfo testException = this.GetType ().GetMethod ("_TestException", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, new Type [] {
} typeof (float),
typeof (float)
class LuaEventArgsHandler : LuaInterface.Method.LuaDelegate }, null);
{ lua.RegisterFunction ("Multiply", this, testException);
void CallFunction (object sender, EventArgs eventArgs) lua.RegisterLuaDelegateType (typeof (EventHandler<EventArgs>), typeof (LuaEventArgsHandler) );
{ //create the lua event handler code for the entity
object [] args = new object [] {sender, eventArgs }; //includes the bad code!
object [] inArgs = new object [] { sender, eventArgs }; lua.DoString ("function OnClick(sender, eventArgs)\r\n" +
int [] outArgs = new int [] { }; "--Multiply expects 2 floats, but instead receives 2 strings\r\n" +
"Multiply(asd, we)\r\n" +
base.callFunction (args, inArgs, outArgs); "end");
} //create the lua event handler code for the entity
} //good code
//lua.DoString("function OnClick(sender, eventArgs)\r\n" +
[Test] // "--Multiply expects 2 floats\r\n" +
public void TestEventException () // "Multiply(2, 50)\r\n" +
{ // "end");
using (Lua lua = new Lua ()) { //Create the event handler script
//Register a C# function lua.DoString ("function SubscribeEntity(e)\r\ne.Clicked:Add(OnClick)\r\nend");
MethodInfo testException = this.GetType ().GetMethod ("_TestException", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, new Type [] { typeof (float), typeof (float) }, null); //Create the entity object
lua.RegisterFunction ("Multiply", this, testException); Entity entity = new Entity ();
lua.RegisterLuaDelegateType (typeof (EventHandler<EventArgs>), typeof (LuaEventArgsHandler)); //Register the entity object with the event handler inside lua
LuaFunction lf = lua.GetFunction ("SubscribeEntity");
//create the lua event handler code for the entity lf.Call (new object [1] { entity });
//includes the bad code!
lua.DoString ("function OnClick(sender, eventArgs)\r\n" + try {
"--Multiply expects 2 floats, but instead receives 2 strings\r\n" + //Cause the event to be fired
"Multiply(asd, we)\r\n" + entity.Click ();
"end"); //failed
Assert.True (false);
//create the lua event handler code for the entity } catch (LuaException e) {
//good code //passed
//lua.DoString("function OnClick(sender, eventArgs)\r\n" + Assert.True (true);
// "--Multiply expects 2 floats\r\n" + }
// "Multiply(2, 50)\r\n" + }
// "end"); }
//Create the event handler script [Test]
lua.DoString ("function SubscribeEntity(e)\r\ne.Clicked:Add(OnClick)\r\nend"); public void TestExceptionWithChunkOverload ()
{
//Create the entity object using (Lua lua = new Lua () ) {
Entity entity = new Entity (); try {
lua.DoString ("thiswillthrowanerror", "MyChunk");
//Register the entity object with the event handler inside lua } catch (Exception e) {
LuaFunction lf = lua.GetFunction ("SubscribeEntity"); Assert.True (e.Message.StartsWith ("[string \"MyChunk\"]") );
lf.Call (new object [1] { entity }); }
}
try { }
//Cause the event to be fired
entity.Click (); [Test]
public void TestGenerics ()
//failed {
Assert.True (false); //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"
catch (LuaException e) { //TestClassGeneric<string> genericClass = new TestClassGeneric<string>();
//passed //lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod"));
Assert.True (true); //lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod"));
} using (Lua lua = new Lua () ) {
} TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod ();
} lua.RegisterFunction ("genericMethod2", classWithGenericMethod, typeof (TestClassWithGenericMethod).GetMethod ("GenericMethod") );
[Test] try {
public void TestExceptionWithChunkOverload () lua.DoString ("genericMethod2(100)");
{ } catch {
using (Lua lua = new Lua ()) { }
try {
lua.DoString ("thiswillthrowanerror", "MyChunk"); Assert.True (classWithGenericMethod.GenericMethodSuccess);
} Assert.True (classWithGenericMethod.Validate<double> (100) ); //note the gotcha: numbers are all being passed to generic methods as doubles
catch (Exception e) {
Assert.True (e.Message.StartsWith ("[string \"MyChunk\"]")); try {
} lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
} lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
} lua.DoString ("test=TestClass(56)");
lua.DoString ("genericMethod2(test)");
[Test] } catch {
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 Assert.True (classWithGenericMethod.GenericMethodSuccess);
//specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked" Assert.AreEqual (56, (classWithGenericMethod.PassedValue as TestClass).val);
}
//TestClassGeneric<string> genericClass = new TestClassGeneric<string>(); }
//lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod")); [Test]
//lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod")); public void RegisterFunctionStressTest ()
{
using (Lua lua = new Lua ()) { LuaFunction fc = null;
TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod (); const int Count = 200; // it seems to work with 41
using (Lua lua = new Lua () ) {
lua.RegisterFunction ("genericMethod2", classWithGenericMethod, typeof (TestClassWithGenericMethod).GetMethod ("GenericMethod")); MyClass t = new MyClass ();
try { for (int i = 1; i < Count - 1; ++i) {
lua.DoString ("genericMethod2(100)"); fc = lua.RegisterFunction ("func" + i, t, typeof (MyClass).GetMethod ("Func1") );
} }
catch { }
fc = lua.RegisterFunction ("func" + (Count - 1), t, typeof (MyClass).GetMethod ("Func1") );
Assert.True (classWithGenericMethod.GenericMethodSuccess); lua.DoString ("print(func1())");
Assert.True (classWithGenericMethod.Validate<double> (100)); //note the gotcha: numbers are all being passed to generic methods as doubles }
}
try {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); [Test]
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); public void TestMultipleOutParameters ()
lua.DoString ("test=TestClass(56)"); {
lua.DoString ("genericMethod2(test)"); using (Lua lua = new Lua () ) {
} TestClass t1 = new TestClass ();
catch { } lua ["netobj"] = t1;
lua.DoString ("a,b,c=netobj:outValMutiple(2)");
Assert.True (classWithGenericMethod.GenericMethodSuccess); int a = (int) lua.GetNumber ("a");
Assert.AreEqual (56, (classWithGenericMethod.PassedValue as TestClass).val); string b = (string) lua.GetString ("b");
} string c = (string) lua.GetString ("c");
} Assert.AreEqual (2, a);
Assert.NotNull (b);
Assert.NotNull (c);
[Test] }
public void RegisterFunctionStressTest () }
{
LuaFunction fc = null; [Test]
const int Count = 200; // it seems to work with 41 public void TestLoadStringLeak ()
{
using (Lua lua = new Lua ()) { //Test to prevent stack overflow
//See: http://code.google.com/p/luainterface/issues/detail?id=5
MyClass t = new MyClass (); //number of iterations to test
int count = 1000;
for (int i = 1; i < Count - 1; ++i) { using (Lua lua = new Lua () ) {
fc = lua.RegisterFunction ("func" + i, t, typeof (MyClass).GetMethod ("Func1")); for (int i = 0; i < count; i++) {
} lua.LoadString ("abc = 'def'", string.Empty);
fc = lua.RegisterFunction ("func" + (Count - 1), t, typeof (MyClass).GetMethod ("Func1")); }
}
lua.DoString ("print(func1())"); //any thrown exceptions cause the test run to fail
} }
}
[Test]
[Test] public void TestLoadFileLeak ()
public void TestMultipleOutParameters () {
{ //Test to prevent stack overflow
using (Lua lua = new Lua ()) { //See: http://code.google.com/p/luainterface/issues/detail?id=5
TestClass t1 = new TestClass (); //number of iterations to test
lua ["netobj"] = t1; int count = 1000;
lua.DoString ("a,b,c=netobj:outValMutiple(2)"); using (Lua lua = new Lua () ) {
int a = (int)lua.GetNumber ("a"); for (int i = 0; i < count; i++) {
string b = (string)lua.GetString ("b"); lua.LoadFile (Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "test.lua");
string c = (string)lua.GetString ("c"); }
}
Assert.AreEqual (2, a); //any thrown exceptions cause the test run to fail
Assert.NotNull (b); }
Assert.NotNull (c);
} [Test]
} public void TestRegisterFunction ()
{
[Test] using (Lua lua = new Lua () ) {
public void TestLoadStringLeak () lua.RegisterFunction ("func1", null, typeof (TestClass2).GetMethod ("func") );
{ object[] vals1 = lua.GetFunction ("func1").Call (2, 3);
//Test to prevent stack overflow Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]) );
//See: http://code.google.com/p/luainterface/issues/detail?id=5 TestClass2 obj = new TestClass2 ();
lua.RegisterFunction ("func2", obj, typeof (TestClass2).GetMethod ("funcInstance") );
//number of iterations to test vals1 = lua.GetFunction ("func2").Call (2, 3);
int count = 1000; Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0]) );
}
}
using (Lua lua = new Lua ()) {
for (int i = 0; i < count; i++) { /*
lua.LoadString ("abc = 'def'", string.Empty); * Tests if DoString is correctly returning values
} */
} [Test]
public void DoString ()
//any thrown exceptions cause the test run to fail {
} using (Lua lua = new Lua () ) {
object[] res = lua.DoString ("a=2\nreturn a,3");
[Test] //Console.WriteLine("a="+res[0]+", b="+res[1]);
public void TestLoadFileLeak () Assert.AreEqual (res [0], 2d);
{ Assert.AreEqual (res [1], 3d);
//Test to prevent stack overflow }
//See: http://code.google.com/p/luainterface/issues/detail?id=5 }
/*
//number of iterations to test * Tests getting of global numeric variables
int count = 1000; */
[Test]
public void GetGlobalNumber ()
using (Lua lua = new Lua ()) { {
for (int i = 0; i < count; i++) { using (Lua lua = new Lua () ) {
lua.LoadFile (Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "test.lua"); lua.DoString ("a=2");
} double num = lua.GetNumber ("a");
} //Console.WriteLine("a="+num);
Assert.AreEqual (num, 2d);
//any thrown exceptions cause the test run to fail }
} }
/*
[Test] * Tests setting of global numeric variables
public void TestRegisterFunction () */
{ [Test]
using (Lua lua = new Lua ()) { public void SetGlobalNumber ()
{
lua.RegisterFunction ("func1", null, typeof (TestClass2).GetMethod ("func")); using (Lua lua = new Lua () ) {
object[] vals1 = lua.GetFunction ("func1").Call (2, 3); lua.DoString ("a=2");
lua ["a"] = 3;
Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0])); double num = lua.GetNumber ("a");
//Console.WriteLine("a="+num);
TestClass2 obj = new TestClass2 (); Assert.AreEqual (num, 3d);
lua.RegisterFunction ("func2", obj, typeof (TestClass2).GetMethod ("funcInstance")); }
vals1 = lua.GetFunction ("func2").Call (2, 3); }
/*
Assert.AreEqual (5.0f, Convert.ToSingle (vals1 [0])); * Tests getting of numeric variables from tables
} * by specifying variable path
} */
[Test]
/* public void GetNumberInTable ()
* Tests if DoString is correctly returning values {
*/ using (Lua lua = new Lua () ) {
[Test] lua.DoString ("a={b={c=2}}");
public void DoString () double num = lua.GetNumber ("a.b.c");
{ //Console.WriteLine("a.b.c="+num);
using (Lua lua = new Lua ()) { Assert.AreEqual (num, 2d);
object[] res = lua.DoString ("a=2\nreturn a,3"); }
//Console.WriteLine("a="+res[0]+", b="+res[1]); }
Assert.AreEqual (res [0], 2d); /*
Assert.AreEqual (res [1], 3d); * Tests setting of numeric variables from tables
} * by specifying variable path
} */
/* [Test]
* Tests getting of global numeric variables public void SetNumberInTable ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void GetGlobalNumber () lua.DoString ("a={b={c=2}}");
{ lua ["a.b.c"] = 3;
using (Lua lua = new Lua ()) { double num = lua.GetNumber ("a.b.c");
lua.DoString ("a=2"); //Console.WriteLine("a.b.c="+num);
double num = lua.GetNumber ("a"); Assert.AreEqual (num, 3d);
//Console.WriteLine("a="+num); }
Assert.AreEqual (num, 2d); }
} /*
} * Tests getting of global string variables
/* */
* Tests setting of global numeric variables [Test]
*/ public void GetGlobalString ()
[Test] {
public void SetGlobalNumber () using (Lua lua = new Lua () ) {
{ lua.DoString ("a=\"test\"");
using (Lua lua = new Lua ()) { string str = lua.GetString ("a");
lua.DoString ("a=2"); //Console.WriteLine("a="+str);
lua ["a"] = 3; Assert.AreEqual (str, "test");
double num = lua.GetNumber ("a"); }
//Console.WriteLine("a="+num); }
Assert.AreEqual (num, 3d); /*
} * Tests setting of global string variables
} */
/* [Test]
* Tests getting of numeric variables from tables public void SetGlobalString ()
* by specifying variable path {
*/ using (Lua lua = new Lua () ) {
[Test] lua.DoString ("a=\"test\"");
public void GetNumberInTable () lua ["a"] = "new test";
{ string str = lua.GetString ("a");
using (Lua lua = new Lua ()) { //Console.WriteLine("a="+str);
lua.DoString ("a={b={c=2}}"); Assert.AreEqual (str, "new test");
double num = lua.GetNumber ("a.b.c"); }
//Console.WriteLine("a.b.c="+num); }
Assert.AreEqual (num, 2d); /*
} * Tests getting of string variables from tables
} * by specifying variable path
/* */
* Tests setting of numeric variables from tables [Test]
* by specifying variable path public void GetStringInTable ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void SetNumberInTable () lua.DoString ("a={b={c=\"test\"}}");
{ string str = lua.GetString ("a.b.c");
using (Lua lua = new Lua ()) { //Console.WriteLine("a.b.c="+str);
lua.DoString ("a={b={c=2}}"); Assert.AreEqual (str, "test");
lua ["a.b.c"] = 3; }
double num = lua.GetNumber ("a.b.c"); }
//Console.WriteLine("a.b.c="+num); /*
Assert.AreEqual (num, 3d); * Tests setting of string variables from tables
} * by specifying variable path
} */
/* [Test]
* Tests getting of global string variables public void SetStringInTable ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void GetGlobalString () lua.DoString ("a={b={c=\"test\"}}");
{ lua ["a.b.c"] = "new test";
using (Lua lua = new Lua ()) { string str = lua.GetString ("a.b.c");
lua.DoString ("a=\"test\""); //Console.WriteLine("a.b.c="+str);
string str = lua.GetString ("a"); Assert.AreEqual (str, "new test");
//Console.WriteLine("a="+str); }
Assert.AreEqual (str, "test"); }
} /*
} * Tests getting and setting of global table variables
/* */
* Tests setting of global string variables [Test]
*/ public void GetAndSetTable ()
[Test] {
public void SetGlobalString () using (Lua lua = new Lua () ) {
{ lua.DoString ("a={b={c=2}}\nb={c=3}");
using (Lua lua = new Lua ()) { LuaTable tab = lua.GetTable ("b");
lua.DoString ("a=\"test\""); lua ["a.b"] = tab;
lua ["a"] = "new test"; double num = lua.GetNumber ("a.b.c");
string str = lua.GetString ("a"); //Console.WriteLine("a.b.c="+num);
//Console.WriteLine("a="+str); Assert.AreEqual (num, 3d);
Assert.AreEqual (str, "new test"); }
} }
} /*
/* * Tests getting of numeric field of a table
* Tests getting of string variables from tables */
* by specifying variable path [Test]
*/ public void GetTableNumericField1 ()
[Test] {
public void GetStringInTable () using (Lua lua = new Lua () ) {
{ lua.DoString ("a={b={c=2}}");
using (Lua lua = new Lua ()) { LuaTable tab = lua.GetTable ("a.b");
lua.DoString ("a={b={c=\"test\"}}"); double num = (double) tab ["c"];
string str = lua.GetString ("a.b.c"); //Console.WriteLine("a.b.c="+num);
//Console.WriteLine("a.b.c="+str); Assert.AreEqual (num, 2d);
Assert.AreEqual (str, "test"); }
} }
} /*
/* * Tests getting of numeric field of a table
* Tests setting of string variables from tables * (the field is inside a subtable)
* by specifying variable path */
*/ [Test]
[Test] public void GetTableNumericField2 ()
public void SetStringInTable () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { lua.DoString ("a={b={c=2}}");
lua.DoString ("a={b={c=\"test\"}}"); LuaTable tab = lua.GetTable ("a");
lua ["a.b.c"] = "new test"; double num = (double) tab ["b.c"];
string str = lua.GetString ("a.b.c"); //Console.WriteLine("a.b.c="+num);
//Console.WriteLine("a.b.c="+str); Assert.AreEqual (num, 2d);
Assert.AreEqual (str, "new test"); }
} }
} /*
/* * Tests setting of numeric field of a table
* Tests getting and setting of global table variables */
*/ [Test]
[Test] public void SetTableNumericField1 ()
public void GetAndSetTable () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { lua.DoString ("a={b={c=2}}");
lua.DoString ("a={b={c=2}}\nb={c=3}"); LuaTable tab = lua.GetTable ("a.b");
LuaTable tab = lua.GetTable ("b"); tab ["c"] = 3;
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 setting of numeric field of a table
* Tests getting of numeric field of a table * (the field is inside a subtable)
*/ */
[Test] [Test]
public void GetTableNumericField1 () 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.b"); LuaTable tab = lua.GetTable ("a");
double num = (double)tab ["c"]; tab ["b.c"] = 3;
//Console.WriteLine("a.b.c="+num); double num = lua.GetNumber ("a.b.c");
Assert.AreEqual (num, 2d); //Console.WriteLine("a.b.c="+num);
} Assert.AreEqual (num, 3d);
} }
/* }
* Tests getting of numeric field of a table /*
* (the field is inside a subtable) * Tests getting of string field of a table
*/ */
[Test] [Test]
public void GetTableNumericField2 () public void GetTableStringField1 ()
{ {
using (Lua lua = new Lua ()) { using (Lua lua = new Lua () ) {
lua.DoString ("a={b={c=2}}"); lua.DoString ("a={b={c=\"test\"}}");
LuaTable tab = lua.GetTable ("a"); LuaTable tab = lua.GetTable ("a.b");
double num = (double)tab ["b.c"]; string str = (string) tab ["c"];
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (num, 2d); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests setting of numeric field of a table * Tests getting of string field of a table
*/ * (the field is inside a subtable)
[Test] */
public void SetTableNumericField1 () [Test]
{ public void GetTableStringField2 ()
using (Lua lua = new Lua ()) { {
lua.DoString ("a={b={c=2}}"); using (Lua lua = new Lua () ) {
LuaTable tab = lua.GetTable ("a.b"); lua.DoString ("a={b={c=\"test\"}}");
tab ["c"] = 3; LuaTable tab = lua.GetTable ("a");
double num = lua.GetNumber ("a.b.c"); string str = (string) tab ["b.c"];
//Console.WriteLine("a.b.c="+num); //Console.WriteLine("a.b.c="+str);
Assert.AreEqual (num, 3d); Assert.AreEqual (str, "test");
} }
} }
/* /*
* Tests setting of numeric field of a table * Tests setting of string field of a table
* (the field is inside a subtable) */
*/ [Test]
[Test] public void SetTableStringField1 ()
public void SetTableNumericField2 () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { lua.DoString ("a={b={c=\"test\"}}");
lua.DoString ("a={b={c=2}}"); LuaTable tab = lua.GetTable ("a.b");
LuaTable tab = lua.GetTable ("a"); tab ["c"] = "new test";
tab ["b.c"] = 3; string str = lua.GetString ("a.b.c");
double num = lua.GetNumber ("a.b.c"); //Console.WriteLine("a.b.c="+str);
//Console.WriteLine("a.b.c="+num); Assert.AreEqual (str, "new test");
Assert.AreEqual (num, 3d); }
} }
} /*
/* * Tests setting of string field of a table
* Tests getting of string field of a table * (the field is inside a subtable)
*/ */
[Test] [Test]
public void GetTableStringField1 () 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.b"); LuaTable tab = lua.GetTable ("a");
string str = (string)tab ["c"]; tab ["b.c"] = "new test";
//Console.WriteLine("a.b.c="+str); string str = lua.GetString ("a.b.c");
Assert.AreEqual (str, "test"); //Console.WriteLine("a.b.c="+str);
} Assert.AreEqual (str, "new test");
} }
/* }
* Tests getting of string field of a table /*
* (the field is inside a subtable) * Tests calling of a global function with zero arguments
*/ */
[Test] [Test]
public void GetTableStringField2 () public void CallGlobalFunctionNoArgs ()
{ {
using (Lua lua = new Lua ()) { using (Lua lua = new Lua () ) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a=2\nfunction f()\na=3\nend");
LuaTable tab = lua.GetTable ("a"); lua.GetFunction ("f").Call ();
string str = (string)tab ["b.c"]; double num = lua.GetNumber ("a");
//Console.WriteLine("a.b.c="+str); //Console.WriteLine("a="+num);
Assert.AreEqual (str, "test"); Assert.AreEqual (num, 3d);
} }
} }
/* /*
* Tests setting of string field of a table * Tests calling of a global function with one argument
*/ */
[Test] [Test]
public void SetTableStringField1 () public void CallGlobalFunctionOneArg ()
{ {
using (Lua lua = new Lua ()) { using (Lua lua = new Lua () ) {
lua.DoString ("a={b={c=\"test\"}}"); lua.DoString ("a=2\nfunction f(x)\na=a+x\nend");
LuaTable tab = lua.GetTable ("a.b"); lua.GetFunction ("f").Call (1);
tab ["c"] = "new test"; double num = lua.GetNumber ("a");
string str = lua.GetString ("a.b.c"); //Console.WriteLine("a="+num);
//Console.WriteLine("a.b.c="+str); Assert.AreEqual (num, 3d);
Assert.AreEqual (str, "new test"); }
} }
} /*
/* * Tests calling of a global function with two arguments
* Tests setting of string field of a table */
* (the field is inside a subtable) [Test]
*/ public void CallGlobalFunctionTwoArgs ()
[Test] {
public void SetTableStringField2 () using (Lua lua = new Lua () ) {
{ lua.DoString ("a=2\nfunction f(x,y)\na=x+y\nend");
using (Lua lua = new Lua ()) { lua.GetFunction ("f").Call (1, 3);
lua.DoString ("a={b={c=\"test\"}}"); double num = lua.GetNumber ("a");
LuaTable tab = lua.GetTable ("a"); //Console.WriteLine("a="+num);
tab ["b.c"] = "new test"; Assert.AreEqual (num, 4d);
string str = lua.GetString ("a.b.c"); }
//Console.WriteLine("a.b.c="+str); }
Assert.AreEqual (str, "new test"); /*
} * Tests calling of a global function that returns one value
} */
/* [Test]
* Tests calling of a global function with zero arguments public void CallGlobalFunctionOneReturn ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void CallGlobalFunctionNoArgs () lua.DoString ("function f(x)\nreturn x+2\nend");
{ object[] ret = lua.GetFunction ("f").Call (3);
using (Lua lua = new Lua ()) { //Console.WriteLine("ret="+ret[0]);
lua.DoString ("a=2\nfunction f()\na=3\nend"); Assert.AreEqual (1, ret.Length);
lua.GetFunction ("f").Call (); Assert.AreEqual (5, (double) ret [0]);
double num = lua.GetNumber ("a"); }
//Console.WriteLine("a="+num); }
Assert.AreEqual (num, 3d); /*
} * Tests calling of a global function that returns two values
} */
/* [Test]
* Tests calling of a global function with one argument public void CallGlobalFunctionTwoReturns ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void CallGlobalFunctionOneArg () lua.DoString ("function f(x,y)\nreturn x,x+y\nend");
{ object[] ret = lua.GetFunction ("f").Call (3, 2);
using (Lua lua = new Lua ()) { //Console.WriteLine("ret="+ret[0]+","+ret[1]);
lua.DoString ("a=2\nfunction f(x)\na=a+x\nend"); Assert.AreEqual (2, ret.Length);
lua.GetFunction ("f").Call (1); Assert.AreEqual (3, (double) ret [0]);
double num = lua.GetNumber ("a"); Assert.AreEqual (5, (double) ret [1]);
//Console.WriteLine("a="+num); }
Assert.AreEqual (num, 3d); }
} /*
} * Tests calling of a function inside a table
/* */
* Tests calling of a global function with two arguments [Test]
*/ public void CallTableFunctionTwoReturns ()
[Test] {
public void CallGlobalFunctionTwoArgs () using (Lua lua = new Lua () ) {
{ lua.DoString ("a={}\nfunction a.f(x,y)\nreturn x,x+y\nend");
using (Lua lua = new Lua ()) { object[] ret = lua.GetFunction ("a.f").Call (3, 2);
lua.DoString ("a=2\nfunction f(x,y)\na=x+y\nend"); //Console.WriteLine("ret="+ret[0]+","+ret[1]);
lua.GetFunction ("f").Call (1, 3); Assert.AreEqual (2, ret.Length);
double num = lua.GetNumber ("a"); Assert.AreEqual (3, (double) ret [0]);
//Console.WriteLine("a="+num); Assert.AreEqual (5, (double) ret [1]);
Assert.AreEqual (num, 4d); }
} }
} /*
/* * Tests setting of a global variable to a CLR object value
* Tests calling of a global function that returns one value */
*/ [Test]
[Test] public void SetGlobalObject ()
public void CallGlobalFunctionOneReturn () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { TestClass t1 = new TestClass ();
lua.DoString ("function f(x)\nreturn x+2\nend"); t1.testval = 4;
object[] ret = lua.GetFunction ("f").Call (3); lua ["netobj"] = t1;
//Console.WriteLine("ret="+ret[0]); object o = lua ["netobj"];
Assert.AreEqual (1, ret.Length); Assert.True (o is TestClass);
Assert.AreEqual (5, (double)ret [0]); TestClass t2 = (TestClass) lua ["netobj"];
} Assert.AreEqual (t2.testval, 4);
} Assert.True (t1 == t2);
/* }
* Tests calling of a global function that returns two values }
*/ ///*
[Test] // * Tests if CLR object is being correctly collected by Lua
public void CallGlobalFunctionTwoReturns () // */
{ //[Test]
using (Lua lua = new Lua ()) { //public void GarbageCollection()
lua.DoString ("function f(x,y)\nreturn x,x+y\nend"); //{
object[] ret = lua.GetFunction ("f").Call (3, 2); // using (Lua lua = new Lua())
//Console.WriteLine("ret="+ret[0]+","+ret[1]); // {
Assert.AreEqual (2, ret.Length); // TestClass t1 = new TestClass();
Assert.AreEqual (3, (double)ret [0]); // t1.testval = 4;
Assert.AreEqual (5, (double)ret [1]); // lua["netobj"] = t1;
} // TestClass t2 = (TestClass)lua["netobj"];
} // Assert.True(lua[0] != null);
/* // lua.DoString("netobj=nil;collectgarbage();");
* Tests calling of a function inside a table // Assert.True(lua.translator.objects[0] == null);
*/ // }
[Test] //}
public void CallTableFunctionTwoReturns () /*
{ * Tests setting of a table field to a CLR object value
using (Lua lua = new Lua ()) { */
lua.DoString ("a={}\nfunction a.f(x,y)\nreturn x,x+y\nend"); [Test]
object[] ret = lua.GetFunction ("a.f").Call (3, 2); public void SetTableObjectField1 ()
//Console.WriteLine("ret="+ret[0]+","+ret[1]); {
Assert.AreEqual (2, ret.Length); using (Lua lua = new Lua () ) {
Assert.AreEqual (3, (double)ret [0]); lua.DoString ("a={b={c=\"test\"}}");
Assert.AreEqual (5, (double)ret [1]); LuaTable tab = lua.GetTable ("a.b");
} TestClass t1 = new TestClass ();
} t1.testval = 4;
/* tab ["c"] = t1;
* Tests setting of a global variable to a CLR object value TestClass t2 = (TestClass) lua ["a.b.c"];
*/ //Console.WriteLine("a.b.c="+t2.testval);
[Test] Assert.AreEqual (t2.testval, 4);
public void SetGlobalObject () Assert.True (t1 == t2);
{ }
using (Lua lua = new Lua ()) { }
TestClass t1 = new TestClass (); /*
t1.testval = 4; * Tests reading and writing of an object's field
lua ["netobj"] = t1; */
object o = lua ["netobj"]; [Test]
TestClass t2 = (TestClass)lua ["netobj"]; public void AccessObjectField ()
Assert.AreEqual (t2.testval, 4); {
Assert.True (t1 == t2); using (Lua lua = new Lua () ) {
} TestClass t1 = new TestClass ();
} t1.val = 4;
///* lua ["netobj"] = t1;
// * Tests if CLR object is being correctly collected by Lua lua.DoString ("var=netobj.val");
// */ double var = (double) lua ["var"];
//[Test] //Console.WriteLine("value from Lua="+var);
//public void GarbageCollection() Assert.AreEqual (4, var);
//{ lua.DoString ("netobj.val=3");
// using (Lua lua = new Lua()) Assert.AreEqual (3, t1.val);
// { //Console.WriteLine("new val (from Lua)="+t1.val);
// TestClass t1 = new TestClass(); }
// t1.testval = 4; }
// lua["netobj"] = t1; /*
// TestClass t2 = (TestClass)lua["netobj"]; * Tests reading and writing of an object's non-indexed
// Assert.True(lua[0] != null); * property
// lua.DoString("netobj=nil;collectgarbage();"); */
// Assert.True(lua.translator.objects[0] == null); [Test]
// } public void AccessObjectProperty ()
//} {
/* using (Lua lua = new Lua () ) {
* Tests setting of a table field to a CLR object value TestClass t1 = new TestClass ();
*/ t1.testval = 4;
[Test] lua ["netobj"] = t1;
public void SetTableObjectField1 () lua.DoString ("var=netobj.testval");
{ double var = (double) lua ["var"];
using (Lua lua = new Lua ()) { //Console.WriteLine("value from Lua="+var);
lua.DoString ("a={b={c=\"test\"}}"); Assert.AreEqual (4, var);
LuaTable tab = lua.GetTable ("a.b"); lua.DoString ("netobj.testval=3");
TestClass t1 = new TestClass (); Assert.AreEqual (3, t1.testval);
t1.testval = 4; //Console.WriteLine("new val (from Lua)="+t1.testval);
tab ["c"] = t1; }
TestClass t2 = (TestClass)lua ["a.b.c"]; }
//Console.WriteLine("a.b.c="+t2.testval); /*
Assert.AreEqual (t2.testval, 4); * Tests calling of an object's method with no overloads
Assert.True (t1 == t2); */
} [Test]
} public void CallObjectMethod ()
/* {
* Tests reading and writing of an object's field using (Lua lua = new Lua () ) {
*/ TestClass t1 = new TestClass ();
[Test] t1.testval = 4;
public void AccessObjectField () lua ["netobj"] = t1;
{ lua.DoString ("netobj:setVal(3)");
using (Lua lua = new Lua ()) { Assert.AreEqual (3, t1.testval);
TestClass t1 = new TestClass (); //Console.WriteLine("new val(from C#)="+t1.testval);
t1.val = 4; lua.DoString ("val=netobj:getVal()");
lua ["netobj"] = t1; int val = (int) lua.GetNumber ("val");
lua.DoString ("var=netobj.val"); Assert.AreEqual (3, val);
double var = (double)lua ["var"]; //Console.WriteLine("new val(from Lua)="+val);
//Console.WriteLine("value from Lua="+var); }
Assert.AreEqual (4, var); }
lua.DoString ("netobj.val=3"); /*
Assert.AreEqual (3, t1.val); * Tests calling of an object's method with overloading
//Console.WriteLine("new val (from Lua)="+t1.val); */
} [Test]
} public void CallObjectMethodByType ()
/* {
* Tests reading and writing of an object's non-indexed using (Lua lua = new Lua () ) {
* property TestClass t1 = new TestClass ();
*/ lua ["netobj"] = t1;
[Test] lua.DoString ("netobj:setVal('str')");
public void AccessObjectProperty () Assert.AreEqual ("str", t1.getStrVal () );
{ //Console.WriteLine("new val(from C#)="+t1.getStrVal());
using (Lua lua = new Lua ()) { }
TestClass t1 = new TestClass (); }
t1.testval = 4; /*
lua ["netobj"] = t1; * Tests calling of an object's method with no overloading
lua.DoString ("var=netobj.testval"); * and out parameters
double var = (double)lua ["var"]; */
//Console.WriteLine("value from Lua="+var); [Test]
Assert.AreEqual (4, var); public void CallObjectMethodOutParam ()
lua.DoString ("netobj.testval=3"); {
Assert.AreEqual (3, t1.testval); using (Lua lua = new Lua () ) {
//Console.WriteLine("new val (from Lua)="+t1.testval); TestClass t1 = new TestClass ();
} lua ["netobj"] = t1;
} lua.DoString ("a,b=netobj:outVal()");
/* int a = (int) lua.GetNumber ("a");
* Tests calling of an object's method with no overloads int b = (int) lua.GetNumber ("b");
*/ Assert.AreEqual (3, a);
[Test] Assert.AreEqual (5, b);
public void CallObjectMethod () //Console.WriteLine("function returned (from lua)="+a+","+b);
{ }
using (Lua lua = new Lua ()) { }
TestClass t1 = new TestClass (); /*
t1.testval = 4; * Tests calling of an object's method with overloading and
lua ["netobj"] = t1; * out params
lua.DoString ("netobj:setVal(3)"); */
Assert.AreEqual (3, t1.testval); [Test]
//Console.WriteLine("new val(from C#)="+t1.testval); public void CallObjectMethodOverloadedOutParam ()
lua.DoString ("val=netobj:getVal()"); {
int val = (int)lua.GetNumber ("val"); using (Lua lua = new Lua () ) {
Assert.AreEqual (3, val); TestClass t1 = new TestClass ();
//Console.WriteLine("new val(from Lua)="+val); lua ["netobj"] = t1;
} lua.DoString ("a,b=netobj:outVal(2)");
} int a = (int) lua.GetNumber ("a");
/* int b = (int) lua.GetNumber ("b");
* Tests calling of an object's method with overloading Assert.AreEqual (2, a);
*/ Assert.AreEqual (5, b);
[Test] //Console.WriteLine("function returned (from lua)="+a+","+b);
public void CallObjectMethodByType () }
{ }
using (Lua lua = new Lua ()) { /*
TestClass t1 = new TestClass (); * Tests calling of an object's method with ref params
lua ["netobj"] = t1; */
lua.DoString ("netobj:setVal('str')"); [Test]
Assert.AreEqual ("str", t1.getStrVal ()); public void CallObjectMethodByRefParam ()
//Console.WriteLine("new val(from C#)="+t1.getStrVal()); {
} using (Lua lua = new Lua () ) {
} TestClass t1 = new TestClass ();
/* lua ["netobj"] = t1;
* Tests calling of an object's method with no overloading lua.DoString ("a,b=netobj:outVal(2,3)");
* and out parameters int a = (int) lua.GetNumber ("a");
*/ int b = (int) lua.GetNumber ("b");
[Test] Assert.AreEqual (2, a);
public void CallObjectMethodOutParam () Assert.AreEqual (5, b);
{ //Console.WriteLine("function returned (from lua)="+a+","+b);
using (Lua lua = new Lua ()) { }
TestClass t1 = new TestClass (); }
lua ["netobj"] = t1; /*
lua.DoString ("a,b=netobj:outVal()"); * Tests calling of two versions of an object's method that have
int a = (int)lua.GetNumber ("a"); * the same name and signature but implement different interfaces
int b = (int)lua.GetNumber ("b"); */
Assert.AreEqual (3, a); [Test]
Assert.AreEqual (5, b); public void CallObjectMethodDistinctInterfaces ()
//Console.WriteLine("function returned (from lua)="+a+","+b); {
} using (Lua lua = new Lua () ) {
} TestClass t1 = new TestClass ();
/* lua ["netobj"] = t1;
* Tests calling of an object's method with overloading and lua.DoString ("a=netobj:foo()");
* out params lua.DoString ("b=netobj['LuaInterfaceTest.Mock.IFoo1.foo']");
*/ int a = (int) lua.GetNumber ("a");
[Test] int b = (int) lua.GetNumber ("b");
public void CallObjectMethodOverloadedOutParam () Assert.AreEqual (5, a);
{ Assert.AreEqual (1, b);
using (Lua lua = new Lua ()) { //Console.WriteLine("function returned (from lua)="+a+","+b);
TestClass t1 = new TestClass (); }
lua ["netobj"] = t1; }
lua.DoString ("a,b=netobj:outVal(2)"); /*
int a = (int)lua.GetNumber ("a"); * Tests instantiating an object with no-argument constructor
int b = (int)lua.GetNumber ("b"); */
Assert.AreEqual (2, a); [Test]
Assert.AreEqual (5, b); public void CreateNetObjectNoArgsCons ()
//Console.WriteLine("function returned (from lua)="+a+","+b); {
} using (Lua lua = new Lua () ) {
} lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
/* lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
* Tests calling of an object's method with ref params lua.DoString ("test=TestClass()");
*/ lua.DoString ("test:setVal(3)");
[Test] object[] res = lua.DoString ("return test");
public void CallObjectMethodByRefParam () TestClass test = (TestClass) res [0];
{ //Console.WriteLine("returned: "+test.testval);
using (Lua lua = new Lua ()) { Assert.AreEqual (3, test.testval);
TestClass t1 = new TestClass (); }
lua ["netobj"] = t1; }
lua.DoString ("a,b=netobj:outVal(2,3)"); /*
int a = (int)lua.GetNumber ("a"); * Tests instantiating an object with one-argument constructor
int b = (int)lua.GetNumber ("b"); */
Assert.AreEqual (2, a); [Test]
Assert.AreEqual (5, b); public void CreateNetObjectOneArgCons ()
//Console.WriteLine("function returned (from lua)="+a+","+b); {
} using (Lua lua = new Lua () ) {
} lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
/* lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
* Tests calling of two versions of an object's method that have lua.DoString ("test=TestClass(3)");
* the same name and signature but implement different interfaces object[] res = lua.DoString ("return test");
*/ TestClass test = (TestClass) res [0];
[Test] //Console.WriteLine("returned: "+test.testval);
public void CallObjectMethodDistinctInterfaces () Assert.AreEqual (3, test.testval);
{ }
using (Lua lua = new Lua ()) { }
TestClass t1 = new TestClass (); /*
lua ["netobj"] = t1; * Tests instantiating an object with overloaded constructor
lua.DoString ("a=netobj:foo()"); */
lua.DoString ("b=netobj['LuaInterfaceTest.Mock.IFoo1.foo']"); [Test]
int a = (int)lua.GetNumber ("a"); public void CreateNetObjectOverloadedCons ()
int b = (int)lua.GetNumber ("b"); {
Assert.AreEqual (5, a); using (Lua lua = new Lua () ) {
Assert.AreEqual (1, b); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
//Console.WriteLine("function returned (from lua)="+a+","+b); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
} lua.DoString ("test=TestClass('str')");
} object[] res = lua.DoString ("return test");
/* TestClass test = (TestClass) res [0];
* Tests instantiating an object with no-argument constructor //Console.WriteLine("returned: "+test.getStrVal());
*/ Assert.AreEqual ("str", test.getStrVal () );
[Test] }
public void CreateNetObjectNoArgsCons () }
{ /*
using (Lua lua = new Lua ()) { * Tests getting item of a CLR array
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); */
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); [Test]
lua.DoString ("test=TestClass()"); public void ReadArrayField ()
lua.DoString ("test:setVal(3)"); {
object[] res = lua.DoString ("return test"); using (Lua lua = new Lua () ) {
TestClass test = (TestClass)res [0]; string[] arr = new string [] { "str1", "str2", "str3" };
//Console.WriteLine("returned: "+test.testval); lua ["netobj"] = arr;
Assert.AreEqual (3, test.testval); lua.DoString ("val=netobj[1]");
} string val = lua.GetString ("val");
} Assert.AreEqual ("str2", val);
/* //Console.WriteLine("new val(from array to Lua)="+val);
* Tests instantiating an object with one-argument constructor }
*/ }
[Test] /*G
public void CreateNetObjectOneArgCons () * Tests setting item of a CLR array
{ */
using (Lua lua = new Lua ()) { [Test]
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); public void WriteArrayField ()
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); {
lua.DoString ("test=TestClass(3)"); using (Lua lua = new Lua () ) {
object[] res = lua.DoString ("return test"); string[] arr = new string [] { "str1", "str2", "str3" };
TestClass test = (TestClass)res [0]; lua ["netobj"] = arr;
//Console.WriteLine("returned: "+test.testval); lua.DoString ("netobj[1]='test'");
Assert.AreEqual (3, test.testval); Assert.AreEqual ("test", arr [1]);
} //Console.WriteLine("new val(from Lua to array)="+arr[1]);
} }
/* }
* Tests instantiating an object with overloaded constructor /*
*/ * Tests creating a new CLR array
[Test] */
public void CreateNetObjectOverloadedCons () [Test]
{ public void CreateArray ()
using (Lua lua = new Lua ()) { {
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); using (Lua lua = new Lua () ) {
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")");
lua.DoString ("test=TestClass('str')"); lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")");
object[] res = lua.DoString ("return test"); lua.DoString ("arr=TestClass[3]");
TestClass test = (TestClass)res [0]; lua.DoString ("for i=0,2 do arr[i]=TestClass(i+1) end");
//Console.WriteLine("returned: "+test.getStrVal()); TestClass[] arr = (TestClass[]) lua ["arr"];
Assert.AreEqual ("str", test.getStrVal ()); Assert.AreEqual (arr [1].testval, 2);
} }
} }
/* /*
* Tests getting item of a CLR array * Tests passing a Lua function to a delegate
*/ * with value-type arguments
[Test] */
public void ReadArrayField () [Test]
{ public void LuaDelegateValueTypes ()
using (Lua lua = new Lua ()) { {
string[] arr = new string [] { "str1", "str2", "str3" }; using (Lua lua = new Lua () ) {
lua ["netobj"] = arr; lua.RegisterLuaDelegateType (typeof (TestDelegate1), typeof (LuaTestDelegate1Handler) );
lua.DoString ("val=netobj[1]"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
string val = lua.GetString ("val"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
Assert.AreEqual ("str2", val); lua.DoString ("test=TestClass()");
//Console.WriteLine("new val(from array to Lua)="+val); lua.DoString ("function func(x,y) return x+y; end");
} lua.DoString ("test=TestClass()");
} lua.DoString ("a=test:callDelegate1(func)");
/*G int a = (int) lua.GetNumber ("a");
* Tests setting item of a CLR array Assert.AreEqual (5, a);
*/ //Console.WriteLine("delegate returned: "+a);
[Test] }
public void WriteArrayField () }
{ /*
using (Lua lua = new Lua ()) { * Tests passing a Lua function to a delegate
string[] arr = new string [] { "str1", "str2", "str3" }; * with value-type arguments and out params
lua ["netobj"] = arr; */
lua.DoString ("netobj[1]='test'"); [Test]
Assert.AreEqual ("test", arr [1]); public void LuaDelegateValueTypesOutParam ()
//Console.WriteLine("new val(from Lua to array)="+arr[1]); {
} using (Lua lua = new Lua () ) {
} lua.RegisterLuaDelegateType (typeof (TestDelegate2), typeof (LuaTestDelegate2Handler) );
/* lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
* Tests creating a new CLR array lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
*/ lua.DoString ("test=TestClass()");
[Test] lua.DoString ("function func(x) return x,x*2; end");
public void CreateArray () lua.DoString ("test=TestClass()");
{ lua.DoString ("a=test:callDelegate2(func)");
using (Lua lua = new Lua ()) { int a = (int) lua.GetNumber ("a");
lua.DoString ("luanet.load_assembly(\"LuaInterfaceTest\")"); Assert.AreEqual (6, a);
lua.DoString ("TestClass=luanet.import_type(\"LuaInterfaceTest.Mock.TestClass\")"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("arr=TestClass[3]"); }
lua.DoString ("for i=0,2 do arr[i]=TestClass(i+1) end"); }
TestClass[] arr = (TestClass [])lua ["arr"]; /*
Assert.AreEqual (arr [1].testval, 2); * Tests passing a Lua function to a delegate
} * with value-type arguments and ref params
} */
/* [Test]
* Tests passing a Lua function to a delegate public void LuaDelegateValueTypesByRefParam ()
* with value-type arguments {
*/ using (Lua lua = new Lua () ) {
[Test] lua.RegisterLuaDelegateType (typeof (TestDelegate3), typeof (LuaTestDelegate3Handler) );
public void LuaDelegateValueTypes () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.RegisterLuaDelegateType (typeof (TestDelegate1), typeof (LuaTestDelegate1Handler)); lua.DoString ("function func(x,y) return x+y; end");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("a=test:callDelegate3(func)");
lua.DoString ("test=TestClass()"); int a = (int) lua.GetNumber ("a");
lua.DoString ("function func(x,y) return x+y; end"); Assert.AreEqual (5, a);
lua.DoString ("test=TestClass()"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("a=test:callDelegate1(func)"); }
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (5, a); /*
//Console.WriteLine("delegate returned: "+a); * Tests passing a Lua function to a delegate
} * with value-type arguments that returns a reference type
} */
/* [Test]
* Tests passing a Lua function to a delegate public void LuaDelegateValueTypesReturnReferenceType ()
* with value-type arguments and out params {
*/ using (Lua lua = new Lua () ) {
[Test] lua.RegisterLuaDelegateType (typeof (TestDelegate4), typeof (LuaTestDelegate4Handler) );
public void LuaDelegateValueTypesOutParam () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.RegisterLuaDelegateType (typeof (TestDelegate2), typeof (LuaTestDelegate2Handler)); lua.DoString ("function func(x,y) return TestClass(x+y); end");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("a=test:callDelegate4(func)");
lua.DoString ("test=TestClass()"); int a = (int) lua.GetNumber ("a");
lua.DoString ("function func(x) return x,x*2; end"); Assert.AreEqual (5, a);
lua.DoString ("test=TestClass()"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("a=test:callDelegate2(func)"); }
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (6, a); /*
//Console.WriteLine("delegate returned: "+a); * Tests passing a Lua function to a delegate
} * with reference type arguments
} */
/* [Test]
* Tests passing a Lua function to a delegate public void LuaDelegateReferenceTypes ()
* with value-type arguments and ref params {
*/ using (Lua lua = new Lua () ) {
[Test] lua.RegisterLuaDelegateType (typeof (TestDelegate5), typeof (LuaTestDelegate5Handler) );
public void LuaDelegateValueTypesByRefParam () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.RegisterLuaDelegateType (typeof (TestDelegate3), typeof (LuaTestDelegate3Handler)); lua.DoString ("function func(x,y) return x.testval+y.testval; end");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("a=test:callDelegate5(func)");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); int a = (int) lua.GetNumber ("a");
lua.DoString ("test=TestClass()"); Assert.AreEqual (5, a);
lua.DoString ("function func(x,y) return x+y; end"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("test=TestClass()"); }
lua.DoString ("a=test:callDelegate3(func)"); }
int a = (int)lua.GetNumber ("a"); /*
Assert.AreEqual (5, a); * Tests passing a Lua function to a delegate
//Console.WriteLine("delegate returned: "+a); * with reference type arguments and an out param
} */
} [Test]
/* public void LuaDelegateReferenceTypesOutParam ()
* Tests passing a Lua function to a delegate {
* with value-type arguments that returns a reference type using (Lua lua = new Lua () ) {
*/ lua.RegisterLuaDelegateType (typeof (TestDelegate6), typeof (LuaTestDelegate6Handler) );
[Test] lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
public void LuaDelegateValueTypesReturnReferenceType () lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
{ lua.DoString ("test=TestClass()");
using (Lua lua = new Lua ()) { lua.DoString ("function func(x) return x,TestClass(x*2); end");
lua.RegisterLuaDelegateType (typeof (TestDelegate4), typeof (LuaTestDelegate4Handler)); lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("a=test:callDelegate6(func)");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); int a = (int) lua.GetNumber ("a");
lua.DoString ("test=TestClass()"); Assert.AreEqual (6, a);
lua.DoString ("function func(x,y) return TestClass(x+y); end"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("test=TestClass()"); }
lua.DoString ("a=test:callDelegate4(func)"); }
int a = (int)lua.GetNumber ("a"); /*
Assert.AreEqual (5, a); * Tests passing a Lua function to a delegate
//Console.WriteLine("delegate returned: "+a); * with reference type arguments and a ref param
} */
} [Test]
/* public void LuaDelegateReferenceTypesByRefParam ()
* Tests passing a Lua function to a delegate {
* with reference type arguments using (Lua lua = new Lua () ) {
*/ lua.RegisterLuaDelegateType (typeof (TestDelegate7), typeof (LuaTestDelegate7Handler) );
[Test] lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
public void LuaDelegateReferenceTypes () lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
{ lua.DoString ("test=TestClass()");
using (Lua lua = new Lua ()) { lua.DoString ("function func(x,y) return TestClass(x+y.testval); end");
lua.RegisterLuaDelegateType (typeof (TestDelegate5), typeof (LuaTestDelegate5Handler)); lua.DoString ("a=test:callDelegate7(func)");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); int a = (int) lua.GetNumber ("a");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); Assert.AreEqual (5, a);
lua.DoString ("test=TestClass()"); //Console.WriteLine("delegate returned: "+a);
lua.DoString ("function func(x,y) return x.testval+y.testval; end"); }
lua.DoString ("a=test:callDelegate5(func)"); }
int a = (int)lua.GetNumber ("a");
Assert.AreEqual (5, a);
//Console.WriteLine("delegate returned: "+a); /*
} * Tests passing a Lua table as an interface and
} * calling one of its methods with value-type params
/* */
* Tests passing a Lua function to a delegate [Test]
* with reference type arguments and an out param public void LuaInterfaceAAValueTypes ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void LuaDelegateReferenceTypesOutParam () lua.RegisterLuaClassType (typeof (ITest), typeof (LuaITestClassHandler) );
{ lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
using (Lua lua = new Lua ()) { lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.RegisterLuaDelegateType (typeof (TestDelegate6), typeof (LuaTestDelegate6Handler)); lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("itest={}");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("function itest:test1(x,y) return x+y; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("function func(x) return x,TestClass(x*2); end"); lua.DoString ("a=test:callInterface1(itest)");
lua.DoString ("test=TestClass()"); int a = (int) lua.GetNumber ("a");
lua.DoString ("a=test:callDelegate6(func)"); Assert.AreEqual (5, a);
int a = (int)lua.GetNumber ("a"); //Console.WriteLine("interface returned: "+a);
Assert.AreEqual (6, a); }
//Console.WriteLine("delegate returned: "+a); }
} /*
} * Tests passing a Lua table as an interface and
/* * calling one of its methods with value-type params
* Tests passing a Lua function to a delegate * and an out param
* with reference type arguments and a ref param */
*/ [Test]
[Test] public void LuaInterfaceValueTypesOutParam ()
public void LuaDelegateReferenceTypesByRefParam () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.RegisterLuaDelegateType (typeof (TestDelegate7), typeof (LuaTestDelegate7Handler)); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("itest={}");
lua.DoString ("test=TestClass()"); lua.DoString ("function itest:test2(x) return x,x*2; end");
lua.DoString ("function func(x,y) return TestClass(x+y.testval); end"); lua.DoString ("test=TestClass()");
lua.DoString ("a=test:callDelegate7(func)"); lua.DoString ("a=test:callInterface2(itest)");
int a = (int)lua.GetNumber ("a"); int a = (int) lua.GetNumber ("a");
Assert.AreEqual (5, a); Assert.AreEqual (6, a);
//Console.WriteLine("delegate returned: "+a); //Console.WriteLine("interface returned: "+a);
} }
} }
/*
* Tests passing a Lua table as an interface and
/* * calling one of its methods with value-type params
* Tests passing a Lua table as an interface and * and a ref param
* calling one of its methods with value-type params */
*/ [Test]
[Test] public void LuaInterfaceValueTypesByRefParam ()
public void LuaInterfaceAAValueTypes () {
{ using (Lua lua = new Lua () ) {
using (Lua lua = new Lua ()) { lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.RegisterLuaClassType (typeof(ITest), typeof (LuaGeneratedClassX)); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("itest={}");
lua.DoString ("test=TestClass()"); lua.DoString ("function itest:test3(x,y) return x+y; end");
lua.DoString ("itest={}"); lua.DoString ("test=TestClass()");
lua.DoString ("function itest:test1(x,y) return x+y; end"); lua.DoString ("a=test:callInterface3(itest)");
lua.DoString ("test=TestClass()"); int a = (int) lua.GetNumber ("a");
lua.DoString ("a=test:callInterface1(itest)"); Assert.AreEqual (5, a);
int a = (int)lua.GetNumber ("a"); //Console.WriteLine("interface returned: "+a);
Assert.AreEqual (5, a); }
//Console.WriteLine("interface returned: "+a); }
} /*
} * Tests passing a Lua table as an interface and
/* * calling one of its methods with value-type params
* Tests passing a Lua table as an interface and * returning a reference type param
* calling one of its methods with value-type params */
* and an out param [Test]
*/ public void LuaInterfaceValueTypesReturnReferenceType ()
[Test] {
public void LuaInterfaceValueTypesOutParam () using (Lua lua = new Lua () ) {
{ lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
using (Lua lua = new Lua ()) { lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("itest={}");
lua.DoString ("test=TestClass()"); lua.DoString ("function itest:test4(x,y) return TestClass(x+y); end");
lua.DoString ("itest={}"); lua.DoString ("test=TestClass()");
lua.DoString ("function itest:test2(x) return x,x*2; end"); lua.DoString ("a=test:callInterface4(itest)");
lua.DoString ("test=TestClass()"); int a = (int) lua.GetNumber ("a");
lua.DoString ("a=test:callInterface2(itest)"); Assert.AreEqual (5, a);
int a = (int)lua.GetNumber ("a"); //Console.WriteLine("interface returned: "+a);
Assert.AreEqual (6, a); }
//Console.WriteLine("interface returned: "+a); }
} /*
} * Tests passing a Lua table as an interface and
/* * calling one of its methods with reference type params
* Tests passing a Lua table as an interface and */
* calling one of its methods with value-type params [Test]
* and a ref param public void LuaInterfaceReferenceTypes ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void LuaInterfaceValueTypesByRefParam () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("itest={}");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("function itest:test5(x,y) return x.testval+y.testval; end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("a=test:callInterface5(itest)");
lua.DoString ("function itest:test3(x,y) return x+y; end"); int a = (int) lua.GetNumber ("a");
lua.DoString ("test=TestClass()"); Assert.AreEqual (5, a);
lua.DoString ("a=test:callInterface3(itest)"); //Console.WriteLine("interface returned: "+a);
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (5, a); }
//Console.WriteLine("interface returned: "+a); /*
} * Tests passing a Lua table as an interface and
} * calling one of its methods with reference type params
/* * and an out param
* Tests passing a Lua table as an interface and */
* calling one of its methods with value-type params [Test]
* returning a reference type param public void LuaInterfaceReferenceTypesOutParam ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void LuaInterfaceValueTypesReturnReferenceType () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("itest={}");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("function itest:test6(x) return x,TestClass(x*2); end");
lua.DoString ("test=TestClass()"); lua.DoString ("test=TestClass()");
lua.DoString ("itest={}"); lua.DoString ("a=test:callInterface6(itest)");
lua.DoString ("function itest:test4(x,y) return TestClass(x+y); end"); int a = (int) lua.GetNumber ("a");
lua.DoString ("test=TestClass()"); Assert.AreEqual (6, a);
lua.DoString ("a=test:callInterface4(itest)"); //Console.WriteLine("interface returned: "+a);
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (5, a); }
//Console.WriteLine("interface returned: "+a); /*
} * Tests passing a Lua table as an interface and
} * calling one of its methods with reference type params
/* * and a ref param
* Tests passing a Lua table as an interface and */
* calling one of its methods with reference type params [Test]
*/ public void LuaInterfaceReferenceTypesByRefParam ()
[Test] {
public void LuaInterfaceReferenceTypes () using (Lua lua = new Lua () ) {
{ lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
using (Lua lua = new Lua ()) { lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("test=TestClass()");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("itest={}");
lua.DoString ("test=TestClass()"); lua.DoString ("function itest:test7(x,y) return TestClass(x+y.testval); end");
lua.DoString ("itest={}"); lua.DoString ("a=test:callInterface7(itest)");
lua.DoString ("function itest:test5(x,y) return x.testval+y.testval; end"); int a = (int) lua.GetNumber ("a");
lua.DoString ("test=TestClass()"); Assert.AreEqual (5, a);
lua.DoString ("a=test:callInterface5(itest)"); //Console.WriteLine("interface returned: "+a);
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (5, a); }
//Console.WriteLine("interface returned: "+a);
}
} #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,
* Tests passing a Lua table as an interface and * this class is usually dynamic generated using System.Reflection.Emit, but this will not work on iOS. */
* calling one of its methods with reference type params class LuaITestClassHandler : ILuaGeneratedType, ITest
* and an out param {
*/ public LuaTable __luaInterface_luaTable;
[Test] public Type[][] __luaInterface_returnTypes;
public void LuaInterfaceReferenceTypesOutParam ()
{ public LuaITestClassHandler (LuaTable luaTable, Type[][] returnTypes)
using (Lua lua = new Lua ()) { {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); __luaInterface_luaTable = luaTable;
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); __luaInterface_returnTypes = returnTypes;
lua.DoString ("test=TestClass()"); }
lua.DoString ("itest={}");
lua.DoString ("function itest:test6(x) return x,TestClass(x*2); end"); public LuaTable __luaInterface_getLuaTable ()
lua.DoString ("test=TestClass()"); {
lua.DoString ("a=test:callInterface6(itest)"); return __luaInterface_luaTable;
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (6, a);
//Console.WriteLine("interface returned: "+a); public int intProp
} {
} get {
/* object [] args = new object [] { __luaInterface_luaTable };
* Tests passing a Lua table as an interface and object [] inArgs = new object [] { __luaInterface_luaTable };
* calling one of its methods with reference type params int [] outArgs = new int [] { };
* and a ref param Type [] returnTypes = __luaInterface_returnTypes [0];
*/ LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_intProp");
[Test] object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
public void LuaInterfaceReferenceTypesByRefParam () return (int) ret;
{ } set {
using (Lua lua = new Lua ()) { int i = value;
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); object [] args = new object [] { __luaInterface_luaTable , i};
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); object [] inArgs = new object [] { __luaInterface_luaTable, i };
lua.DoString ("test=TestClass()"); int [] outArgs = new int [] { };
lua.DoString ("itest={}"); Type [] returnTypes = __luaInterface_returnTypes [1];
lua.DoString ("function itest:test7(x,y) return TestClass(x+y.testval); end"); LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_intProp");
lua.DoString ("a=test:callInterface7(itest)"); LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
int a = (int)lua.GetNumber ("a"); }
Assert.AreEqual (5, a); }
//Console.WriteLine("interface returned: "+a);
} public TestClass refProp
} {
get {
/*** "Dynamic generated class X***/ object [] args = new object [] { __luaInterface_luaTable };
class LuaGeneratedClassX : ILuaGeneratedType, ITest object [] inArgs = new object [] { __luaInterface_luaTable };
{ int [] outArgs = new int [] { };
public LuaTable __luaInterface_luaTable; Type [] returnTypes = __luaInterface_returnTypes [2];
public Type[][] __luaInterface_returnTypes; LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "get_refProp");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
public LuaGeneratedClassX (LuaTable luaTable, Type [][] returnTypes) return (TestClass) ret;
{ } set {
__luaInterface_luaTable = luaTable; TestClass test = value;
__luaInterface_returnTypes = returnTypes; object [] args = new object [] { __luaInterface_luaTable , test};
} object [] inArgs = new object [] { __luaInterface_luaTable, test };
int [] outArgs = new int [] { };
public LuaTable __luaInterface_getLuaTable () Type [] returnTypes = __luaInterface_returnTypes [3];
{ LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "set_refProp");
return __luaInterface_luaTable; LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
} }
}
public int intProp
{ public int test1 (int a, int b)
get { {
return 0; object [] args = new object [] {
} __luaInterface_luaTable,
a,
set { b
} };
} object [] inArgs = new object [] {
public TestClass refProp __luaInterface_luaTable,
{ a,
get { b
return null; };
} int [] outArgs = new int [] { };
set { Type [] returnTypes = __luaInterface_returnTypes [4];
} LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test1");
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
} return (int) ret;
}
public int test1(int a, int b)
{ public int test2 (int a, out int b)
object [] args = new object [] { __luaInterface_luaTable, a, b }; {
object [] inArgs = new object [] { __luaInterface_luaTable, a, b }; object [] args = new object [] {
int [] outArgs = new int [] { }; __luaInterface_luaTable,
Type [] returnTypes = __luaInterface_returnTypes [4]; a,
0
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test1"); };
object [] inArgs = new object [] {
__luaInterface_luaTable,
object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs); a
};
return (int) ret; int [] outArgs = new int [] { 1 };
} Type [] returnTypes = __luaInterface_returnTypes [5];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test2");
public int test2(int a, out int b) object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
{ b = (int)args [1];
b = 0; return (int) ret;
return 0; }
}
public void test3 (int a, ref int b)
public void test3(int a, ref int b) {
{ object [] args = new object [] {
} __luaInterface_luaTable,
a,
public TestClass test4(int a, int b) b
{ };
return null; object [] inArgs = new object [] {
} __luaInterface_luaTable,
a,
public int test5(TestClass a, TestClass b) b
{ };
return 0; int [] outArgs = new int [] { 1 };
} Type [] returnTypes = __luaInterface_returnTypes [6];
LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test3");
public int test6(int a, out TestClass b) LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
{ b = (int)args [1];
b = null; }
return 0;
} public TestClass test4 (int a, int b)
{
public void test7(int a, ref TestClass b) object [] args = new object [] {
{ __luaInterface_luaTable,
} a,
} b
};
object [] inArgs = new object [] {
__luaInterface_luaTable,
/* a,
* Tests passing a Lua table as an interface and b
* accessing one of its value-type properties };
*/ int [] outArgs = new int [] { };
[Test] Type [] returnTypes = __luaInterface_returnTypes [7];
public void LuaInterfaceValueProperty () LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test4");
{ object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
using (Lua lua = new Lua ()) { return (TestClass) ret;
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); }
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()"); public int test5 (TestClass a, TestClass b)
lua.DoString ("itest={}"); {
lua.DoString ("function itest:get_intProp() return itest.int_prop; end"); object [] args = new object [] {
lua.DoString ("function itest:set_intProp(val) itest.int_prop=val; end"); __luaInterface_luaTable,
lua.DoString ("a=test:callInterface8(itest)"); a,
int a = (int)lua.GetNumber ("a"); b
Assert.AreEqual (3, a); };
//Console.WriteLine("interface returned: "+a); object [] inArgs = new object [] {
} __luaInterface_luaTable,
} a,
/* b
* Tests passing a Lua table as an interface and };
* accessing one of its reference type properties int [] outArgs = new int [] { };
*/ Type [] returnTypes = __luaInterface_returnTypes [8];
[Test] LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test5");
public void LuaInterfaceReferenceProperty () object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
{ return (int) ret;
using (Lua lua = new Lua ()) { }
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); public int test6 (int a, out TestClass b)
lua.DoString ("test=TestClass()"); {
lua.DoString ("itest={}"); object [] args = new object [] {
lua.DoString ("function itest:get_refProp() return TestClass(itest.int_prop); end"); __luaInterface_luaTable,
lua.DoString ("function itest:set_refProp(val) itest.int_prop=val.testval; end"); a,
lua.DoString ("a=test:callInterface9(itest)"); null
int a = (int)lua.GetNumber ("a"); };
Assert.AreEqual (3, a); object [] inArgs = new object [] {
//Console.WriteLine("interface returned: "+a); __luaInterface_luaTable,
} a,
} };
int [] outArgs = new int [] { 1};
Type [] returnTypes = __luaInterface_returnTypes [9];
/* LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test6");
* Tests making an object from a Lua table and calling the base object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
* class version of one of the methods the table overrides. b = (TestClass)args [1];
*/
[Test] return (int) ret;
public void LuaTableBaseMethod () }
{
using (Lua lua = new Lua ()) { public void test7 (int a, ref TestClass b)
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); {
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); object [] args = new object [] {
lua.DoString ("test={}"); __luaInterface_luaTable,
lua.DoString ("function test:overridableMethod(x,y) print(self[base]); return 6 end"); a,
lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); b
lua.DoString ("a=TestClass.callOverridable(test,2,3)"); };
int a = (int)lua.GetNumber ("a"); object [] inArgs = new object [] {
lua.DoString ("luanet.free_object(test)"); __luaInterface_luaTable,
Assert.AreEqual (6, a); a,
b
// lua.DoString("luanet.load_assembly('LuaInterfaceTest')"); };
// lua.DoString("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); int [] outArgs = new int [] { 1 };
// lua.DoString("test={}"); Type [] returnTypes = __luaInterface_returnTypes [10];
// LuaFunction function = LuaInterface.Method.LuaClassHelper.getTableFunction (__luaInterface_luaTable, "test7");
// lua.DoString("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')"); object ret = LuaInterface.Method.LuaClassHelper.callFunction (function, args, returnTypes, inArgs, outArgs);
// lua.DoString ("function test.overridableMethod(test,x,y) return 2*test.base.overridableMethod(test,x,y); end"); b = (TestClass)args [1];
// lua.DoString("a=TestClass.callOverridable(test,2,3)"); }
// int a = (int)lua.GetNumber("a"); }
// lua.DoString("luanet.free_object(test)"); #endregion
// Assert.AreEqual(10, a);
//Console.WriteLine("interface returned: "+a); /*
} * Tests passing a Lua table as an interface and
} * accessing one of its value-type properties
/* */
* Tests getting an object's method by its signature [Test]
* (from object) public void LuaInterfaceValueProperty ()
*/ {
[Test] using (Lua lua = new Lua () ) {
public void GetMethodBySignatureFromObj () lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
{ lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("itest={}");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("function itest:get_intProp() return itest.int_prop; end");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("function itest:set_intProp(val) itest.int_prop=val; end");
lua.DoString ("test=TestClass()"); lua.DoString ("a=test:callInterface8(itest)");
lua.DoString ("setMethod=luanet.get_method_bysig(test,'setVal','System.String')"); int a = (int) lua.GetNumber ("a");
lua.DoString ("setMethod('test')"); Assert.AreEqual (3, a);
TestClass test = (TestClass)lua ["test"]; //Console.WriteLine("interface returned: "+a);
Assert.AreEqual ("test", test.getStrVal ()); }
//Console.WriteLine("interface returned: "+test.getStrVal()); }
} /*
} * Tests passing a Lua table as an interface and
/* * accessing one of its reference type properties
* Tests getting an object's method by its signature */
* (from type) [Test]
*/ public void LuaInterfaceReferenceProperty ()
[Test] {
public void GetMethodBySignatureFromType () using (Lua lua = new Lua () ) {
{ lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
using (Lua lua = new Lua ()) { lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("luanet.load_assembly('mscorlib')"); lua.DoString ("test=TestClass()");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); lua.DoString ("itest={}");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("function itest:get_refProp() return TestClass(itest.int_prop); end");
lua.DoString ("test=TestClass()"); lua.DoString ("function itest:set_refProp(val) itest.int_prop=val.testval; end");
lua.DoString ("setMethod=luanet.get_method_bysig(TestClass,'setVal','System.String')"); lua.DoString ("a=test:callInterface9(itest)");
lua.DoString ("setMethod(test,'test')"); int a = (int) lua.GetNumber ("a");
TestClass test = (TestClass)lua ["test"]; Assert.AreEqual (3, a);
Assert.AreEqual ("test", test.getStrVal ()); //Console.WriteLine("interface returned: "+a);
//Console.WriteLine("interface returned: "+test.getStrVal()); }
} }
}
/*
* Tests getting a type's method by its signature /*
*/ * Tests making an object from a Lua table and calling the base
[Test] * class version of one of the methods the table overrides.
public void GetStaticMethodBySignature () */
{ [Test]
using (Lua lua = new Lua ()) { public void LuaTableBaseMethod ()
lua.DoString ("luanet.load_assembly('mscorlib')"); {
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); using (Lua lua = new Lua () ) {
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("make_method=luanet.get_method_bysig(TestClass,'makeFromString','System.String')"); lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=make_method('test')"); lua.DoString ("test={}");
TestClass test = (TestClass)lua ["test"]; lua.DoString ("function test:overridableMethod(x,y) print(self[base]); return 6 end");
Assert.AreEqual ("test", test.getStrVal ()); lua.DoString ("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
//Console.WriteLine("interface returned: "+test.getStrVal()); lua.DoString ("a=TestClass.callOverridable(test,2,3)");
} int a = (int) lua.GetNumber ("a");
} lua.DoString ("luanet.free_object(test)");
/* Assert.AreEqual (6, a);
* Tests getting an object's constructor by its signature // lua.DoString("luanet.load_assembly('LuaInterfaceTest')");
*/ // lua.DoString("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
[Test] // lua.DoString("test={}");
public void GetConstructorBySignature () //
{ // lua.DoString("luanet.make_object(test,'LuaInterfaceTest.Mock.TestClass')");
using (Lua lua = new Lua ()) { // lua.DoString ("function test.overridableMethod(test,x,y) return 2*test.base.overridableMethod(test,x,y); end");
lua.DoString ("luanet.load_assembly('mscorlib')"); // lua.DoString("a=TestClass.callOverridable(test,2,3)");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')"); // int a = (int)lua.GetNumber("a");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')"); // lua.DoString("luanet.free_object(test)");
lua.DoString ("test_cons=luanet.get_constructor_bysig(TestClass,'System.String')"); // Assert.AreEqual(10, a);
lua.DoString ("test=test_cons('test')"); //Console.WriteLine("interface returned: "+a);
TestClass test = (TestClass)lua ["test"]; }
Assert.AreEqual ("test", test.getStrVal ()); }
//Console.WriteLine("interface returned: "+test.getStrVal()); /*
} * Tests getting an object's method by its signature
} * (from object)
*/
} [Test]
public void GetMethodBySignatureFromObj ()
{
using (Lua lua = new Lua () ) {
lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()");
lua.DoString ("setMethod=luanet.get_method_bysig(test,'setVal','System.String')");
lua.DoString ("setMethod('test')");
TestClass test = (TestClass) lua ["test"];
Assert.AreEqual ("test", test.getStrVal () );
//Console.WriteLine("interface returned: "+test.getStrVal());
}
}
/*
* Tests getting an object's method by its signature
* (from type)
*/
[Test]
public void GetMethodBySignatureFromType ()
{
using (Lua lua = new Lua () ) {
lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test=TestClass()");
lua.DoString ("setMethod=luanet.get_method_bysig(TestClass,'setVal','System.String')");
lua.DoString ("setMethod(test,'test')");
TestClass test = (TestClass) lua ["test"];
Assert.AreEqual ("test", test.getStrVal () );
//Console.WriteLine("interface returned: "+test.getStrVal());
}
}
/*
* Tests getting a type's method by its signature
*/
[Test]
public void GetStaticMethodBySignature ()
{
using (Lua lua = new Lua () ) {
lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("make_method=luanet.get_method_bysig(TestClass,'makeFromString','System.String')");
lua.DoString ("test=make_method('test')");
TestClass test = (TestClass) lua ["test"];
Assert.AreEqual ("test", test.getStrVal () );
//Console.WriteLine("interface returned: "+test.getStrVal());
}
}
/*
* Tests getting an object's constructor by its signature
*/
[Test]
public void GetConstructorBySignature ()
{
using (Lua lua = new Lua () ) {
lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('LuaInterfaceTest')");
lua.DoString ("TestClass=luanet.import_type('LuaInterfaceTest.Mock.TestClass')");
lua.DoString ("test_cons=luanet.get_constructor_bysig(TestClass,'System.String')");
lua.DoString ("test=test_cons('test')");
TestClass test = (TestClass) lua ["test"];
Assert.AreEqual ("test", test.getStrVal () );
//Console.WriteLine("interface returned: "+test.getStrVal());
}
}
}
} }
...@@ -167,7 +167,6 @@ namespace LuaInterfaceTest.Mock ...@@ -167,7 +167,6 @@ namespace LuaInterfaceTest.Mock
/// </summary> /// </summary>
class DoWorkClass class DoWorkClass
{ {
private object _Lock = new object();
public void DoWork() public void DoWork()
{ {
......
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