• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fluentassertions / fluentassertions.datasets / 12413638127

19 Dec 2024 01:27PM UTC coverage: 94.362%. First build
12413638127

Pull #5

github

web-flow
Merge e71ce3c7f into daac80f4c
Pull Request #5: GitHub actions

458 of 498 branches covered (91.97%)

Branch coverage included in aggregate %.

1266 of 1329 relevant lines covered (95.26%)

2324.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/Src/FluentAssertions.DataSets/DataRowAssertions.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
using System.Diagnostics;
5
using System.Linq;
6
using FluentAssertions.DataSets.Common;
7
using FluentAssertions.Equivalency;
8
using FluentAssertions.Execution;
9
using FluentAssertions.Primitives;
10

11
namespace FluentAssertions.DataSets;
12

13
/// <summary>
14
/// Provides convenient assertion methods on a <see cref="DataRow"/> that can be
15
/// used to assert equivalency and the presence of columns.
16
/// </summary>
17
[DebuggerNonUserCode]
18
public class DataRowAssertions<TDataRow> : ReferenceTypeAssertions<TDataRow, DataRowAssertions<TDataRow>>
19
    where TDataRow : DataRow
20
{
21
    public DataRowAssertions(TDataRow dataRow, AssertionChain assertionChain)
22
        : base(dataRow, assertionChain)
50✔
23
    {
24
    }
50✔
25

26
    /// <summary>
27
    /// Asserts that an instance of <see cref="DataRow"/> has a column with the expected column name.
28
    /// </summary>
29
    /// <param name="expectedColumnName">The value that is expected in <see cref="DataColumn.ColumnName"/>.</param>
30
    /// <param name="because">
31
    /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
32
    /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
33
    /// </param>
34
    /// <param name="becauseArgs">
35
    /// Zero or more objects to format using the placeholders in <paramref name="because"/>.
36
    /// </param>
37
    public AndWhichConstraint<DataRowAssertions<TDataRow>, DataColumn> HaveColumn(string expectedColumnName, string because = "",
38
        params object[] becauseArgs)
39
    {
40
        var subjectColumn = default(DataColumn);
6✔
41

42
        if (Subject is null)
6✔
43
        {
44
            CurrentAssertionChain
2✔
45
                .BecauseOf(because, becauseArgs)
2✔
46
                .FailWith("Expected {context:DataRow} to contain a column named {0}{reason}, but found <null>.",
2✔
47
                    expectedColumnName);
2✔
48
        }
49
        else if (!Subject.Table.Columns.Contains(expectedColumnName))
4✔
50
        {
51
            CurrentAssertionChain
2✔
52
                .BecauseOf(because, becauseArgs)
2✔
53
                .FailWith("Expected {context:DataRow} to contain a column named {0}{reason}, but it does not.",
2✔
54
                    expectedColumnName);
2✔
55
        }
56
        else
57
        {
58
            subjectColumn = Subject.Table.Columns[expectedColumnName];
2✔
59
        }
60

61
        return new AndWhichConstraint<DataRowAssertions<TDataRow>, DataColumn>(this, subjectColumn);
2✔
62
    }
63

64
    /// <summary>
65
    /// Asserts that an instance of <see cref="DataRow"/> has columns with all of the supplied expected column names.
66
    /// </summary>
67
    /// <param name="expectedColumnNames">An array of values expected in <see cref="DataColumn.ColumnName"/>.</param>
68
    public AndConstraint<DataRowAssertions<TDataRow>> HaveColumns(params string[] expectedColumnNames)
69
    {
70
        return HaveColumns((IEnumerable<string>)expectedColumnNames);
6✔
71
    }
72

73
    /// <summary>
74
    /// Asserts that an instance of <see cref="DataRow"/> has columns with all of the supplied expected column names.
75
    /// </summary>
76
    /// <param name="expectedColumnNames">An <see cref="IEnumerable{T}"/> of string values expected in <see cref="DataColumn.ColumnName"/>.</param>
77
    /// <param name="because">
78
    /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
79
    /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
80
    /// </param>
81
    /// <param name="becauseArgs">
82
    /// Zero or more objects to format using the placeholders in <paramref name="because"/>.
83
    /// </param>
84
    public AndConstraint<DataRowAssertions<TDataRow>> HaveColumns(IEnumerable<string> expectedColumnNames, string because = "",
85
        params object[] becauseArgs)
86
    {
87
        CurrentAssertionChain
10✔
88
            .ForCondition(Subject is not null)
10✔
89
            .BecauseOf(because, becauseArgs)
10✔
90
            .FailWith(
10✔
91
                "Expected {context:DataRow} to be in a table containing {0} column(s) with specific names{reason}, but found <null>.",
10✔
92
                () => expectedColumnNames.Count());
12✔
93

94
        if (CurrentAssertionChain.Succeeded)
10✔
95
        {
96
            foreach (var expectedColumnName in expectedColumnNames)
68✔
97
            {
98
                CurrentAssertionChain
28✔
99
                    .ForCondition(Subject.Table.Columns.Contains(expectedColumnName))
28✔
100
                    .BecauseOf(because, becauseArgs)
28✔
101
                    .FailWith("Expected table containing {context:DataRow} to contain a column named {0}{reason}, but it does not.",
28✔
102
                        expectedColumnName);
28✔
103
            }
104
        }
105

106
        return new AndConstraint<DataRowAssertions<TDataRow>>(this);
6✔
107
    }
108

109
    /// <summary>
110
    /// Asserts that an instance of <see cref="DataRow"/> is equivalent to another.
111
    /// </summary>
112
    /// <remarks>
113
    /// Data rows are equivalent when they contain identical field data for the row they represent, and
114
    /// the following members have the same values:
115
    ///
116
    /// <list type="bullet">
117
    ///   <item><description>HasErrors</description></item>
118
    ///   <item><description>RowState</description></item>
119
    /// </list>
120
    ///
121
    /// The <see cref="DataRow"/> objects must be of the same type; if two <see cref="DataRow"/> objects
122
    /// are equivalent in all ways, except that one is part of a typed <see cref="DataTable"/> and is of a subclass
123
    /// of <see cref="DataRow"/>, then by default, they will not be considered equivalent. This can be overridden
124
    /// by using the overload that takes <see cref="IDataEquivalencyAssertionOptions{DataSet}"/>.
125
    /// </remarks>
126
    /// <param name="expectation">A <see cref="DataColumn"/> with the expected configuration.</param>
127
    /// <param name="because">
128
    /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
129
    /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
130
    /// </param>
131
    /// <param name="becauseArgs">
132
    /// Zero or more objects to format using the placeholders in <paramref name="because"/>.
133
    /// </param>
134
    public AndConstraint<DataRowAssertions<TDataRow>> BeEquivalentTo(DataRow expectation, string because = "",
135
        params object[] becauseArgs)
136
    {
137
        return BeEquivalentTo(
20✔
138
            expectation,
20✔
139
            options => options,
20✔
140
            because,
20✔
141
            becauseArgs);
20✔
142
    }
143

144
    /// <summary>
145
    /// Asserts that an instance of <see cref="DataRow"/> is equivalent to another.
146
    /// </summary>
147
    /// <remarks>
148
    /// Data rows are equivalent when they contain identical field data for the row they represent, and
149
    /// the following members have the same values:
150
    ///
151
    /// <list type="bullet">
152
    ///   <item><description>HasErrors</description></item>
153
    ///   <item><description>RowState</description></item>
154
    /// </list>
155
    ///
156
    /// <para>
157
    /// The <see cref="DataRow"/> objects must be of the same type; if two <see cref="DataRow"/> objects
158
    /// are equivalent in all ways, except that one is part of a typed <see cref="DataTable"/> and is of a subclass
159
    /// of <see cref="DataRow"/>, then by default, they will not be considered equivalent.
160
    /// </para>
161
    /// <para>
162
    /// This, as well as testing of any property can be overridden using the <paramref name="config"/> callback.
163
    /// By calling <see cref="IDataEquivalencyAssertionOptions{T}.AllowingMismatchedTypes"/>, two <see cref="DataRow"/>
164
    /// objects of differing types can be considered equivalent. Exclude specific properties using
165
    /// <see cref="IDataEquivalencyAssertionOptions{T}.Excluding(System.Linq.Expressions.Expression{Func{T, object}})"/>.
166
    /// Exclude columns of the data table (which also excludes the related field data in <see cref="DataRow"/>
167
    /// objects) using <see cref="IDataEquivalencyAssertionOptions{T}.ExcludingColumn(DataColumn)"/> or a related function.
168
    /// </para>
169
    /// </remarks>
170
    ///
171
    /// You can use <see cref="IDataEquivalencyAssertionOptions{T}.ExcludingRelated(System.Linq.Expressions.Expression{Func{DataTable, object}})"/>
172
    /// and related functions to exclude properties on other related System.Data types.
173
    /// <param name="expectation">A <see cref="DataColumn"/> with the expected configuration.</param>
174
    /// <param name="config">
175
    /// A reference to the <see cref="IDataEquivalencyAssertionOptions{DataRow}"/> configuration object that can be used
176
    /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
177
    /// <see cref="IDataEquivalencyAssertionOptions{DataRow}"/> class. The global defaults are determined by the
178
    /// <see cref="AssertionOptions"/> class.
179
    /// </param>
180
    /// <param name="because">
181
    /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
182
    /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
183
    /// </param>
184
    /// <param name="becauseArgs">
185
    /// Zero or more objects to format using the placeholders in <paramref name="because"/>.
186
    /// </param>
187
    /// <exception cref="ArgumentNullException"><paramref name="config"/> is <see langword="null"/>.</exception>
188
    public AndConstraint<DataRowAssertions<TDataRow>> BeEquivalentTo(DataRow expectation,
189
        Func<IDataEquivalencyAssertionOptions<DataRow>, IDataEquivalencyAssertionOptions<DataRow>> config, string because = "",
190
        params object[] becauseArgs)
191
    {
192
        Guard.ThrowIfArgumentIsNull(config);
34✔
193

194
        var defaults = new DataEquivalencyAssertionOptions<DataRow>(AssertionOptions.CloneDefaults<DataRow>());
34✔
195
        config(defaults);
34✔
196

197
        ((object)Subject).Should().BeEquivalentTo(expectation, _ => defaults, because, becauseArgs);
68✔
198

199
        return new AndConstraint<DataRowAssertions<TDataRow>>(this);
20✔
200
    }
201

202
    protected override string Identifier => "DataRow";
×
203
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc