Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. using System;
  2. using MbUnit.Framework;
  3. using System.Threading;
  4.  
  5. namespace WatiNTestProject
  6. {
  7. public class LambdAssert
  8. {
  9. #region Helpers
  10.  
  11. private int _msWait = 2500;
  12. private int _msInterval = 500;
  13.  
  14. public LambdAssert WaitFor(int ms)
  15. {
  16. _msWait = ms;
  17. return this;
  18. }
  19. public LambdAssert WaitForInterval(int ms)
  20. {
  21. _msInterval = ms;
  22. return this;
  23. }
  24. #endregion
  25.  
  26. #region ___Now
  27.  
  28. public LambdAssert IsTrueNow(bool condition)
  29. {
  30. Assert.IsTrue(condition);
  31. return this;
  32. }
  33. public LambdAssert IsFalseNow(bool condition)
  34. {
  35. Assert.IsFalse(condition);
  36. return this;
  37. }
  38. public LambdAssert EqualToNow<T>(T o1, T o2)
  39. {
  40. Assert.AreEqual(o1, o2);
  41. return this;
  42. }
  43. public LambdAssert NotEqualToNow<T>(T o1, T o2)
  44. {
  45. Assert.AreNotEqual(o1, o2);
  46. return this;
  47. }
  48.  
  49. public LambdAssert DoNow(Action act)
  50. {
  51. act();
  52. return this;
  53. }
  54. #endregion
  55.  
  56. #region The Good Stuff
  57.  
  58. public LambdAssert IsTrue(Func<bool> condition)
  59. {
  60. DateTime start = DateTime.Now;
  61. bool cond;
  62.  
  63. while (!(cond = condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
  64. Thread.Sleep(_msInterval);
  65.  
  66. Assert.IsTrue(cond);
  67. return this;
  68. }
  69. public LambdAssert IsFalse(Func<bool> condition)
  70. {
  71. DateTime start = DateTime.Now;
  72. bool cond;
  73.  
  74. while ((cond = condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
  75. Thread.Sleep(_msInterval);
  76.  
  77. Assert.IsFalse(cond);
  78. return this;
  79. }
  80. public LambdAssert EqualTo<T>(T expectedValue, Func<T> call) where T : IComparable
  81. {
  82. DateTime start = DateTime.Now;
  83. while (!expectedValue.Equals(call()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
  84. Thread.Sleep(_msInterval);
  85.  
  86. Assert.AreEqual(expectedValue, call());
  87.  
  88. return this;
  89. }
  90. public LambdAssert NotEqualTo<T>(T expectedValue, Func<T> call) where T : IComparable
  91. {
  92. DateTime start = DateTime.Now;
  93. while (expectedValue.Equals(call()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
  94. Thread.Sleep(_msInterval);
  95.  
  96. Assert.AreNotEqual(expectedValue, call());
  97.  
  98. return this;
  99. }
  100.  
  101. ////////////////////////////////////////////////
  102. // Wait stuff below:
  103.  
  104. public LambdAssert Wait(Func<bool> waitFor)
  105. {
  106. return Wait(waitFor, _msWait);
  107. }
  108. public LambdAssert Wait(Func<bool> waitFor, int duration)
  109. {
  110. DateTime start = DateTime.Now;
  111. while (!waitFor() && DateTime.Now.Subtract(start).TotalMilliseconds < duration)
  112. Thread.Sleep(_msInterval);
  113.  
  114. return this;
  115. }
  116. public LambdAssert Wait(Func<bool> waitFor, Action failAction)
  117. {
  118. return Wait(waitFor, failAction, _msWait);
  119. }
  120. public LambdAssert Wait(Func<bool> waitFor, Action failAction, int duration)
  121. {
  122. DateTime start = DateTime.Now;
  123. while (!waitFor() && DateTime.Now.Subtract(start).TotalMilliseconds < duration)
  124. Thread.Sleep(_msInterval);
  125.  
  126. if (!waitFor())
  127. failAction();
  128.  
  129. return this;
  130. }
  131.  
  132. ////////////////////////////////////////////////
  133. // Various ___Delay options below:
  134.  
  135. public LambdAssert ConditionalDelay(Action call, Func<bool> condition)
  136. {
  137. DateTime start = DateTime.Now;
  138. while ((condition == null || !condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
  139. Thread.Sleep(_msInterval);
  140.  
  141. call();
  142. return this;
  143. }
  144. public LambdAssert StaticDelay(Action call, int duration)
  145. {
  146. Thread.Sleep(duration);
  147. call();
  148. return this;
  149. }
  150. public LambdAssert NormalDelay(Action call)
  151. {
  152. Thread.Sleep(_msWait);
  153. call();
  154. return this;
  155. }
  156. #endregion
  157. }
  158.  
  159. public abstract class LambdAssertable
  160. {
  161. protected LambdAssert LambdAssert = new LambdAssert();
  162. }
  163. }
Add Comment
Please, Sign In to add comment