Skip to content

Commit

Permalink
Add difference to numeric assertion failure messages (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
iliashkolyar committed Apr 2, 2022
1 parent e32b95f commit 51b0232
Show file tree
Hide file tree
Showing 33 changed files with 1,584 additions and 72 deletions.
40 changes: 20 additions & 20 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -496,7 +496,7 @@ public static ComparableTypeAssertions<T> Should<T>(this IComparable<T> comparab
[Pure]
public static NumericAssertions<int> Should(this int actualValue)
{
return new NumericAssertions<int>(actualValue);
return new Int32Assertions(actualValue);
}

/// <summary>
Expand All @@ -506,7 +506,7 @@ public static NumericAssertions<int> Should(this int actualValue)
[Pure]
public static NullableNumericAssertions<int> Should(this int? actualValue)
{
return new NullableNumericAssertions<int>(actualValue);
return new NullableInt32Assertions(actualValue);
}

/// <summary>
Expand All @@ -516,7 +516,7 @@ public static NullableNumericAssertions<int> Should(this int? actualValue)
[Pure]
public static NumericAssertions<uint> Should(this uint actualValue)
{
return new NumericAssertions<uint>(actualValue);
return new UInt32Assertions(actualValue);
}

/// <summary>
Expand All @@ -526,7 +526,7 @@ public static NumericAssertions<uint> Should(this uint actualValue)
[Pure]
public static NullableNumericAssertions<uint> Should(this uint? actualValue)
{
return new NullableNumericAssertions<uint>(actualValue);
return new NullableUInt32Assertions(actualValue);
}

/// <summary>
Expand All @@ -536,7 +536,7 @@ public static NullableNumericAssertions<uint> Should(this uint? actualValue)
[Pure]
public static NumericAssertions<decimal> Should(this decimal actualValue)
{
return new NumericAssertions<decimal>(actualValue);
return new DecimalAssertions(actualValue);
}

/// <summary>
Expand All @@ -546,7 +546,7 @@ public static NumericAssertions<decimal> Should(this decimal actualValue)
[Pure]
public static NullableNumericAssertions<decimal> Should(this decimal? actualValue)
{
return new NullableNumericAssertions<decimal>(actualValue);
return new NullableDecimalAssertions(actualValue);
}

/// <summary>
Expand All @@ -556,7 +556,7 @@ public static NullableNumericAssertions<decimal> Should(this decimal? actualValu
[Pure]
public static NumericAssertions<byte> Should(this byte actualValue)
{
return new NumericAssertions<byte>(actualValue);
return new ByteAssertions(actualValue);
}

/// <summary>
Expand All @@ -566,7 +566,7 @@ public static NumericAssertions<byte> Should(this byte actualValue)
[Pure]
public static NullableNumericAssertions<byte> Should(this byte? actualValue)
{
return new NullableNumericAssertions<byte>(actualValue);
return new NullableByteAssertions(actualValue);
}

/// <summary>
Expand All @@ -576,7 +576,7 @@ public static NullableNumericAssertions<byte> Should(this byte? actualValue)
[Pure]
public static NumericAssertions<sbyte> Should(this sbyte actualValue)
{
return new NumericAssertions<sbyte>(actualValue);
return new SByteAssertions(actualValue);
}

/// <summary>
Expand All @@ -586,7 +586,7 @@ public static NumericAssertions<sbyte> Should(this sbyte actualValue)
[Pure]
public static NullableNumericAssertions<sbyte> Should(this sbyte? actualValue)
{
return new NullableNumericAssertions<sbyte>(actualValue);
return new NullableSByteAssertions(actualValue);
}

/// <summary>
Expand All @@ -596,7 +596,7 @@ public static NullableNumericAssertions<sbyte> Should(this sbyte? actualValue)
[Pure]
public static NumericAssertions<short> Should(this short actualValue)
{
return new NumericAssertions<short>(actualValue);
return new Int16Assertions(actualValue);
}

/// <summary>
Expand All @@ -606,7 +606,7 @@ public static NumericAssertions<short> Should(this short actualValue)
[Pure]
public static NullableNumericAssertions<short> Should(this short? actualValue)
{
return new NullableNumericAssertions<short>(actualValue);
return new NullableInt16Assertions(actualValue);
}

/// <summary>
Expand All @@ -616,7 +616,7 @@ public static NullableNumericAssertions<short> Should(this short? actualValue)
[Pure]
public static NumericAssertions<ushort> Should(this ushort actualValue)
{
return new NumericAssertions<ushort>(actualValue);
return new UInt16Assertions(actualValue);
}

/// <summary>
Expand All @@ -626,7 +626,7 @@ public static NumericAssertions<ushort> Should(this ushort actualValue)
[Pure]
public static NullableNumericAssertions<ushort> Should(this ushort? actualValue)
{
return new NullableNumericAssertions<ushort>(actualValue);
return new NullableUInt16Assertions(actualValue);
}

/// <summary>
Expand All @@ -636,7 +636,7 @@ public static NullableNumericAssertions<ushort> Should(this ushort? actualValue)
[Pure]
public static NumericAssertions<long> Should(this long actualValue)
{
return new NumericAssertions<long>(actualValue);
return new Int64Assertions(actualValue);
}

/// <summary>
Expand All @@ -646,7 +646,7 @@ public static NumericAssertions<long> Should(this long actualValue)
[Pure]
public static NullableNumericAssertions<long> Should(this long? actualValue)
{
return new NullableNumericAssertions<long>(actualValue);
return new NullableInt64Assertions(actualValue);
}

/// <summary>
Expand All @@ -656,7 +656,7 @@ public static NullableNumericAssertions<long> Should(this long? actualValue)
[Pure]
public static NumericAssertions<ulong> Should(this ulong actualValue)
{
return new NumericAssertions<ulong>(actualValue);
return new UInt64Assertions(actualValue);
}

/// <summary>
Expand All @@ -666,7 +666,7 @@ public static NumericAssertions<ulong> Should(this ulong actualValue)
[Pure]
public static NullableNumericAssertions<ulong> Should(this ulong? actualValue)
{
return new NullableNumericAssertions<ulong>(actualValue);
return new NullableUInt64Assertions(actualValue);
}

/// <summary>
Expand All @@ -676,7 +676,7 @@ public static NullableNumericAssertions<ulong> Should(this ulong? actualValue)
[Pure]
public static NumericAssertions<float> Should(this float actualValue)
{
return new FloatAssertions(actualValue);
return new SingleAssertions(actualValue);
}

/// <summary>
Expand All @@ -686,7 +686,7 @@ public static NumericAssertions<float> Should(this float actualValue)
[Pure]
public static NullableNumericAssertions<float> Should(this float? actualValue)
{
return new NullableFloatAssertions(actualValue);
return new NullableSingleAssertions(actualValue);
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions Src/FluentAssertions/Numeric/ByteAssertions.cs
@@ -0,0 +1,22 @@
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="byte"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class ByteAssertions : NumericAssertions<byte>
{
internal ByteAssertions(byte value)
: base(value)
{
}

private protected override byte? CalculateDifferenceForFailureMessage(byte expected)
{
var difference = (byte?)(Subject - expected);
return difference != 0 ? difference : null;
}
}
}
30 changes: 30 additions & 0 deletions Src/FluentAssertions/Numeric/DecimalAssertions.cs
@@ -0,0 +1,30 @@
using System;
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="decimal"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class DecimalAssertions : NumericAssertions<decimal>
{
internal DecimalAssertions(decimal value)
: base(value)
{
}

private protected override decimal? CalculateDifferenceForFailureMessage(decimal expected)
{
try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
}
}
}
16 changes: 14 additions & 2 deletions Src/FluentAssertions/Numeric/DoubleAssertions.cs
@@ -1,12 +1,24 @@
namespace FluentAssertions.Numeric
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="double"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class DoubleAssertions : NumericAssertions<double>
{
public DoubleAssertions(double value)
internal DoubleAssertions(double value)
: base(value)
{
}

private protected override bool IsNaN(double value) => double.IsNaN(value);

private protected override double? CalculateDifferenceForFailureMessage(double expected)
{
var difference = Subject - expected;
return difference != 0 ? difference : null;
}
}
}
12 changes: 0 additions & 12 deletions Src/FluentAssertions/Numeric/FloatAssertions.cs

This file was deleted.

27 changes: 27 additions & 0 deletions Src/FluentAssertions/Numeric/Int16Assertions.cs
@@ -0,0 +1,27 @@
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="short"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class Int16Assertions : NumericAssertions<short>
{
internal Int16Assertions(short value)
: base(value)
{
}

private protected override short? CalculateDifferenceForFailureMessage(short expected)
{
if (Subject < 10 && expected < 10)
{
return null;
}

var difference = (short?)(Subject - expected);
return difference != 0 ? difference : null;
}
}
}
35 changes: 35 additions & 0 deletions Src/FluentAssertions/Numeric/Int32Assertions.cs
@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="int"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class Int32Assertions : NumericAssertions<int>
{
internal Int32Assertions(int value)
: base(value)
{
}

private protected override int? CalculateDifferenceForFailureMessage(int expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
}
}
}
35 changes: 35 additions & 0 deletions Src/FluentAssertions/Numeric/Int64Assertions.cs
@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="long"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class Int64Assertions : NumericAssertions<long>
{
internal Int64Assertions(long value)
: base(value)
{
}

private protected override long? CalculateDifferenceForFailureMessage(long expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
}
}
}
22 changes: 22 additions & 0 deletions Src/FluentAssertions/Numeric/NullableByteAssertions.cs
@@ -0,0 +1,22 @@
using System.Diagnostics;

namespace FluentAssertions.Numeric
{
/// <summary>
/// Contains a number of methods to assert that a nullable <see cref="byte"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
internal class NullableByteAssertions : NullableNumericAssertions<byte>
{
internal NullableByteAssertions(byte? value)
: base(value)
{
}

private protected override byte? CalculateDifferenceForFailureMessage(byte expected)
{
var difference = (byte?)(Subject - expected);
return difference != 0 ? difference : null;
}
}
}

0 comments on commit 51b0232

Please sign in to comment.