From a93a59b4140ac23e41e5528a35306df04612ac00 Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Fri, 18 Feb 2022 05:25:44 -0600 Subject: [PATCH] Added type NonGenericCollectionWrapper.cs in FluentAssertions/Common. This ensures that the Count property is passed through to the underlying implementation, and also allows code that knows about it to directly access the wrapped collection object. Updated AssertionExtensions.c to use NonGenericCollectionWrapper instead of System.Linq's .Cast to produce the generic collections for use with GenericCollectionAssertions in Should overloads for DataTableCollection, DataColumnCollection and DataRowCollection. Updated the ContainTableWithName methods in DataTableCollectionAssertionExtensions.cs and ContainColumnWithName in DataColumnCollectionAssertionExtensions.cs to check if the assertion subject is a NonGenericCollectionWrapper and, if so, use it to gain access to the underlying Contains functionality rather than requiring the collection to be enumerated. --- Src/FluentAssertions/AssertionExtensions.cs | 9 ++-- .../Common/NonGenericCollectionWrapper.cs | 41 +++++++++++++++++++ ...DataColumnCollectionAssertionExtensions.cs | 26 +++++++++++- .../DataTableCollectionAssertionExtensions.cs | 26 +++++++++++- 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 Src/FluentAssertions/Common/NonGenericCollectionWrapper.cs diff --git a/Src/FluentAssertions/AssertionExtensions.cs b/Src/FluentAssertions/AssertionExtensions.cs index 9b80852e53..680d93f597 100644 --- a/Src/FluentAssertions/AssertionExtensions.cs +++ b/Src/FluentAssertions/AssertionExtensions.cs @@ -391,7 +391,8 @@ public static StringCollectionAssertions Should(this IEnumerable @this) [Pure] public static GenericCollectionAssertions Should(this DataTableCollection actualValue) { - return new GenericCollectionAssertions(actualValue.Cast()); + return new GenericCollectionAssertions( + new NonGenericCollectionWrapper(actualValue)); } /// @@ -400,7 +401,8 @@ public static GenericCollectionAssertions Should(this DataTableCollec [Pure] public static GenericCollectionAssertions Should(this DataColumnCollection actualValue) { - return new GenericCollectionAssertions(actualValue.Cast()); + return new GenericCollectionAssertions( + new NonGenericCollectionWrapper(actualValue)); } /// @@ -409,7 +411,8 @@ public static GenericCollectionAssertions Should(this DataColumnColl [Pure] public static GenericCollectionAssertions Should(this DataRowCollection actualValue) { - return new GenericCollectionAssertions(actualValue.Cast()); + return new GenericCollectionAssertions( + new NonGenericCollectionWrapper(actualValue)); } /// diff --git a/Src/FluentAssertions/Common/NonGenericCollectionWrapper.cs b/Src/FluentAssertions/Common/NonGenericCollectionWrapper.cs new file mode 100644 index 0000000000..a1e3f35899 --- /dev/null +++ b/Src/FluentAssertions/Common/NonGenericCollectionWrapper.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace FluentAssertions.Common +{ + internal class NonGenericCollectionWrapper : ICollection, IEnumerable + where TCollection : ICollection, IEnumerable + { + public TCollection UnderlyingCollection { get; private set; } + + public NonGenericCollectionWrapper(TCollection collection) + { + UnderlyingCollection = collection; + } + + public int Count => UnderlyingCollection.Count; + + public bool IsReadOnly => true; + + public IEnumerator GetEnumerator() => UnderlyingCollection.Cast().GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => UnderlyingCollection.GetEnumerator(); + + public bool Contains(TItem item) => UnderlyingCollection.Cast().Contains(item); + + public void CopyTo(TItem[] array, int arrayIndex) => UnderlyingCollection.CopyTo(array, arrayIndex); + + // Mutation is not supported, but these methods must be implemented to satisfy ICollection: + // * Add + // * Clear + // * Remove + + public void Add(TItem item) => throw new NotSupportedException(); + + public void Clear() => throw new NotSupportedException(); + + public bool Remove(TItem item) => throw new NotSupportedException(); + } +} diff --git a/Src/FluentAssertions/Data/Extensions/DataColumnCollectionAssertionExtensions.cs b/Src/FluentAssertions/Data/Extensions/DataColumnCollectionAssertionExtensions.cs index 0431279df7..45232c201b 100644 --- a/Src/FluentAssertions/Data/Extensions/DataColumnCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/Data/Extensions/DataColumnCollectionAssertionExtensions.cs @@ -114,7 +114,18 @@ public static class DataColumnCollectionAssertionExtensions assertion.Subject); } - if (!assertion.Subject.Any(column => column.ColumnName == expectedColumnName)) + bool containsColumn; + + if (assertion.Subject is NonGenericCollectionWrapper wrapper) + { + containsColumn = wrapper.UnderlyingCollection.Contains(expectedColumnName); + } + else + { + containsColumn = assertion.Subject.Any(column => column.ColumnName == expectedColumnName); + } + + if (!containsColumn) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -150,7 +161,18 @@ public static class DataColumnCollectionAssertionExtensions assertion.Subject); } - if (assertion.Subject.Any(column => column.ColumnName == unexpectedColumnName)) + bool containsColumn; + + if (assertion.Subject is NonGenericCollectionWrapper wrapper) + { + containsColumn = wrapper.UnderlyingCollection.Contains(unexpectedColumnName); + } + else + { + containsColumn = assertion.Subject.Any(column => column.ColumnName == unexpectedColumnName); + } + + if (containsColumn) { Execute.Assertion .BecauseOf(because, becauseArgs) diff --git a/Src/FluentAssertions/Data/Extensions/DataTableCollectionAssertionExtensions.cs b/Src/FluentAssertions/Data/Extensions/DataTableCollectionAssertionExtensions.cs index 8e2e404be5..d34e600b43 100644 --- a/Src/FluentAssertions/Data/Extensions/DataTableCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/Data/Extensions/DataTableCollectionAssertionExtensions.cs @@ -101,7 +101,18 @@ public static class DataTableCollectionAssertionExtensions assertion.Subject); } - if (!assertion.Subject.Any(table => table.TableName == expectedTableName)) + bool containsTable; + + if (assertion.Subject is NonGenericCollectionWrapper wrapper) + { + containsTable = wrapper.UnderlyingCollection.Contains(expectedTableName); + } + else + { + containsTable = assertion.Subject.Any(table => table.TableName == expectedTableName); + } + + if (!containsTable) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -137,7 +148,18 @@ public static class DataTableCollectionAssertionExtensions assertion.Subject); } - if (assertion.Subject.Any(table => table.TableName == unexpectedTableName)) + bool containsTable; + + if (assertion.Subject is NonGenericCollectionWrapper wrapper) + { + containsTable = wrapper.UnderlyingCollection.Contains(unexpectedTableName); + } + else + { + containsTable = assertion.Subject.Any(table => table.TableName == unexpectedTableName); + } + + if (containsTable) { Execute.Assertion .BecauseOf(because, becauseArgs)