forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEqualityAsserts.cs
198 lines (181 loc) · 9.8 KB
/
EqualityAsserts.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Globalization;
using Xunit.Sdk;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Verifies that two objects are equal, using a default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
public static void Equal<T>(T expected, T actual)
{
Equal(expected, actual, GetEqualityComparer<T>());
}
/// <summary>
/// Verifies that two objects are equal, using a custom equatable comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="comparer">The comparer used to compare the two objects</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
public static void Equal<T>(T expected, T actual, IEqualityComparer<T> comparer)
{
Assert.GuardArgumentNotNull("comparer", comparer);
if (!comparer.Equals(expected, actual))
throw new EqualException(expected, actual);
}
/// <summary>
/// Verifies that two <see cref="double"/> values are equal, within the number of decimal
/// places given by <paramref name="precision"/>. The values are rounded before comparison.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(double expected, double actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (!GetEqualityComparer<double>().Equals(expectedRounded, actualRounded))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="decimal"/> values are equal, within the number of decimal
/// places given by <paramref name="precision"/>. The values are rounded before comparison.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-28)</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(decimal expected, decimal actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (!GetEqualityComparer<decimal>().Equals(expectedRounded, actualRounded))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="DateTime"/> values are equal, within the precision
/// given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The allowed difference in time where the two dates are considered equal</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(DateTime expected, DateTime actual, TimeSpan precision)
{
var difference = (expected - actual).Duration();
if (difference > precision)
{
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} ", expected),
string.Format(CultureInfo.CurrentCulture, "{0} difference {1} is larger than {2}",
actual,
difference.ToString(),
precision.ToString()
));
}
}
/// <summary>
/// Verifies that two objects are strictly equal, using the type's default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
public static void StrictEqual<T>(T expected, T actual)
{
Equal(expected, actual, EqualityComparer<T>.Default);
}
/// <summary>
/// Verifies that two objects are not equal, using a default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
public static void NotEqual<T>(T expected, T actual)
{
NotEqual(expected, actual, GetEqualityComparer<T>());
}
/// <summary>
/// Verifies that two objects are not equal, using a custom equality comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <param name="comparer">The comparer used to examine the objects</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
public static void NotEqual<T>(T expected, T actual, IEqualityComparer<T> comparer)
{
Assert.GuardArgumentNotNull("comparer", comparer);
if (comparer.Equals(expected, actual))
throw new NotEqualException(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual));
}
/// <summary>
/// Verifies that two <see cref="double"/> values are not equal, within the number of decimal
/// places given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
/// <exception cref="EqualException">Thrown when the values are equal</exception>
public static void NotEqual(double expected, double actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (GetEqualityComparer<double>().Equals(expectedRounded, actualRounded))
throw new NotEqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="decimal"/> values are not equal, within the number of decimal
/// places given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-28)</param>
/// <exception cref="EqualException">Thrown when the values are equal</exception>
public static void NotEqual(decimal expected, decimal actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (GetEqualityComparer<decimal>().Equals(expectedRounded, actualRounded))
throw new NotEqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two objects are strictly not equal, using the type's default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
public static void NotStrictEqual<T>(T expected, T actual)
{
NotEqual(expected, actual, EqualityComparer<T>.Default);
}
}
}