Commit 2271a771 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed test on iOS (device)

parent bf8c5fe6
//note: this should be cleaned up and replaced with moq mocks where possible //note: this should be cleaned up and replaced with moq mocks where possible
namespace LuaInterfaceTest.Mock namespace LuaInterfaceTest.Mock
{ {
using System; using System;
using LuaInterface; using LuaInterface;
using System.Threading; using System.Threading;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
/* /*
* Delegates used for testing Lua function -> delegate translation * Delegates used for testing Lua function -> delegate translation
*/ */
public delegate int TestDelegate1 (int a, int b); public delegate int TestDelegate1 (int a, int b);
public delegate int TestDelegate2 (int a, out int b); public delegate int TestDelegate2 (int a, out int b);
public delegate void TestDelegate3 (int a, ref int b); public delegate void TestDelegate3 (int a, ref int b);
public delegate TestClass TestDelegate4 (int a, int b); public delegate TestClass TestDelegate4 (int a, int b);
public delegate int TestDelegate5 (TestClass a, TestClass b); public delegate int TestDelegate5 (TestClass a, TestClass b);
public delegate int TestDelegate6 (int a, out TestClass b); public delegate int TestDelegate6 (int a, out TestClass b);
public delegate void TestDelegate7 (int a, ref TestClass b); public delegate void TestDelegate7 (int a, ref TestClass b);
/* Delegate Lua-handlers */ /* Delegate Lua-handlers */
class LuaTestDelegate1Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate1Handler : LuaInterface.Method.LuaDelegate
{ {
int CallFunction (int a, int b) int CallFunction (int a, int b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b }; object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (int)ret; return (int)ret;
} }
} }
class LuaTestDelegate2Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate2Handler : LuaInterface.Method.LuaDelegate
{ {
int CallFunction (int a, out int b) int CallFunction (int a, out int b)
{ {
object [] args = new object [] { a, 0 }; object [] args = new object [] { a, 0 };
object [] inArgs = new object [] { a }; object [] inArgs = new object [] { a };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
b = (int)args [1]; b = (int)args [1];
return (int)ret; return (int)ret;
} }
} }
class LuaTestDelegate3Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate3Handler : LuaInterface.Method.LuaDelegate
{ {
void CallFunction (int a, ref int b) void CallFunction (int a, ref int b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b }; object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
base.callFunction (args, inArgs, outArgs); base.callFunction (args, inArgs, outArgs);
b = (int)args [1]; b = (int)args [1];
} }
} }
class LuaTestDelegate4Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate4Handler : LuaInterface.Method.LuaDelegate
{ {
TestClass CallFunction (int a, int b) TestClass CallFunction (int a, int b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b }; object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (TestClass)ret; return (TestClass)ret;
} }
} }
class LuaTestDelegate5Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate5Handler : LuaInterface.Method.LuaDelegate
{ {
int CallFunction (TestClass a, TestClass b) int CallFunction (TestClass a, TestClass b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b }; object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { }; int [] outArgs = new int [] { };
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
return (int)ret; return (int)ret;
} }
} }
class LuaTestDelegate6Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate6Handler : LuaInterface.Method.LuaDelegate
{ {
int CallFunction (int a, ref TestClass b) int CallFunction (int a, ref TestClass b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a }; object [] inArgs = new object [] { a };
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
object ret = base.callFunction (args, inArgs, outArgs); object ret = base.callFunction (args, inArgs, outArgs);
b = (TestClass)args [1]; b = (TestClass)args [1];
return (int)ret; return (int)ret;
} }
} }
class LuaTestDelegate7Handler : LuaInterface.Method.LuaDelegate class LuaTestDelegate7Handler : LuaInterface.Method.LuaDelegate
{ {
void CallFunction (int a, ref TestClass b) void CallFunction (int a, ref TestClass b)
{ {
object [] args = new object [] { a, b }; object [] args = new object [] { a, b };
object [] inArgs = new object [] { a , b}; object [] inArgs = new object [] { a , b};
int [] outArgs = new int [] { 1 }; int [] outArgs = new int [] { 1 };
base.callFunction (args, inArgs, outArgs); base.callFunction (args, inArgs, outArgs);
b = (TestClass)args [1]; b = (TestClass)args [1];
} }
} }
/* /*
* Interface used for testing Lua table -> interface translation * Interface used for testing Lua table -> interface translation
*/ */
public interface ITest public interface ITest
{ {
int intProp { int intProp {
get; get;
set; set;
} }
TestClass refProp { TestClass refProp {
get; get;
set; set;
} }
int test1 (int a, int b); int test1 (int a, int b);
int test2 (int a, out int b); int test2 (int a, out int b);
void test3 (int a, ref int b); void test3 (int a, ref int b);
TestClass test4 (int a, int b); TestClass test4 (int a, int b);
int test5 (TestClass a, TestClass b); int test5 (TestClass a, TestClass b);
int test6 (int a, out TestClass b); int test6 (int a, out TestClass b);
void test7 (int a, ref TestClass b); void test7 (int a, ref TestClass b);
} }
public interface IFoo1 public interface IFoo1
{ {
int foo (); int foo ();
} }
public interface IFoo2 public interface IFoo2
{ {
int foo (); int foo ();
} }
class MyClass class MyClass
{ {
public int Func1 () public int Func1 ()
{ {
return 1; return 1;
} }
} }
/// <summary> /// <summary>
/// Use to test threading /// Use to test threading
/// </summary> /// </summary>
class DoWorkClass class DoWorkClass
{ {
public void DoWork () public void DoWork ()
{ {
//simulate work by sleeping //simulate work by sleeping
//Console.WriteLine("Started to do work on thread: " + Thread.CurrentThread.ManagedThreadId); //Console.WriteLine("Started to do work on thread: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep (new Random ().Next (0, 1000)); Thread.Sleep (new Random ().Next (0, 1000));
//Console.WriteLine("Finished work on thread: " + Thread.CurrentThread.ManagedThreadId); //Console.WriteLine("Finished work on thread: " + Thread.CurrentThread.ManagedThreadId);
} }
} }
/// <summary> /// <summary>
/// test structure passing /// test structure passing
/// </summary> /// </summary>
public struct TestStruct public struct TestStruct
{ {
public TestStruct (float val) public TestStruct (float val)
{ {
v = val; v = val;
} }
public float v; public float v;
public float val { public float val {
get { return v; } get { return v; }
set { v = value; } set { v = value; }
} }
} }
/// <summary> /// <summary>
/// Generic class with generic and non-generic methods /// Generic class with generic and non-generic methods
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public class TestClassGeneric<T> public class TestClassGeneric<T>
{ {
private object _PassedValue; private object _PassedValue;
private bool _RegularMethodSuccess; private bool _RegularMethodSuccess;
public bool RegularMethodSuccess { public bool RegularMethodSuccess {
get { return _RegularMethodSuccess; } get { return _RegularMethodSuccess; }
} }
private bool _GenericMethodSuccess; private bool _GenericMethodSuccess;
public bool GenericMethodSuccess { public bool GenericMethodSuccess {
get { return _GenericMethodSuccess; } get { return _GenericMethodSuccess; }
} }
public void GenericMethod (T value) public void GenericMethod (T value)
{ {
_PassedValue = value; _PassedValue = value;
_GenericMethodSuccess = true; _GenericMethodSuccess = true;
} }
public void RegularMethod () public void RegularMethod ()
{ {
_RegularMethodSuccess = true; _RegularMethodSuccess = true;
} }
/// <summary> /// <summary>
/// Returns true if the generic method was successfully passed a matching value /// Returns true if the generic method was successfully passed a matching value
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
/// <returns></returns> /// <returns></returns>
public bool Validate (T value) public bool Validate (T value)
{ {
return value.Equals (_PassedValue); return value.Equals (_PassedValue);
} }
} }
/// <summary> /// <summary>
/// Normal class containing a generic method /// Normal class containing a generic method
/// </summary> /// </summary>
public class TestClassWithGenericMethod public class TestClassWithGenericMethod
{ {
private object _PassedValue; private object _PassedValue;
public object PassedValue { public object PassedValue {
get { return _PassedValue; } get { return _PassedValue; }
} }
private bool _GenericMethodSuccess; private bool _GenericMethodSuccess;
public bool GenericMethodSuccess { public bool GenericMethodSuccess {
get { return _GenericMethodSuccess; } get { return _GenericMethodSuccess; }
} }
public void GenericMethod<T> (T value) public void GenericMethod<T> (T value)
{ {
_PassedValue = value; _PassedValue = value;
_GenericMethodSuccess = true; _GenericMethodSuccess = true;
} }
internal bool Validate<T> (T value) internal bool Validate<T> (T value)
{ {
return value.Equals (_PassedValue); return value.Equals (_PassedValue);
} }
} }
public class TestClass2 public class TestClass2
{ {
public static int func (int x, int y) public static int func (int x, int y)
{ {
return x + y; return x + y;
} }
public int funcInstance (int x, int y) public int funcInstance (int x, int y)
{ {
return x + y; return x + y;
} }
} }
/* /*
* Sample class used in several test cases to check if * Sample class used in several test cases to check if
* Lua scripts are accessing objects correctly * Lua scripts are accessing objects correctly
*/ */
public class TestClass : IFoo1, IFoo2 public class TestClass : IFoo1, IFoo2
{ {
public int val; public int val;
private string strVal; private string strVal;
public TestClass () public TestClass ()
{ {
val = 0; val = 0;
} }
public TestClass (int val) public TestClass (int val)
{ {
this.val = val; this.val = val;
} }
public TestClass (string val) public TestClass (string val)
{ {
this.strVal = val; this.strVal = val;
} }
public static TestClass makeFromString (String str) public static TestClass makeFromString (String str)
{ {
return new TestClass (str); return new TestClass (str);
} }
bool? nb2 = null; bool? nb2 = null;
public bool? NullableBool { public bool? NullableBool {
get { return nb2; } get { return nb2; }
set { nb2 = value; } set { nb2 = value; }
} }
TestStruct s = new TestStruct (); TestStruct s = new TestStruct ();
public TestStruct Struct { public TestStruct Struct {
get { return s; } get { return s; }
set { s = (TestStruct)value; } set { s = (TestStruct)value; }
} }
public int testval { public int testval {
get { get {
return this.val; return this.val;
} }
set { set {
this.val = value; this.val = value;
} }
} }
public int this [int index] { public int this [int index] {
get { return 1; } get { return 1; }
set { } set { }
} }
public int this [string index] { public int this [string index] {
get { return 1; } get { return 1; }
set { } set { }
} }
public int sum (int x, int y) public int sum (int x, int y)
{ {
return x + y; return x + y;
} }
public void setVal (int newVal) public void setVal (int newVal)
{ {
val = newVal; val = newVal;
} }
public void setVal (string newVal) public void setVal (string newVal)
{ {
strVal = newVal; strVal = newVal;
} }
public int getVal () public int getVal ()
{ {
return val; return val;
} }
public string getStrVal () public string getStrVal ()
{ {
return strVal; return strVal;
} }
public int outVal (out int val) public int outVal (out int val)
{ {
val = 5; val = 5;
return 3; return 3;
} }
public int outVal (out int val, int val2) public int outVal (out int val, int val2)
{ {
val = 5; val = 5;
return val2; return val2;
} }
public int outVal (int val, ref int val2) public int outVal (int val, ref int val2)
{ {
val2 = val + val2; val2 = val + val2;
return val; return val;
} }
public int outValMutiple (int arg, out string arg2, out string arg3) public int outValMutiple (int arg, out string arg2, out string arg3)
{ {
arg2 = Guid.NewGuid ().ToString (); arg2 = Guid.NewGuid ().ToString ();
arg3 = Guid.NewGuid ().ToString (); arg3 = Guid.NewGuid ().ToString ();
return arg; return arg;
} }
public int callDelegate1 (TestDelegate1 del) public int callDelegate1 (TestDelegate1 del)
{ {
return del (2, 3); return del (2, 3);
} }
public int callDelegate2 (TestDelegate2 del) public int callDelegate2 (TestDelegate2 del)
{ {
int a = 3; int a = 3;
int b = del (2, out a); int b = del (2, out a);
return a + b; return a + b;
} }
public int callDelegate3 (TestDelegate3 del) public int callDelegate3 (TestDelegate3 del)
{ {
int a = 3; int a = 3;
del (2, ref a); del (2, ref a);
//Console.WriteLine(a); //Console.WriteLine(a);
return a; return a;
} }
public int callDelegate4 (TestDelegate4 del) public int callDelegate4 (TestDelegate4 del)
{ {
return del (2, 3).testval; return del (2, 3).testval;
} }
public int callDelegate5 (TestDelegate5 del) public int callDelegate5 (TestDelegate5 del)
{ {
return del (new TestClass (2), new TestClass (3)); return del (new TestClass (2), new TestClass (3));
} }
public int callDelegate6 (TestDelegate6 del) public int callDelegate6 (TestDelegate6 del)
{ {
TestClass test = new TestClass (); TestClass test = new TestClass ();
int a = del (2, out test); int a = del (2, out test);
return a + test.testval; return a + test.testval;
} }
public int callDelegate7 (TestDelegate7 del) public int callDelegate7 (TestDelegate7 del)
{ {
TestClass test = new TestClass (3); TestClass test = new TestClass (3);
del (2, ref test); del (2, ref test);
return test.testval; return test.testval;
} }
public int callInterface1 (ITest itest) public int callInterface1 (ITest itest)
{ {
return itest.test1 (2, 3); return itest.test1 (2, 3);
} }
public int callInterface2 (ITest itest) public int callInterface2 (ITest itest)
{ {
int a = 3; int a = 3;
int b = itest.test2 (2, out a); int b = itest.test2 (2, out a);
return a + b; return a + b;
} }
public int callInterface3 (ITest itest) public int callInterface3 (ITest itest)
{ {
int a = 3; int a = 3;
itest.test3 (2, ref a); itest.test3 (2, ref a);
//Console.WriteLine(a); //Console.WriteLine(a);
return a; return a;
} }
public int callInterface4 (ITest itest) public int callInterface4 (ITest itest)
{ {
return itest.test4 (2, 3).testval; return itest.test4 (2, 3).testval;
} }
public int callInterface5 (ITest itest) public int callInterface5 (ITest itest)
{ {
return itest.test5 (new TestClass (2), new TestClass (3)); return itest.test5 (new TestClass (2), new TestClass (3));
} }
public int callInterface6 (ITest itest) public int callInterface6 (ITest itest)
{ {
TestClass test = new TestClass (); TestClass test = new TestClass ();
int a = itest.test6 (2, out test); int a = itest.test6 (2, out test);
return a + test.testval; return a + test.testval;
} }
public int callInterface7 (ITest itest) public int callInterface7 (ITest itest)
{ {
TestClass test = new TestClass (3); TestClass test = new TestClass (3);
itest.test7 (2, ref test); itest.test7 (2, ref test);
return test.testval; return test.testval;
} }
public int callInterface8 (ITest itest) public int callInterface8 (ITest itest)
{ {
itest.intProp = 3; itest.intProp = 3;
return itest.intProp; return itest.intProp;
} }
public int callInterface9 (ITest itest) public int callInterface9 (ITest itest)
{ {
itest.refProp = new TestClass (3); itest.refProp = new TestClass (3);
return itest.refProp.testval; return itest.refProp.testval;
} }
public void exceptionMethod () public void exceptionMethod ()
{ {
throw new Exception ("exception test"); throw new Exception ("exception test");
} }
public virtual int overridableMethod (int x, int y) public virtual int overridableMethod (int x, int y)
{ {
return x + y; return x + y;
} }
public static int callOverridable (TestClass test, int x, int y) public static int callOverridable (TestClass test, int x, int y)
{ {
return test.overridableMethod (x, y); return test.overridableMethod (x, y);
} }
int IFoo1.foo () int IFoo1.foo ()
{ {
return 3; return 3;
} }
public int foo () public int foo ()
{ {
return 5; return 5;
} }
private void _PrivateMethod () private void _PrivateMethod ()
{ {
Console.WriteLine ("Private method called"); Console.WriteLine ("Private method called");
} }
public void MethodOverload () public void MethodOverload ()
{ {
Console.WriteLine ("Method with no params"); Console.WriteLine ("Method with no params");
} }
public void MethodOverload (TestClass testClass) public void MethodOverload (TestClass testClass)
{ {
Console.WriteLine ("Method with testclass param"); Console.WriteLine ("Method with testclass param");
} }
public void MethodOverload (int i, int j, int k) public void MethodOverload (int i, int j, int k)
{ {
Console.WriteLine ("Overload without out param: " + i + ", " + j + ", " + k); Console.WriteLine ("Overload without out param: " + i + ", " + j + ", " + k);
} }
public void MethodOverload (int i, int j, out int k) public void MethodOverload (int i, int j, out int k)
{ {
k = 5; k = 5;
Console.WriteLine ("Overload with out param" + i + ", " + j); Console.WriteLine ("Overload with out param" + i + ", " + j);
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment