Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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<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
Showing
4 changed files
with
95 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Src/FluentAssertions/Common/NonGenericCollectionWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters