Assertions.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEngine;
  2. using Object = UnityEngine.Object;
  3. namespace UnityTest
  4. {
  5. public static class Assertions
  6. {
  7. public static void CheckAssertions ()
  8. {
  9. var assertions = Object.FindObjectsOfType (typeof (AssertionComponent)) as AssertionComponent[];
  10. CheckAssertions (assertions);
  11. }
  12. public static void CheckAssertions (AssertionComponent assertion)
  13. {
  14. CheckAssertions (new[] {assertion});
  15. }
  16. public static void CheckAssertions (GameObject gameObject)
  17. {
  18. CheckAssertions (gameObject.GetComponents<AssertionComponent> ());
  19. }
  20. public static void CheckAssertions (AssertionComponent[] assertions)
  21. {
  22. if (!Debug.isDebugBuild)
  23. return;
  24. foreach (var assertion in assertions)
  25. {
  26. assertion.checksPerformed++;
  27. var result = assertion.Action.Compare ();
  28. if (!result)
  29. {
  30. assertion.hasFailed = true;
  31. string message = "";
  32. if (assertion.Action is ComparerBase)
  33. { //needs different message for different comapre to type.
  34. var comparer = assertion.Action as ComparerBase;
  35. message = assertion.name + " assertion failed.\n(" + assertion.Action.go + ")." + assertion.Action.thisPropertyPath + " "
  36. + comparer.compareToType;
  37. switch (comparer.compareToType)
  38. {
  39. case ComparerBase.CompareToType.CompareToObject:
  40. message +=" (" + comparer.other + ")." + comparer.otherPropertyPath + " failed.";
  41. break;
  42. case ComparerBase.CompareToType.CompareToConstantValue:
  43. message += comparer.ConstValue + " failed.";
  44. break;
  45. case ComparerBase.CompareToType.CompareToNull:
  46. message += " failed.";
  47. break;
  48. }
  49. }
  50. else
  51. {
  52. message = assertion.name + " assertion failed.\n(" + assertion.Action.go + ")." + assertion.Action.thisPropertyPath + " failed.";
  53. }
  54. Debug.LogException (new AssertionException (message), assertion);
  55. }
  56. }
  57. }
  58. }
  59. }