Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing ClearExpectation #1105

Merged
merged 1 commit into from Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 27 additions & 9 deletions Src/FluentAssertions/Collections/CollectionAssertions.cs
Expand Up @@ -45,7 +45,9 @@ public AndConstraint<TAssertions> BeEmpty(string because = "", params object[] b
.Then
.Given(() => Subject.Cast<object>())
.ForCondition(collection => !collection.Any())
.FailWith("but found {0}.", collection => collection);
.FailWith("but found {0}.", collection => collection)
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down Expand Up @@ -265,7 +267,9 @@ public AndConstraint<TAssertions> Equal(IEnumerable expected, string because = "
.Given(() => Subject.ConvertOrCastToCollection<TActual>())
.AssertCollectionsHaveSameCount(expectedItems.Count)
.Then
.AssertCollectionsHaveSameItems(expectedItems, (a, e) => a.IndexOfFirstDifferenceWith(e, equalityComparison));
.AssertCollectionsHaveSameItems(expectedItems, (a, e) => a.IndexOfFirstDifferenceWith(e, equalityComparison))
.Then
.ClearExpectation();
}

/// <summary>
Expand Down Expand Up @@ -1447,7 +1451,9 @@ public AndConstraint<TAssertions> StartWith(object element, string because = "",
.Then
.AssertCollectionHasEnoughItems(expected.Length)
.Then
.AssertCollectionsHaveSameItems(expected, (a, e) => a.Take(e.Count).IndexOfFirstDifferenceWith(e, equalityComparison));
.AssertCollectionsHaveSameItems(expected, (a, e) => a.Take(e.Count).IndexOfFirstDifferenceWith(e, equalityComparison))
.Then
.ClearExpectation();
}

protected void AssertCollectionStartsWith<TActual, TExpected>(IEnumerable<TActual> actualItems, ICollection<TExpected> expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
Expand All @@ -1460,7 +1466,9 @@ public AndConstraint<TAssertions> StartWith(object element, string because = "",
.Then
.AssertCollectionHasEnoughItems(expected.Count)
.Then
.AssertCollectionsHaveSameItems(expected, (a, e) => a.Take(e.Count).IndexOfFirstDifferenceWith(e, equalityComparison));
.AssertCollectionsHaveSameItems(expected, (a, e) => a.Take(e.Count).IndexOfFirstDifferenceWith(e, equalityComparison))
.Then
.ClearExpectation();
}

/// <summary>
Expand Down Expand Up @@ -1498,7 +1506,9 @@ public AndConstraint<TAssertions> EndWith(object element, string because = "", p
int firstIndexToCompare = a.Count - e.Count;
int index = a.Skip(firstIndexToCompare).IndexOfFirstDifferenceWith(e, equalityComparison);
return index >= 0 ? index + firstIndexToCompare : index;
});
})
.Then
.ClearExpectation();
}

protected void AssertCollectionEndsWith<TActual, TExpected>(IEnumerable<TActual> actual, ICollection<TExpected> expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
Expand All @@ -1516,7 +1526,9 @@ public AndConstraint<TAssertions> EndWith(object element, string because = "", p
int firstIndexToCompare = a.Count - e.Count;
int index = a.Skip(firstIndexToCompare).IndexOfFirstDifferenceWith(e, equalityComparison);
return index >= 0 ? index + firstIndexToCompare : index;
});
})
.Then
.ClearExpectation();
}

/// <summary>
Expand Down Expand Up @@ -1586,7 +1598,9 @@ public AndConstraint<TAssertions> HaveElementSucceeding(object predecessor, obje
.Then
.Given(subject => SuccessorOf(predecessor, subject))
.ForCondition(successor => successor.IsSameOrEqualTo(expectation))
.FailWith("but found {0}.", successor => successor);
.FailWith("but found {0}.", successor => successor)
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down Expand Up @@ -1640,7 +1654,9 @@ public AndConstraint<TAssertions> AllBeAssignableTo(Type expectedType, string be
.FailWith("but found a null element.")
.Then
.ForCondition(subject => subject.All(x => expectedType.GetTypeInfo().IsAssignableFrom(GetType(x).GetTypeInfo())))
.FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]");
.FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]")
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down Expand Up @@ -1682,7 +1698,9 @@ public AndConstraint<TAssertions> AllBeOfType(Type expectedType, string because
.FailWith("but found a null element.")
.Then
.ForCondition(subject => subject.All(x => expectedType == GetType(x)))
.FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]");
.FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]")
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
Expand Up @@ -419,7 +419,9 @@ public AndConstraint<GenericCollectionAssertions<T>> SatisfyRespectively(IEnumer
Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to satisfy all inspectors{reason}, but some inspectors are not satisfied:")
.FailWithPreFormatted(failureMessage);
.FailWithPreFormatted(failureMessage)
.Then
.ClearExpectation();
}

return new AndConstraint<GenericCollectionAssertions<T>>(this);
Expand Down
44 changes: 39 additions & 5 deletions Src/FluentAssertions/Execution/GivenSelectorExtensions.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using FluentAssertions.Common;

Expand Down Expand Up @@ -59,13 +60,46 @@ public static ContinuationOfGiven<ICollection<T>> AssertCollectionHasEnoughItems
.AssertCollectionHasNotTooManyItems(length);
}

public static void AssertCollectionsHaveSameItems<TActual, TExpected>(this GivenSelector<ICollection<TActual>> givenSelector,
public static ContinuationOfGiven<ICollection<TActual>> AssertCollectionsHaveSameItems<TActual, TExpected>(this GivenSelector<ICollection<TActual>> givenSelector,
ICollection<TExpected> expected, Func<ICollection<TActual>, ICollection<TExpected>, int> findIndex)
{
givenSelector
.Given(actual => new { Items = actual, Index = findIndex(actual, expected) })
.ForCondition(diff => diff.Index == -1)
.FailWith("but {0} differs at index {1}.", diff => diff.Items, diff => diff.Index);
return givenSelector
.Given<ICollection<TActual>>(actual => new CollectionWithIndex<TActual>(actual, findIndex(actual, expected)))
.ForCondition(diff => diff.As<CollectionWithIndex<TActual>>().Index == -1)
.FailWith("but {0} differs at index {1}.",
diff => diff.As<CollectionWithIndex<TActual>>().Items,
diff => diff.As<CollectionWithIndex<TActual>>().Index);
}

private sealed class CollectionWithIndex<T> : ICollection<T>
{
public ICollection<T> Items { get; }

public int Index { get; }

public CollectionWithIndex(ICollection<T> items, int index)
{
Items = items;
Index = index;
}

public int Count => Items.Count;

public bool IsReadOnly => Items.IsReadOnly;

public void Add(T item) => Items.Add(item);

public void Clear() => Items.Clear();

public bool Contains(T item) => Items.Contains(item);

public void CopyTo(T[] array, int arrayIndex) => Items.CopyTo(array, arrayIndex);

public IEnumerator<T> GetEnumerator() => Items.GetEnumerator();

public bool Remove(T item) => Items.Remove(item);

IEnumerator IEnumerable.GetEnumerator() => Items.GetEnumerator();
}
}
}
3 changes: 2 additions & 1 deletion Src/FluentAssertions/Primitives/DateTimeAssertions.cs
Expand Up @@ -697,7 +697,8 @@ public AndConstraint<DateTimeAssertions> NotHaveHour(int unexpected, string beca
.ForCondition(Subject.Value.Second != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith(", but it was.")
.Then.ClearExpectation();
.Then
.ClearExpectation();

return new AndConstraint<DateTimeAssertions>(this);
}
Expand Down
64 changes: 48 additions & 16 deletions Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs
Expand Up @@ -390,7 +390,9 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
.Then
.ForCondition(Subject.Value.Year == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Year);
.FailWith("but it was {0}.", Subject.Value.Year)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -416,7 +418,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveYear(int unexpected, strin
.Then
.ForCondition(Subject.Value.Year != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -443,7 +447,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveYear(int unexpected, strin
.Then
.ForCondition(Subject.Value.Month == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Month);
.FailWith("but it was {0}.", Subject.Value.Month)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -469,7 +475,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveMonth(int unexpected, stri
.Then
.ForCondition(Subject.Value.Month != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -496,7 +504,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveMonth(int unexpected, stri
.Then
.ForCondition(Subject.Value.Day == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Day);
.FailWith("but it was {0}.", Subject.Value.Day)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -522,7 +532,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveDay(int unexpected, string
.Then
.ForCondition(Subject.Value.Day != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -549,7 +561,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveDay(int unexpected, string
.Then
.ForCondition(Subject.Value.Hour == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Hour);
.FailWith("but it was {0}.", Subject.Value.Hour)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -575,7 +589,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Hour != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -602,7 +618,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Minute == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Minute);
.FailWith("but it was {0}.", Subject.Value.Minute)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -629,7 +647,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Minute != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -656,7 +676,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Second == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Second);
.FailWith("but it was {0}.", Subject.Value.Second)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -683,7 +705,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Second != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -710,7 +734,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Offset == expected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value.Offset);
.FailWith("but it was {0}.", Subject.Value.Offset)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand All @@ -737,7 +763,9 @@ public AndConstraint<DateTimeOffsetAssertions> NotHaveHour(int unexpected, strin
.Then
.ForCondition(Subject.Value.Offset != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand Down Expand Up @@ -827,7 +855,9 @@ public DateTimeOffsetRangeAssertions BeLessThan(TimeSpan timeSpan)
.Then
.ForCondition(Subject.Value.Date == expectedDate)
.BecauseOf(because, becauseArgs)
.FailWith("but it was {0}.", Subject.Value);
.FailWith("but it was {0}.", Subject.Value)
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand Down Expand Up @@ -856,7 +886,9 @@ public DateTimeOffsetRangeAssertions BeLessThan(TimeSpan timeSpan)
.Then
.ForCondition(Subject.Value.Date != unexpectedDate)
.BecauseOf(because, becauseArgs)
.FailWith("but it was.");
.FailWith("but it was.")
.Then
.ClearExpectation();

return new AndConstraint<DateTimeOffsetAssertions>(this);
}
Expand Down