Skip to content

Commit

Permalink
Merge pull request #1891 from jnyrup/FakeShould
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Apr 16, 2022
2 parents e87aa94 + 9987ff3 commit 94dd522
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -1059,6 +1059,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 @@ -121,6 +121,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 @@ -129,6 +129,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 @@ -121,6 +121,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 @@ -121,6 +121,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 @@ -120,6 +120,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 @@ -121,6 +121,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

0 comments on commit 94dd522

Please sign in to comment.