-
Notifications
You must be signed in to change notification settings - Fork 7
/
LuaUtilityTest.cs
832 lines (749 loc) · 28.6 KB
/
LuaUtilityTest.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Tests
{
public class LuaUtilityTest
{
const string TestStr = @"hah duil uuu uii123 4432.fds_dfei sdfesdfe";
static string TestLexStateBaseDataStr = "{ int_val = 123, float_val = 4.56789, bool_val = true, str_val = '"+TestStr+"', nil_val = nil, }";
static string TestLexStateStr1 = "return "+TestLexStateBaseDataStr;
[Test]
public void TestLexState()
{
LuaUtility.LexState ls = new LuaUtility.LexState(TestLexStateStr1);
Assert.AreEqual((int)LuaUtility.KeyWord.Return, ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual('{', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Name, ls.t.token);
Assert.AreEqual("int_val", ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual('=', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Integer, ls.t.token);
Assert.AreEqual(123, ls.t.i);
ls.MoveToNextToken();
Assert.AreEqual(',', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Name, ls.t.token);
Assert.AreEqual("float_val", ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual('=', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.FLOAT, ls.t.token);
Assert.AreEqual(4.56789, ls.t.d);
ls.MoveToNextToken();
Assert.AreEqual(',', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Name, ls.t.token);
Assert.AreEqual("bool_val", ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual('=', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.True, ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual(',', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Name, ls.t.token);
Assert.AreEqual("str_val", ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual('=', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.String, ls.t.token);
Assert.AreEqual(TestStr, ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual(',', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Name, ls.t.token);
Assert.AreEqual("nil_val", ls.t.str);
ls.MoveToNextToken();
Assert.AreEqual('=', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.Nil, ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual(',', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual('}', (char)ls.t.token);
ls.MoveToNextToken();
Assert.AreEqual((int)LuaUtility.KeyWord.EOZ, ls.t.token);
}
[Test]
public void TestFromLuaInt()
{
var str = @"return {i=1}";
var obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 1);
str = @"return {i=-1}";
obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, -1);
str = @"{i=0}";
obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 0);
}
[Test]
public void TestFromLuaIntExtraData()
{
var str = @"return {i=1, ii=3, t={}, f=1.23, b=false, tt={1,2,3, ttt={a=1}}}";
LuaUtility.IsShowLog = true;
var obj = LuaUtility.FromLua<TestLuaInt>(str);
LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 1);
}
[Test]
public void TestFromLuaIntLocal()
{
var str = @"local t = {i=1} return t";
var obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 1);
// CAT_TODO: return前的赋值语法
LuaUtility.IsShowLog = true;
str = @"local t = {i=1} t['i']=1 t['i']=2 return t";
obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 1);
LuaUtility.IsShowLog = false;
}
[Test]
public void TestToLuaInt()
{
var data = new TestLuaInt();
data.i = 1234567890;
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("i = 1234567890,"));
}
[Test]
public void TestFromLuaFloat()
{
var str = @"return {f=1.2345}";
var obj = LuaUtility.FromLua<TestLuaFloat>(str);
Assert.IsNotNull(obj);
AssertFloat(1.2345, obj.f);
str = @"return {f=-1e+11}";
obj = LuaUtility.FromLua<TestLuaFloat>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(float.Parse("-1e+11"), obj.f);
AssertFloat(float.Parse("-1e+11"), obj.f);
str = @"return {f=.2345}";
obj = LuaUtility.FromLua<TestLuaFloat>(str);
Assert.IsNotNull(obj);
AssertFloat(0.2345, obj.f);
}
[Test]
public void TestFromLuaComment()
{
var str = @"--哈哈忽略注释吧
--[[123456
忽略多行注释吧1
忽略多行注释吧2
654321--]]
return {i=33}";
var obj = LuaUtility.FromLua<TestLuaInt>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(obj.i, 33);
}
[Test]
public void TestFromLuaString()
{
var str = "return {str=\""+TestStr+"\"}";
var obj = LuaUtility.FromLua<TestLuaString>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(TestStr, obj.str);
str = @"return {str='"+TestStr+"'}";
obj = LuaUtility.FromLua<TestLuaString>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(TestStr, obj.str);
var str_with_line = @"abcde\
fghijk";
str = "return {str=\""+str_with_line+"\"}";
Debug.LogFormat("LuaUtilityTest[191:04] str:{0}", str);
obj = LuaUtility.FromLua<TestLuaString>(str);
Assert.IsNotNull(obj);
//注意expect_str是故意和str_with_line不一样的,因为lua会把\符号给忽略掉或者转义
var expect_str = @"abcde
fghijk";
Assert.AreEqual(expect_str, obj.str);
//CAT_TODO:加上\符号的转义测试
// str_with_line = @"abcde\\a\\bfghijk";
// str = "return {str=\""+str_with_line+"\"}";
// Debug.LogFormat("LuaUtilityTest[191:04] str:{0}", str);
// obj = LuaUtility.FromLua<TestLuaString>(str);
// Assert.IsNotNull(obj);
// //注意expect_str是故意和str_with_line不一样的,因为lua会把\符号给忽略掉或者转义
// expect_str = @"abcdefghijk";
// Assert.AreEqual(expect_str, obj.str);
string test_str_mut = @"123456
多行字符串1
多行字符串2
654321";
str = @"return {str=[["+test_str_mut+"]]}";
obj = LuaUtility.FromLua<TestLuaString>(str);
Assert.IsNotNull(obj);
Assert.AreEqual(test_str_mut, obj.str);
}
[Test]
public void TestFromLuaBase()
{
var obj = LuaUtility.FromLua<TestDataBase>(TestLexStateStr1);
TestDataBase(obj);
}
[Test]
public void TestToLuaBase()
{
var data = new TestDataBase();
data.int_val = 1;
data.float_val = 2.34;
data.str_val = TestStr;
data.bool_val = true;
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("int_val = 1,"));
Assert.IsTrue(str.Contains("float_val = 2.34,"));
Assert.IsTrue(str.Contains("str_val = \""+TestStr));
Assert.IsTrue(str.Contains("bool_val = true,"));
}
public void TestDataBase(TestDataBase obj)
{
Assert.IsNotNull(obj);
Assert.AreEqual(123, obj.int_val);
Assert.AreEqual(4.56789, obj.float_val);
Assert.AreEqual(TestStr, obj.str_val);
Assert.AreEqual(true, obj.bool_val);
}
[Test]
public void TestFromLuaInlay()
{
string test_code = "return { id = 987, data_int = { i = 23847, }, data_base = "+TestLexStateBaseDataStr+", content = '"+TestStr+"' }";
var obj = LuaUtility.FromLua<TestDataInlay>(test_code);
Assert.IsNotNull(obj);
Assert.AreEqual(987, obj.id);
Assert.AreEqual(TestStr, obj.content);
TestDataBase(obj.data_base);
Assert.IsNotNull(obj.data_int);
Assert.AreEqual(23847, obj.data_int.i);
}
[Test]
public void TestToLuaInlay()
{
var data = new TestDataInlay();
data.id = 1;
data.data_int = new TestLuaInt();
data.data_int.i = 2;
data.content = TestStr;
data.data_base = new TestDataBase();
data.data_base.int_val = 3;
data.data_base.float_val = 4.56;
data.data_base.str_val = TestStr;
data.data_base.bool_val = false;
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("id = 1,"));
Assert.IsTrue(str.Contains("data_int = {"));
Assert.IsTrue(str.Contains("i = 2,"));
Assert.IsTrue(str.Contains("data_base = {"));
Assert.IsTrue(str.Contains("int_val = 3,"));
Assert.IsTrue(str.Contains("float_val = 4.56,"));
Assert.IsTrue(str.Contains("str_val = \""+TestStr));
Assert.IsTrue(str.Contains("bool_val = false,"));
Assert.IsTrue(str.Contains("content = \""+TestStr));
}
[Test]
public void TestFromLuaList()
{
string test_code = "return {nums={0,1,2,3,4,5,6,7,8,9,10}}";
var obj = LuaUtility.FromLua<TestDataList>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.nums);
Assert.AreEqual(11, obj.nums.Count);
for (int i = 0; i < 11; i++)
{
Assert.AreEqual(i, obj.nums[i]);
}
}
[Test]
public void TestToLuaList()
{
var data = new TestDataList();
data.nums = new List<int>();
data.nums.Add(1);
data.nums.Add(21);
data.nums.Add(621);
data.nums.Add(2331);
data.nums.Add(0);
data.nums.Add(564);
data.nums.Add(45643);
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("nums = {"));
int lastIndex = -1;
for (int i = 0; i < data.nums.Count; i++)
{
var newIndex = str.IndexOf(data.nums[i]+",");
Assert.IsTrue(newIndex!=-1);
Assert.IsTrue(newIndex > lastIndex);
lastIndex = newIndex;
}
}
private void SwitchListValue(List<string> list, int a, int b)
{
var temp = list[a];
list[a] = list[b];
list[b] = temp;
}
[Test]
public void TestFromLuaList2()
{
var test_list = new List<int>(){8,6,5,3,4,2,1,7,0,9,10};
string test_code = "return {nums={";
var str_list = new List<string>();
for (int i = 0; i < test_list.Count; i++)
{
str_list.Add("["+(i+1)+"] = "+test_list[i]+", ");
}
//打乱顺序
SwitchListValue(str_list, 0, 3);
SwitchListValue(str_list, 1, 6);
SwitchListValue(str_list, 2, 9);
SwitchListValue(str_list, 4, 7);
foreach (var str in str_list)
{
test_code += str;
}
test_code += "}}";
var obj = LuaUtility.FromLua<TestDataList>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.nums);
Assert.AreEqual(test_list.Count, obj.nums.Count);
for (int i = 0; i < test_list.Count; i++)
{
Assert.AreEqual(test_list[i], obj.nums[i]);
}
}
[Test]
public void TestFromLuaListTable()
{
string test_code = "return {data={{i=1}, {i=2}, {i=3}, {i=4}, {i=5}, {i=6}, {i=7}, {i=8}, {i=9}, {i=10}, {i=11}, }, }";
var obj = LuaUtility.FromLua<TestDataListTable>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.data);
Assert.AreEqual(11, obj.data.Count);
for (int i = 0; i < 11; i++)
{
Assert.AreEqual(i+1, obj.data[i].i);
}
}
private void AssertFloat(double expected, double actual)
{
var isEqual = Mathf.Abs((float)(expected - actual)) < 0.0000001;
if (!isEqual)
Assert.AreEqual(expected, actual);
}
[Test]
public void TestFromLuaUnity()
{
string test_code = "return {vec3={x=1.2, y=3.45, z=6.7890}, vec2={x=1, y=2}, rota={x=1,y=2,z=3,w=4}, color={r=1,g=2,b=3,a=4}}";
var obj = LuaUtility.FromLua<TestLuaUnity>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.vec2);
Assert.IsNotNull(obj.vec3);
Assert.IsNotNull(obj.color);
Assert.IsNotNull(obj.rota);
AssertFloat(1.2, obj.vec3.x);
AssertFloat(3.45, obj.vec3.y);
AssertFloat(6.7890, obj.vec3.z);
AssertFloat(1, obj.vec2.x);
AssertFloat(2, obj.vec2.y);
AssertFloat(1, obj.rota.x);
AssertFloat(2, obj.rota.y);
AssertFloat(3, obj.rota.z);
AssertFloat(4, obj.rota.w);
AssertFloat(1, obj.color.r);
AssertFloat(2, obj.color.g);
AssertFloat(3, obj.color.b);
AssertFloat(4, obj.color.a);
}
[Test]
public void TestToLuaUnity()
{
var data = new TestLuaUnity();
data.vec2 = new Vector2(1, 2);
data.vec3 = new Vector3(3, 4.5f, 6.7f);
data.rota = new Quaternion(9, 8, 7, 6);
data.color = new Color(1, 2, 3, 4);
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("vec2 = {"));
Assert.IsTrue(str.Contains("x = 1,"));
Assert.IsTrue(str.Contains("y = 2,"));
Assert.IsTrue(str.Contains("vec3 = {"));
Assert.IsTrue(str.Contains("x = 3,"));
Assert.IsTrue(str.Contains("y = 4.5,"));
Assert.IsTrue(str.Contains("z = 6.7,"));
Assert.IsTrue(str.Contains("rota = {"));
Assert.IsTrue(str.Contains("eulerAngles = {"));
Assert.IsTrue(str.Contains("x = 359.0035,"));
Assert.IsTrue(str.Contains("y = 105.124,"));
Assert.IsTrue(str.Contains("z = 97.49586,"));
Assert.IsTrue(str.Contains("color = {"));
Assert.IsTrue(str.Contains("r = 1,"));
Assert.IsTrue(str.Contains("g = 2,"));
Assert.IsTrue(str.Contains("b = 3,"));
Assert.IsTrue(str.Contains("a = 4,"));
}
[Test]
public void TestFromLuaDic()
{
string test_code = "return {dic={a=1.2, b=3.45, c=6.7890}}";
var obj = LuaUtility.FromLua<TestDataDicStrFlt>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.dic);
AssertFloat(1.2, obj.dic["a"]);
AssertFloat(3.45, obj.dic["b"]);
AssertFloat(6.7890, obj.dic["c"]);
}
[Test]
public void TestToLuaDic()
{
var data = new TestDataDicStrFlt();
data.dic = new Dictionary<string, double>();
data.dic.Add("a", 1.23);
data.dic.Add("c", 4);
data.dic.Add("d", -5.01);
data.dic.Add("e", 12345);
data.dic.Add("b", -0.3333);
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("dic = {"));
Assert.IsTrue(str.Contains("[\"a\"] = 1.23,"));
Assert.IsTrue(str.Contains("[\"c\"] = 4,"));
Assert.IsTrue(str.Contains("[\"d\"] = -5.01,"));
Assert.IsTrue(str.Contains("[\"e\"] = 12345,"));
Assert.IsTrue(str.Contains("[\"b\"] = -0.3333,"));
}
[Test]
public void TestFromLuaDic2()
{
string test_code = "return {dic={[\"a\"]=1.2, [\'b\']=3.45, [\"c\"]=6.7890}}";
var obj = LuaUtility.FromLua<TestDataDicStrFlt>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.dic);
AssertFloat(1.2, obj.dic["a"]);
AssertFloat(3.45, obj.dic["b"]);
AssertFloat(6.7890, obj.dic["c"]);
}
[Test]
public void TestToLuaDic2()
{
var data = new TestDataDicIntStr();
data.dic = new Dictionary<int, string>();
data.dic.Add(1, "a");
data.dic.Add(3, "c");
data.dic.Add(111, "d");
data.dic.Add(-1234, "e");
data.dic.Add(0, "b");
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("dic = {"));
Assert.IsTrue(str.Contains("[1] = \"a\","));
Assert.IsTrue(str.Contains("[3] = \"c\","));
Assert.IsTrue(str.Contains("[111] = \"d\","));
Assert.IsTrue(str.Contains("[-1234] = \"e\","));
Assert.IsTrue(str.Contains("[0] = \"b\","));
}
[Test]
public void TestToLuaDic3()
{
var data = new TestDataDicIntStr();
data.dic = new Dictionary<int, string>();
data.dic.Add(1, "a");
data.dic.Add(3, "c");
data.dic.Add(111, "d");
data.dic.Add(-1234, "e");
data.dic.Add(0, "b");
var str = LuaUtility.ToLua(data.dic);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("[1] = \"a\","));
Assert.IsTrue(str.Contains("[3] = \"c\","));
Assert.IsTrue(str.Contains("[111] = \"d\","));
Assert.IsTrue(str.Contains("[-1234] = \"e\","));
Assert.IsTrue(str.Contains("[0] = \"b\","));
}
[Test]
public void TestFromLuaDic3()
{
string test_code = "return {dic={[3]=\"haha\", [1234]=\"abcdefg_ ieow\", [555]=\"uwioerlskdjfo:123342\\354-=|\"}}";
var obj = LuaUtility.FromLua<TestDataDicIntStr>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.dic);
Assert.AreEqual("haha", obj.dic[3]);
Assert.AreEqual("abcdefg_ ieow", obj.dic[1234]);
Assert.AreEqual("uwioerlskdjfo:123342\\354-=|", obj.dic[555]);
test_code = "return {dic={\"haha\", \"abcdefg_ ieow\", [555]=\"uwioerlskdjfo:123342\\354-=|\"}}";
LuaUtility.IsShowLog = true;
obj = LuaUtility.FromLua<TestDataDicIntStr>(test_code);
LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.dic);
Assert.AreEqual("haha", obj.dic[1]);
Assert.AreEqual("abcdefg_ ieow", obj.dic[2]);
Assert.AreEqual("uwioerlskdjfo:123342\\354-=|", obj.dic[555]);
}
[Test]
public void TestFromLuaDic4()
{
string test_code = "return {[0]=\"haha\", \"abcdefg_ ieow\", \"uwioerlskdjfo:123342\\354-=|\"}";
LuaUtility.IsShowLog = true;
var obj = LuaUtility.FromLua<Dictionary<int,string>>(test_code);
LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.IsTrue(obj.ContainsKey(0));
Assert.IsTrue(obj.ContainsKey(1));
Assert.IsTrue(obj.ContainsKey(2));
Assert.AreEqual("haha", obj[0]);
Assert.AreEqual("abcdefg_ ieow", obj[1]);
Assert.AreEqual("uwioerlskdjfo:123342\\354-=|", obj[2]);
test_code = "return {[0]=\"00\", [2]=\"22\", \"11\"}";
LuaUtility.IsShowLog = true;
obj = LuaUtility.FromLua<Dictionary<int,string>>(test_code);
LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.IsTrue(obj.ContainsKey(0));
Assert.IsTrue(obj.ContainsKey(1));
Assert.IsTrue(obj.ContainsKey(2));
Assert.AreEqual("00", obj[0]);
Assert.AreEqual("11", obj[1]);
Assert.AreEqual("22", obj[2]);
test_code = "return {[0]=\"00\", [5]=\"55\", [11]=\"1111\", \"11\", \"22\", [3]=\"33\"}";
LuaUtility.IsShowLog = true;
obj = LuaUtility.FromLua<Dictionary<int,string>>(test_code);
LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.IsTrue(obj.ContainsKey(0));
Assert.IsTrue(obj.ContainsKey(1));
Assert.IsTrue(obj.ContainsKey(2));
Assert.IsTrue(obj.ContainsKey(3));
Assert.IsTrue(obj.ContainsKey(5));
Assert.IsTrue(obj.ContainsKey(11));
Assert.AreEqual("00", obj[0]);
Assert.AreEqual("11", obj[1]);
Assert.AreEqual("22", obj[2]);
Assert.AreEqual("33", obj[3]);
Assert.AreEqual("55", obj[5]);
Assert.AreEqual("1111", obj[11]);
}
[Test]
public void TestFromLuaEnum()
{
string test_code = "return {e=1, E=3}";
var obj = LuaUtility.FromLua<TestDataEnum>(test_code);
Assert.IsNotNull(obj);
Assert.IsNotNull(obj.e);
Assert.AreEqual(TestEnum.Haha, obj.e);
Assert.AreEqual(TestEnum2.D, obj.E);
}
[Test]
public void TestToLuaEnum()
{
var data = new TestDataEnum();
data.e = TestEnum.Haha;
data.E = TestEnum2.D;
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
Assert.IsTrue(str.Contains("e = 1,"));
Assert.IsTrue(str.Contains("E = 3,"));
}
[Test]
public void TestToLuaPolymorphic()
{
var data = new List<TestAnimal>();
data.Add(new TestDog());
data.Add(new TestCat());
var str = LuaUtility.ToLua(data);
Assert.IsNotNull(str);
var dog_type_name = typeof(TestDog).FullName+", "+typeof(TestDog).Assembly.GetName().Name;
var cat_type_name = typeof(TestCat).FullName+", "+typeof(TestCat).Assembly.GetName().Name;
Assert.IsTrue(str.Contains(string.Format("[\"$type\"] = \"{0}\"", dog_type_name)));
Assert.IsTrue(str.Contains(string.Format("[\"$type\"] = \"{0}\"", cat_type_name)));
Assert.IsFalse(str.Contains("TestAnimal"));
}
//测试多态
[Test]
public void TestFromLuaPolymorphic()
{
var dog_type_name = typeof(TestDog).AssemblyQualifiedName;
var dog_code = "{[\"$type\"] = \""+dog_type_name+"\", type=\"dog\", can_run=true}";
var cat_type_name = typeof(TestCat).AssemblyQualifiedName;
var cat_code = "{[\"$type\"] = \""+cat_type_name+"\", type=\"cat\", life=9}";
string test_code = "return {"+dog_code+", "+cat_code+"}";
Debug.LogFormat("LuaUtilityTest[574:49] test_code:{0}", test_code);
// LuaUtility.IsShowLog = true;
var obj = LuaUtility.FromLua<List<TestAnimal>>(test_code);
// LuaUtility.IsShowLog = false;
Assert.IsNotNull(obj);
Assert.IsNotNull(obj[0]);
Assert.IsNotNull(obj[1]);
Assert.AreEqual(obj[0].GetType(), typeof(TestDog));
Assert.AreEqual(obj[1].GetType(), typeof(TestCat));
Assert.AreEqual(obj[0].type, "dog");
Assert.AreEqual(obj[1].type, "cat");
Assert.AreEqual((obj[0] as TestDog).can_run, true);
Assert.AreEqual((obj[1] as TestCat).life, 9);
}
[Test]
public void TestMultiLineStr()
{
var test_str = @"1.哈哈哈
2。行要 国为\n3.line33333
4.asdfes
5.seirewsl
6。槈吸灯这;要
";
var obj = new TestLuaString(){str=test_str};
var code = LuaUtility.ToLua(obj);
Debug.LogFormat("LuaUtilityTest[604:50] code:{0}", code);
var obj_new = LuaUtility.FromLua<TestLuaString>("return "+code);
Assert.IsNotNull(obj_new);
Assert.AreEqual(test_str, obj_new.str);
}
[Test]
public void TestToLuaStaticArray()
{
var obj = new TestDoubleStaticArray();
for (int i = 0; i < obj.ds.Length; i++)
{
obj.ds[i] = i;
}
var code = LuaUtility.ToLua(obj);
Debug.LogFormat("LuaUtilityTest[604:50 TestToLuaStaticArray] code:{0}", code);
for (int i = 0; i < obj.ds.Length; i++)
{
Assert.IsTrue(code.Contains(string.Format("{0},", i)));
}
}
[Test]
public void TestFromLuaStaticArray()
{
var code = @"{ds={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}";
var obj_new = LuaUtility.FromLua<TestDoubleStaticArray>("return "+code);
Assert.IsNotNull(obj_new);
Assert.AreEqual(12, obj_new.ds.Length);
for (int i = 0; i < obj_new.ds.Length; i++)
{
Assert.AreEqual(i, obj_new.ds[i]);
}
var code2 = @"{ds={12, 33.80, 55.123, }}";
var obj_new2 = LuaUtility.FromLua<TestDoubleStaticArray>("return "+code2);
Assert.IsNotNull(obj_new2);
Assert.AreEqual(3, obj_new2.ds.Length);
AssertFloat(12, obj_new2.ds[0]);
AssertFloat(33.80, obj_new2.ds[1]);
AssertFloat(55.123, obj_new2.ds[2]);
}
}
public class TestLuaInt
{
public int i;
}
public class TestLuaFloat
{
public float f;
}
public class TestLuaString
{
public string str;
}
public class TestLuaUnity
{
public Vector2 vec2;
public Vector3 vec3;
public Quaternion rota;
public Color color;
}
public class TestDataBase
{
public int int_val;
public double float_val;
public string str_val;
public bool bool_val;
}
public class TestDataInlay
{
public int id;
public TestLuaInt data_int;
public TestDataBase data_base;
public string content;
}
public class TestDataList
{
public List<int> nums;
}
public class TestDataListTable
{
public List<TestLuaInt> data;
}
public class TestDoubleStaticArray
{
public double[] ds = new double[12];
}
public class TestDataDicStrFlt
{
public Dictionary<string, double> dic;
}
public class TestDataDicIntStr
{
public Dictionary<int, string> dic;
}
public enum TestEnum
{
None = 0,
Haha = 1,
P = 3,
}
public enum TestEnum2
{
A = 0,
B = 1,
C = 2,
D = 3,
}
public class TestDataEnum
{
public TestEnum e;
private TestEnum2 e2;
public TestEnum2 E
{
get {return e2;}
set {e2 = value;}
}
}
//测试多态
public class TestAnimal
{
public string type = "Animal";
}
public class TestDog : TestAnimal
{
public bool can_run = true;
public TestDog()
{
type = "Dog";
}
}
public class TestCat : TestAnimal
{
public int life = 9;
public TestCat()
{
type = "Cat";
}
}
}