/// <summary> /// Test whether the constraint is satisfied by a given reference. /// Overridden to wait for the specified delay period before /// calling the base constraint with the dereferenced value. /// </summary> /// <param name="actual">A reference to the value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(ref TActual actual) { int remainingDelay = delayInMilliseconds; while (pollingInterval > 0 && pollingInterval < remainingDelay) { remainingDelay -= pollingInterval; Thread.Sleep(pollingInterval); try { ConstraintResult result = baseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } catch (Exception) { // Ignore any exceptions when polling } } if (remainingDelay > 0) { Thread.Sleep(remainingDelay); } return(new ConstraintResult(this, actual, baseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for if the base constraint fails, false if it succeeds</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, TimeSpan.FromMilliseconds(delayInMilliseconds)); if (pollingInterval > 0) { long nextPoll = TimestampOffset(now, TimeSpan.FromMilliseconds(pollingInterval)); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { Thread.Sleep((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, TimeSpan.FromMilliseconds(pollingInterval)); ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { Thread.Sleep((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for if the base constraint fails, false if it succeeds</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, DelayInterval.AsTimeSpan); if (PollingInterval.IsNotZero) { long nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Tests whether the object provides the expected attribute. /// </summary> /// <param name="actual">A Type, MethodInfo, or other ICustomAttributeProvider</param> /// <returns>True if the expected attribute is present, otherwise false</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { Guard.ArgumentNotNull(actual, "actual"); Attribute[] attrs = AttributeHelper.GetCustomAttributes(actual, expectedType, true); ConstraintResult result = new ConstraintResult(this, actual); result.Status = attrs.Length > 0 ? ConstraintStatus.Success : ConstraintStatus.Failure; return(result); }
public ThrowsConstraintResult(ThrowsConstraint constraint, Exception caughtException, ConstraintResult baseResult) : base(constraint, caughtException) { if (caughtException != null && baseResult.IsSuccess) { Status = ConstraintStatus.Success; } else { Status = ConstraintStatus.Failure; } this.baseResult = baseResult; }
/// <summary> /// Tests whether the object provides the expected attribute. /// </summary> /// <param name="actual">A Type, MethodInfo, or other ICustomAttributeProvider</param> /// <returns>True if the expected attribute is present, otherwise false</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { System.Reflection.ICustomAttributeProvider attrProvider = actual as System.Reflection.ICustomAttributeProvider; if (attrProvider == null) { throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual"); } ConstraintResult result = new ConstraintResult(this, actual); result.Status = attrProvider.GetCustomAttributes(expectedType, true).Length > 0 ? ConstraintStatus.Success : ConstraintStatus.Failure; return(result); }
public void TestTwoExtraValues() { List <object> actualList = new List <object>() { "one", "two", "three", "four" }; ConstraintResult cr = _constraint.ApplyTo(actualList); cr.WriteMessageTo(_writer); string expectedMsg = " Expected: equivalent to < \"one\", \"two\" >" + Environment.NewLine + " But was: < \"one\", \"two\", \"three\", \"four\" >" + Environment.NewLine + " Extra (2): < \"three\", \"four\" >" + Environment.NewLine; Assert.AreEqual(expectedMsg, _writer.ToString()); }
public void TestOneMissingValue() { List <object> actualList = new List <object>() { "one" }; ConstraintResult cr = _constraint.ApplyTo(actualList); cr.WriteMessageTo(_writer); string expectedMsg = " Expected: equivalent to < \"one\", \"two\" >" + Environment.NewLine + " But was: < \"one\" >" + Environment.NewLine + " Missing (1): < \"two\" >" + Environment.NewLine; Assert.AreEqual(expectedMsg, _writer.ToString()); }
public void TestOnlyDisplaysUpToTenDifferences() { List <object> actualList = new List <object>() { "one", "two", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven" }; ConstraintResult cr = _constraint.ApplyTo(actualList); cr.WriteMessageTo(_writer); string expectedMsg = " Expected: equivalent to < \"one\", \"two\" >" + Environment.NewLine + " But was: < \"one\", \"two\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\"... >" + Environment.NewLine + " Extra (11): < \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\", \"ten\"... >" + Environment.NewLine; Assert.AreEqual(expectedMsg, _writer.ToString()); }
/// <summary> /// Test whether the constraint is satisfied by a delegate /// </summary> /// <param name="del">The delegate whose value is to be tested</param> /// <returns>A ConstraintResult</returns> public override ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, DelayInterval.AsTimeSpan); object actual; if (PollingInterval.IsNotZero) { long nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); actual = InvokeDelegate(del); try { ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new DelegatingConstraintResult(this, result)); } } catch (Exception) { // Ignore any exceptions when polling } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { ThreadUtility.BlockingDelay((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } actual = InvokeDelegate(del); return(new DelegatingConstraintResult(this, BaseConstraint.ApplyTo(actual))); }
/// <summary> /// Test whether the constraint is satisfied by a delegate /// </summary> /// <param name="del">The delegate whose value is to be tested</param> /// <returns>A ConstraintResult</returns> public override ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, TimeSpan.FromMilliseconds(delayInMilliseconds)); object actual; if (pollingInterval > 0) { long nextPoll = TimestampOffset(now, TimeSpan.FromMilliseconds(pollingInterval)); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { Thread.Sleep((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, TimeSpan.FromMilliseconds(pollingInterval)); actual = InvokeDelegate(del); try { ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } catch (Exception) { // Ignore any exceptions when polling } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { Thread.Sleep((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } actual = InvokeDelegate(del); return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Test whether the constraint is satisfied by a given reference. /// Overridden to wait for the specified delay period before /// calling the base constraint with the dereferenced value. /// </summary> /// <param name="actual">A reference to the value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(ref TActual actual) { long now = Stopwatch.GetTimestamp(); long delayEnd = TimestampOffset(now, DelayInterval.AsTimeSpan); if (PollingInterval.IsNotZero) { long nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); while ((now = Stopwatch.GetTimestamp()) < delayEnd) { if (nextPoll > now) { Thread.Sleep((int)TimestampDiff(delayEnd < nextPoll ? delayEnd : nextPoll, now).TotalMilliseconds); } nextPoll = TimestampOffset(now, PollingInterval.AsTimeSpan); try { ConstraintResult result = BaseConstraint.ApplyTo(actual); if (result.IsSuccess) { return(new ConstraintResult(this, actual, true)); } } catch (Exception) { // Ignore any exceptions when polling } } } if ((now = Stopwatch.GetTimestamp()) < delayEnd) { Thread.Sleep((int)TimestampDiff(delayEnd, now).TotalMilliseconds); } return(new ConstraintResult(this, actual, BaseConstraint.ApplyTo(actual).IsSuccess)); }
/// <summary> /// Display Expected and Actual lines for a constraint. This /// is called by MessageWriter's default implementation of /// WriteMessageTo and provides the generic two-line display. /// </summary> /// <param name="result">The failing constraint result</param> public abstract void DisplayDifferences(ConstraintResult result);
/// <summary> /// Display Expected and Actual lines for a constraint. This /// is called by MessageWriter's default implementation of /// WriteMessageTo and provides the generic two-line display. /// </summary> /// <param name="result">The result of the constraint that failed</param> public override void DisplayDifferences(ConstraintResult result) { WriteExpectedLine(result); WriteActualLine(result); }
public DelegatingConstraintResult(IConstraint constraint, ConstraintResult innerResult) : base(constraint, innerResult.ActualValue, innerResult.Status) { _innerResult = innerResult; }
private static void ReportFailure(ConstraintResult result, string message) { ReportFailure(result, message, null); }
/// <summary> /// Write the generic 'Expected' line for a constraint /// </summary> /// <param name="constraint">The constraint that failed</param> private void WriteExpectedLine(ConstraintResult result) { Write(Pfx_Expected); WriteLine(result.Description); }
/// <summary> /// Write the generic 'Actual' line for a constraint /// </summary> /// <param name="result">The ConstraintResult for which the actual value is to be written</param> private void WriteActualLine(ConstraintResult result) { Write(Pfx_Actual); result.WriteActualValueTo(this); WriteLine(); //WriteLine(MsgUtils.FormatValue(result.ActualValue)); }
private static void ReportFailure(ConstraintResult result, string message, params object[] args) { MessageWriter writer = new TextMessageWriter(message, args); result.WriteMessageTo(writer); ReportFailure(writer.ToString()); }
public void Add(MemberInfo member, ConstraintResult constraint) { _Members.Add(member, constraint); }