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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for accidental Equals usage #2006

Merged
merged 23 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 22 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
Expand Up @@ -35,6 +35,8 @@ public GenericCollectionAssertions(TCollection actualValue)
}
}

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
[DebuggerNonUserCode]
public class GenericCollectionAssertions<TCollection, T, TAssertions> :
ReferenceTypeAssertions<TCollection, TAssertions>
Expand Down Expand Up @@ -3449,6 +3451,10 @@ private AndConstraint<TAssertions> NotBeInOrder(IComparer<T> comparer, SortOrder
return new AndConstraint<TAssertions>((TAssertions)this);
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean BeSameAs() or Equal() instead?");
nycdotnet marked this conversation as resolved.
Show resolved Hide resolved

private static int IndexOf(IList<T> items, T item, int startIndex)
{
Func<T, T, bool> comparer = ObjectExtensions.GetComparer<T>();
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Numeric/NumericAssertions.cs
Expand Up @@ -474,7 +474,7 @@ public AndConstraint<TAssertions> NotBeOfType(Type unexpectedType, string becaus

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
jnyrup marked this conversation as resolved.
Show resolved Hide resolved

private protected virtual bool IsNaN(T value) => false;

Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/BooleanAssertions.cs
Expand Up @@ -120,5 +120,5 @@ public AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", pa

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/DateOnlyAssertions.cs
Expand Up @@ -512,7 +512,7 @@ public AndConstraint<TAssertions> BeOneOf(IEnumerable<DateOnly?> validValues, st

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}

#endif
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/DateTimeAssertions.cs
Expand Up @@ -925,5 +925,5 @@ public AndConstraint<TAssertions> BeIn(DateTimeKind expectedKind, string because

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
Expand Up @@ -1094,5 +1094,5 @@ public AndConstraint<TAssertions> BeOneOf(IEnumerable<DateTimeOffset?> validValu

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
Expand Up @@ -132,5 +132,5 @@ private static string PositionRelativeToTarget(DateTimeOffset actual, DateTimeOf

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Before() or After() instead?");
nycdotnet marked this conversation as resolved.
Show resolved Hide resolved
dennisdoomen marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/DateTimeRangeAssertions.cs
Expand Up @@ -135,5 +135,5 @@ private static string PositionRelativeToTarget(DateTime actual, DateTime target)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Before() or After() instead?");
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/EnumAssertions.cs
Expand Up @@ -443,5 +443,5 @@ private static string GetName<T>(T @enum)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/GuidAssertions.cs
Expand Up @@ -171,5 +171,5 @@ public AndConstraint<TAssertions> NotBe(Guid unexpected, string because = "", pa

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
dennisdoomen marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 6 additions & 0 deletions Src/FluentAssertions/Primitives/ObjectAssertions.cs
Expand Up @@ -17,6 +17,8 @@ public ObjectAssertions(object value)
}
}

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Contains a number of methods to assert that a <typeparamref name="TSubject"/> is in the expected state.
/// </summary>
Expand Down Expand Up @@ -220,6 +222,10 @@ public AndConstraint<TAssertions> NotBe(TSubject unexpected, string because = ""
return new AndConstraint<TAssertions>((TAssertions)this);
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() or BeSameAs() instead?");
nycdotnet marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Returns the type of the subject the assertion applies on.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/ReferenceTypeAssertions.cs
Expand Up @@ -422,5 +422,5 @@ public AndConstraint<TAssertions> NotBeAssignableTo(Type type, string because =

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
nycdotnet marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Up @@ -301,5 +301,5 @@ public AndConstraint<TAssertions> BeGreaterThan(TimeSpan expected, string becaus

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/TimeOnlyAssertions.cs
Expand Up @@ -566,7 +566,7 @@ public AndConstraint<TAssertions> BeOneOf(IEnumerable<TimeOnly?> validValues, st

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}

#endif
Expand Up @@ -231,5 +231,5 @@ public AndConstraint<ExecutionTimeAssertions> BeCloseTo(TimeSpan expectedDuratio

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean BeLessThanOrEqualTo() or BeGreaterThanOrEqualTo() instead?");
}
Expand Up @@ -184,7 +184,7 @@ protected TaskCompletionSourceAssertionsBase(IClock clock)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean CompleteWithinAsync() instead?");

/// <summary>
/// Monitors the specified task whether it completes withing the remaining time span.
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Types/MethodInfoSelectorAssertions.cs
Expand Up @@ -330,5 +330,5 @@ private static string GetDescriptionsFor(IEnumerable<MethodInfo> methods)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
Expand Up @@ -229,5 +229,5 @@ private static string GetDescriptionsFor(IEnumerable<PropertyInfo> properties)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Types/TypeSelectorAssertions.cs
Expand Up @@ -477,5 +477,5 @@ private static string GetDescriptionFor(Type type)

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Calling Equals on Assertion classes is not supported.");
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
nycdotnet marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Up @@ -463,6 +463,7 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> Equal(params T[] elements) { }
public FluentAssertions.AndConstraint<TAssertions> Equal(System.Collections.Generic.IEnumerable<T> expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> Equal<TExpectation>(System.Collections.Generic.IEnumerable<TExpectation> expectation, System.Func<T, TExpectation, bool> equalityComparison, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(System.Linq.Expressions.Expression<System.Func<int, bool>> countPredicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCountGreaterOrEqualTo(int expected, string because = "", params object[] becauseArgs) { }
Expand Down Expand Up @@ -2100,6 +2101,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> Be(TSubject expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
public FluentAssertions.AndConstraint<TAssertions> NotBe(TSubject unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
Expand Down
Expand Up @@ -476,6 +476,7 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> Equal(params T[] elements) { }
public FluentAssertions.AndConstraint<TAssertions> Equal(System.Collections.Generic.IEnumerable<T> expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> Equal<TExpectation>(System.Collections.Generic.IEnumerable<TExpectation> expectation, System.Func<T, TExpectation, bool> equalityComparison, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(System.Linq.Expressions.Expression<System.Func<int, bool>> countPredicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCountGreaterOrEqualTo(int expected, string because = "", params object[] becauseArgs) { }
Expand Down Expand Up @@ -2184,6 +2185,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> Be(TSubject expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(TSubject unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
Expand Down
Expand Up @@ -463,6 +463,7 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> Equal(params T[] elements) { }
public FluentAssertions.AndConstraint<TAssertions> Equal(System.Collections.Generic.IEnumerable<T> expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> Equal<TExpectation>(System.Collections.Generic.IEnumerable<TExpectation> expectation, System.Func<T, TExpectation, bool> equalityComparison, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(System.Linq.Expressions.Expression<System.Func<int, bool>> countPredicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCountGreaterOrEqualTo(int expected, string because = "", params object[] becauseArgs) { }
Expand Down Expand Up @@ -2100,6 +2101,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> Be(TSubject expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(TSubject unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
Expand Down
Expand Up @@ -463,6 +463,7 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> Equal(params T[] elements) { }
public FluentAssertions.AndConstraint<TAssertions> Equal(System.Collections.Generic.IEnumerable<T> expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> Equal<TExpectation>(System.Collections.Generic.IEnumerable<TExpectation> expectation, System.Func<T, TExpectation, bool> equalityComparison, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCount(System.Linq.Expressions.Expression<System.Func<int, bool>> countPredicate, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> HaveCountGreaterOrEqualTo(int expected, string because = "", params object[] becauseArgs) { }
Expand Down Expand Up @@ -2100,6 +2101,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> Be(TSubject expected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expectation, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(TSubject unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBeEquivalentTo<TExpectation>(TExpectation unexpected, System.Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>, FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs) { }
Expand Down