/
DataTableCollectionAssertionExtensions.cs
175 lines (158 loc) · 9.27 KB
/
DataTableCollectionAssertionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Data;
using System.Linq;
using FluentAssertions.Collections;
using FluentAssertions.Common;
using FluentAssertions.Data.Wrappers;
using FluentAssertions.Execution;
namespace FluentAssertions
{
public static class DataTableCollectionAssertionExtensions
{
/// <summary>
/// Assert that the current <see cref="DataTableCollection"/> has the same number of tables as <paramref name="otherDataSet" />.
/// </summary>
/// <param name="otherDataSet">The other <see cref="DataSet"/> with the same expected number of tables</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<DataTable>> HaveSameCount(this GenericCollectionAssertions<DataTable> assertion, DataSet otherDataSet, string because = "",
params object[] becauseArgs)
{
return assertion.HaveSameCount(otherDataSet.Tables, because, becauseArgs);
}
/// <summary>
/// Assert that the current <see cref="DataTableCollection"/> does not have the same number of tables as <paramref name="otherDataSet" />.
/// </summary>
/// <param name="otherDataSet">The other <see cref="DataSet"/> with the unexpected number of tables</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<DataTable>> NotHaveSameCount(this GenericCollectionAssertions<DataTable> assertion, DataSet otherDataSet, string because = "",
params object[] becauseArgs)
{
return assertion.NotHaveSameCount(otherDataSet.Tables, because, becauseArgs);
}
/// <summary>
/// Assert that the current <see cref="DataTableCollection"/> has the same number of tables as <paramref name="otherCollection" />.
/// </summary>
/// <param name="otherCollection">The other <see cref="DataSet"/> with the same expected number of tables</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<DataTable>> HaveSameCount(this GenericCollectionAssertions<DataTable> assertion, DataTableCollection otherCollection, string because = "",
params object[] becauseArgs)
{
return assertion.HaveSameCount(otherCollection.OfType<DataTable>(), because, becauseArgs);
}
/// <summary>
/// Assert that the current <see cref="DataTableCollection"/> does not have the same number of tables as <paramref name="otherCollection" />.
/// </summary>
/// <param name="otherCollection">The other <see cref="DataTableCollection"/> with the unexpected number of tables</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<DataTable>> NotHaveSameCount(this GenericCollectionAssertions<DataTable> assertion, DataTableCollection otherCollection, string because = "",
params object[] becauseArgs)
{
return assertion.NotHaveSameCount(otherCollection.OfType<DataTable>(), because, becauseArgs);
}
/// <summary>
/// Asserts that the current <see cref="DataTableCollection"/> contains a <see cref="DataTable"/> with the specified <paramref name="expectedTableName"/> name.
/// </summary>
/// <param name="expectedTableName">A name for a <see cref="DataTable"/> that is expected to be in the <see cref="DataTableCollection"/>.</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<DataTable>> ContainTableWithName(this GenericCollectionAssertions<DataTable> assertion, string expectedTableName, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedTableName, nameof(expectedTableName), "Cannot verify that the collection contains a <null> DataTable.");
if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expeected {context:collection} to contain table named {0}{reason}, but found {1}.", expectedTableName,
assertion.Subject);
}
bool containsTable;
if (assertion.Subject is DataTableCollectionWrapper dataTableCollection)
{
containsTable = dataTableCollection.Contains(expectedTableName);
}
else
{
containsTable = assertion.Subject.Any(table => table.TableName == expectedTableName);
}
if (!containsTable)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:collection} to contain table named {0}{reason}, but it does not.",
expectedTableName);
}
return new AndConstraint<GenericCollectionAssertions<DataTable>>(assertion);
}
/// <summary>
/// Asserts that the current <see cref="DataTableCollection"/> does not contain a table with the the supplied <paramref name="unexpectedTableName" />.
/// </summary>
/// <param name="unexpectedTableName">The table name that is not expected to be in the <see cref="DataTableCollection"/></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<DataTable>> NotContainTableWithName(this GenericCollectionAssertions<DataTable> assertion, string unexpectedTableName, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpectedTableName, nameof(unexpectedTableName), "Cannot verify that the collection does not contain a <null> DataTable.");
if (assertion.Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expeected {context:collection} to not contain table named {0}{reason}, but found {1}.", unexpectedTableName,
assertion.Subject);
}
bool containsTable;
if (assertion.Subject is DataTableCollectionWrapper dataTableCollection)
{
containsTable = dataTableCollection.Contains(unexpectedTableName);
}
else
{
containsTable = assertion.Subject.Any(table => table.TableName == unexpectedTableName);
}
if (containsTable)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:collection} to not contain table named {0}{reason}, but it does.",
unexpectedTableName);
}
return new AndConstraint<GenericCollectionAssertions<DataTable>>(assertion);
}
}
}