UnitTests.cs 10.0 KB

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