Skip to content

Commit

Permalink
Updated AssertionExtensions.cs with overloads to pass calls to .Shoul…
Browse files Browse the repository at this point in the history
…d() on DataTableCollection, DataColumnCollection and DataRowCollection into GenericCollectionAssertions using LINQ's .Cast<T>() method.

Added type DataTableCollectionAssertionExtensions.cs with assertions specific to DataTableCollection as extensions on GenericCollectionAssertions<DataTable>.
Added type DataColumnCollectionAssertionExtensions.cs with assertions specific to DataColumnCollection as extensions on GenericCollectionAssertions<DataColumn>.
Added type DataRowCollectionAssertionExtensions.cs with assertions specific to DataRowCollection as extensions on GenericCollectionAssertions<DataRow>.
Reran AcceptApiChanges.ps1 to update the approved API surface.
Added automated tests of DataTableCollectionAssertionExtensions in new class Collections/Data/DataTableCollectionAssertionExtensionsSpecs.cs in FluentAssertions.Specs.
Added automated tests of DataColumnCollectionAssertionExtensions in new class Collections/Data/DataColumnCollectionAssertionExtesnionsSpecs.cs in FluentAssertions.Specs.
Added automated tests of DataRowCollectionAssertionExtensions in new class Collections/Data/DataRowCollectionAssertionExtensionsSpecs.cs in FluentAssertions.Specs.
Added documentation of the new methods to docs/_pages/data.md.
Added a corresponding line to the release notes in docs/_pages/releases.md.
  • Loading branch information
logiclrd committed Feb 17, 2022
1 parent 8450e98 commit f579d96
Show file tree
Hide file tree
Showing 14 changed files with 2,260 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Src/FluentAssertions/AssertionExtensions.cs
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
Expand Down Expand Up @@ -384,6 +385,33 @@ public static StringCollectionAssertions Should(this IEnumerable<string> @this)
return new GenericDictionaryAssertions<TCollection, TKey, TValue>(actualValue);
}

/// <summary>
/// Returns an assertions object that can be used to assert the current <see cref="DataTableCollection"/>.
/// </summary>
[Pure]
public static GenericCollectionAssertions<DataTable> Should(this DataTableCollection actualValue)
{
return new GenericCollectionAssertions<DataTable>(actualValue.Cast<DataTable>());
}

/// <summary>
/// Returns an assertions object that can be used to assert the current <see cref="DataColumnCollection"/>.
/// </summary>
[Pure]
public static GenericCollectionAssertions<DataColumn> Should(this DataColumnCollection actualValue)
{
return new GenericCollectionAssertions<DataColumn>(actualValue.Cast<DataColumn>());
}

/// <summary>
/// Returns an assertions object that can be used to assert the current <see cref="DataRowCollection"/>.
/// </summary>
[Pure]
public static GenericCollectionAssertions<DataRow> Should(this DataRowCollection actualValue)
{
return new GenericCollectionAssertions<DataRow>(actualValue.Cast<DataRow>());
}

/// <summary>
/// Returns a <see cref="DataColumnAssertions"/> object that can be used to assert the
/// current <see cref="DataColumn"/>.
Expand Down
@@ -0,0 +1,165 @@
using System.Data;
using System.Linq;

using FluentAssertions.Collections;
using FluentAssertions.Common;
using FluentAssertions.Execution;

namespace FluentAssertions
{
public static class DataColumnCollectionAssertionExtensions
{
/// <summary>
/// Assert that the current collection has the same number of elements as <paramref name="otherCollection" />.
/// </summary>
/// <param name="otherCollection">The other collection with the same expected number of elements</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public static AndConstraint<GenericCollectionAssertions<DataColumn>> HaveSameCount(this GenericCollectionAssertions<DataColumn> assertion, DataColumnCollection otherCollection, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a <null> collection.");

if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to have the same count as {0}{reason}, but found {1}.",
otherCollection,
assertion.Subject);
}

int actualCount = assertion.Subject.Count();
int expectedCount = otherCollection.Count;

Execute.Assertion
.ForCondition(actualCount == expectedCount)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to have {0} column(s){reason}, but found {1}.", expectedCount, actualCount);

return new AndConstraint<GenericCollectionAssertions<DataColumn>>(assertion);
}

/// <summary>
/// Assert that the current <see cref="DataColumnCollection"/> does not have the same number of columns as <paramref name="otherCollection" />.
/// </summary>
/// <param name="otherCollection">The other <see cref="DataColumnCollection"/> with the unexpected number of elements</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public static AndConstraint<GenericCollectionAssertions<DataColumn>> NotHaveSameCount(this GenericCollectionAssertions<DataColumn> assertion, DataColumnCollection otherCollection, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a <null> collection.");

if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to not have the same count as {0}{reason}, but found {1}.",
otherCollection,
assertion.Subject);
}

if (ReferenceEquals(assertion.Subject, otherCollection))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} {0} to not have the same count as {1}{reason}, but they both reference the same object.",
assertion.Subject,
otherCollection);
}

int actualCount = assertion.Subject.Count();
int expectedCount = otherCollection.Count;

Execute.Assertion
.ForCondition(actualCount != expectedCount)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to not have {0} column(s){reason}, but found {1}.", expectedCount, actualCount);

return new AndConstraint<GenericCollectionAssertions<DataColumn>>(assertion);
}

/// <summary>
/// Asserts that the current <see cref="DataColumnCollection"/> contains a <see cref="DataColumn"/> with the specified <paramref name="expectedColumnName"/> name.
/// </summary>
/// <param name="expectedColumnName">A name for a <see cref="DataColumn"/> that is expected to be in the <see cref="DataColumnCollection"/>.</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public static AndConstraint<GenericCollectionAssertions<DataColumn>> ContainColumnWithName(this GenericCollectionAssertions<DataColumn> assertion, string expectedColumnName, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedColumnName, nameof(expectedColumnName), "Cannot verify that the collection contains a <null> DataColumn.");

if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expeected {context:collection} to contain column named {0}{reason}, but found {1}.", expectedColumnName,
assertion.Subject);
}

if (!assertion.Subject.Any(column => column.ColumnName == expectedColumnName))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:collection} to contain column named {0}{reason}, but it does not.",
expectedColumnName);
}

return new AndConstraint<GenericCollectionAssertions<DataColumn>>(assertion);
}

/// <summary>
/// Asserts that the current <see cref="DataColumnCollection"/> does not contain a column with the the supplied <paramref name="unexpectedColumnName" />.
/// </summary>
/// <param name="unexpectedColumnName">The column name that is not expected to be in the <see cref="DataColumnCollection"/></param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public static AndConstraint<GenericCollectionAssertions<DataColumn>> NotContainColumnWithName(this GenericCollectionAssertions<DataColumn> assertion, string unexpectedColumnName, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpectedColumnName, nameof(unexpectedColumnName), "Cannot verify that the collection does not contain a <null> DataColumn.");

if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expeected {context:collection} to not contain column named {0}{reason}, but found {1}.", unexpectedColumnName,
assertion.Subject);
}

if (assertion.Subject.Any(column => column.ColumnName == unexpectedColumnName))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:collection} to not contain column named {0}{reason}, but it does.",
unexpectedColumnName);
}

return new AndConstraint<GenericCollectionAssertions<DataColumn>>(assertion);
}
}
}

0 comments on commit f579d96

Please sign in to comment.