Skip to content

Commit

Permalink
Ensures expectations involving multiple assertions can be cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Sep 25, 2018
1 parent 68ca857 commit e99ff63
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 85 deletions.
8 changes: 5 additions & 3 deletions Src/FluentAssertions/Collections/CollectionAssertions.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -246,7 +246,7 @@ public AndConstraint<TAssertions> Equal(IEnumerable expected, string because = "

ICollection<TExpected> expectedItems = expectation.ConvertOrCastToCollection<TExpected>();

AssertionScope assertion = Execute.Assertion.BecauseOf(because, becauseArgs);
IAssertionScope assertion = Execute.Assertion.BecauseOf(because, becauseArgs);
if (subjectIsNull)
{
assertion.FailWith("Expected {context:collection} to be equal to {0}{reason}, but found <null>.", expectedItems);
Expand Down Expand Up @@ -1371,7 +1371,9 @@ public AndConstraint<TAssertions> HaveElementPreceding(object successor, object
.Then
.Given(subject => PredecessorOf(successor, subject))
.ForCondition(predecessor => predecessor.IsSameOrEqualTo(expectation))
.FailWith("but found {0}.", predecessor => predecessor);
.FailWith("but found {0}.", predecessor => predecessor)
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
Expand Up @@ -17,7 +17,7 @@ public static Continuation AssertCollectionsHaveSameCount<T>(ICollection<object>
.AssertCollectionHasNotTooManyItems(subject, expectation);
}

public static Continuation AssertEitherCollectionIsNotEmpty<T>(this AssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
public static Continuation AssertEitherCollectionIsNotEmpty<T>(this IAssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
{
return scope
.ForCondition((subject.Count > 0) || (expectation.Count == 0))
Expand All @@ -30,7 +30,7 @@ public static Continuation AssertEitherCollectionIsNotEmpty<T>(this AssertionSco
Environment.NewLine);
}

public static Continuation AssertCollectionHasEnoughItems<T>(this AssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
public static Continuation AssertCollectionHasEnoughItems<T>(this IAssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
{
return scope
.ForCondition(subject.Count >= expectation.Count)
Expand All @@ -41,7 +41,7 @@ public static Continuation AssertCollectionHasEnoughItems<T>(this AssertionScope
Environment.NewLine);
}

public static Continuation AssertCollectionHasNotTooManyItems<T>(this AssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
public static Continuation AssertCollectionHasNotTooManyItems<T>(this IAssertionScope scope, ICollection<object> subject, ICollection<T> expectation)
{
return scope
.ForCondition(subject.Count <= expectation.Count)
Expand Down
Expand Up @@ -184,7 +184,9 @@ private static Type GetIDictionaryInterface(Type expectedType)
.FailWith("but has additional key(s) {0}", keyDifference.AdditionalKeys)
.Then
.ForCondition(!hasMissingKeys || !hasAdditionalKeys)
.FailWith("but it misses key(s) {0} and has additional key(s) {1}", keyDifference.MissingKeys, keyDifference.AdditionalKeys);
.FailWith("but it misses key(s) {0} and has additional key(s) {1}", keyDifference.MissingKeys, keyDifference.AdditionalKeys)
.Then
.ClearExpectation();
}

private static KeyDifference<TSubjectKey, TExpectedKey> CalculateKeyDifference<TSubjectKey, TSubjectValue, TExpectedKey,
Expand Down
61 changes: 28 additions & 33 deletions Src/FluentAssertions/Execution/AssertionScope.cs
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Linq;
using System.Threading;
using FluentAssertions.Common;

#endregion
Expand All @@ -11,7 +12,7 @@ namespace FluentAssertions.Execution
/// <summary>
/// Represents an implicit or explicit scope within which multiple assertions can be collected.
/// </summary>
public class AssertionScope : IDisposable
public class AssertionScope : IAssertionScope
{
#region Private Definitions

Expand All @@ -25,8 +26,7 @@ public class AssertionScope : IDisposable
private static AssertionScope current;

private AssertionScope parent;
private Func<string> expectation = null;
private readonly bool evaluateCondition = true;
private Func<string> expectation;
private string fallbackIdentifier = "object";

#endregion
Expand Down Expand Up @@ -69,21 +69,6 @@ public AssertionScope(string context)
/// </summary>
public string Context { get; set; }

/// <summary>
/// Creates a nested scope used during chaining.
/// </summary>
internal AssertionScope(AssertionScope sourceScope, bool sourceSucceeded)
{
assertionStrategy = sourceScope.assertionStrategy;
contextData = sourceScope.contextData;
reason = sourceScope.reason;
useLineBreaks = sourceScope.useLineBreaks;
parent = sourceScope.parent;
expectation = sourceScope.expectation;
evaluateCondition = sourceSucceeded;
Context = sourceScope.Context;
}

/// <summary>
/// Gets the current thread-specific assertion scope.
/// </summary>
Expand All @@ -96,7 +81,7 @@ public static AssertionScope Current
/// <summary>
/// Indicates that every argument passed into <see cref="FailWith"/> is displayed on a separate line.
/// </summary>
public AssertionScope UsingLineBreaks
public IAssertionScope UsingLineBreaks
{
get
{
Expand All @@ -108,7 +93,7 @@ public AssertionScope UsingLineBreaks
/// <summary>
/// Gets a value indicating whether or not the last assertion executed through this scope succeeded.
/// </summary>
public bool Succeeded { get; private set; }
public bool? Succeeded { get; private set; }

/// <summary>
/// Specify the reason why you expect the condition to be <c>true</c>.
Expand All @@ -123,7 +108,7 @@ public AssertionScope UsingLineBreaks
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
/// </param>
public AssertionScope BecauseOf(string because, params object[] becauseArgs)
public IAssertionScope BecauseOf(string because, params object[] becauseArgs)
{
reason = () =>
{
Expand Down Expand Up @@ -156,7 +141,7 @@ public AssertionScope BecauseOf(string because, params object[] becauseArgs)
/// </remarks>
/// <param name="expectation">The format string that represents the failure message.</param>
/// <param name="args">Optional arguments to any numbered placeholders.</param>
public AssertionScope WithExpectation(string message, params object[] args)
public IAssertionScope WithExpectation(string message, params object[] args)
{
var localReason = reason;
expectation = () =>
Expand All @@ -171,6 +156,17 @@ public AssertionScope WithExpectation(string message, params object[] args)
return this;
}

/// <summary>
/// Clears the expectation set by <see cref="WithExpectation"/>.
/// </summary>
// SMELL: It would be better to give the expectation an explicit scope, but that would be a breaking change.
public Continuation ClearExpectation()
{
expectation = null;

return new Continuation(this, !Succeeded.HasValue || Succeeded.Value);
}

/// <summary>
/// Allows to safely select the subject for successive assertions, even when the prior assertion has failed.
/// </summary>
Expand All @@ -179,7 +175,7 @@ public AssertionScope WithExpectation(string message, params object[] args)
/// </paramref>
public GivenSelector<T> Given<T>(Func<T> selector)
{
return new GivenSelector<T>(selector, evaluateCondition, this);
return new GivenSelector<T>(selector, !Succeeded.HasValue || Succeeded.Value, this);
}

/// <summary>
Expand All @@ -188,12 +184,9 @@ public GivenSelector<T> Given<T>(Func<T> selector)
/// <param name="condition">
/// If <c>true</c> the assertion will be treated as successful and no exceptions will be thrown.
/// </param>
public AssertionScope ForCondition(bool condition)
public IAssertionScope ForCondition(bool condition)
{
if (evaluateCondition)
{
Succeeded = condition;
}
Succeeded = condition;

return this;
}
Expand Down Expand Up @@ -221,7 +214,7 @@ public Continuation FailWith(string message, params object[] args)
{
try
{
if (evaluateCondition && !Succeeded)
if (!Succeeded.HasValue || !Succeeded.Value)
{
string localReason = reason != null ? reason() : "";
var messageBuilder = new MessageBuilder(useLineBreaks);
Expand All @@ -234,13 +227,15 @@ public Continuation FailWith(string message, params object[] args)
}

assertionStrategy.HandleFailure(result.Capitalize());

Succeeded = false;
}

return new Continuation(this, Succeeded);
return new Continuation(this, Succeeded.Value);
}
finally
{
Succeeded = false;
Succeeded = null;
}
}

Expand Down Expand Up @@ -314,9 +309,9 @@ public void Dispose()
}
}

public AssertionScope WithDefaultIdentifier(string identifier)
public IAssertionScope WithDefaultIdentifier(string identifier)
{
this.fallbackIdentifier = identifier;
fallbackIdentifier = identifier;
return this;
}
}
Expand Down
68 changes: 68 additions & 0 deletions Src/FluentAssertions/Execution/ChainedAssertionScope.cs
@@ -0,0 +1,68 @@
using System;

namespace FluentAssertions.Execution
{
public class ContinuedAssertionScope : IAssertionScope
{
private readonly AssertionScope predecessor;
private readonly bool predecessorSucceeded;

public ContinuedAssertionScope(AssertionScope predecessor, bool predecessorSucceeded)
{
this.predecessorSucceeded = predecessorSucceeded;
this.predecessor = predecessor;
}

public GivenSelector<T> Given<T>(Func<T> selector)
{
return predecessor.Given<T>(selector);
}

public IAssertionScope ForCondition(bool condition)
{
if (predecessorSucceeded)
{
return predecessor.ForCondition(condition);
}

return this;
}

public Continuation FailWith(string message, params object[] args)
{
if (predecessorSucceeded)
{
return predecessor.FailWith(message, args);
}

return new Continuation(predecessor, false);
}

public IAssertionScope BecauseOf(string because, params object[] becauseArgs)
{
return predecessor.BecauseOf(because, becauseArgs);
}

public Continuation ClearExpectation()
{
return predecessor.ClearExpectation();
}

public IAssertionScope WithExpectation(string message, params object[] args)
{
return predecessor.WithExpectation(message, args);
}

public IAssertionScope WithDefaultIdentifier(string identifier)
{
return predecessor.WithDefaultIdentifier(identifier);
}

public IAssertionScope UsingLineBreaks => predecessor.UsingLineBreaks;

public void Dispose()
{
predecessor?.Dispose();
}
}
}
13 changes: 4 additions & 9 deletions Src/FluentAssertions/Execution/Continuation.cs
Expand Up @@ -5,32 +5,27 @@ namespace FluentAssertions.Execution
/// </summary>
public class Continuation
{
#region Private Definition

private readonly AssertionScope sourceScope;
private readonly bool sourceSucceeded;

#endregion

public Continuation(AssertionScope sourceScope, bool sourceSucceeded)
{
this.sourceScope = sourceScope;
this.sourceSucceeded = sourceSucceeded;
SourceSucceeded = sourceSucceeded;
}

/// <summary>
/// Continuous the assertion chain if the previous assertion was successful.
/// </summary>
public AssertionScope Then => new AssertionScope(sourceScope, sourceSucceeded);
public IAssertionScope Then => new ContinuedAssertionScope(sourceScope, SourceSucceeded);

public bool SourceSucceeded => sourceSucceeded;
public bool SourceSucceeded { get; }

/// <summary>
/// Provides back-wards compatibility for code that expects <see cref="AssertionScope.FailWith"/> to return a boolean.
/// </summary>
public static implicit operator bool(Continuation continuation)
{
return continuation.sourceSucceeded;
return continuation.SourceSucceeded;
}
}
}
5 changes: 2 additions & 3 deletions Src/FluentAssertions/Execution/ContinuationOfGiven.cs
Expand Up @@ -7,21 +7,20 @@ public class ContinuationOfGiven<TSubject>
{
#region Private Definitions

private readonly GivenSelector<TSubject> parent;
private readonly bool succeeded;

#endregion

public ContinuationOfGiven(GivenSelector<TSubject> parent, bool succeeded)
{
this.parent = parent;
Then = parent;
this.succeeded = succeeded;
}

/// <summary>
/// Continuous the assertion chain if the previous assertion was successful.
/// </summary>
public GivenSelector<TSubject> Then => parent;
public GivenSelector<TSubject> Then { get; }

/// <summary>
/// Provides back-wards compatibility for code that expects <see cref="AssertionScope.FailWith"/> to return a boolean.
Expand Down

0 comments on commit e99ff63

Please sign in to comment.