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

More testing of guarding methods #1891

Merged
merged 2 commits into from Apr 16, 2022
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
16 changes: 16 additions & 0 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -1028,6 +1028,22 @@ public static void Should(this TypeSelectorAssertions _)
InvalidShouldCall();
}

/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TAssertions>(this DateTimeRangeAssertions<TAssertions> _)
where TAssertions : DateTimeAssertions<TAssertions>
{
InvalidShouldCall();
}

/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TAssertions>(this DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : DateTimeOffsetAssertions<TAssertions>
{
InvalidShouldCall();
}

[DoesNotReturn]
private static void InvalidShouldCall()
{
Expand Down
Expand Up @@ -118,6 +118,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -126,6 +126,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -118,6 +118,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -118,6 +118,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -117,6 +117,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -118,6 +118,14 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeOffsetRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeOffsetAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.DateTimeRangeAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.DateTimeAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
public static void Should<TAssertions>(this FluentAssertions.Primitives.GuidAssertions<TAssertions> _)
where TAssertions : FluentAssertions.Primitives.GuidAssertions<TAssertions> { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
Expand Down
Expand Up @@ -503,7 +503,7 @@ public void When_overriding_with_custom_assertion_it_should_be_chainable()
}

[Fact]
public void When_a_nullable_property_is_overriden_with_a_custom_assertion_it_should_use_it()
public void When_a_nullable_property_is_overridden_with_a_custom_assertion_it_should_use_it()
{
// Arrange
var actual = new SimpleWithNullable
Expand Down
44 changes: 43 additions & 1 deletion Tests/FluentAssertions.Specs/AssertionExtensionsSpecs.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentAssertions.Common;
using FluentAssertions.Numeric;
using FluentAssertions.Primitives;
using FluentAssertions.Specialized;
Expand All @@ -13,7 +14,7 @@ namespace FluentAssertions.Specs
public class AssertionExtensionsSpecs
{
[Fact]
public void Assertions_classes_have_overriden_equals()
public void Assertions_classes_override_equals()
{
// Arrange / Act
var equalsOverloads = AllTypes.From(typeof(FluentAssertions.AssertionExtensions).Assembly)
Expand All @@ -35,11 +36,47 @@ private static bool OverridesEquals(Type t)
return equals is not null;
}

public static object[][] ClassesWithGuardEquals() => new object[][]
{
new object[] { new ObjectAssertions<object, ObjectAssertions>(default) },
new object[] { new BooleanAssertions<BooleanAssertions>(default) },
new object[] { new DateTimeAssertions<DateTimeAssertions>(default) },
new object[] { new DateTimeRangeAssertions<DateTimeAssertions>(default, default, default, default) },
new object[] { new DateTimeOffsetAssertions<DateTimeOffsetAssertions>(default) },
new object[] { new DateTimeOffsetRangeAssertions<DateTimeOffsetAssertions>(default, default, default, default) },
#if NET6_0_OR_GREATER
new object[] { new DateOnlyAssertions<DateOnlyAssertions>(default) },
new object[] { new TimeOnlyAssertions<TimeOnlyAssertions>(default) },
#endif
new object[] { new ExecutionTimeAssertions(new ExecutionTime(() => { }, () => new StopwatchTimer())) },
new object[] { new GuidAssertions<GuidAssertions>(default) },
new object[] { new MethodInfoSelectorAssertions() },
new object[] { new NumericAssertions<int, NumericAssertions<int>>(default) },
new object[] { new PropertyInfoSelectorAssertions() },
new object[] { new SimpleTimeSpanAssertions<SimpleTimeSpanAssertions>(default) },
new object[] { new TaskCompletionSourceAssertions<int>(default) },
new object[] { new TypeSelectorAssertions() },
new object[] { new EnumAssertions<StringComparison, EnumAssertions<StringComparison>>(default) }
};

[Theory]
[MemberData(nameof(ClassesWithGuardEquals))]
public void Guarding_equals_throws(object obj)
{
// Act
Action act = () => obj.Equals(null);

// Assert
act.Should().ThrowExactly<NotSupportedException>();
}

[Theory]
[InlineData(typeof(ReferenceTypeAssertions<object, ObjectAssertions>))]
[InlineData(typeof(BooleanAssertions<BooleanAssertions>))]
[InlineData(typeof(DateTimeAssertions<DateTimeAssertions>))]
[InlineData(typeof(DateTimeRangeAssertions<DateTimeAssertions>))]
[InlineData(typeof(DateTimeOffsetAssertions<DateTimeOffsetAssertions>))]
[InlineData(typeof(DateTimeOffsetRangeAssertions<DateTimeOffsetAssertions>))]
#if NET6_0_OR_GREATER
[InlineData(typeof(DateOnlyAssertions<DateOnlyAssertions>))]
[InlineData(typeof(TimeOnlyAssertions<TimeOnlyAssertions>))]
Expand Down Expand Up @@ -95,6 +132,11 @@ public void Should_methods_have_a_matching_overload_to_guard_against_chaining_an
.Where(m => !IsGuardOverload(m))
.Select(t => GetMostParentType(t.ReturnType))
.Distinct()
.Concat(new[]
{
typeof(DateTimeRangeAssertions<>),
typeof(DateTimeOffsetRangeAssertions<>)
})
.ToList();

List<Type> fakeOverloads = shouldOverloads
Expand Down