From 8bbcb7913ac285937d245a03ab90bfd8cc1a6a2a Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 27 Apr 2018 11:35:58 +0200 Subject: [PATCH 1/8] Add UsingTestingSequence to enforce simpler & correct usage --- MoreLinq.Test/RankTest.cs | 39 ++++++++++++++++++-------------- MoreLinq.Test/TestingSequence.cs | 15 ++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/MoreLinq.Test/RankTest.cs b/MoreLinq.Test/RankTest.cs index b0132fe91..deaf0aaed 100644 --- a/MoreLinq.Test/RankTest.cs +++ b/MoreLinq.Test/RankTest.cs @@ -1,6 +1,7 @@ namespace MoreLinq.Test { using System; + using System.Collections.Generic; using NUnit.Framework; /// @@ -34,7 +35,8 @@ public void TestRankByIsLazy() public void TestRankNullComparer() { var sequence = Enumerable.Repeat(1, 10); - sequence.AsTestingSequence().Rank(null).AssertSequenceEqual(sequence); + sequence.UsingTestingSequence(ts => ts.Rank(null)) + .AssertSequenceEqual(sequence); } /// @@ -44,7 +46,8 @@ public void TestRankNullComparer() public void TestRankByNullComparer() { var sequence = Enumerable.Repeat(1, 10); - sequence.AsTestingSequence().RankBy(x => x, null).AssertSequenceEqual(sequence); + sequence.UsingTestingSequence(ts => ts.RankBy(x => x, null)) + .AssertSequenceEqual(sequence); } /// @@ -56,11 +59,11 @@ public void TestRankDescendingSequence() { const int count = 100; var sequence = Enumerable.Range(456, count).Reverse(); - var result = sequence.AsTestingSequence().Rank().ToArray(); - var expectedResult = Enumerable.Range(1, count); + + var result = sequence.UsingTestingSequence(ts => ts.Rank()); Assert.AreEqual(count, result.Length); - Assert.IsTrue(result.SequenceEqual(expectedResult)); + Assert.IsTrue(result.SequenceEqual(Enumerable.Range(1, count))); } /// @@ -72,11 +75,11 @@ public void TestRankByAscendingSeries() { const int count = 100; var sequence = Enumerable.Range(456, count); - var result = sequence.AsTestingSequence().Rank().ToArray(); - var expectedResult = Enumerable.Range(1, count).Reverse(); + + var result = sequence.UsingTestingSequence(ts => ts.Rank()); Assert.AreEqual(count, result.Length); - Assert.IsTrue(result.SequenceEqual(expectedResult)); + Assert.IsTrue(result.SequenceEqual(Enumerable.Range(1, count).Reverse())); } /// @@ -87,7 +90,7 @@ public void TestRankEquivalentItems() { const int count = 100; var sequence = Enumerable.Repeat(1234, count); - var result = sequence.AsTestingSequence().Rank().ToArray(); + var result = sequence.UsingTestingSequence(ts => ts.Rank()); Assert.AreEqual(count, result.Length); Assert.IsTrue(result.SequenceEqual(Enumerable.Repeat(1, count))); @@ -103,7 +106,8 @@ public void TestRankGroupedItems() var sequence = Enumerable.Range(0, count) .Concat(Enumerable.Range(0, count)) .Concat(Enumerable.Range(0, count)); - var result = sequence.AsTestingSequence().Rank(); + + var result = sequence.UsingTestingSequence(ts => ts.Rank()); Assert.AreEqual(count, result.Distinct().Count()); Assert.IsTrue(result.SequenceEqual(sequence.Reverse().Select(x => x + 1))); @@ -115,11 +119,11 @@ public void TestRankGroupedItems() [Test] public void TestRankOfHighestItemIsOne() { - const int count = 10; - var sequence = Enumerable.Range(1, count); - var result = sequence.AsTestingSequence().Rank(); + var result = + Enumerable.Range(1, 10).UsingTestingSequence(ts => + ts.Rank().OrderBy(x => x).First()); - Assert.AreEqual(1, result.OrderBy(x => x).First()); + Assert.AreEqual(1, result); } /// @@ -139,7 +143,8 @@ public void TestRankByKeySelector() new { Name = "Jim", Age = 74, ExpectedRank = 1 }, new { Name = "Jes", Age = 11, ExpectedRank = 8 }, }; - var result = sequence.AsTestingSequence().RankBy(x => x.Age).ToArray(); + var result = sequence.UsingTestingSequence(ts => + ts.RankBy(x => x.Age)); Assert.AreEqual(sequence.Length, result.Length); Assert.IsTrue(result.SequenceEqual(sequence.Select(x => x.ExpectedRank))); @@ -155,8 +160,8 @@ public void TestRankCustomComparer() var ordinals = Enumerable.Range(1, count); var sequence = ordinals.Select( x => new DateTime(2010,x,20-x) ); // invert the CompareTo operation to Rank in reverse order (ascening to descending) - var resultA = sequence.AsTestingSequence().Rank(Comparer.Create((a, b) => -a.CompareTo(b))); - var resultB = sequence.AsTestingSequence().RankBy(x => x.Day, Comparer.Create((a, b) => -a.CompareTo(b))); + var resultA = sequence.UsingTestingSequence(ts => ts.Rank(Comparer.Create((a, b) => -a.CompareTo(b)))); + var resultB = sequence.UsingTestingSequence(ts => ts.RankBy(x => x.Day, Comparer.Create((a, b) => -a.CompareTo(b)))); Assert.IsTrue(resultA.SequenceEqual(ordinals)); Assert.IsTrue(resultB.SequenceEqual(ordinals.Reverse())); diff --git a/MoreLinq.Test/TestingSequence.cs b/MoreLinq.Test/TestingSequence.cs index 6abdac68e..79d3733a9 100644 --- a/MoreLinq.Test/TestingSequence.cs +++ b/MoreLinq.Test/TestingSequence.cs @@ -27,10 +27,25 @@ static class TestingSequence internal static TestingSequence Of(params T[] elements) => new TestingSequence(elements); + [Obsolete("Use " + nameof(UsingTestingSequence) + " instead.")] internal static TestingSequence AsTestingSequence(this IEnumerable source) => source != null ? new TestingSequence(source) : throw new ArgumentNullException(nameof(source)); + + public static TResult UsingTestingSequence(this IEnumerable source, + Func, TResult> user) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (user == null) throw new ArgumentNullException(nameof(user)); + + using (var ts = source.AsTestingSequence()) + return user(ts); + } + + public static TResult[] UsingTestingSequence(this IEnumerable source, + Func, IEnumerable> user) => + source.UsingTestingSequence(ts => user(ts).ToArray()); } /// From b6ea36c9aa7fb725a723a9f00e7c1e9ba379b3e0 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sun, 29 Apr 2018 13:50:15 +0200 Subject: [PATCH 2/8] Add Use for TestingSequence --- MoreLinq.Test/RankTest.cs | 7 ++----- MoreLinq.Test/TestingSequence.cs | 18 +++++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/MoreLinq.Test/RankTest.cs b/MoreLinq.Test/RankTest.cs index deaf0aaed..0681ef367 100644 --- a/MoreLinq.Test/RankTest.cs +++ b/MoreLinq.Test/RankTest.cs @@ -119,11 +119,8 @@ public void TestRankGroupedItems() [Test] public void TestRankOfHighestItemIsOne() { - var result = - Enumerable.Range(1, 10).UsingTestingSequence(ts => - ts.Rank().OrderBy(x => x).First()); - - Assert.AreEqual(1, result); + using (var ts = Enumerable.Range(1, 10).AsTestingSequence()) + Assert.AreEqual(1, ts.Rank().OrderBy(x => x).First()); } /// diff --git a/MoreLinq.Test/TestingSequence.cs b/MoreLinq.Test/TestingSequence.cs index 79d3733a9..15d4b2d1f 100644 --- a/MoreLinq.Test/TestingSequence.cs +++ b/MoreLinq.Test/TestingSequence.cs @@ -27,25 +27,29 @@ static class TestingSequence internal static TestingSequence Of(params T[] elements) => new TestingSequence(elements); - [Obsolete("Use " + nameof(UsingTestingSequence) + " instead.")] internal static TestingSequence AsTestingSequence(this IEnumerable source) => source != null ? new TestingSequence(source) : throw new ArgumentNullException(nameof(source)); - public static TResult UsingTestingSequence(this IEnumerable source, - Func, TResult> user) + public static TResult[] Use(this TestingSequence source, + Func, IEnumerable> user) { if (source == null) throw new ArgumentNullException(nameof(source)); if (user == null) throw new ArgumentNullException(nameof(user)); - using (var ts = source.AsTestingSequence()) - return user(ts); + return user(source).ToArray(); } public static TResult[] UsingTestingSequence(this IEnumerable source, - Func, IEnumerable> user) => - source.UsingTestingSequence(ts => user(ts).ToArray()); + Func, IEnumerable> user) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (source is TestingSequence) throw new ArgumentException("Source is already a testing sequence instance."); + if (user == null) throw new ArgumentNullException(nameof(user)); + + return source.AsTestingSequence().Use(user); + } } /// From 2db1cc3fd0022593d2a4d07bb324db1c691048da Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sun, 29 Apr 2018 13:28:36 +0200 Subject: [PATCH 3/8] Refactor CountDown tests --- MoreLinq.Test/CountDownTest.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/MoreLinq.Test/CountDownTest.cs b/MoreLinq.Test/CountDownTest.cs index 7a4d0b420..19348b6c7 100644 --- a/MoreLinq.Test/CountDownTest.cs +++ b/MoreLinq.Test/CountDownTest.cs @@ -56,14 +56,9 @@ static IEnumerable GetData(Func selector) .SetName($"{nameof(WithSequence)}({{ {string.Join(", ", e.Source)} }}, {e.Count})"); [TestCaseSource(nameof(SequenceData))] - public IEnumerable<(int, int?)> WithSequence(int[] xs, int count) - { - using (var ts = xs.Select(x => x).AsTestingSequence()) - { - foreach (var e in ts.CountDown(count, ValueTuple.Create)) - yield return e; - } - } + public IEnumerable<(int, int?)> WithSequence(int[] xs, int count) => + xs.Select(x => x) + .UsingTestingSequence(ts => ts.CountDown(count, ValueTuple.Create)); static readonly IEnumerable ListData = from e in GetData((xs, count, countdown) => new From 3639c881e209828ff72ad9150d2532bb425be677 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Mon, 30 Apr 2018 23:29:21 +0200 Subject: [PATCH 4/8] Refactor Fold tests --- MoreLinq.Test/FoldTest.cs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/MoreLinq.Test/FoldTest.cs b/MoreLinq.Test/FoldTest.cs index 90906849c..015e301ff 100644 --- a/MoreLinq.Test/FoldTest.cs +++ b/MoreLinq.Test/FoldTest.cs @@ -49,22 +49,22 @@ public void Fold() { const string alphabet = "abcdefghijklmnopqrstuvwxyz"; - using (var ts = alphabet.Take( 1).AsTestingSequence()) Assert.That(ts.Fold(a => string.Join(string.Empty, a )), Is.EqualTo("a" ), "fold 1" ); - using (var ts = alphabet.Take( 2).AsTestingSequence()) Assert.That(ts.Fold((a, b ) => string.Join(string.Empty, a, b )), Is.EqualTo("ab" ), "fold 2" ); - using (var ts = alphabet.Take( 3).AsTestingSequence()) Assert.That(ts.Fold((a, b, c ) => string.Join(string.Empty, a, b, c )), Is.EqualTo("abc" ), "fold 3" ); - using (var ts = alphabet.Take( 4).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d ) => string.Join(string.Empty, a, b, c, d )), Is.EqualTo("abcd" ), "fold 4" ); - using (var ts = alphabet.Take( 5).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e ) => string.Join(string.Empty, a, b, c, d, e )), Is.EqualTo("abcde" ), "fold 5" ); - using (var ts = alphabet.Take( 6).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f ) => string.Join(string.Empty, a, b, c, d, e, f )), Is.EqualTo("abcdef" ), "fold 6" ); - using (var ts = alphabet.Take( 7).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g ) => string.Join(string.Empty, a, b, c, d, e, f, g )), Is.EqualTo("abcdefg" ), "fold 7" ); - using (var ts = alphabet.Take( 8).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h ) => string.Join(string.Empty, a, b, c, d, e, f, g, h )), Is.EqualTo("abcdefgh" ), "fold 8" ); - using (var ts = alphabet.Take( 9).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i )), Is.EqualTo("abcdefghi" ), "fold 9" ); - using (var ts = alphabet.Take(10).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j )), Is.EqualTo("abcdefghij" ), "fold 10"); - using (var ts = alphabet.Take(11).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k )), Is.EqualTo("abcdefghijk" ), "fold 11"); - using (var ts = alphabet.Take(12).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l )), Is.EqualTo("abcdefghijkl" ), "fold 12"); - using (var ts = alphabet.Take(13).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m )), Is.EqualTo("abcdefghijklm" ), "fold 13"); - using (var ts = alphabet.Take(14).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n )), Is.EqualTo("abcdefghijklmn" ), "fold 14"); - using (var ts = alphabet.Take(15).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o )), Is.EqualTo("abcdefghijklmno" ), "fold 15"); - using (var ts = alphabet.Take(16).AsTestingSequence()) Assert.That(ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)), Is.EqualTo("abcdefghijklmnop"), "fold 16"); + Assert.That(alphabet.Take( 1).UsingTestingSequence(ts => ts.Fold(a => string.Join(string.Empty, a ))), Is.EqualTo("a" ), "fold 1" ); + Assert.That(alphabet.Take( 2).UsingTestingSequence(ts => ts.Fold((a, b ) => string.Join(string.Empty, a, b ))), Is.EqualTo("ab" ), "fold 2" ); + Assert.That(alphabet.Take( 3).UsingTestingSequence(ts => ts.Fold((a, b, c ) => string.Join(string.Empty, a, b, c ))), Is.EqualTo("abc" ), "fold 3" ); + Assert.That(alphabet.Take( 4).UsingTestingSequence(ts => ts.Fold((a, b, c, d ) => string.Join(string.Empty, a, b, c, d ))), Is.EqualTo("abcd" ), "fold 4" ); + Assert.That(alphabet.Take( 5).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e ) => string.Join(string.Empty, a, b, c, d, e ))), Is.EqualTo("abcde" ), "fold 5" ); + Assert.That(alphabet.Take( 6).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f ) => string.Join(string.Empty, a, b, c, d, e, f ))), Is.EqualTo("abcdef" ), "fold 6" ); + Assert.That(alphabet.Take( 7).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g ) => string.Join(string.Empty, a, b, c, d, e, f, g ))), Is.EqualTo("abcdefg" ), "fold 7" ); + Assert.That(alphabet.Take( 8).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h ) => string.Join(string.Empty, a, b, c, d, e, f, g, h ))), Is.EqualTo("abcdefgh" ), "fold 8" ); + Assert.That(alphabet.Take( 9).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i ))), Is.EqualTo("abcdefghi" ), "fold 9" ); + Assert.That(alphabet.Take(10).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j ))), Is.EqualTo("abcdefghij" ), "fold 10"); + Assert.That(alphabet.Take(11).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k ))), Is.EqualTo("abcdefghijk" ), "fold 11"); + Assert.That(alphabet.Take(12).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l ))), Is.EqualTo("abcdefghijkl" ), "fold 12"); + Assert.That(alphabet.Take(13).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m ))), Is.EqualTo("abcdefghijklm" ), "fold 13"); + Assert.That(alphabet.Take(14).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n ))), Is.EqualTo("abcdefghijklmn" ), "fold 14"); + Assert.That(alphabet.Take(15).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o ) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o ))), Is.EqualTo("abcdefghijklmno" ), "fold 15"); + Assert.That(alphabet.Take(16).UsingTestingSequence(ts => ts.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) => string.Join(string.Empty, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p))), Is.EqualTo("abcdefghijklmnop"), "fold 16"); } } } From 44afc77245b3a885918546167a0c867452f0a34a Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Tue, 26 Nov 2019 19:35:37 +0100 Subject: [PATCH 5/8] Remove unused import --- MoreLinq.Test/RankTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MoreLinq.Test/RankTest.cs b/MoreLinq.Test/RankTest.cs index 989108d58..c55025c98 100644 --- a/MoreLinq.Test/RankTest.cs +++ b/MoreLinq.Test/RankTest.cs @@ -18,7 +18,6 @@ namespace MoreLinq.Test { using System; - using System.Collections.Generic; using NUnit.Framework; /// From c9a873255d72185dbf917726048323f7178f26ec Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 30 Nov 2022 22:05:36 +0100 Subject: [PATCH 6/8] Add missing final newline --- MoreLinq.Test/CountDownTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/CountDownTest.cs b/MoreLinq.Test/CountDownTest.cs index 33ae8c210..2f35b7c2e 100644 --- a/MoreLinq.Test/CountDownTest.cs +++ b/MoreLinq.Test/CountDownTest.cs @@ -209,4 +209,4 @@ public ReadOnlyCollection(ICollection collection, } } } -} \ No newline at end of file +} From 369576cd720c10eebf0b09ff80dec882ed77b893 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 30 Nov 2022 22:22:21 +0100 Subject: [PATCH 7/8] Use using statement --- MoreLinq.Test/RankTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MoreLinq.Test/RankTest.cs b/MoreLinq.Test/RankTest.cs index c55025c98..af0bd26e4 100644 --- a/MoreLinq.Test/RankTest.cs +++ b/MoreLinq.Test/RankTest.cs @@ -135,8 +135,8 @@ public void TestRankGroupedItems() [Test] public void TestRankOfHighestItemIsOne() { - using (var ts = Enumerable.Range(1, 10).AsTestingSequence()) - Assert.AreEqual(1, ts.Rank().OrderBy(x => x).First()); + using var ts = Enumerable.Range(1, 10).AsTestingSequence(); + Assert.AreEqual(1, ts.Rank().OrderBy(x => x).First()); } /// From 22acf6f0866309f68cbd1909663698280ca2f00a Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 30 Nov 2022 22:39:53 +0100 Subject: [PATCH 8/8] Address "EndsWith" tests --- MoreLinq.Test/EndsWithTest.cs | 56 +++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/MoreLinq.Test/EndsWithTest.cs b/MoreLinq.Test/EndsWithTest.cs index 7d4c1cb14..b76bb1ba9 100644 --- a/MoreLinq.Test/EndsWithTest.cs +++ b/MoreLinq.Test/EndsWithTest.cs @@ -18,7 +18,6 @@ namespace MoreLinq.Test { using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; using NUnit.Framework; [TestFixture] @@ -29,7 +28,9 @@ public class EndsWithTest [TestCase(new[] {1, 2, 3}, new[] {0, 1, 2, 3}, ExpectedResult = false)] public bool EndsWithWithIntegers(IEnumerable first, IEnumerable second) { - return first.EndsWith(second); + using var fts = first.AsTestingSequence(); + using var sts = second.AsTestingSequence(); + return fts.EndsWith(sts); } [TestCase(new[] {'1', '2', '3'}, new[] {'2', '3'}, ExpectedResult = true)] @@ -37,7 +38,9 @@ public bool EndsWithWithIntegers(IEnumerable first, IEnumerable second [TestCase(new[] {'1', '2', '3'}, new[] {'0', '1', '2', '3'}, ExpectedResult = false)] public bool EndsWithWithChars(IEnumerable first, IEnumerable second) { - return first.EndsWith(second); + using var fts = first.AsTestingSequence(); + using var sts = second.AsTestingSequence(); + return fts.EndsWith(sts); } [TestCase("123", "23", ExpectedResult = true)] @@ -45,50 +48,71 @@ public bool EndsWithWithChars(IEnumerable first, IEnumerable second) [TestCase("123", "0123", ExpectedResult = false)] public bool EndsWithWithStrings(string first, string second) { - // Conflict with String.EndsWith(), which has precedence in this case - return MoreEnumerable.EndsWith(first, second); + using var fts = first.AsTestingSequence(); + using var sts = second.AsTestingSequence(); + return fts.EndsWith(sts); } [Test] public void EndsWithReturnsTrueIfBothEmpty() { - Assert.True(new int[0].EndsWith(new int[0])); + using var fts = TestingSequence.Of(); + using var sts = TestingSequence.Of(); + Assert.True(fts.EndsWith(sts)); } [Test] public void EndsWithReturnsFalseIfOnlyFirstIsEmpty() { - Assert.False(new int[0].EndsWith(new[] {1,2,3})); + using var fts = TestingSequence.Of(); + using var sts = TestingSequence.Of(1, 2, 3); + Assert.False(fts.EndsWith(sts)); } [TestCase("", "", ExpectedResult = true)] [TestCase("1", "", ExpectedResult = true)] public bool EndsWithReturnsTrueIfSecondIsEmpty(string first, string second) { - // Conflict with String.EndsWith(), which has precedence in this case - return MoreEnumerable.EndsWith(first, second); + using var fts = first.AsTestingSequence(); + using var sts = second.AsTestingSequence(); + return fts.EndsWith(sts); } [Test] public void EndsWithDisposesBothSequenceEnumerators() { - using var first = TestingSequence.Of(1,2,3); + using var first = TestingSequence.Of(1, 2, 3); using var second = TestingSequence.Of(1); first.EndsWith(second); } [Test] - [SuppressMessage("ReSharper", "RedundantArgumentDefaultValue")] - public void EndsWithUsesSpecifiedEqualityComparerOrDefault() + public void EndsWithUsesDefaultEqualityComparerByDefault() { - var first = new[] {1,2,3}; - var second = new[] {4,5,6}; + using var first = TestingSequence.Of(1, 2, 3); + using var second = TestingSequence.Of(4, 5, 6); Assert.False(first.EndsWith(second)); + } + + [Test] + public void EndsWithUsesDefaultEqualityComparerWhenNullSpecified() + { + using var first = TestingSequence.Of(1, 2, 3); + using var second = TestingSequence.Of(4, 5, 6); + Assert.False(first.EndsWith(second, null)); - Assert.False(first.EndsWith(second, EqualityComparer.Create(delegate { return false; }))); - Assert.True(first.EndsWith(second, EqualityComparer.Create(delegate { return true; }))); + } + + [Test] + [TestCase(false, ExpectedResult = false)] + [TestCase(true, ExpectedResult = true)] + public bool EndsWithUsesSpecifiedEqualityComparer(bool result) + { + using var first = TestingSequence.Of(1, 2, 3); + using var second = TestingSequence.Of(4, 5, 6); + return first.EndsWith(second, EqualityComparer.Create((_, _) => result)); } [TestCase(SourceKind.BreakingCollection)]