Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MbUnit.Framework;
- using System.Threading;
- namespace WatiNTestProject
- {
- public class LambdAssert
- {
- #region Helpers
- private int _msWait = 2500;
- private int _msInterval = 500;
- public LambdAssert WaitFor(int ms)
- {
- _msWait = ms;
- return this;
- }
- public LambdAssert WaitForInterval(int ms)
- {
- _msInterval = ms;
- return this;
- }
- #endregion
- #region ___Now
- public LambdAssert IsTrueNow(bool condition)
- {
- Assert.IsTrue(condition);
- return this;
- }
- public LambdAssert IsFalseNow(bool condition)
- {
- Assert.IsFalse(condition);
- return this;
- }
- public LambdAssert EqualToNow<T>(T o1, T o2)
- {
- Assert.AreEqual(o1, o2);
- return this;
- }
- public LambdAssert NotEqualToNow<T>(T o1, T o2)
- {
- Assert.AreNotEqual(o1, o2);
- return this;
- }
- public LambdAssert DoNow(Action act)
- {
- act();
- return this;
- }
- #endregion
- #region The Good Stuff
- public LambdAssert IsTrue(Func<bool> condition)
- {
- DateTime start = DateTime.Now;
- bool cond;
- while (!(cond = condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
- Thread.Sleep(_msInterval);
- Assert.IsTrue(cond);
- return this;
- }
- public LambdAssert IsFalse(Func<bool> condition)
- {
- DateTime start = DateTime.Now;
- bool cond;
- while ((cond = condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
- Thread.Sleep(_msInterval);
- Assert.IsFalse(cond);
- return this;
- }
- public LambdAssert EqualTo<T>(T expectedValue, Func<T> call) where T : IComparable
- {
- DateTime start = DateTime.Now;
- while (!expectedValue.Equals(call()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
- Thread.Sleep(_msInterval);
- Assert.AreEqual(expectedValue, call());
- return this;
- }
- public LambdAssert NotEqualTo<T>(T expectedValue, Func<T> call) where T : IComparable
- {
- DateTime start = DateTime.Now;
- while (expectedValue.Equals(call()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
- Thread.Sleep(_msInterval);
- Assert.AreNotEqual(expectedValue, call());
- return this;
- }
- ////////////////////////////////////////////////
- // Wait stuff below:
- public LambdAssert Wait(Func<bool> waitFor)
- {
- return Wait(waitFor, _msWait);
- }
- public LambdAssert Wait(Func<bool> waitFor, int duration)
- {
- DateTime start = DateTime.Now;
- while (!waitFor() && DateTime.Now.Subtract(start).TotalMilliseconds < duration)
- Thread.Sleep(_msInterval);
- return this;
- }
- public LambdAssert Wait(Func<bool> waitFor, Action failAction)
- {
- return Wait(waitFor, failAction, _msWait);
- }
- public LambdAssert Wait(Func<bool> waitFor, Action failAction, int duration)
- {
- DateTime start = DateTime.Now;
- while (!waitFor() && DateTime.Now.Subtract(start).TotalMilliseconds < duration)
- Thread.Sleep(_msInterval);
- if (!waitFor())
- failAction();
- return this;
- }
- ////////////////////////////////////////////////
- // Various ___Delay options below:
- public LambdAssert ConditionalDelay(Action call, Func<bool> condition)
- {
- DateTime start = DateTime.Now;
- while ((condition == null || !condition()) && DateTime.Now.Subtract(start).TotalMilliseconds < _msWait)
- Thread.Sleep(_msInterval);
- call();
- return this;
- }
- public LambdAssert StaticDelay(Action call, int duration)
- {
- Thread.Sleep(duration);
- call();
- return this;
- }
- public LambdAssert NormalDelay(Action call)
- {
- Thread.Sleep(_msWait);
- call();
- return this;
- }
- #endregion
- }
- public abstract class LambdAssertable
- {
- protected LambdAssert LambdAssert = new LambdAssert();
- }
- }
Add Comment
Please, Sign In to add comment