Skip to content

Instantly share code, notes, and snippets.

@Krusen
Created March 3, 2020 08:24
Show Gist options
  • Save Krusen/b9d957aa2d396fac327f6d1b70e053aa to your computer and use it in GitHub Desktop.
Save Krusen/b9d957aa2d396fac327f6d1b70e053aa to your computer and use it in GitHub Desktop.
public static class Verify
{
/// <summary>
/// Verifies that the fluent assertions in the <paramref name="action" /> are correct.
/// </summary>
/// <param name="action">An action with one or more fluent assertions</param>
public static T That<T>(Action<T> action)
{
return ArgumentMatcher.Enqueue(new AssertionMatcher<T>(action));
}
private class AssertionMatcher<T> : IArgumentMatcher<T>
{
private readonly Action<T> _assertion;
public AssertionMatcher(Action<T> assertion)
{
_assertion = assertion;
}
public bool IsSatisfiedBy(T argument)
{
using (var scope = new AssertionScope())
{
_assertion(argument);
var failures = scope.Discard().ToList();
if (failures.Count == 0)
{
return true;
}
failures.ForEach(x => Trace.WriteLine(x));
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment