Skip to content

Commit

Permalink
Merge pull request #401 from ankurMalhotra/master
Browse files Browse the repository at this point in the history
Compare Custom classes that don't implement IComparable (#364)
  • Loading branch information
JakeGinnivan committed Oct 15, 2016
2 parents d9f4e7d + e76c168 commit 0cecbee
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ public class static ShouldBeTestExtensions
public static void ShouldBeAssignableTo(this object actual, System.Type expected, [JetBrains.Annotations.InstantHandleAttribute()] System.Func<string> customMessage) { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected)
where T : System.IComparable<> { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer) { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, string customMessage) { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, System.Func<string> customMessage) { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected, string customMessage)
where T : System.IComparable<> { }
public static void ShouldBeGreaterThan<T>(this T actual, T expected, [JetBrains.Annotations.InstantHandleAttribute()] System.Func<string> customMessage)
Expand All @@ -345,6 +348,9 @@ public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected)
where T : System.IComparable<> { }
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, string customMessage)
where T : System.IComparable<> { }
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer) { }
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, string customMessage) { }
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, System.Func<string> customMessage) { }
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, [JetBrains.Annotations.InstantHandleAttribute()] System.Func<string> customMessage)
where T : System.IComparable<> { }
public static void ShouldBeInRange<T>(this T actual, T from, T to)
Expand All @@ -357,12 +363,18 @@ public static void ShouldBeLessThan<T>(this T actual, T expected)
where T : System.IComparable<> { }
public static void ShouldBeLessThan<T>(this T actual, T expected, string customMessage)
where T : System.IComparable<> { }
public static void ShouldBeLessThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer) { }
public static void ShouldBeLessThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, string customMessage) { }
public static void ShouldBeLessThan<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, System.Func<string> customMessage) { }
public static void ShouldBeLessThan<T>(this T actual, T expected, [JetBrains.Annotations.InstantHandleAttribute()] System.Func<string> customMessage)
where T : System.IComparable<> { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected)
where T : System.IComparable<> { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, string customMessage)
where T : System.IComparable<> { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer) { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, string customMessage) { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, System.Collections.Generic.IComparer<T> comparer, System.Func<string> customMessage) { }
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, [JetBrains.Annotations.InstantHandleAttribute()] System.Func<string> customMessage)
where T : System.IComparable<> { }
public static void ShouldBeNegative(this decimal actual) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Shouldly.Tests.Strings;
using Shouldly.Tests.TestHelpers;
using Xunit;

namespace Shouldly.Tests.ShouldBeGreaterOrEqualTo
{
public class CustomObjectScenario
{
[Fact]
public void CustomObjectScenarioShouldFail()
{
var customA = new Custom { Val = 1 };
var customB = new Custom { Val = 2 };
var comparer = new CustomComparer<Custom>();
Verify.ShouldFail(() =>
customA.ShouldBeGreaterThanOrEqualTo(customB, comparer, "Some additional context"),

errorWithSource:
@"customA
should be greater than or equal to
Shouldly.Tests.TestHelpers.Custom (000000)
but was
Shouldly.Tests.TestHelpers.Custom (000000)
Additional Info:
Some additional context",

errorWithoutSource:
@"Shouldly.Tests.TestHelpers.Custom (000000)
should be greater than or equal to
Shouldly.Tests.TestHelpers.Custom (000000)
but was not
Additional Info:
Some additional context"
);
}

[Fact]
public void ShouldPass()
{
var customA = new Custom { Val = 1 };
var customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
customA.ShouldBeGreaterThanOrEqualTo(customB, comparer);
}
}
}
49 changes: 49 additions & 0 deletions src/Shouldly.Tests/ShouldBeGreaterThan/CustomObjectScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Shouldly.Tests.Strings;
using Shouldly.Tests.TestHelpers;
using Xunit;

namespace Shouldly.Tests.ShouldBeGreaterThan
{
public class CustomObjectScenario
{

[Fact]
public void CustomObjectScenarioShouldFail()
{
var customA = new Custom { Val = 1 };
var customB = new Custom { Val = 2 };
var comparer = new CustomComparer<Custom>();
Verify.ShouldFail(() =>
customA.ShouldBeGreaterThan(customB, comparer, "Some additional context"),

errorWithSource:
@"customA
should be greater than
Shouldly.Tests.TestHelpers.Custom (000000)
but was
Shouldly.Tests.TestHelpers.Custom (000000)
Additional Info:
Some additional context",

errorWithoutSource:
@"Shouldly.Tests.TestHelpers.Custom (000000)
should be greater than
Shouldly.Tests.TestHelpers.Custom (000000)
but was not
Additional Info:
Some additional context"
);
}

[Fact]
public void ShouldPass()
{
var customA = new Custom { Val = 2 };
var customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
customA.ShouldBeGreaterThan(customB, comparer);
}
}
}
48 changes: 48 additions & 0 deletions src/Shouldly.Tests/ShouldBeLessThan/CustomObjectScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Shouldly.Tests.Strings;
using Shouldly.Tests.TestHelpers;
using Xunit;

namespace Shouldly.Tests.ShouldBeLessThan
{
public class CustomObjectScenario
{
[Fact]
public void CustomObjectScenarioShouldFail()
{
var customA = new Custom { Val = 1 };
var customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
Verify.ShouldFail(() =>
customA.ShouldBeLessThan(customB, comparer, "Some additional context"),

errorWithSource:
@"customA
should be less than
Shouldly.Tests.TestHelpers.Custom (000000)
but was
Shouldly.Tests.TestHelpers.Custom (000000)
Additional Info:
Some additional context",

errorWithoutSource:
@"Shouldly.Tests.TestHelpers.Custom (000000)
should be less than
Shouldly.Tests.TestHelpers.Custom (000000)
but was not
Additional Info:
Some additional context"
);
}

[Fact]
public void ShouldPass()
{
var customA = new Custom { Val = 2 };
var customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
customB.ShouldBeLessThan(customA, comparer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Shouldly.Tests.Strings;
using Shouldly.Tests.TestHelpers;
using Xunit;

namespace Shouldly.Tests.ShouldBeLessThanOrEqualTo
{
public class CustomObjectScenario
{
[Fact]
public void CustomObjectScenarioShouldFail()
{
var customA = new Custom { Val = 2 };
var customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
Verify.ShouldFail(() =>
customA.ShouldBeLessThanOrEqualTo(customB, comparer, "Some additional context"),

errorWithSource:
@"customA
should be less than or equal to
Shouldly.Tests.TestHelpers.Custom (000000)
but was
Shouldly.Tests.TestHelpers.Custom (000000)
Additional Info:
Some additional context",

errorWithoutSource:
@"Shouldly.Tests.TestHelpers.Custom (000000)
should be less than or equal to
Shouldly.Tests.TestHelpers.Custom (000000)
but was not
Additional Info:
Some additional context"
);
}

[Fact]
public void ShouldPass()
{
Custom customA = new Custom { Val = 1 };
Custom customB = new Custom { Val = 1 };
var comparer = new CustomComparer<Custom>();
customA.ShouldBeLessThanOrEqualTo(customB, comparer);
}
}
}
21 changes: 21 additions & 0 deletions src/Shouldly.Tests/TestHelpers/CustomComparerClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;

namespace Shouldly.Tests.TestHelpers
{
internal class CustomComparer<T> : IComparer<T>
{
public int Compare(T x, T y)
{
Custom x1 = x as Custom;
Custom x2 = y as Custom;
if (x1.Val == x2.Val)
return 0;
return x1.Val > x2.Val ? 1 : -1;
}
}

internal class Custom
{
public int Val { get; set; }
}
}
24 changes: 24 additions & 0 deletions src/Shouldly/Internals/Is.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,45 @@ public static bool GreaterThanOrEqualTo<T>(IComparable<T> comparable, T expected
return Compare(comparable, expected) >= 0;
}

public static bool GreaterThanOrEqualTo<T>(T actual, T expected, IComparer<T> comparer)
{
return Compare(actual, expected, comparer) >= 0;
}

public static bool LessThanOrEqualTo<T>(IComparable<T> comparable, T expected)
{
return Compare(comparable, expected) <= 0;
}

public static bool LessThanOrEqualTo<T>(T actual, T expected, IComparer<T> comparer)
{
return Compare(actual, expected, comparer) <= 0;
}

public static bool GreaterThan<T>(IComparable<T> comparable, T expected)
{
return Compare(comparable, expected) > 0;
}

public static bool GreaterThan<T>(T actual, T expected, IComparer<T> comparer)
{
return Compare(actual, expected, comparer) > 0;
}

public static bool LessThan<T>(IComparable<T> comparable, T expected)
{
return Compare(comparable, expected) < 0;
}

public static bool LessThan<T>(T actual, T expected, IComparer<T> comparer)
{
return Compare(actual, expected, comparer) < 0;
}

static decimal Compare<T>(T actual, T expected, IComparer<T> comparer)
{
return comparer.Compare(actual, expected);
}
static decimal Compare<T>(IComparable<T> comparable, T expected)
{
#if NewReflection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using JetBrains.Annotations;
using System.Collections.Generic;

namespace Shouldly
{
Expand All @@ -10,6 +11,20 @@ public static partial class ShouldBeTestExtensions
ShouldBeGreaterThan(actual, expected, () => null);
}

public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer)
{
ShouldBeGreaterThan(actual, expected, comparer, () => null);
}

public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage)
{
ShouldBeGreaterThan(actual, expected, comparer, () => customMessage);
}

public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage)
{
actual.AssertAwesomely(v => Is.GreaterThan(actual, expected, comparer), actual, expected, customMessage);
}
public static void ShouldBeGreaterThan<T>(this T actual, T expected, string customMessage) where T : IComparable<T>
{
ShouldBeGreaterThan(actual, expected, () => customMessage);
Expand All @@ -30,6 +45,21 @@ public static partial class ShouldBeTestExtensions
ShouldBeLessThan(actual, expected, () => customMessage);
}

public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer)
{
ShouldBeLessThan(actual, expected, comparer, () => null);
}

public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage)
{
ShouldBeLessThan(actual, expected, comparer, () => customMessage);
}

public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage)
{
actual.AssertAwesomely(v => Is.LessThan(actual, expected, comparer), actual, expected, customMessage);
}

public static void ShouldBeLessThan<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>
{
actual.AssertAwesomely(v => Is.LessThan(v, expected), actual, expected, customMessage);
Expand All @@ -45,6 +75,21 @@ public static partial class ShouldBeTestExtensions
ShouldBeGreaterThanOrEqualTo(actual, expected, () => customMessage);
}

public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer)
{
ShouldBeGreaterThanOrEqualTo(actual, expected, comparer, () => null);
}

public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage)
{
ShouldBeGreaterThanOrEqualTo(actual, expected, comparer, () => customMessage);
}

public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage)
{
actual.AssertAwesomely(v => Is.GreaterThanOrEqualTo(actual, expected, comparer), actual, expected, customMessage);
}

public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>
{
actual.AssertAwesomely(v => Is.GreaterThanOrEqualTo(v, expected), actual, expected, customMessage);
Expand All @@ -60,6 +105,21 @@ public static partial class ShouldBeTestExtensions
ShouldBeLessThanOrEqualTo(actual, expected, () => customMessage);
}

public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer)
{
ShouldBeLessThanOrEqualTo(actual, expected, comparer, () => null);
}

public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage)
{
ShouldBeLessThanOrEqualTo(actual, expected, comparer, () => customMessage);
}

public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage)
{
actual.AssertAwesomely(v => Is.LessThanOrEqualTo(actual, expected, comparer), actual, expected, customMessage);
}

public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>
{
actual.AssertAwesomely(v => Is.LessThanOrEqualTo(v, expected), actual, expected, customMessage);
Expand Down

0 comments on commit 0cecbee

Please sign in to comment.