public void Max_Throws_If_Argument_Is_Null() { var source = Enumerable.Empty <Foo>(); var comparer = XComparer.By(_getFooValue); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed TestHelper.AssertThrowsWhenArgumentNull(() => source.Max(comparer)); }
public void Min_Throws_If_Source_Is_Null() { IEnumerable <Foo> source = null; var comparer = XComparer.By(_getFooValue); // ReSharper disable once AssignNullToNotNullAttribute // ReSharper disable once ReturnValueOfPureMethodIsNotUsed var ex = Assert.Throws <ArgumentNullException>(() => source.Min(comparer)); ex.ParamName.Should().Be("source"); }
public void By_Returns_A_Comparer_Based_On_Key() { var a = new Foo { X = 0, Y = 0 }; var b = new Foo { X = 1, Y = 0 }; Func <Foo, int> keySelector = f => f.X; var comparer = XComparer.By(keySelector); int expected = -1; int actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); }
public void ThenByDescending_Returns_A_Comparer_Based_On_First_Then_Descending_Second_Key() { var a = new Foo { X = 0, Y = 0 }; var b = new Foo { X = 0, Y = 1 }; Func <Foo, int> keySelector1 = f => f.X; Func <Foo, int> keySelector2 = f => f.Y; var comparer = XComparer.By(keySelector1).ThenByDescending(keySelector2); int expected = 1; int actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); }
public void ChainWith_Uses_Next_Comparer_If_First_Returns_Zero() { var a = new Foo { X = 0, Y = 0 }; var b = new Foo { X = 0, Y = 1 }; var first = XComparer <Foo> .By(f => f.X); var next = Intercept(XComparer <Foo> .By(f => f.Y)); var comparer = first.ChainWith(next); int expected = -1; int actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); Assert.AreEqual(1, next.CallCount); }
public void RankBy_Uses_Specified_Key_Comparer() { var source = new[] { new Player("Alice", -42), new Player("Bob", -25), new Player("Charlie", 75), new Player("David", -17), new Player("Emily", 31) }.ForbidMultipleEnumeration(); var keyComparer = XComparer <int> .By(Math.Abs); var result = source.RankBy(p => p.Score, (player, rank) => $"{rank}. {player.Name}", keyComparer); result.Should().Equal( "1. David", "2. Bob", "3. Emily", "4. Alice", "5. Charlie"); }
public void Chaining_Multiple_Comparers_Works_As_Well() { var a = new Foo { X = 0, Y = 0, Z = 1 }; var b = new Foo { X = 0, Y = 0, Z = 0 }; var first = XComparer <Foo> .By(f => f.X); var second = XComparer <Foo> .By(f => f.Y); var third = XComparer <Foo> .By(f => f.Z); // Test various ways of chaining // (first.second).third var comparer = first.ChainWith(second).ChainWith(third); int expected = 1; int actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); // first.(second.third) comparer = first.ChainWith(second.ChainWith(third)); expected = 1; actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); // (first.second).(first.second) comparer = first.ChainWith(second).ChainWith(first.ChainWith(second)); expected = 0; actual = comparer.Compare(a, b); Assert.AreEqual(expected, actual); }
public static IComparer <T> By <TKey>( [NotNull] Func <T, TKey> keySelector, IComparer <TKey>?keyComparer = null) { return(XComparer.By(keySelector, keyComparer)); }