-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
TestLua.cs
executable file
·634 lines (512 loc) · 11.3 KB
/
TestLua.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//note: this should be cleaned up and replaced with moq mocks where possible
namespace NLuaTest.Mock
{
using System;
using NLua;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
/*
* Delegates used for testing Lua function -> delegate translation
*/
public delegate int TestDelegate1 (int a, int b);
public delegate int TestDelegate2 (int a, out int b);
public delegate void TestDelegate3 (int a, ref int b);
public delegate TestClass TestDelegate4 (int a, int b);
public delegate int TestDelegate5 (TestClass a, TestClass b);
public delegate int TestDelegate6 (int a, out TestClass b);
public delegate void TestDelegate7 (int a, ref TestClass b);
/* Delegate Lua-handlers */
class LuaTestDelegate1Handler : NLua.Method.LuaDelegate
{
int CallFunction (int a, int b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { };
object ret = base.CallFunction (args, inArgs, outArgs);
return (int)ret;
}
}
class LuaTestDelegate2Handler : NLua.Method.LuaDelegate
{
int CallFunction (int a, out int b)
{
object [] args = new object [] { a, 0 };
object [] inArgs = new object [] { a };
int [] outArgs = new int [] { 1 };
object ret = base.CallFunction (args, inArgs, outArgs);
b = (int)args [1];
return (int)ret;
}
}
class LuaTestDelegate3Handler : NLua.Method.LuaDelegate
{
void CallFunction (int a, ref int b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { 1 };
base.CallFunction (args, inArgs, outArgs);
b = (int)args [1];
}
}
class LuaTestDelegate4Handler : NLua.Method.LuaDelegate
{
TestClass CallFunction (int a, int b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { };
object ret = base.CallFunction (args, inArgs, outArgs);
return (TestClass)ret;
}
}
class LuaTestDelegate5Handler : NLua.Method.LuaDelegate
{
int CallFunction (TestClass a, TestClass b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a, b };
int [] outArgs = new int [] { };
object ret = base.CallFunction (args, inArgs, outArgs);
return (int)ret;
}
}
class LuaTestDelegate6Handler : NLua.Method.LuaDelegate
{
int CallFunction (int a, ref TestClass b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a };
int [] outArgs = new int [] { 1 };
object ret = base.CallFunction (args, inArgs, outArgs);
b = (TestClass)args [1];
return (int)ret;
}
}
class LuaTestDelegate7Handler : NLua.Method.LuaDelegate
{
void CallFunction (int a, ref TestClass b)
{
object [] args = new object [] { a, b };
object [] inArgs = new object [] { a , b};
int [] outArgs = new int [] { 1 };
base.CallFunction (args, inArgs, outArgs);
b = (TestClass)args [1];
}
}
/*
* Interface used for testing Lua table -> interface translation
*/
public interface ITest
{
int intProp {
get;
set;
}
TestClass refProp {
get;
set;
}
int test1 (int a, int b);
int test2 (int a, out int b);
void test3 (int a, ref int b);
TestClass test4 (int a, int b);
int test5 (TestClass a, TestClass b);
int test6 (int a, out TestClass b);
void test7 (int a, ref TestClass b);
}
public interface IFoo1
{
int foo ();
}
public interface IFoo2
{
int foo ();
}
class MyClass
{
public int Func1 ()
{
return 1;
}
}
/// <summary>
/// Use to test threading
/// </summary>
class DoWorkClass
{
public void DoWork ()
{
//simulate work by sleeping
//Console.WriteLine("Started to do work on thread: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep (new Random ().Next (0, 1000));
//Console.WriteLine("Finished work on thread: " + Thread.CurrentThread.ManagedThreadId);
}
}
/// <summary>
/// test structure passing
/// </summary>
public struct TestStruct
{
public TestStruct(float val)
{
v = val;
}
public float v;
public float val
{
get { return v; }
set { v = value; }
}
}
/// <summary>
/// test enum
/// </summary>
public enum TestEnum
{
ValueA,
ValueB
}
/// <summary>
/// Generic class with generic and non-generic methods
/// </summary>
/// <typeparam name="T"></typeparam>
public class TestClassGeneric<T>
{
private object _PassedValue;
private bool _RegularMethodSuccess;
public bool RegularMethodSuccess {
get { return _RegularMethodSuccess; }
}
private bool _GenericMethodSuccess;
public bool GenericMethodSuccess {
get { return _GenericMethodSuccess; }
}
public void GenericMethod (T value)
{
_PassedValue = value;
_GenericMethodSuccess = true;
}
public void RegularMethod ()
{
_RegularMethodSuccess = true;
}
/// <summary>
/// Returns true if the generic method was successfully passed a matching value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Validate (T value)
{
return value.Equals (_PassedValue);
}
}
/// <summary>
/// Normal class containing a generic method
/// </summary>
public class TestClassWithGenericMethod
{
private object _PassedValue;
public object PassedValue {
get { return _PassedValue; }
}
private bool _GenericMethodSuccess;
public bool GenericMethodSuccess {
get { return _GenericMethodSuccess; }
}
public void GenericMethod<T> (T value)
{
_PassedValue = value;
_GenericMethodSuccess = true;
}
internal bool Validate<T> (T value)
{
return value.Equals (_PassedValue);
}
}
public class TestClass2
{
public static int func (int x, int y)
{
return x + y;
}
public int funcInstance (int x, int y)
{
return x + y;
}
}
/*
* Sample class used in several test cases to check if
* Lua scripts are accessing objects correctly
*/
public class TestClass : IFoo1, IFoo2
{
public int val;
private string strVal;
public TestClass ()
{
val = 0;
}
public TestClass (int val)
{
this.val = val;
}
public TestClass (string val)
{
this.strVal = val;
}
public static TestClass makeFromString (String str)
{
return new TestClass (str);
}
bool? nb2 = null;
public bool? NullableBool {
get { return nb2; }
set { nb2 = value; }
}
TestStruct s = new TestStruct ();
public TestStruct Struct {
get { return s; }
set { s = (TestStruct)value; }
}
public int testval {
get {
return this.val;
}
set {
this.val = value;
}
}
public string teststrval {
get {
return this.strVal;
}
set {
this.strVal = value;
}
}
public int this [int index] {
get { return 1; }
set { }
}
public int this [string index] {
get { return 1; }
set { }
}
public TimeSpan? NullableMethod (TimeSpan? input)
{
return input;
}
public int? NullableMethod2 (int? input)
{
return input;
}
public object[] TestLuaFunction (LuaFunction func)
{
if (func != null) {
return func.Call (1, 2);
}
return null;
}
public int sum (int x, int y)
{
return x + y;
}
public void setVal (int newVal)
{
val = newVal;
}
public void setVal (string newVal)
{
strVal = newVal;
}
public int getVal ()
{
return val;
}
public string getStrVal ()
{
return strVal;
}
public int outVal (out int val)
{
val = 5;
return 3;
}
public int outVal (out int val, int val2)
{
val = 5;
return val2;
}
public int outVal (int val, ref int val2)
{
val2 = val + val2;
return val;
}
public int outValMutiple (int arg, out string arg2, out string arg3)
{
arg2 = Guid.NewGuid ().ToString ();
arg3 = Guid.NewGuid ().ToString ();
return arg;
}
public int callDelegate1 (TestDelegate1 del)
{
return del (2, 3);
}
public int callDelegate2 (TestDelegate2 del)
{
int a = 3;
int b = del (2, out a);
return a + b;
}
public int callDelegate3 (TestDelegate3 del)
{
int a = 3;
del (2, ref a);
//Console.WriteLine(a);
return a;
}
public int callDelegate4 (TestDelegate4 del)
{
return del (2, 3).testval;
}
public int callDelegate5 (TestDelegate5 del)
{
return del (new TestClass (2), new TestClass (3));
}
public int callDelegate6 (TestDelegate6 del)
{
TestClass test = new TestClass ();
int a = del (2, out test);
return a + test.testval;
}
public int callDelegate7 (TestDelegate7 del)
{
TestClass test = new TestClass (3);
del (2, ref test);
return test.testval;
}
public int callInterface1 (ITest itest)
{
return itest.test1 (2, 3);
}
public int callInterface2 (ITest itest)
{
int a = 3;
int b = itest.test2 (2, out a);
return a + b;
}
public int callInterface3 (ITest itest)
{
int a = 3;
itest.test3 (2, ref a);
//Console.WriteLine(a);
return a;
}
public int callInterface4 (ITest itest)
{
return itest.test4 (2, 3).testval;
}
public int callInterface5 (ITest itest)
{
return itest.test5 (new TestClass (2), new TestClass (3));
}
public int callInterface6 (ITest itest)
{
TestClass test = new TestClass ();
int a = itest.test6 (2, out test);
return a + test.testval;
}
public int callInterface7 (ITest itest)
{
TestClass test = new TestClass (3);
itest.test7 (2, ref test);
return test.testval;
}
public int callInterface8 (ITest itest)
{
itest.intProp = 3;
return itest.intProp;
}
public int callInterface9 (ITest itest)
{
itest.refProp = new TestClass (3);
return itest.refProp.testval;
}
public void exceptionMethod ()
{
throw new Exception ("exception test");
}
public virtual int overridableMethod (int x, int y)
{
return x + y;
}
public static int callOverridable (TestClass test, int x, int y)
{
return test.overridableMethod (x, y);
}
int IFoo1.foo ()
{
return 3;
}
public int foo ()
{
return 5;
}
private void _PrivateMethod ()
{
Console.WriteLine ("Private method called");
}
public void MethodOverload ()
{
Console.WriteLine ("Method with no params");
}
public void MethodOverload (TestClass testClass)
{
Console.WriteLine ("Method with testclass param");
}
public void MethodOverload (int i, int j, int k)
{
Console.WriteLine ("Overload without out param: " + i + ", " + j + ", " + k);
}
public void MethodOverload (int i, int j, out int k)
{
k = 5;
Console.WriteLine ("Overload with out param" + i + ", " + j);
}
public void Print(object format,params object[] args)
{
//just for test,this is not printf implements
var output = format.ToString() + "\t";
foreach(var msg in args)
{
output += msg.ToString() + "\t";
}
Console.WriteLine(output);
}
static public int MethodWithParams (int a, params int[] others) {
Console.WriteLine (a);
int i = 0;
foreach (int val in others) {
Console.WriteLine (val);
i++;
}
return i;
}
}
public class TestClassWithOverloadedMethod
{
public int CallsToStringFunc {get;set;}
public int CallsToIntFunc {get;set;}
public void Func (string param)
{
CallsToStringFunc++;
}
public void Func (int param)
{
CallsToIntFunc++;
}
}
}