Skip to content

Commit

Permalink
Added type NonGenericCollectionWrapper.cs in FluentAssertions/Common.…
Browse files Browse the repository at this point in the history
… 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<TItem> to produce the generic collections for use with GenericCollectionAssertions<TItem> 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.
  • Loading branch information
logiclrd committed Feb 18, 2022
1 parent f579d96 commit a93a59b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
9 changes: 6 additions & 3 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -391,7 +391,8 @@ public static StringCollectionAssertions Should(this IEnumerable<string> @this)
[Pure]
public static GenericCollectionAssertions<DataTable> Should(this DataTableCollection actualValue)
{
return new GenericCollectionAssertions<DataTable>(actualValue.Cast<DataTable>());
return new GenericCollectionAssertions<DataTable>(
new NonGenericCollectionWrapper<DataTableCollection, DataTable>(actualValue));
}

/// <summary>
Expand All @@ -400,7 +401,8 @@ public static GenericCollectionAssertions<DataTable> Should(this DataTableCollec
[Pure]
public static GenericCollectionAssertions<DataColumn> Should(this DataColumnCollection actualValue)
{
return new GenericCollectionAssertions<DataColumn>(actualValue.Cast<DataColumn>());
return new GenericCollectionAssertions<DataColumn>(
new NonGenericCollectionWrapper<DataColumnCollection, DataColumn>(actualValue));
}

/// <summary>
Expand All @@ -409,7 +411,8 @@ public static GenericCollectionAssertions<DataColumn> Should(this DataColumnColl
[Pure]
public static GenericCollectionAssertions<DataRow> Should(this DataRowCollection actualValue)
{
return new GenericCollectionAssertions<DataRow>(actualValue.Cast<DataRow>());
return new GenericCollectionAssertions<DataRow>(
new NonGenericCollectionWrapper<DataRowCollection, DataRow>(actualValue));
}

/// <summary>
Expand Down
41 changes: 41 additions & 0 deletions 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<TCollection, TItem> : ICollection<TItem>, IEnumerable<TItem>
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<TItem> GetEnumerator() => UnderlyingCollection.Cast<TItem>().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => UnderlyingCollection.GetEnumerator();

public bool Contains(TItem item) => UnderlyingCollection.Cast<TItem>().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<T>:
// * 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();
}
}
Expand Up @@ -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<DataColumnCollection, DataColumn> wrapper)
{
containsColumn = wrapper.UnderlyingCollection.Contains(expectedColumnName);
}
else
{
containsColumn = assertion.Subject.Any(column => column.ColumnName == expectedColumnName);
}

if (!containsColumn)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -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<DataColumnCollection, DataColumn> wrapper)
{
containsColumn = wrapper.UnderlyingCollection.Contains(unexpectedColumnName);
}
else
{
containsColumn = assertion.Subject.Any(column => column.ColumnName == unexpectedColumnName);
}

if (containsColumn)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down
Expand Up @@ -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<DataTableCollection, DataTable> wrapper)
{
containsTable = wrapper.UnderlyingCollection.Contains(expectedTableName);
}
else
{
containsTable = assertion.Subject.Any(table => table.TableName == expectedTableName);
}

if (!containsTable)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -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<DataTableCollection, DataTable> wrapper)
{
containsTable = wrapper.UnderlyingCollection.Contains(unexpectedTableName);
}
else
{
containsTable = assertion.Subject.Any(table => table.TableName == unexpectedTableName);
}

if (containsTable)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down

0 comments on commit a93a59b

Please sign in to comment.