Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.41 KB | None | 0 0
  1. using System;
  2. using NUnit.Framework;
  3. using NUnit.Framework.Constraints;
  4. using NUnit.Framework.Internal;
  5.  
  6. namespace Basware.P2P.UI.Lib.Common
  7. {
  8. /// <summary>
  9. /// Wrapper for NUnit's Assert class for page objects
  10. ///
  11. /// Main difference is not setting the test as failed if assertion exception
  12. /// is catched later. This enables using Bassert in try-catch blocks.
  13. ///
  14. /// Once all page objects are updated to use a better mechanism for
  15. /// condition checks, this class becomes obsolete.
  16. /// </summary>
  17. public class Bassert
  18. {
  19. #region Direct methods
  20. public static void Fail(string message)
  21. {
  22. Assert.Fail(message);
  23. }
  24.  
  25. public static void Fail(string message, params object[] args)
  26. {
  27. Assert.Fail(message, args);
  28. }
  29. #endregion
  30.  
  31. public static void AreEqual(object expected, object actual)
  32. {
  33. That<object>(actual, (IResolveConstraint) Is.EqualTo(expected), (string) null, (object[]) null);
  34. }
  35.  
  36. public static void AreNotEqual(object expected, object actual)
  37. {
  38. That<object>(actual, (IResolveConstraint) Is.Not.EqualTo(expected), null, null);
  39. }
  40.  
  41. public static void AreNotEqual(object expected,
  42. object actual,
  43. string message,
  44. params object[] args)
  45. {
  46. That<object>(actual, (IResolveConstraint) Is.Not.EqualTo(expected), message, args);
  47. }
  48.  
  49. public static void AreEqual(object expected, object actual, string message, params object[] args)
  50. {
  51. That<object>(actual, (IResolveConstraint) Is.EqualTo(expected), message, args);
  52. }
  53.  
  54. public static void False(bool condition)
  55. {
  56. That<bool>(condition, (IResolveConstraint) Is.False, (string) null, (object[]) null);
  57. }
  58.  
  59. public static void IsFalse(bool condition)
  60. {
  61. That<bool>(condition, (IResolveConstraint) Is.False, (string) null, (object[]) null);
  62. }
  63.  
  64. public static void IsFalse(bool condition, string message, params object[] args)
  65. {
  66. That<bool>(condition, (IResolveConstraint) Is.False, message, args);
  67. }
  68.  
  69. public static void IsTrue(bool condition)
  70. {
  71. That<bool>(condition, (IResolveConstraint) Is.True, (string) null, (object[]) null);
  72. }
  73.  
  74. public static void IsTrue(bool condition, string message, params object[] args)
  75. {
  76. That<bool>(condition, (IResolveConstraint) Is.True, message, args);
  77. }
  78.  
  79. public static void IsNotNull(object anObject)
  80. {
  81. That<object>(anObject, (IResolveConstraint) Is.Not.Null, (string) null, (object[]) null);
  82. }
  83.  
  84.  
  85. public static void NotNull(object anObject)
  86. {
  87. That<object>(anObject, (IResolveConstraint) Is.Null, (string) null, (object[]) null);
  88. }
  89.  
  90. public static void Greater(int arg1, int arg2)
  91. {
  92. That<int>(arg1, (IResolveConstraint) Is.GreaterThan((object) arg2), (string) null, (object[]) null);
  93. }
  94.  
  95. public static void Less(int arg1, int arg2)
  96. {
  97. That<int>(arg1, (IResolveConstraint) Is.LessThan((object) arg2), (string) null, (object[]) null);
  98. }
  99.  
  100. #region Assert.That
  101.  
  102. #region Boolean
  103.  
  104. /// <summary>
  105. /// Asserts that a condition is true. If the condition is false the method throws
  106. /// an <see cref="AssertionException"/>.
  107. /// </summary>
  108. /// <param name="condition">The evaluated condition</param>
  109. /// <param name="message">The message to display if the condition is false</param>
  110. /// <param name="args">Arguments to be used in formatting the message</param>
  111. public static void That(bool condition, string message, params object[] args)
  112. {
  113. That(condition, Is.True, message, args);
  114. }
  115.  
  116. /// <summary>
  117. /// Asserts that a condition is true. If the condition is false the method throws
  118. /// an <see cref="AssertionException"/>.
  119. /// </summary>
  120. /// <param name="condition">The evaluated condition</param>
  121. public static void That(bool condition)
  122. {
  123. That(condition, Is.True, null, null);
  124. }
  125.  
  126. /// <summary>
  127. /// Asserts that a condition is true. If the condition is false the method throws
  128. /// an <see cref="AssertionException"/>.
  129. /// </summary>
  130. /// <param name="condition">The evaluated condition</param>
  131. /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
  132. public static void That(bool condition, Func<string> getExceptionMessage)
  133. {
  134. That(condition, Is.True, getExceptionMessage);
  135. }
  136.  
  137. #endregion
  138.  
  139. #region Lambda returning Boolean
  140.  
  141. /// <summary>
  142. /// Asserts that a condition is true. If the condition is false the method throws
  143. /// an <see cref="AssertionException"/>.
  144. /// </summary>
  145. /// <param name="condition">A lambda that returns a Boolean</param>
  146. /// <param name="message">The message to display if the condition is false</param>
  147. /// <param name="args">Arguments to be used in formatting the message</param>
  148. public static void That(Func<bool> condition, string message, params object[] args)
  149. {
  150. That(condition.Invoke(), Is.True, message, args);
  151. }
  152.  
  153. /// <summary>
  154. /// Asserts that a condition is true. If the condition is false the method throws
  155. /// an <see cref="AssertionException"/>.
  156. /// </summary>
  157. /// <param name="condition">A lambda that returns a Boolean</param>
  158. public static void That(Func<bool> condition)
  159. {
  160. That(condition.Invoke(), Is.True, null, null);
  161. }
  162.  
  163. /// <summary>
  164. /// Asserts that a condition is true. If the condition is false the method throws
  165. /// an <see cref="AssertionException"/>.
  166. /// </summary>
  167. /// <param name="condition">A lambda that returns a Boolean</param>
  168. /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
  169. public static void That(Func<bool> condition, Func<string> getExceptionMessage)
  170. {
  171. That(condition.Invoke(), Is.True, getExceptionMessage);
  172. }
  173.  
  174. #endregion
  175.  
  176. #region ActualValueDelegate
  177.  
  178. /// <summary>
  179. /// Apply a constraint to an actual value, succeeding if the constraint
  180. /// is satisfied and throwing an assertion exception on failure.
  181. /// </summary>
  182. /// <typeparam name="TActual">The Type being compared.</typeparam>
  183. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  184. /// <param name="expr">A Constraint expression to be applied</param>
  185. public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr)
  186. {
  187. That(del, expr.Resolve(), null, null);
  188. }
  189.  
  190. /// <summary>
  191. /// Apply a constraint to an actual value, succeeding if the constraint
  192. /// is satisfied and throwing an assertion exception on failure.
  193. /// </summary>
  194. /// <typeparam name="TActual">The Type being compared.</typeparam>
  195. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  196. /// <param name="expr">A Constraint expression to be applied</param>
  197. /// <param name="message">The message that will be displayed on failure</param>
  198. /// <param name="args">Arguments to be used in formatting the message</param>
  199. public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, string message, params object[] args)
  200. {
  201. var constraint = expr.Resolve();
  202.  
  203. IncrementAssertCount();
  204. var result = constraint.ApplyTo(del);
  205. if (!result.IsSuccess)
  206. ReportFailure(result, message, args);
  207. }
  208.  
  209. /// <summary>
  210. /// Apply a constraint to an actual value, succeeding if the constraint
  211. /// is satisfied and throwing an assertion exception on failure.
  212. /// </summary>
  213. /// <typeparam name="TActual">The Type being compared.</typeparam>
  214. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  215. /// <param name="expr">A Constraint expression to be applied</param>
  216. /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
  217. public static void That<TActual>(
  218. ActualValueDelegate<TActual> del,
  219. IResolveConstraint expr,
  220. Func<string> getExceptionMessage)
  221. {
  222. var constraint = expr.Resolve();
  223.  
  224. IncrementAssertCount();
  225. var result = constraint.ApplyTo(del);
  226. if (!result.IsSuccess)
  227. ReportFailure(result, getExceptionMessage());
  228. }
  229.  
  230. #endregion
  231.  
  232. #region TestDelegate
  233.  
  234. /// <summary>
  235. /// Asserts that the code represented by a delegate throws an exception
  236. /// that satisfies the constraint provided.
  237. /// </summary>
  238. /// <param name="code">A TestDelegate to be executed</param>
  239. /// <param name="constraint">A Constraint expression to be applied</param>
  240. public static void That(TestDelegate code, IResolveConstraint constraint)
  241. {
  242. That(code, constraint, null, null);
  243. }
  244.  
  245. /// <summary>
  246. /// Asserts that the code represented by a delegate throws an exception
  247. /// that satisfies the constraint provided.
  248. /// </summary>
  249. /// <param name="code">A TestDelegate to be executed</param>
  250. /// <param name="constraint">A Constraint expression to be applied</param>
  251. /// <param name="message">The message that will be displayed on failure</param>
  252. /// <param name="args">Arguments to be used in formatting the message</param>
  253. public static void That(TestDelegate code, IResolveConstraint constraint, string message, params object[] args)
  254. {
  255. That((object)code, constraint, message, args);
  256. }
  257.  
  258. /// <summary>
  259. /// Asserts that the code represented by a delegate throws an exception
  260. /// that satisfies the constraint provided.
  261. /// </summary>
  262. /// <param name="code">A TestDelegate to be executed</param>
  263. /// <param name="constraint">A Constraint expression to be applied</param>
  264. /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
  265. public static void That(TestDelegate code, IResolveConstraint constraint, Func<string> getExceptionMessage)
  266. {
  267. That((object)code, constraint, getExceptionMessage);
  268. }
  269.  
  270. #endregion
  271.  
  272. #endregion
  273.  
  274. #region Assert.That<TActual>
  275.  
  276. /// <summary>
  277. /// Apply a constraint to an actual value, succeeding if the constraint
  278. /// is satisfied and throwing an assertion exception on failure.
  279. /// </summary>
  280. /// <typeparam name="TActual">The Type being compared.</typeparam>
  281. /// <param name="actual">The actual value to test</param>
  282. /// <param name="expression">A Constraint expression to be applied</param>
  283. public static void That<TActual>(TActual actual, IResolveConstraint expression)
  284. {
  285. That(actual, expression, null, null);
  286. }
  287.  
  288. /// <summary>
  289. /// Apply a constraint to an actual value, succeeding if the constraint
  290. /// is satisfied and throwing an assertion exception on failure.
  291. /// </summary>
  292. /// <typeparam name="TActual">The Type being compared.</typeparam>
  293. /// <param name="actual">The actual value to test</param>
  294. /// <param name="expression">A Constraint expression to be applied</param>
  295. /// <param name="message">The message that will be displayed on failure</param>
  296. /// <param name="args">Arguments to be used in formatting the message</param>
  297. public static void That<TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
  298. {
  299. var constraint = expression.Resolve();
  300.  
  301. IncrementAssertCount();
  302. var result = constraint.ApplyTo(actual);
  303. if (!result.IsSuccess)
  304. ReportFailure(result, message, args);
  305. }
  306.  
  307. /// <summary>
  308. /// Apply a constraint to an actual value, succeeding if the constraint
  309. /// is satisfied and throwing an assertion exception on failure.
  310. /// </summary>
  311. /// <typeparam name="TActual">The Type being compared.</typeparam>
  312. /// <param name="actual">The actual value to test</param>
  313. /// <param name="expression">A Constraint expression to be applied</param>
  314. /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
  315. public static void That<TActual>(
  316. TActual actual,
  317. IResolveConstraint expression,
  318. Func<string> getExceptionMessage)
  319. {
  320. var constraint = expression.Resolve();
  321.  
  322. IncrementAssertCount();
  323. var result = constraint.ApplyTo(actual);
  324. if (!result.IsSuccess)
  325. ReportFailure(result, getExceptionMessage());
  326. }
  327.  
  328. #endregion
  329.  
  330. #region Assert.ByVal
  331.  
  332. /// <summary>
  333. /// Apply a constraint to an actual value, succeeding if the constraint
  334. /// is satisfied and throwing an assertion exception on failure.
  335. /// Used as a synonym for That in rare cases where a private setter
  336. /// causes a Visual Basic compilation error.
  337. /// </summary>
  338. /// <param name="actual">The actual value to test</param>
  339. /// <param name="expression">A Constraint expression to be applied</param>
  340. public static void ByVal(object actual, IResolveConstraint expression)
  341. {
  342. That(actual, expression, null, null);
  343. }
  344.  
  345. /// <summary>
  346. /// Apply a constraint to an actual value, succeeding if the constraint
  347. /// is satisfied and throwing an assertion exception on failure.
  348. /// Used as a synonym for That in rare cases where a private setter
  349. /// causes a Visual Basic compilation error.
  350. /// </summary>
  351. /// <remarks>
  352. /// This method is provided for use by VB developers needing to test
  353. /// the value of properties with private setters.
  354. /// </remarks>
  355. /// <param name="actual">The actual value to test</param>
  356. /// <param name="expression">A Constraint expression to be applied</param>
  357. /// <param name="message">The message that will be displayed on failure</param>
  358. /// <param name="args">Arguments to be used in formatting the message</param>
  359. public static void ByVal(object actual, IResolveConstraint expression, string message, params object[] args)
  360. {
  361. That(actual, expression, message, args);
  362. }
  363.  
  364. #endregion
  365.  
  366. #region Assert.cs hacks
  367. private static void ReportFailure(ConstraintResult result, string message)
  368. {
  369. ReportFailure(result, message, null);
  370. }
  371.  
  372. private static void ReportFailure(ConstraintResult result, string message, params object[] args)
  373. {
  374. MessageWriter writer = new TextMessageWriter(message, args);
  375. result.WriteMessageTo(writer);
  376.  
  377. ReportFailure(writer.ToString());
  378. }
  379.  
  380. /// <summary>
  381. /// Hack : This method has switched to a throwing method
  382. /// </summary>
  383. /// <param name="message"></param>
  384. private static void ReportFailure(string message)
  385. {
  386. // Record the failure in an <assertion> element
  387. var result = TestExecutionContext.CurrentContext.CurrentResult;
  388.  
  389. // NOTE: These commented lines below are the hack
  390.  
  391. //result.RecordAssertion(AssertionStatus.Failed, message, GetStackTrace());
  392. //result.RecordTestCompletion();
  393. //// If we are outside any multiple assert block, then throw
  394. //if (TestExecutionContext.CurrentContext.MultipleAssertLevel == 0)
  395. // throw new AssertionException(result.Message);
  396.  
  397. throw new AssertionException(message);
  398. }
  399.  
  400. private static void IncrementAssertCount()
  401. {
  402. TestExecutionContext.CurrentContext.IncrementAssertCount();
  403. }
  404. #endregion
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement