diff --git a/.gitignore b/.gitignore index 009d238..2d71489 100644 --- a/.gitignore +++ b/.gitignore @@ -71,4 +71,5 @@ Documentation-*/ *.crunchsolution.xml *.ncrunchsolution *.ncrunchsolution.user -*.ncrunchproject \ No newline at end of file +*.ncrunchproject +_NCrunch_*/ diff --git a/TddToolkit/ImplementationDetails/AssertionRecorder.cs b/TddToolkit/ImplementationDetails/AssertionRecorder.cs index 6d962b6..04eeab2 100644 --- a/TddToolkit/ImplementationDetails/AssertionRecorder.cs +++ b/TddToolkit/ImplementationDetails/AssertionRecorder.cs @@ -3,34 +3,43 @@ using System.Linq; using System.Text; using FluentAssertions; +using System.Diagnostics; namespace TddEbook.TddToolkit.ImplementationDetails { public class AssertionRecorder { - public List _exceptions = new List(); + private int assertionNumber = 1; + public List _exceptions = new List(); internal void AssertTrue() { XAssert.Equal(0, _exceptions.Count, ExtractMessagesFromAll(_exceptions)); } - private string ExtractMessagesFromAll(List _exceptions) + private string ExtractMessagesFromAll(List _failedAssertions) { string result = "the following assertions shouldn't have failed: " + Environment.NewLine; - foreach (var e in _exceptions) + foreach (var failedAssertion in _failedAssertions) { - result += e.Message + Environment.NewLine; + result += failedAssertion.Header(); } - foreach (var e in _exceptions) + foreach (var failedAssertion in _failedAssertions) { - result += e + Environment.NewLine; + result += failedAssertion + Environment.NewLine; } return result; } + private string ExtractDescriptionFrom(Exception e) + { + var st = new StackTrace(e, true); + var frame = st.GetFrames().Last(); + return frame.ToString(); + } + public void Equal(T expected, T actual) { LogException(() => XAssert.Equal(expected, actual)); @@ -76,9 +85,27 @@ public void Alike(T expected, T actual) LogException(() => XAssert.Alike(expected, actual)); } + public void IsInstanceOf(object o) + { + LogException(() => o.Should().BeOfType()); + } + + public void IsAssignableTo(object o) + { + LogException(() => o.Should().BeAssignableTo()); + } + + public void Null(object o) + { + LogException(() => o.Should().BeNull()); + } + public void Same(T expected, T other) + { + LogException(() => other.Should().BeSameAs(expected)); + } - public void LogException(Action action) + internal void LogException(Action action) { try { @@ -86,8 +113,37 @@ public void LogException(Action action) } catch (Exception e) { - _exceptions.Add(e); + _exceptions.Add(AssertionFailed.With(e, assertionNumber)); } + + assertionNumber++; + } + } + + public class AssertionFailed + { + private Exception e; + private int assertionNumber; + + public AssertionFailed(Exception e, int assertionNumber) + { + this.e = e; + this.assertionNumber = assertionNumber; + } + + public static AssertionFailed With(Exception e, int assertionNumber) + { + return new AssertionFailed(e, assertionNumber); + } + + public string Header() + { + return "Assertion no. " + assertionNumber + " failed: " + e.Message + " " + Environment.NewLine; + } + + public override string ToString() + { + return e.ToString(); } } } diff --git a/TddToolkit/XAssert.Reflection.cs b/TddToolkit/XAssert.Reflection.cs index 976df5c..d2f37c6 100644 --- a/TddToolkit/XAssert.Reflection.cs +++ b/TddToolkit/XAssert.Reflection.cs @@ -17,8 +17,11 @@ public static void AttributeExistsOnMethodOf( method.HasAttribute(attr.GetType(), attr).Should().BeTrue(); } - - + public static void AttributeExistsOnMethodOf(Expression> methodExpression) + { + var method = Method.Of(methodExpression); + method.HasAttribute().Should().BeTrue(); + } } public class Method diff --git a/TddToolkitSpecification/XAssertSpecification.cs b/TddToolkitSpecification/XAssertSpecification.cs index 8ea149c..ba7b844 100644 --- a/TddToolkitSpecification/XAssertSpecification.cs +++ b/TddToolkitSpecification/XAssertSpecification.cs @@ -46,19 +46,20 @@ public void ShouldAggregateMultipleAssertionsWhenAssertionAll() assert.Equal(1, 3); assert.Equal(2, 44); assert.Equal("aa", "123"); + assert.True(true); assert.Contains("bb", "aa"); }) ); - StringAssert.Contains("Expected object to be 1, but found 3", + StringAssert.Contains("Assertion no. 1 failed: Expected object to be 1, but found 3", exception.ToString()); - StringAssert.Contains("Expected object to be 2, but found 44", + StringAssert.Contains("Assertion no. 2 failed: Expected object to be 2, but found 44", exception.ToString()); - StringAssert.Contains("Expected object to be \"aa\", but found \"123\"", + StringAssert.Contains("Assertion no. 3 failed: Expected object to be \"aa\", but found \"123\"", exception.ToString()); - StringAssert.Contains("Expected string \"bb\" to contain \"aa\"", + StringAssert.DoesNotContain("Assertion no. 4 failed", exception.ToString()); + StringAssert.Contains("Assertion no. 5 failed: Expected string \"bb\" to contain \"aa\"", exception.ToString()); - } [Test]