暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FastAssert.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using NUnit.Framework;
  4. /// <summary>
  5. /// This class mirrors parts of the Assert API from NUnit in a sane way.
  6. /// The problem with NUnit is that stuff like Assert.AreEqual(15, 16) creates allocations behind the scenes, so tests
  7. /// that checks large collections will spend the vast majority of their time just checking their results.
  8. /// You can use this by writing
  9. /// using Assert = FastAssert;
  10. /// at the top of your file. There are some parts of the API that you may need to fix up manually, mainly because this
  11. /// class does not expose overloads like Assert.AreEqual(object a, object b) because that's just asking for pain.
  12. /// </summary>
  13. internal static class FastAssert
  14. {
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static void IsTrue(bool b)
  17. {
  18. if (!b)
  19. {
  20. Assert.IsTrue(b);
  21. }
  22. }
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public static void IsTrue(bool b, string msg)
  25. {
  26. if (!b)
  27. {
  28. Assert.IsTrue(b, msg);
  29. }
  30. }
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public static void True(bool b)
  33. {
  34. if (!b)
  35. {
  36. Assert.IsTrue(b);
  37. }
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static void True(bool b, string msg)
  41. {
  42. if (!b)
  43. {
  44. Assert.IsTrue(b, msg);
  45. }
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public static void IsFalse(bool b)
  49. {
  50. if (b)
  51. {
  52. Assert.IsFalse(b);
  53. }
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public static void IsFalse(bool b, string msg)
  57. {
  58. if (b)
  59. {
  60. Assert.IsFalse(b, msg);
  61. }
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public static void False(bool b)
  65. {
  66. if (b)
  67. {
  68. Assert.IsFalse(b);
  69. }
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public static void False(bool b, string msg)
  73. {
  74. if (b)
  75. {
  76. Assert.IsFalse(b, msg);
  77. }
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. public static void AreEqual<T>(in T a, in T b) where T : IEquatable<T>
  81. {
  82. if (!a.Equals(b))
  83. {
  84. Assert.Fail($"{a} != {b}");
  85. }
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static void AreEqual<T>(in T a, in T b, string msg) where T : IEquatable<T>
  89. {
  90. if (!a.Equals(b))
  91. {
  92. Assert.Fail($"{a} != {b}: {msg}");
  93. }
  94. }
  95. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  96. public static void AreNotEqual<T>(in T a, in T b) where T : IEquatable<T>
  97. {
  98. if (a.Equals(b))
  99. {
  100. Assert.Fail($"{a} == {b}");
  101. }
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public static void AreNotEqual<T>(in T a, in T b, string msg) where T : IEquatable<T>
  105. {
  106. if (a.Equals(b))
  107. {
  108. Assert.Fail($"{a} == {b}: {msg}");
  109. }
  110. }
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. public static void LessOrEqual<T>(in T a, in T b) where T : IComparable<T>
  113. {
  114. if (a.CompareTo(b) > 0)
  115. {
  116. Assert.Fail($"{a} > {b}");
  117. }
  118. }
  119. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  120. public static void LessOrEqual<T>(in T a, in T b, string msg) where T : IComparable<T>
  121. {
  122. if (a.CompareTo(b) > 0)
  123. {
  124. Assert.Fail($"{a} > {b}: {msg}");
  125. }
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. public static void Less<T>(in T a, in T b) where T : IComparable<T>
  129. {
  130. if (a.CompareTo(b) >= 0)
  131. {
  132. Assert.Fail($"{a} >= {b}");
  133. }
  134. }
  135. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  136. public static void Less<T>(in T a, in T b, string msg) where T : IComparable<T>
  137. {
  138. if (a.CompareTo(b) >= 0)
  139. {
  140. Assert.Fail($"{a} >= {b}: {msg}");
  141. }
  142. }
  143. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  144. public static void GreaterOrEqual<T>(in T a, in T b) where T : IComparable<T>
  145. {
  146. if (a.CompareTo(b) < 0)
  147. {
  148. Assert.Fail($"{a} < {b}");
  149. }
  150. }
  151. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  152. public static void GreaterOrEqual<T>(in T a, in T b, string msg) where T : IComparable<T>
  153. {
  154. if (a.CompareTo(b) < 0)
  155. {
  156. Assert.Fail($"{a} < {b}: {msg}");
  157. }
  158. }
  159. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  160. public static void Greater<T>(in T a, in T b) where T : IComparable<T>
  161. {
  162. if (a.CompareTo(b) <= 0)
  163. {
  164. Assert.Fail($"{a} <= {b}");
  165. }
  166. }
  167. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  168. public static void Greater<T>(in T a, in T b, string msg) where T : IComparable<T>
  169. {
  170. if (a.CompareTo(b) <= 0)
  171. {
  172. Assert.Fail($"{a} <= {b}: {msg}");
  173. }
  174. }
  175. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  176. public static void DoesNotThrow(TestDelegate del)
  177. {
  178. Assert.DoesNotThrow(del);
  179. }
  180. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  181. public static void Throws<T>(TestDelegate del) where T : Exception
  182. {
  183. Assert.Throws<T>(del);
  184. }
  185. }