From 0824fa696e63cd5144f800a1ed515b22e6652a79 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 8 Nov 2026 18:31:18 +0100 Subject: [PATCH] SortingGenericList: move to .NET 10, fix DescendingCompare null contract and IndexOf duplicates Target net10.0, update xunit 2.9.3, xunit.runner.visualstudio 4.0.1, Microsoft.NET.Test.Sdk 18.11.0 and coverlet.collector 10.1.0, and replace FluentAssertions with plain xUnit asserts. DescendingCompare goes through Comparer.Default so two nulls compare as equal, MySortedList.IndexOf returns the first of several equal items, and Program.cs gains a Comparer.Create example. Four new tests cover the fixes. --- .../src/SortingGenericList.Console/Program.cs | 16 ++ .../SortingGenericList.Console.csproj | 2 +- .../DataModels/Product.cs | 2 +- .../MySortedList.cs | 6 +- .../SortingGenericList.Library.csproj | 2 +- .../DataModelsTests/ProductTests.cs | 54 +++++-- .../MySortedListTests.cs | 142 +++++++++++------- .../ProductIdComparerTests.cs | 23 ++- .../ProductPriceIComparerTests.cs | 20 ++- .../SortingGenericList.Test.csproj | 11 +- .../test/SortingGenericList.Test/Usings.cs | 1 - 11 files changed, 179 insertions(+), 100 deletions(-) diff --git a/collections-lists/SortingGenericList/src/SortingGenericList.Console/Program.cs b/collections-lists/SortingGenericList/src/SortingGenericList.Console/Program.cs index b6462e2cf3..f8340291af 100644 --- a/collections-lists/SortingGenericList/src/SortingGenericList.Console/Program.cs +++ b/collections-lists/SortingGenericList/src/SortingGenericList.Console/Program.cs @@ -43,6 +43,22 @@ PrintProducts(productList); Console.WriteLine(); +Console.WriteLine("----------List In Place Sort With Comparer.Create (Order: Price - Descending)----------"); +Console.WriteLine("Products:"); +PrintProducts(unsortedProducts); +Console.WriteLine(); + +productList.Clear(); +productList.AddRange(unsortedProducts); + +var byPriceDescending = Comparer.Create( + (a, b) => Nullable.Compare(b?.Price, a?.Price)); + +productList.Sort(byPriceDescending); +Console.WriteLine("**Sorted**"); +PrintProducts(productList); +Console.WriteLine(); + Console.WriteLine("----------List In Place Sort With Comparison (Lambda: Order - Category)----------"); Console.WriteLine("Products:"); PrintProducts(unsortedProducts); diff --git a/collections-lists/SortingGenericList/src/SortingGenericList.Console/SortingGenericList.Console.csproj b/collections-lists/SortingGenericList/src/SortingGenericList.Console/SortingGenericList.Console.csproj index ac2a0b7476..11f47a70d4 100644 --- a/collections-lists/SortingGenericList/src/SortingGenericList.Console/SortingGenericList.Console.csproj +++ b/collections-lists/SortingGenericList/src/SortingGenericList.Console/SortingGenericList.Console.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/collections-lists/SortingGenericList/src/SortingGenericList.Library/DataModels/Product.cs b/collections-lists/SortingGenericList/src/SortingGenericList.Library/DataModels/Product.cs index 0730b5d0d5..71cf194ee7 100644 --- a/collections-lists/SortingGenericList/src/SortingGenericList.Library/DataModels/Product.cs +++ b/collections-lists/SortingGenericList/src/SortingGenericList.Library/DataModels/Product.cs @@ -14,7 +14,7 @@ public sealed record Product(string ProductName, string Category, decimal Price) public static bool operator >=(Product? left, Product? right) => Comparer.Default.Compare(left, right) >= 0; - public static int DescendingCompare(Product? a, Product? b) => b?.CompareTo(a) ?? -1; + public static int DescendingCompare(Product? a, Product? b) => Comparer.Default.Compare(b, a); public int CompareTo(object? obj) { diff --git a/collections-lists/SortingGenericList/src/SortingGenericList.Library/MySortedList.cs b/collections-lists/SortingGenericList/src/SortingGenericList.Library/MySortedList.cs index 7280af11c8..4d118e8f55 100644 --- a/collections-lists/SortingGenericList/src/SortingGenericList.Library/MySortedList.cs +++ b/collections-lists/SortingGenericList/src/SortingGenericList.Library/MySortedList.cs @@ -48,8 +48,12 @@ public void CopyTo(Span dest) public int IndexOf(T item) { var index = _list.BinarySearch(item, _comparer); + if (index < 0) return -1; - return index < 0 ? -1 : index; + var comparer = _comparer ?? Comparer.Default; + while (index > 0 && comparer.Compare(_list[index - 1], item) == 0) index--; + + return index; } public void Insert(int index, T item) diff --git a/collections-lists/SortingGenericList/src/SortingGenericList.Library/SortingGenericList.Library.csproj b/collections-lists/SortingGenericList/src/SortingGenericList.Library/SortingGenericList.Library.csproj index 0542335e61..237d661670 100644 --- a/collections-lists/SortingGenericList/src/SortingGenericList.Library/SortingGenericList.Library.csproj +++ b/collections-lists/SortingGenericList/src/SortingGenericList.Library/SortingGenericList.Library.csproj @@ -1,7 +1,7 @@  - net7.0 + net10.0 enable enable diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/DataModelsTests/ProductTests.cs b/collections-lists/SortingGenericList/test/SortingGenericList.Test/DataModelsTests/ProductTests.cs index 85c8120d3d..65a5555246 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/DataModelsTests/ProductTests.cs +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/DataModelsTests/ProductTests.cs @@ -6,7 +6,7 @@ public sealed class ProductTests public void Product_Default_Id_Should_Not_Be_Empty() { var product = new Product("Product A", "Category1", 10.0m); - product.Id.Should().NotBeEmpty(); + Assert.NotEqual(Guid.Empty, product.Id); } [Fact] @@ -16,15 +16,15 @@ public void Product_Comparisons_Should_Be_Correct() var product2 = new Product("Product B", "Category1", 10.0m); var product3 = new Product("Product A", "Category1", 20.0m); - product1.Should().BeLessThan(product2); - product1.Should().BeLessOrEqualTo(product2); - product2.Should().BeGreaterThan(product1); - product2.Should().BeGreaterOrEqualTo(product1); + Assert.True(product1 < product2); + Assert.True(product1 <= product2); + Assert.True(product2 > product1); + Assert.True(product2 >= product1); - product1.Should().NotBe(product3); - product1.CompareTo(product3).Should().BeNegative(); + Assert.NotEqual(product1, product3); + Assert.True(product1.CompareTo(product3) < 0); - product1.CompareTo(product1).Should().Be(0); + Assert.Equal(0, product1.CompareTo(product1)); } [Fact] @@ -32,10 +32,10 @@ public void Product_Comparison_With_Null_Should_Be_Correct() { var product = new Product("Product A", "Category1", 10.0m); - product.Invoking(p => p.CompareTo(new object())) - .Should().Throw().WithMessage($"Object must be of type {nameof(Product)}"); + var exception = Assert.Throws(() => product.CompareTo(new object())); + Assert.Equal($"Object must be of type {nameof(Product)}", exception.Message); - product.CompareTo(null).Should().Be(1); + Assert.Equal(1, product.CompareTo(null)); } [Fact] @@ -46,7 +46,7 @@ public void ShouldCorrectlyPerformDescendComparison_ForSameProducts_Ascending() var result = Product.DescendingCompare(product1, product2); - result.Should().BePositive(); + Assert.True(result > 0); } [Fact] @@ -57,7 +57,7 @@ public void ShouldCorrectlyPerformDescendComparison_ForSameProducts_Descending() var result = Product.DescendingCompare(product1, product2); - result.Should().BeNegative(); + Assert.True(result < 0); } [Fact] @@ -68,6 +68,30 @@ public void ShouldCorrectlyPerformDescendComparison_ForEqualProducts() var result = Product.DescendingCompare(product1, product2); - result.Should().Be(0); + Assert.Equal(0, result); } -} \ No newline at end of file + + [Fact] + public void DescendingCompare_ForTwoNulls_ReturnsZero() + { + Assert.Equal(0, Product.DescendingCompare(null, null)); + } + + [Fact] + public void DescendingCompare_WhenListHasManyNulls_SortsWithNullsLast() + { + var random = new Random(1); + var products = Enumerable.Range(0, 200) + .Select(i => i % 5 == 0 ? null : new Product($"P{random.Next(1000):D3}", "Category", 1m)) + .ToList(); + + products.Sort(Product.DescendingCompare); + + Assert.All(products.Take(160), Assert.NotNull); + Assert.All(products.Skip(160), Assert.Null); + Assert.Equal( + products.Take(160).OrderByDescending(p => p!.ProductName, StringComparer.OrdinalIgnoreCase) + .Select(p => p!.ProductName), + products.Take(160).Select(p => p!.ProductName)); + } +} diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/MySortedListTests.cs b/collections-lists/SortingGenericList/test/SortingGenericList.Test/MySortedListTests.cs index 3f0f242e1d..c70b4daa36 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/MySortedListTests.cs +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/MySortedListTests.cs @@ -2,13 +2,21 @@ public class MySortedListTests { - private static readonly int[] UnsortedListData = {2, 4, 1, 8, 9, 6, 5, 3, 7}; - private static readonly int[] SortedListData = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + private static readonly int[] UnsortedListData = [2, 4, 1, 8, 9, 6, 5, 3, 7]; + private static readonly int[] SortedListData = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + + private static void AssertAscending(IEnumerable items) => Assert.Equal(items.Order(), items); + + private static void AssertNotSorted(int[] items) + { + Assert.NotEqual(items.Order(), items); + Assert.NotEqual(items.OrderDescending(), items); + } [Fact] public void Init_WhenCreatingNew_ListIsEmpty() { - new MySortedList().Should().BeEmpty(); + Assert.Empty(new MySortedList()); } [Fact] @@ -17,7 +25,7 @@ public void Init_WhenCalledWithCapacity_HasExpectedCapacity() const int capacity = 10; var list = new MySortedList(capacity); - list.Capacity.Should().Be(capacity); + Assert.Equal(capacity, list.Capacity); } [Fact] @@ -25,7 +33,7 @@ public void Init_WhenCalledWithIEnumerable_HasExpectedValuesInSortedOrder() { var list = new MySortedList(UnsortedListData); - list.Should().BeEquivalentTo(UnsortedListData).And.BeInAscendingOrder(); + Assert.Equal(UnsortedListData.Order(), list); } [Fact] @@ -33,7 +41,7 @@ public void Init_WhenCalledWithICompare_HasExpectedSortOrder() { var list = new MySortedList(UnsortedListData, new DescendingIntComparison()); - list.Should().BeInDescendingOrder(); + Assert.Equal(UnsortedListData.OrderDescending(), list); } [Fact] @@ -43,7 +51,7 @@ public void Add_WhenCalledWithSingleItem_CountIncreasesByOne() var currentCount = list.Count; list.Add(1); - list.Count.Should().Be(currentCount + 1); + Assert.Equal(currentCount + 1, list.Count); } [Fact] @@ -51,11 +59,11 @@ public void Add_CallingWithItem_ListContainsItem() { const int expected = 10; var list = new MySortedList(); - list.Should().NotContain(expected); + Assert.DoesNotContain(expected, list); list.Add(expected); - list.Should().Contain(expected); + Assert.Contains(expected, list); } [Fact] @@ -63,95 +71,110 @@ public void Indexer_WhenCalling_ReturnsProperItem() { var list = new MySortedList(SortedListData); - list[2].Should().Be(SortedListData[2]); + Assert.Equal(SortedListData[2], list[2]); } [Fact] public void Indexer_WhenCalledWithInvalidIndex_ThrowsOutOfRange() { - this.Invoking(_ => new MySortedList()[1]).Should().Throw(); + Assert.Throws(() => new MySortedList()[1]); } [Fact] public void Indexer_WhenCalledWithNegativeIndex_ThrowsOutOfRange() { - this.Invoking(_ => new MySortedList()[-1]).Should().Throw(); + Assert.Throws(() => new MySortedList()[-1]); } [Fact] public void Add_WhenCalledWithSortedData_ListIsSorted() { - SortedListData.Should().BeInAscendingOrder(); + AssertAscending(SortedListData); var list = new MySortedList(); foreach (var item in SortedListData) list.Add(item); - list.Should().BeInAscendingOrder(); + AssertAscending(list); } [Fact] public void Init_WhenCalledWithSortedData_ListIsSorted() { - SortedListData.Should().BeInAscendingOrder(); + AssertAscending(SortedListData); var list = new MySortedList(SortedListData); - list.Should().BeInAscendingOrder(); + AssertAscending(list); } [Fact] public void AddRange_WhenCalledWithSortedData_ListIsSorted() { - SortedListData.Should().BeInAscendingOrder(); + AssertAscending(SortedListData); var list = new MySortedList(); list.AddRange(SortedListData); - list.Should().BeInAscendingOrder(); + AssertAscending(list); } [Fact] public void Add_WhenCalledWithUnsortedData_ListIsSorted() { - UnsortedListData.Should().NotBeInAscendingOrder().And.NotBeInDescendingOrder(); + AssertNotSorted(UnsortedListData); var list = new MySortedList(); foreach (var item in UnsortedListData) list.Add(item); - list.Should().BeInAscendingOrder(); + AssertAscending(list); } [Fact] public void Init_WhenWithUnsortedData_ListIsSorted() { - UnsortedListData.Should().NotBeInAscendingOrder().And.NotBeInDescendingOrder(); + AssertNotSorted(UnsortedListData); var list = new MySortedList(UnsortedListData); - list.Should().BeInAscendingOrder(); + AssertAscending(list); } [Fact] public void AddRange_CallingWithUnsortedData_ListIsSorted() { - UnsortedListData.Should().NotBeInAscendingOrder().And.NotBeInDescendingOrder(); + AssertNotSorted(UnsortedListData); var list = new MySortedList(); list.AddRange(UnsortedListData); - list.Should().BeInAscendingOrder(); + AssertAscending(list); + } + + [Fact] + public void Add_WhenCalledWithRandomData_MatchesListSort() + { + var random = new Random(42); + var data = Enumerable.Range(0, 1_000).Select(_ => random.Next(100)).ToArray(); + + var list = new MySortedList(); + foreach (var item in data) list.Add(item); + + var expected = new List(data); + expected.Sort(); + + Assert.Equal(expected, list); } [Fact] public void Clear_WhenCalled_ClearsList() { const int count = 10; - var list = new MySortedList(Enumerable.Range(0, count).Select(x => x)); + var list = new MySortedList(Enumerable.Range(0, count)); Assert.Equal(count, list.Count); list.Clear(); - list.Should().BeEmpty(); + Assert.Empty(list); } [Fact] @@ -159,7 +182,9 @@ public void Contains_CallingWithContainedItem_ReturnsTrue() { var list = new MySortedList {10}; - list.Contains(10).Should().BeTrue(); + var found = list.Contains(10); + + Assert.True(found); } [Fact] @@ -167,7 +192,9 @@ public void Contains_CallingWithNonContainedItem_ReturnsFalse() { var list = new MySortedList {42}; - list.Contains(10).Should().BeFalse(); + var found = list.Contains(10); + + Assert.False(found); } [Fact] @@ -175,7 +202,7 @@ public void Remove_CallingWithNonExistentItem_ReturnsFalse() { var list = new MySortedList {1, 2}; - list.Remove(10).Should().BeFalse(); + Assert.False(list.Remove(10)); } [Fact] @@ -183,7 +210,7 @@ public void Remove_CallingWithExistentItem_ReturnsTrue() { var list = new MySortedList {1, 2, 10}; - list.Remove(10).Should().BeTrue(); + Assert.True(list.Remove(10)); } [Fact] @@ -193,7 +220,7 @@ public void Remove_CallingWithExistentItem_ReducesCount() var currentCount = list.Count; list.Remove(1); - list.Count.Should().Be(currentCount - 1); + Assert.Equal(currentCount - 1, list.Count); } [Fact] @@ -202,11 +229,11 @@ public void Remove_CallingWithExistentItem_RemovesItem() const int itemToRemove = 10; var list = new MySortedList {1, 2, itemToRemove}; - list.Should().Contain(itemToRemove); + Assert.Contains(itemToRemove, list); list.Remove(itemToRemove); - list.Should().NotContain(itemToRemove); + Assert.DoesNotContain(itemToRemove, list); } [Fact] @@ -217,40 +244,43 @@ public void CopyTo_Calling_CopiedArrayIsExpected() list.CopyTo(dest, 0); - dest.Should().BeEquivalentTo(list).And.BeEquivalentTo(SortedListData); + Assert.Equal(list, dest); + Assert.Equal(SortedListData, dest); } [Fact] public void CopyTo_CallingWithSpan_CopiedArrayIsExpected() { - var expectedList = new[] {0, 1, 2, 3, 4}; + int[] expectedList = [0, 1, 2, 3, 4]; var list = new MySortedList(expectedList); var dest = new int[expectedList.Length]; list.CopyTo(dest.AsSpan()); - dest.Should().BeEquivalentTo(list).And.BeEquivalentTo(expectedList); + Assert.Equal(list, dest); + Assert.Equal(expectedList, dest); } [Fact] public void ToArray_WhenCalling_ResultArrayIsExpected() { - var expectedList = new[] {0, 1, 2, 3, 4}; + int[] expectedList = [0, 1, 2, 3, 4]; var list = new MySortedList(expectedList); var dest = list.ToArray(); - dest.Should().BeEquivalentTo(list).And.BeEquivalentTo(expectedList); + Assert.Equal(list, dest); + Assert.Equal(expectedList, dest); } [Fact] public void Init_WhenCallingWithUnsortedStrings_ListIsSorted() { - var unsortedList = new[] {"Bill", "Dan", "Adam", "Chuck"}; + string[] unsortedList = ["Bill", "Dan", "Adam", "Chuck"]; var list = new MySortedList(unsortedList); - list.Should().BeInAscendingOrder(); + Assert.Equal(["Adam", "Bill", "Chuck", "Dan"], list); } [Fact] @@ -258,7 +288,7 @@ public void Insert_Calling_ThrowsNotSupportedException() { var sortedList = new MySortedList {0, 1, 2, 3, 4}; - sortedList.Invoking(list => list.Insert(2, 42)).Should().Throw(); + Assert.Throws(() => sortedList.Insert(2, 42)); } [Fact] @@ -266,45 +296,55 @@ public void Indexer_AttemptingToSet_ThrowsNotSupportedException() { var sortedList = new MySortedList {0, 1, 2, 3, 4}; - sortedList.Invoking(list => list[2] = 42).Should().Throw(); + Assert.Throws(() => sortedList[2] = 42); } [Fact] public void IndexOf_WhenItemIsPresent_ShouldReturnCorrectIndex() { - var sortedList = new MySortedList(new[] {1, 2, 3}); + var sortedList = new MySortedList([1, 2, 3]); + + Assert.Equal(1, sortedList.IndexOf(2)); + } + + [Fact] + public void IndexOf_WhenItemIsDuplicated_ShouldReturnFirstIndex() + { + var sortedList = new MySortedList([1, 5, 5, 5, 5, 5, 5, 9]); - sortedList.IndexOf(2).Should().Be(1); + Assert.Equal(1, sortedList.IndexOf(5)); } [Fact] public void IndexOf_WhenItemIsNotPresent_ShouldReturnNegativeOne() { - var sortedList = new MySortedList(new[] {1, 2, 3}); + var sortedList = new MySortedList([1, 2, 3]); - sortedList.IndexOf(4).Should().Be(-1); + Assert.Equal(-1, sortedList.IndexOf(4)); } [Fact] public void RemoveAt_WhenIndexIsValid_ShouldRemoveItemAtGivenIndex() { - var sortedList = new MySortedList(new[] {1, 2, 3}); + var sortedList = new MySortedList([1, 2, 3]); sortedList.RemoveAt(1); - sortedList.Contains(2).Should().BeFalse(); + var found = sortedList.Contains(2); + + Assert.False(found); } [Fact] public void RemoveAt_WhenIndexIsInvalid_ShouldThrowException() { - var sortedList = new MySortedList(new[] {1, 2, 3}); + var sortedList = new MySortedList([1, 2, 3]); - sortedList.Invoking(list => list.RemoveAt(3)).Should().Throw(); + Assert.Throws(() => sortedList.RemoveAt(3)); } private sealed class DescendingIntComparison : IComparer { public int Compare(int x, int y) => y.CompareTo(x); } -} \ No newline at end of file +} diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductIdComparerTests.cs b/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductIdComparerTests.cs index 2fa8cd5c23..a6ebfc057d 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductIdComparerTests.cs +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductIdComparerTests.cs @@ -9,27 +9,27 @@ public void Compare_SameReference_ShouldReturnZero() { var id = Guid.NewGuid(); var productA = new Product("ProductName", "Category", 100m) {Id = id}; - _comparer.Compare(productA, productA).Should().Be(0); + Assert.Equal(0, _comparer.Compare(productA, productA)); } [Fact] public void Compare_BothNull_ShouldReturnZero() { - _comparer.Compare(null, null).Should().Be(0); + Assert.Equal(0, _comparer.Compare(null, null)); } [Fact] public void Compare_AIsNull_ShouldReturnNegative() { var productB = new Product("ProductName", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(null, productB).Should().BeNegative(); + Assert.True(_comparer.Compare(null, productB) < 0); } [Fact] public void Compare_BIsNull_ShouldReturnPositive() { var productA = new Product("ProductName", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, null).Should().BePositive(); + Assert.True(_comparer.Compare(productA, null) > 0); } [Fact] @@ -38,10 +38,9 @@ public void Compare_AIdIsLessThanBId_ShouldReturnNegative() var idA = Guid.Parse("00000000-0000-0000-0000-000000000000"); var idB = Guid.NewGuid(); - _comparer.Compare( + Assert.True(_comparer.Compare( new Product("ProductName", "Category", 100m) {Id = idA}, - new Product("ProductName", "Category", 100m) {Id = idB}) - .Should().BeNegative(); + new Product("ProductName", "Category", 100m) {Id = idB}) < 0); } [Fact] @@ -50,9 +49,9 @@ public void Compare_AIdIsGreaterThanBId_ShouldReturnPositive() var idA = Guid.Parse("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"); var idB = Guid.NewGuid(); - _comparer.Compare( + Assert.True(_comparer.Compare( new Product("ProductName", "Category", 100m) {Id = idA}, - new Product("ProductName", "Category", 100m) {Id = idB}).Should().BePositive(); + new Product("ProductName", "Category", 100m) {Id = idB}) > 0); } [Fact] @@ -60,8 +59,8 @@ public void Compare_AIdIsEqualToBId_ShouldReturnZero() { var id = Guid.NewGuid(); - _comparer.Compare( + Assert.Equal(0, _comparer.Compare( new Product("ProductName", "Category", 100m) {Id = id}, - new Product("ProductName", "Category", 100m) {Id = id}).Should().Be(0); + new Product("ProductName", "Category", 100m) {Id = id})); } -} \ No newline at end of file +} diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductPriceIComparerTests.cs b/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductPriceIComparerTests.cs index 380531b263..530fce86f3 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductPriceIComparerTests.cs +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/ProductPriceIComparerTests.cs @@ -8,27 +8,25 @@ public class ProductPriceIComparerTests public void Compare_SameReference_ShouldReturnZero() { var productA = new Product("ProductName", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, productA).Should().Be(0); + Assert.Equal(0, _comparer.Compare(productA, productA)); } [Fact] public void Compare_BothNull_ShouldReturnZero() { - _comparer.Compare(null, null).Should().Be(0); + Assert.Equal(0, _comparer.Compare(null, null)); } [Fact] public void Compare_AIsNull_ShouldReturnNegative() { - _comparer.Compare(null, new Product("ProductNameB", "Category", 100m) {Id = Guid.NewGuid()}).Should() - .BeNegative(); + Assert.True(_comparer.Compare(null, new Product("ProductNameB", "Category", 100m) {Id = Guid.NewGuid()}) < 0); } [Fact] public void Compare_BIsNull_ShouldReturnPositive() { - _comparer.Compare(new Product("ProductNameA", "Category", 100m) {Id = Guid.NewGuid()}, null).Should() - .BePositive(); + Assert.True(_comparer.Compare(new Product("ProductNameA", "Category", 100m) {Id = Guid.NewGuid()}, null) > 0); } [Fact] @@ -37,7 +35,7 @@ public void Compare_PriceOfAIsLessThanPriceOfB_ShouldReturnNegative() var productA = new Product("ProductNameA", "Category", 90m) {Id = Guid.NewGuid()}; var productB = new Product("ProductNameB", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, productB).Should().BeNegative(); + Assert.True(_comparer.Compare(productA, productB) < 0); } [Fact] @@ -46,7 +44,7 @@ public void Compare_PriceOfAIsGreaterThanPriceOfB_ShouldReturnPositive() var productA = new Product("ProductNameA", "Category", 100m) {Id = Guid.NewGuid()}; var productB = new Product("ProductNameB", "Category", 90m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, productB).Should().BePositive(); + Assert.True(_comparer.Compare(productA, productB) > 0); } [Fact] @@ -55,7 +53,7 @@ public void Compare_PriceOfAIsEqualToPriceOfB_ShouldReturnZero() var productA = new Product("ProductName", "Category", 100m) {Id = Guid.NewGuid()}; var productB = new Product("ProductName", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, productB).Should().Be(0); + Assert.Equal(0, _comparer.Compare(productA, productB)); } [Fact] @@ -64,6 +62,6 @@ public void Compare_PriceOfAIsEqualToPriceOfB_AndProductNamesDifferent_ShouldRet var productA = new Product("ProductNameA", "Category", 100m) {Id = Guid.NewGuid()}; var productB = new Product("ProductNameB", "Category", 100m) {Id = Guid.NewGuid()}; - _comparer.Compare(productA, productB).Should().BeNegative(); + Assert.True(_comparer.Compare(productA, productB) < 0); } -} \ No newline at end of file +} diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/SortingGenericList.Test.csproj b/collections-lists/SortingGenericList/test/SortingGenericList.Test/SortingGenericList.Test.csproj index f661430553..62afc23be1 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/SortingGenericList.Test.csproj +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/SortingGenericList.Test.csproj @@ -1,7 +1,7 @@  - net7.0 + net10.0 enable enable @@ -10,14 +10,13 @@ - - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/collections-lists/SortingGenericList/test/SortingGenericList.Test/Usings.cs b/collections-lists/SortingGenericList/test/SortingGenericList.Test/Usings.cs index 6200ea2acd..6d58fba992 100644 --- a/collections-lists/SortingGenericList/test/SortingGenericList.Test/Usings.cs +++ b/collections-lists/SortingGenericList/test/SortingGenericList.Test/Usings.cs @@ -1,4 +1,3 @@ global using Xunit; -global using FluentAssertions; global using SortingGenericList.Library; global using SortingGenericList.Library.DataModels; \ No newline at end of file