Skip to content

Commit

Permalink
Enhanced XAssert.All
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek-galezowski committed Dec 4, 2013
1 parent 4fc6904 commit 7be8eff
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ Documentation-*/
*.crunchsolution.xml
*.ncrunchsolution
*.ncrunchsolution.user
*.ncrunchproject
*.ncrunchproject
_NCrunch_*/
72 changes: 64 additions & 8 deletions TddToolkit/ImplementationDetails/AssertionRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,43 @@
using System.Linq;
using System.Text;
using FluentAssertions;
using System.Diagnostics;

namespace TddEbook.TddToolkit.ImplementationDetails
{
public class AssertionRecorder
{
public List<Exception> _exceptions = new List<Exception>();
private int assertionNumber = 1;
public List<AssertionFailed> _exceptions = new List<AssertionFailed>();

internal void AssertTrue()
{
XAssert.Equal(0, _exceptions.Count, ExtractMessagesFromAll(_exceptions));
}

private string ExtractMessagesFromAll(List<Exception> _exceptions)
private string ExtractMessagesFromAll(List<AssertionFailed> _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>(T expected, T actual)
{
LogException(() => XAssert.Equal(expected, actual));
Expand Down Expand Up @@ -76,18 +85,65 @@ public void Alike<T>(T expected, T actual)
LogException(() => XAssert.Alike(expected, actual));
}

public void IsInstanceOf<T>(object o)
{
LogException(() => o.Should().BeOfType<T>());
}

public void IsAssignableTo<T>(object o)
{
LogException(() => o.Should().BeAssignableTo<T>());
}

public void Null(object o)
{
LogException(() => o.Should().BeNull());
}

public void Same<T>(T expected, T other)
{
LogException(() => other.Should().BeSameAs(expected));
}

public void LogException(Action action)
internal void LogException(Action action)
{
try
{
action.Invoke();
}
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();
}
}
}
7 changes: 5 additions & 2 deletions TddToolkit/XAssert.Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ public static void AttributeExistsOnMethodOf<T>(
method.HasAttribute(attr.GetType(), attr).Should().BeTrue();
}



public static void AttributeExistsOnMethodOf<T, TAttr>(Expression<Action<T>> methodExpression)
{
var method = Method.Of<T>(methodExpression);
method.HasAttribute<TAttr>().Should().BeTrue();
}
}

public class Method
Expand Down
11 changes: 6 additions & 5 deletions TddToolkitSpecification/XAssertSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 7be8eff

Please sign in to comment.