/// <summary> /// Construct an ExactCountConstraint on top of an existing constraint /// </summary> /// <param name="expectedCount"></param> /// <param name="itemConstraint"></param> public ExactCountConstraint(int expectedCount, IConstraint itemConstraint) : base(itemConstraint) { Guard.ArgumentNotNull(itemConstraint, nameof(itemConstraint)); _itemConstraint = itemConstraint.Resolve(); _expectedCount = expectedCount; }
/// <inheritdoc cref="Assert.That{T}(T,IResolveConstraint,string,object[])"/> /// <remarks> Delegating to a different name to emphasize that behavior of other Ensure overloads may differ in behavior to Assert.That, and to reduce the number of completion options Intellisense pops up with.</remarks> public static void Ensure <T>(T actual, IConstraint constraint, string message = null, params object[] args) { constraint = constraint.Resolve(); TestExecutionContext.CurrentContext.IncrementAssertCount(); var result = constraint.ApplyTo(actual); if (!result.IsSuccess) { MessageWriter writer = new TextMessageWriter(message, args); result.WriteMessageTo(writer); // If an exception was thrown its stack trace should be displayed, not the stack trace of this assertion exception. throw new AssertionExceptionWithTrimmedStackTrace(writer.ToString(), (result.ActualValue as Exception)?.StackTrace); } }