-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathEqualityAsserts_aot.cs
More file actions
167 lines (145 loc) · 4.76 KB
/
EqualityAsserts_aot.cs
File metadata and controls
167 lines (145 loc) · 4.76 KB
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
#if XUNIT_AOT
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8604
#endif
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Xunit.Sdk;
namespace Xunit
{
partial class Assert
{
/// <summary>
/// Verifies that two values in a <see cref="KeyValuePair{TKey, TValue}"/> are equal.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="expected">The expected key/value pair</param>
/// <param name="actual">The actual key/value pair</param>
[OverloadResolutionPriority(1)]
public static void Equal<TKey, TValue>(
KeyValuePair<TKey, TValue>? expected,
KeyValuePair<TKey, TValue>? actual)
{
if (!expected.HasValue)
{
if (!actual.HasValue)
return;
throw EqualException.ForMismatchedValues(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual));
}
if (actual == null)
throw EqualException.ForMismatchedValues(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual));
var keyComparer = new AssertEqualityComparer<TKey>();
if (!keyComparer.Equals(expected.Value.Key, actual.Value.Key))
throw EqualException.ForMismatchedValues(
ArgumentFormatter.Format(expected.Value.Key),
ArgumentFormatter.Format(actual.Value.Key),
"Keys differ in KeyValuePair"
);
var valueComparer = new AssertEqualityComparer<TValue>();
if (!valueComparer.Equals(expected.Value.Value, actual.Value.Value))
throw EqualException.ForMismatchedValues(
ArgumentFormatter.Format(expected.Value.Value),
ArgumentFormatter.Format(actual.Value.Value),
"Values differ in KeyValuePair"
);
}
#pragma warning disable IDE0060 // Remove unused parameter
#if XUNIT_NULLABLE
static string? GetCollectionDisplay(
Type? expectedType,
Type? expectedTypeDefinition,
Type? actualType,
Type? actualTypeDefinition)
#else
static string? GetCollectionDisplay(
Type expectedType,
Type expectedTypeDefinition,
Type actualType,
Type actualTypeDefinition)
#endif
{
if (expectedTypeDefinition == typeofDictionary && actualTypeDefinition == typeofDictionary)
return "Dictionaries";
else if (expectedTypeDefinition == typeofHashSet && actualTypeDefinition == typeofHashSet)
return "HashSets";
return null;
}
#pragma warning restore IDE0060 // Remove unused parameter
/// <summary>
/// Verifies that two values in a <see cref="KeyValuePair{TKey, TValue}"/> are not equal.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="expected">The expected key/value pair</param>
/// <param name="actual">The actual key/value pair</param>
[OverloadResolutionPriority(1)]
public static void NotEqual<TKey, TValue>(
KeyValuePair<TKey, TValue>? expected,
KeyValuePair<TKey, TValue>? actual)
{
if (expected == null)
{
if (actual != null)
return;
throw NotEqualException.ForEqualValues("null", "null");
}
if (actual == null)
return;
var keyComparer = new AssertEqualityComparer<TKey>();
if (!keyComparer.Equals(expected.Value.Key, actual.Value.Key))
return;
var valueComparer = new AssertEqualityComparer<TValue>();
if (!valueComparer.Equals(expected.Value.Value, actual.Value.Value))
return;
throw NotEqualException.ForEqualValues(ArgumentFormatter.Format(expected.Value), ArgumentFormatter.Format(actual.Value));
}
/// <summary>
/// Verifies that two objects are strictly not equal, using <see cref="object.Equals(object, object)"/>.
/// </summary>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
public static void NotStrictEqual(
#if XUNIT_NULLABLE
object? expected,
object? actual)
#else
object expected,
object actual)
#endif
{
if (!object.Equals(expected, actual))
return;
throw NotStrictEqualException.ForEqualValues(
ArgumentFormatter.Format(expected),
ArgumentFormatter.Format(actual)
);
}
/// <summary>
/// Verifies that two objects are strictly equal, using <see cref="object.Equals(object, object)"/>.
/// </summary>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
public static void StrictEqual(
#if XUNIT_NULLABLE
object? expected,
object? actual)
#else
object expected,
object actual)
#endif
{
if (object.Equals(expected, actual))
return;
throw StrictEqualException.ForEqualValues(
ArgumentFormatter.Format(expected),
ArgumentFormatter.Format(actual)
);
}
}
}
#endif // XUNIT_AOT