private void AssertCollectionsHaveSameCount <T>(T[] expectedItems, T[] actualItems, AssertionScope assertion) { int delta = Math.Abs(expectedItems.Length - actualItems.Length); if (delta != 0) { var expected = (IEnumerable)expectedItems; if (actualItems.Length == 0) { assertion.FailWith("Expected {context:collection} to be equal to {0}{reason}, but found empty collection.", expected); } else if (actualItems.Length < expectedItems.Length) { assertion.FailWith( "Expected {context:collection} to be equal to {0}{reason}, but {1} contains {2} item(s) less.", expected, Subject, delta); } else if (actualItems.Length > expectedItems.Length) { assertion.FailWith( "Expected {context:collection} to be equal to {0}{reason}, but {1} contains {2} item(s) too many.", expected, Subject, delta); } } }
public static void Statisfying <T, C>(this IEnumerable <EnumerableElementAssertions <T, C> > assertions, Action <T, C> predicate) { using (var scope = new AssertionScope()) { int index = 0; foreach (var item in assertions) { try { predicate(item.Subject, item.MatchedElement); } catch (Exception ex) { scope.FailWith("Exception thrown: {0}", ex); } var failures = scope.Discard(); if (failures.Length > 0) { scope.FailWith("In the {context} the {0}. item didn't statisfyed all the condition on the matched element.", index); scope.FailWith("Item: {0}", item.Subject); scope.FailWith("MatchedElement: {0}", item.MatchedElement); scope.AddPreFormattedFailure("Failures:"); foreach (var failure in failures) { scope.AddPreFormattedFailure(failure); } break; } index++; } } }
public void When_the_same_failure_is_handled_twice_or_more_it_should_still_report_it_once() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure"); AssertionScope.Current.FailWith("Failure"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure"); nestedScope.FailWith("Failure"); } //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = scope.Dispose; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- try { act(); } catch (Exception exception) { int matches = new Regex(".*Failure.*").Matches(exception.Message).Count; matches.Should().Be(4); } }
public void Validate(bool expectedEquivalence) { ValidationResult validationResult = Validate(); if (expectedEquivalence && validationResult != null) { assertion.FailWith(validationResult.FormatString, validationResult.FormatParams); } if (!expectedEquivalence && validationResult == null) { assertion.FailWith("Did not expect Xml to be equivalent{reason}, but it is."); } }
public void Validate(bool shouldBeEquivalent) { Failure failure = Validate(); if (shouldBeEquivalent && failure != null) { assertion.FailWith(failure.FormatString, failure.FormatParams); } if (!shouldBeEquivalent && failure is null) { assertion.FailWith("Did not expect Xml to be equivalent{reason}, but it is."); } }
internal static void AssertNoMethod <TResult>(AssertionScope scope, Type assertionType, List <UnionMethodInfo> otherPossibilities) { if (otherPossibilities.Any()) { string Print(UnionMethodInfo info) => string.Join(", ", info.CaseTypes.Select(ReflectionUtils.PrettyPrint)); var prettyPrintedCases = string.Join(",", otherPossibilities.Select(x => $"{{{Print(x)}}}")); scope.FailWith("Multiple Discriminated Union methods found {0}, method types listed {1} non match {2}", assertionType.PrettyPrint(), prettyPrintedCases, typeof(TResult).PrettyPrint()); } scope.FailWith("Unable to find any Discriminated Union method on type {0} for expected type {1}", assertionType.PrettyPrint(), typeof(TResult).PrettyPrint()); }
public void Validate() { subjectReader.MoveToContent(); expectedReader.MoveToContent(); while (!subjectReader.EOF && !expectedReader.EOF) { if (subjectReader.NodeType != expectedReader.NodeType) { assertion.FailWith("Expected node of type {0} at {1}{reason} but found {2}."); } switch (subjectReader.NodeType) { case XmlNodeType.Element: locationStack.Push(subjectReader.LocalName); ValidateStartElement(); ValidateAttributes(); break; case XmlNodeType.EndElement: // No need to verify end element, if it doesn't match // the start element it isn't valid XML, so the parser // would handle that. locationStack.Pop(); break; case XmlNodeType.Text: ValidateText(); break; default: throw new NotImplementedException(); } subjectReader.Read(); expectedReader.Read(); subjectReader.MoveToContent(); expectedReader.MoveToContent(); } if (!expectedReader.EOF) { assertion.FailWith("Expected end of document{reason} but found {0}.", expectedReader.LocalName); } if (!subjectReader.EOF) { assertion.FailWith("Expected {0}{reason}, but found end of document", subjectReader.LocalName); } }
public void When_multiple_scopes_are_nested_it_should_throw_all_failures_from_the_outer_scope() { // Arrange var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure1"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure2"); using (var deeplyNestedScope = new AssertionScope()) { deeplyNestedScope.FailWith("Failure3"); } } // Act Action act = scope.Dispose; // Assert try { act(); } catch (Exception exception) { exception.Message.Should().Contain("Failure1"); exception.Message.Should().Contain("Failure2"); exception.Message.Should().Contain("Failure3"); } }
public void When_a_nested_scope_is_discarded_its_failures_should_also_be_discarded() { // Arrange var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure1"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure2"); using (var deeplyNestedScope = new AssertionScope()) { deeplyNestedScope.FailWith("Failure3"); deeplyNestedScope.Discard(); } } // Act Action act = scope.Dispose; // Assert try { act(); } catch (Exception exception) { exception.Message.Should().Contain("Failure1"); exception.Message.Should().Contain("Failure2"); exception.Message.Should().NotContain("Failure3"); } }
protected override Task AssertOnAllResponses(Action <TResponse> assert) { if (!this.ExpectIsValid) { return(base.AssertOnAllResponses(assert)); } return(base.AssertOnAllResponses((r) => { if (TestClient.Configuration.RunIntegrationTests && !r.IsValid && r.ApiCall.OriginalException != null && !(r.ApiCall.OriginalException is ElasticsearchClientException)) { ExceptionDispatchInfo.Capture(r.ApiCall.OriginalException.Demystify()).Throw(); return; } using (var scope = new AssertionScope()) { assert(r); var failures = scope.Discard(); if (failures.Length <= 0) { return; } var failure = failures[0]; scope.AddReportable("Failure", failure); scope.AddReportable("DebugInformation", r.DebugInformation); scope.FailWith($@"{{Failure}} Response Under Test: {{DebugInformation}}"); } })); }
private bool ValidateAgainstNulls() { if (((expected == null) && (subject != null)) || ((expected != null) && (subject == null))) { assertion.FailWith(ExpectationDescription + "{0}{reason}, but found {1}.", expected, subject); return(false); } return(true); }
public void When_the_same_failure_is_handled_twice_or_more_it_should_still_report_it_once() { // Arrange var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure"); AssertionScope.Current.FailWith("Failure"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure"); nestedScope.FailWith("Failure"); } // Act Action act = scope.Dispose; // Assert act.Should().Throw <XunitException>() .Which.Message.Should().Contain("Failure", Exactly.Times(4)); }
private static bool ShouldCompareMembersThisDeep(INode currentNode, IEquivalencyAssertionOptions options, AssertionScope assertionScope) { bool shouldRecurse = options.AllowInfiniteRecursion || currentNode.Depth < MaxDepth; if (!shouldRecurse) { assertionScope.FailWith("The maximum recursion depth was reached. "); } return(shouldRecurse); }
public void When_custom_strategy_used_respect_its_behavior() { // Arrange var scope = new AssertionScope(new FailWithStupidMessageAssertionStrategy()); // Act Action act = () => scope.FailWith("Failure 1"); // Assert act.Should().ThrowExactly <XunitException>() .WithMessage("Good luck with understanding what's going on!"); }
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 void When_a_nested_scope_is_discarded_its_failures_should_also_be_discarded() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure1"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure2"); using (var deeplyNestedScope = new AssertionScope()) { deeplyNestedScope.FailWith("Failure3"); deeplyNestedScope.Discard(); } } //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = scope.Dispose; ; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- try { act(); } catch (Exception exception) { Assert.IsTrue(exception.Message.Contains("Failure1")); Assert.IsTrue(exception.Message.Contains("Failure2")); Assert.IsFalse(exception.Message.Contains("Failure3")); } }
public void When_a_nested_scope_is_discarded_its_failures_should_also_be_discarded() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure1"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure2"); using (var deeplyNestedScope = new AssertionScope()) { deeplyNestedScope.FailWith("Failure3"); deeplyNestedScope.Discard(); } } //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = scope.Dispose; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- try { act(); } catch (Exception exception) { Assert.IsTrue(exception.Message.Contains("Failure1")); Assert.IsTrue(exception.Message.Contains("Failure2")); Assert.IsFalse(exception.Message.Contains("Failure3")); } }
public void When_multiple_scopes_are_nested_it_should_throw_all_failures_from_the_outer_scope() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure1"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure2"); using (var deeplyNestedScope = new AssertionScope()) { deeplyNestedScope.FailWith("Failure3"); } } //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = scope.Dispose; ; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- try { act(); } catch (Exception exception) { Assert.IsTrue(exception.Message.Contains("Failure1")); Assert.IsTrue(exception.Message.Contains("Failure2")); Assert.IsTrue(exception.Message.Contains("Failure3")); } }
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 void When_the_same_failure_is_handled_twice_or_more_it_should_still_report_it_once() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var scope = new AssertionScope(); AssertionScope.Current.FailWith("Failure"); AssertionScope.Current.FailWith("Failure"); using (var nestedScope = new AssertionScope()) { nestedScope.FailWith("Failure"); nestedScope.FailWith("Failure"); } //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = scope.Dispose; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- try { act(); } catch (Exception exception) { int matches = new Regex(".*Failure.*").Matches(exception.Message).Count; Assert.AreEqual(4, matches); } }
public static void FailWithText(this AssertionScope scope, string text) { var t = text.Replace("{", "{{").Replace("}", "}}"); scope.FailWith(t); }
public static void Fail(this AssertionScope assertionScope, string message) { assertionScope.AddPreFormattedFailure(message); assertionScope.FailWith(string.Empty, Array.Empty <object>()); }