public static Continuation AssertCollectionHasNotTooManyItems <T>(this AssertionScope scope, ICollection <object> subject, ICollection <T> expectation) { return(scope .ForCondition(subject.Count <= expectation.Count) .FailWith(", but {0}{3}contains {1} item(s) more than{3}{2}.", subject, subject.Count - expectation.Count, expectation, Environment.NewLine)); }
public static Continuation AssertCollectionHasNotTooManyItems <T>(this AssertionScope scope, object[] subject, T[] expectation) { return(scope .ForCondition(subject.Length <= expectation.Length) .FailWith(", but {0}{3}contains {1} item(s) more than{3}{2}.", subject, subject.Length - expectation.Length, expectation, Environment.NewLine)); }
/// <summary> /// Asserts that the thrown exception contains an inner exception with the <paramref name = "expectedInnerMessage" />. /// </summary> /// <param name = "expectedInnerMessage">The expected message of the inner exception.</param> /// <param name = "because"> /// The reason why the message of the inner exception should match <paramref name = "expectedInnerMessage" />. /// </param> /// <param name = "becauseArgs">The parameters used when formatting the <paramref name = "because" />.</param> public virtual ExceptionAssertions <TException> WithInnerMessage(string expectedInnerMessage, string because = "", params object[] becauseArgs) { AssertionScope assertion = Execute.Assertion .BecauseOf(because, becauseArgs) .UsingLineBreaks; assertion .ForCondition(Subject.Any()) .FailWith("Expected inner exception{reason}, but no exception was thrown."); assertion .ForCondition(Subject.Any(e => e.InnerException != null)) .FailWith("Expected inner exception{reason}, but the thrown exception has no inner exception."); string[] subjectInnerMessage = Subject.Select(e => e.InnerException.Message).ToArray(); innerMessageAssertion.Execute(subjectInnerMessage, expectedInnerMessage, because, becauseArgs); return(this); }
public static Continuation AssertEitherCollectionIsNotEmpty <T>(this AssertionScope scope, ICollection <object> subject, ICollection <T> expectation) { return(scope .ForCondition((subject.Count > 0) || (expectation.Count == 0)) .FailWith(", but found an empty collection.") .Then .ForCondition((subject.Count == 0) || (expectation.Count > 0)) .FailWith(", but {0}{2}contains {1} item(s).", subject, subject.Count, Environment.NewLine)); }
/// <summary> /// Asserts that the thrown exception has a message that matches <paramref name = "expectedMessage" /> /// depending on the specified matching mode. /// </summary> /// <param name = "expectedMessage"> /// The expected message of the exception. /// </param> /// <param name = "because"> /// A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name = "becauseArgs"> /// Zero or more objects to format using the placeholders in <see cref = "because" />. /// </param> public virtual ExceptionAssertions <TException> WithMessage(string expectedMessage, string because = "", params object[] becauseArgs) { AssertionScope assertion = Execute.Assertion.BecauseOf(because, becauseArgs).UsingLineBreaks; assertion .ForCondition(Subject.Any()) .FailWith("Expected exception with message {0}{reason}, but no exception was thrown.", expectedMessage); outerMessageAssertion.Execute(Subject.Select(exc => exc.Message).ToArray(), expectedMessage, because, becauseArgs); return(this); }
internal static TResult CheckItemHelper <TResult>(AssertionScope scope, TypeValuePair typeValuePair, UnionMethodInfo methodInfo) { var itemType = typeValuePair.Type; var expectedType = typeof(TResult); var expectedTypePretty = expectedType.PrettyPrint(); var givenTypes = methodInfo.OptionalLast ? methodInfo.CaseTypes.Concat(new[] { typeof(None) }).ToArray() : methodInfo.CaseTypes; var givenTypesPretty = string.Join(", ", givenTypes.Select(ReflectionUtils.PrettyPrint)); scope .ForCondition(givenTypes.Contains(expectedType)) .FailWith("Value should be one of {0} but found {1} instead.", givenTypesPretty, expectedTypePretty); scope .ForCondition(expectedType.IsAssignableFrom(itemType)) .FailWith("Value should be assignable to {0} but found {1} instead", expectedTypePretty, itemType.PrettyPrint()); return((TResult)typeValuePair.Value); }
private AndConstraint <HttpResponseAssertions> HaveStatusCode(HttpStatusCode expected, string because = "", params object[] becauseArgs) { AssertionScope assertion = Execute.Assertion; AssertionScope assertionScope = assertion.ForCondition(Subject.StatusCode == expected).BecauseOf(because, becauseArgs); string message = "Expected response to have HttpStatusCode {0}{reason}, but found {1}. Response: {2}"; object[] failArgs = { expected, Subject.StatusCode, Subject.Content.ReadAsStringAsync().Result }; assertionScope.FailWith(message, failArgs); return(new AndConstraint <HttpResponseAssertions>(this)); }
public AndConstraint <object> BeGenerated() { var type = Subject.GetType(); var assertion = GetAssertion(type); Scope = Execute.Assertion; // Assert the value and output any fail messages var message = assertion.Invoke(null, type, Subject); Scope = Scope .ForCondition(message == null) .FailWith(message) .Then; return(new AndConstraint <object>(Subject)); }
protected void AssertSubjectEquality <T>(IEnumerable expectation, Func <T, T, bool> predicate, string because = "", params object[] reasonArgs) { AssertionScope assertion = Execute.Assertion.BecauseOf(because, reasonArgs); bool subjectIsNull = ReferenceEquals(Subject, null); bool expectationIsNull = ReferenceEquals(expectation, null); if (subjectIsNull && expectationIsNull) { return; } if (subjectIsNull) { assertion.FailWith("Expected {context:collection} to be equal{reason}, but found <null>."); } if (expectation == null) { throw new ArgumentNullException("expectation", "Cannot compare collection with <null>."); } T[] expectedItems = expectation.Cast <T>().ToArray(); T[] actualItems = Subject.Cast <T>().ToArray(); AssertCollectionsHaveSameCount(expectedItems, actualItems, assertion); for (int index = 0; index < expectedItems.Length; index++) { assertion .ForCondition((index < actualItems.Length) && predicate(actualItems[index], expectedItems[index])) .FailWith("Expected {context:collection} to be equal to {0}{reason}, but {1} differs at index {2}.", expectation, Subject, index); } }
public static Continuation TestElementApprox(this AssertionScope scope, int expected, string expectedName, int actual, int precision) => scope.ForCondition(Math.Abs(expected - actual) <= precision).FailWith($"Expected {expectedName} to approximate {expected} +/-{precision}, but it differed by {Math.Abs(expected - actual)}.");
public static Continuation TestElement(this AssertionScope scope, int expected, string expectedName, int actual) => scope.ForCondition(expected == actual).FailWith($"Expected {expectedName} to be {expected}, but it is {actual}.");