UnitTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BansheeEngine;
  8. using DebugUnit = System.Diagnostics.Debug;
  9. namespace BansheeEditor
  10. {
  11. class UnitTests
  12. {
  13. static void UnitTest1_ManagedSerialization()
  14. {
  15. SceneObject otherSO = new SceneObject("OtherSO");
  16. UT1_Component2 dbgComponent2 = otherSO.AddComponent<UT1_Component2>();
  17. dbgComponent2.a2 = 33;
  18. SceneObject so = new SceneObject("TestSO");
  19. UT1_Component1 dbgComponent = so.AddComponent<UT1_Component1>();
  20. dbgComponent.a = 5;
  21. dbgComponent.b = "SomeTestVal";
  22. dbgComponent.complex.someValue = 19;
  23. dbgComponent.complex.anotherValue = "AnotherValue";
  24. dbgComponent.complex2.someValue2 = 21;
  25. dbgComponent.complex2.anotherValue2 = "AnotherValue2";
  26. dbgComponent.arrA = new int[5];
  27. dbgComponent.arrA[4] = 5;
  28. dbgComponent.arrB = new string[5];
  29. dbgComponent.arrB[4] = "ArrAnotherValue";
  30. dbgComponent.arrComplex = new UT1_SerzObj[5];
  31. dbgComponent.arrComplex[4].someValue = 99;
  32. dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
  33. dbgComponent.arrComplex2 = new UT1_SerzCls[5];
  34. dbgComponent.arrComplex2[4] = new UT1_SerzCls();
  35. dbgComponent.arrComplex2[4].someValue2 = 101;
  36. dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";
  37. dbgComponent.listA = new List<int>();
  38. dbgComponent.listA.Add(5);
  39. dbgComponent.listB = new List<string>();
  40. dbgComponent.listB.Add("ListAnotherValue");
  41. dbgComponent.listB.Add(null);
  42. dbgComponent.listComplex = new List<UT1_SerzObj>();
  43. dbgComponent.listComplex.Add(new UT1_SerzObj());
  44. dbgComponent.listComplex.Add(new UT1_SerzObj(99, "ListComplexAnotherValue"));
  45. dbgComponent.listComplex2 = new List<UT1_SerzCls>();
  46. dbgComponent.listComplex2.Add(new UT1_SerzCls());
  47. dbgComponent.listComplex2[0].someValue2 = 101;
  48. dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
  49. dbgComponent.listComplex2.Add(null);
  50. dbgComponent.otherComponent = dbgComponent2;
  51. dbgComponent.otherSO = otherSO;
  52. Internal_UT1_GameObjectClone(so);
  53. System.Diagnostics.Debug.Assert(so.GetNumChildren() == 1);
  54. for (int i = 0; i < so.GetNumChildren(); i++)
  55. {
  56. SceneObject childSO = so.GetChild(i);
  57. UT1_Component1 otherComponent = childSO.GetComponent<UT1_Component1>();
  58. DebugUnit.Assert(otherComponent.a == 5);
  59. DebugUnit.Assert(otherComponent.b == "SomeTestVal");
  60. DebugUnit.Assert(otherComponent.complex.someValue == 19);
  61. DebugUnit.Assert(otherComponent.complex2.anotherValue2 == "AnotherValue2");
  62. DebugUnit.Assert(otherComponent.arrA[4] == 5);
  63. DebugUnit.Assert(otherComponent.arrB[4] == "ArrAnotherValue");
  64. DebugUnit.Assert(otherComponent.arrComplex[4].someValue == 99);
  65. DebugUnit.Assert(otherComponent.arrComplex2[4].anotherValue2 == "ArrComplex2AnotherValue");
  66. DebugUnit.Assert(otherComponent.listA[0] == 5);
  67. DebugUnit.Assert(otherComponent.listB[0] == "ListAnotherValue");
  68. DebugUnit.Assert(otherComponent.listComplex[1].someValue == 99);
  69. DebugUnit.Assert(otherComponent.listComplex2[0].anotherValue2 == "ListComplexAnotherValue");
  70. }
  71. }
  72. static void UnitTest2_SerializableProperties()
  73. {
  74. SerializableObject obj = new SerializableObject(typeof(UT1_SerzCls), new UT1_SerzCls());
  75. Debug.Log(obj.fields.Length);
  76. for (int i = 0; i < obj.fields.Length; i++)
  77. {
  78. Debug.Log(i + ". " + obj.fields[i].Name + " - " + obj.fields[i].Type.ToString());
  79. }
  80. SerializableProperty prop = obj.fields[0].GetProperty();
  81. Debug.Log("Old value: " + prop.GetValue<int>());
  82. prop.SetValue<int>(33);
  83. Debug.Log("New value: " + prop.GetValue<int>());
  84. SerializableProperty prop2 = obj.fields[2].GetProperty();
  85. Debug.Log("Old value: " + (prop2.GetValue<UT1_SerzCls>() == null));
  86. UT1_SerzCls child = new UT1_SerzCls();
  87. child.anotherValue2 = "potato";
  88. prop2.SetValue<UT1_SerzCls>(child);
  89. if (prop2.GetValue<UT1_SerzCls>() == null)
  90. Debug.Log("New value: null");
  91. else
  92. Debug.Log("New value: " + prop2.GetValue<UT1_SerzCls>().anotherValue2);
  93. }
  94. static void UnitTest3_ManagedDiff()
  95. {
  96. UT_DiffObj original = new UT_DiffObj();
  97. UT_DiffObj modified = new UT_DiffObj();
  98. modified.plain2 = "banana";
  99. modified.complex = new UT_DiffChildObj();
  100. modified.complex2 = null;
  101. modified.complex3.plain2 = "tomato";
  102. modified.arrPlain1 = new[] {-1, -2, -3, -4};
  103. modified.arrPlain2[2] = "cherry";
  104. modified.arrComplex = new UT_DiffChildObj[3];
  105. modified.arrComplex2[0].plain1 = -10;
  106. modified.listPlain1[0] = -20;
  107. modified.listPlain2 = new List<string>();
  108. modified.listComplex = new List<UT_DiffChildObj>();
  109. modified.listComplex.Add(new UT_DiffChildObj());
  110. modified.listComplex2[1].plain2 = "orange";
  111. modified.dictPlain1.Remove(20);
  112. modified.dictPlain1[-30] = -30;
  113. modified.dictComplex = new Dictionary<int, UT_DiffChildObj>();
  114. modified.dictComplex[-40] = new UT_DiffChildObj();
  115. modified.dictComplex2[31].plain1 = -50;
  116. Internal_UT3_GenerateDiff(original, modified);
  117. Internal_UT3_ApplyDiff(original);
  118. DebugUnit.Assert(original.plain1 == modified.plain1);
  119. DebugUnit.Assert(original.plain2 == modified.plain2);
  120. DebugUnit.Assert(original.complex.plain2 == modified.complex.plain2);
  121. DebugUnit.Assert(original.complex2 == modified.complex2);
  122. DebugUnit.Assert(original.complex3.plain2 == modified.complex3.plain2);
  123. DebugUnit.Assert(original.arrPlain1.Length == modified.arrPlain1.Length);
  124. for (int i = 0; i < original.arrPlain1.Length; i++)
  125. DebugUnit.Assert(original.arrPlain1[i] == modified.arrPlain1[i]);
  126. for (int i = 0; i < original.arrPlain2.Length; i++)
  127. DebugUnit.Assert(original.arrPlain2[i] == modified.arrPlain2[i]);
  128. for (int i = 0; i < original.arrComplex.Length; i++)
  129. DebugUnit.Assert(original.arrComplex[i] == modified.arrComplex[i]);
  130. DebugUnit.Assert(original.arrComplex2[0].plain1 == modified.arrComplex2[0].plain1);
  131. for (int i = 0; i < original.listPlain1.Count; i++)
  132. DebugUnit.Assert(original.listPlain1[i] == modified.listPlain1[i]);
  133. DebugUnit.Assert(original.listPlain2.Count == modified.listPlain2.Count);
  134. for (int i = 0; i < original.listComplex.Count; i++)
  135. DebugUnit.Assert(original.listComplex[i].plain1 == modified.listComplex[i].plain1);
  136. DebugUnit.Assert(original.listComplex2[1].plain2 == modified.listComplex2[1].plain2);
  137. foreach (var entry in modified.dictPlain1)
  138. {
  139. if (!original.dictPlain1.ContainsKey(entry.Key))
  140. DebugUnit.Assert(false);
  141. DebugUnit.Assert(entry.Value == original.dictPlain1[entry.Key]);
  142. }
  143. foreach (var entry in modified.dictPlain2)
  144. {
  145. if (!original.dictPlain2.ContainsKey(entry.Key))
  146. DebugUnit.Assert(false);
  147. DebugUnit.Assert(entry.Value == original.dictPlain2[entry.Key]);
  148. }
  149. foreach (var entry in modified.dictComplex)
  150. {
  151. if (!original.dictComplex.ContainsKey(entry.Key))
  152. DebugUnit.Assert(false);
  153. DebugUnit.Assert(entry.Value.plain1 == original.dictComplex[entry.Key].plain1);
  154. }
  155. foreach (var entry in modified.dictComplex2)
  156. {
  157. if (!original.dictComplex2.ContainsKey(entry.Key))
  158. DebugUnit.Assert(false);
  159. DebugUnit.Assert(entry.Value.plain1 == original.dictComplex2[entry.Key].plain1);
  160. }
  161. }
  162. static void RunTests()
  163. {
  164. UnitTest1_ManagedSerialization();
  165. UnitTest2_SerializableProperties();
  166. UnitTest3_ManagedDiff();
  167. }
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern void Internal_UT1_GameObjectClone(SceneObject so);
  170. [MethodImpl(MethodImplOptions.InternalCall)]
  171. private static extern void Internal_UT3_GenerateDiff(UT_DiffObj oldObj, UT_DiffObj newObj);
  172. [MethodImpl(MethodImplOptions.InternalCall)]
  173. private static extern void Internal_UT3_ApplyDiff(UT_DiffObj obj);
  174. }
  175. }