-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathAssert.AreSame.cs
More file actions
253 lines (213 loc) · 13.3 KB
/
Assert.AreSame.cs
File metadata and controls
253 lines (213 loc) · 13.3 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.ComponentModel;
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
/// <summary>
/// A collection of helper classes to test various conditions within
/// unit tests. If the condition being tested is not met, an exception
/// is thrown.
/// </summary>
public sealed partial class Assert
{
[InterpolatedStringHandler]
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public readonly struct AssertAreSameInterpolatedStringHandler<TArgument>
{
private readonly StringBuilder? _builder;
private readonly TArgument? _expected;
private readonly TArgument? _actual;
public AssertAreSameInterpolatedStringHandler(int literalLength, int formattedCount, TArgument? expected, TArgument? actual, out bool shouldAppend)
{
_expected = expected;
_actual = actual;
shouldAppend = IsAreSameFailing(expected, actual);
if (shouldAppend)
{
_builder = new StringBuilder(literalLength + formattedCount);
}
}
internal void ComputeAssertion(string expectedExpression, string actualExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionTwoParametersMessage, "expected", expectedExpression, "actual", actualExpression) + " ");
ReportAssertAreSameFailed(_expected, _actual, _builder.ToString());
}
}
public void AppendLiteral(string value) => _builder!.Append(value);
public void AppendFormatted<T>(T value) => AppendFormatted(value, format: null);
#if NETCOREAPP3_1_OR_GREATER
public void AppendFormatted(ReadOnlySpan<char> value) => _builder!.Append(value);
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null) => AppendFormatted(value.ToString(), alignment, format);
#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
#endif
// NOTE: All the overloads involving format and/or alignment are not super efficient.
// This code path is only for when an assert is failing, so that's not the common scenario
// and should be okay if not very optimized.
// A more efficient implementation that can be used for .NET 6 and later is to delegate the work to
// the BCL's StringBuilder.AppendInterpolatedStringHandler
public void AppendFormatted<T>(T value, string? format) => _builder!.AppendFormat(null, $"{{0:{format}}}", value);
public void AppendFormatted<T>(T value, int alignment) => _builder!.AppendFormat(null, $"{{0,{alignment}}}", value);
public void AppendFormatted<T>(T value, int alignment, string? format) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
public void AppendFormatted(string? value) => _builder!.Append(value);
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
public void AppendFormatted(string? value, int alignment = 0, string? format = null) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
public void AppendFormatted(object? value, int alignment = 0, string? format = null) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
}
[InterpolatedStringHandler]
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct AssertAreNotSameInterpolatedStringHandler<TArgument>
{
private readonly StringBuilder? _builder;
public AssertAreNotSameInterpolatedStringHandler(int literalLength, int formattedCount, TArgument? notExpected, TArgument? actual, out bool shouldAppend)
{
shouldAppend = IsAreNotSameFailing(notExpected, actual);
if (shouldAppend)
{
_builder = new StringBuilder(literalLength + formattedCount);
}
}
internal void ComputeAssertion(string notExpectedExpression, string actualExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionTwoParametersMessage, "notExpected", notExpectedExpression, "actual", actualExpression) + " ");
ReportAssertAreNotSameFailed(_builder.ToString());
}
}
public void AppendLiteral(string value) => _builder!.Append(value);
public void AppendFormatted<T>(T value) => AppendFormatted(value, format: null);
#if NETCOREAPP3_1_OR_GREATER
public void AppendFormatted(ReadOnlySpan<char> value) => _builder!.Append(value);
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null) => AppendFormatted(value.ToString(), alignment, format);
#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
#endif
// NOTE: All the overloads involving format and/or alignment are not super efficient.
// This code path is only for when an assert is failing, so that's not the common scenario
// and should be okay if not very optimized.
// A more efficient implementation that can be used for .NET 6 and later is to delegate the work to
// the BCL's StringBuilder.AppendInterpolatedStringHandler
public void AppendFormatted<T>(T value, string? format) => _builder!.AppendFormat(null, $"{{0:{format}}}", value);
public void AppendFormatted<T>(T value, int alignment) => _builder!.AppendFormat(null, $"{{0,{alignment}}}", value);
public void AppendFormatted<T>(T value, int alignment, string? format) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
public void AppendFormatted(string? value) => _builder!.Append(value);
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
public void AppendFormatted(string? value, int alignment = 0, string? format = null) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
public void AppendFormatted(object? value, int alignment = 0, string? format = null) => _builder!.AppendFormat(null, $"{{0,{alignment}:{format}}}", value);
#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
/// <inheritdoc cref="AreSame{T}(T, T, string?, string, string)" />
#pragma warning disable IDE0060 // Remove unused parameter - https://github.com/dotnet/roslyn/issues/76578
public static void AreSame<T>(T? expected, T? actual, [InterpolatedStringHandlerArgument(nameof(expected), nameof(actual))] ref AssertAreSameInterpolatedStringHandler<T> message, [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "")
#pragma warning restore IDE0060 // Remove unused parameter
=> message.ComputeAssertion(expectedExpression, actualExpression);
#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads
/// <summary>
/// Tests whether the specified objects both refer to the same object and
/// throws an exception if the two inputs do not refer to the same object.
/// </summary>
/// <typeparam name="T">
/// The type of values to compare.
/// </typeparam>
/// <param name="expected">
/// The first object to compare. This is the value the test expects.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is not the same as <paramref name="expected"/>. The message is shown
/// in test results.
/// </param>
/// <param name="expectedExpression">
/// The syntactic expression of expected as given by the compiler via caller argument expression.
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <param name="actualExpression">
/// The syntactic expression of actual as given by the compiler via caller argument expression.
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual, string? message = "", [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "")
{
if (!IsAreSameFailing(expected, actual))
{
return;
}
string userMessage = BuildUserMessageForExpectedExpressionAndActualExpression(message, expectedExpression, actualExpression);
ReportAssertAreSameFailed(expected, actual, userMessage);
}
private static bool IsAreSameFailing<T>(T? expected, T? actual)
=> !object.ReferenceEquals(expected, actual);
[DoesNotReturn]
private static void ReportAssertAreSameFailed<T>(T? expected, T? actual, string userMessage)
{
string finalMessage = userMessage;
if (expected is ValueType && actual is ValueType)
{
finalMessage = string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreSameGivenValues,
userMessage);
}
ReportAssertFailed("Assert.AreSame", finalMessage);
}
/// <inheritdoc cref="AreNotSame{T}(T, T, string?, string, string)" />
#pragma warning disable IDE0060 // Remove unused parameter - https://github.com/dotnet/roslyn/issues/76578
public static void AreNotSame<T>(T? notExpected, T? actual, [InterpolatedStringHandlerArgument(nameof(notExpected), nameof(actual))] ref AssertAreNotSameInterpolatedStringHandler<T> message, [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "")
#pragma warning restore IDE0060 // Remove unused parameter
=> message.ComputeAssertion(notExpectedExpression, actualExpression);
/// <summary>
/// Tests whether the specified objects refer to different objects and
/// throws an exception if the two inputs refer to the same object.
/// </summary>
/// <typeparam name="T">
/// The type of values to compare.
/// </typeparam>
/// <param name="notExpected">
/// The first object to compare. This is the value the test expects not
/// to match <paramref name="actual"/>.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is the same as <paramref name="notExpected"/>. The message is shown in
/// test results.
/// </param>
/// <param name="notExpectedExpression">
/// The syntactic expression of notExpected as given by the compiler via caller argument expression.
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <param name="actualExpression">
/// The syntactic expression of actual as given by the compiler via caller argument expression.
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="notExpected"/> refers to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreNotSame<T>(T? notExpected, T? actual, string? message = "", [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "")
{
if (IsAreNotSameFailing(notExpected, actual))
{
ReportAssertAreNotSameFailed(BuildUserMessageForNotExpectedExpressionAndActualExpression(message, notExpectedExpression, actualExpression));
}
}
private static bool IsAreNotSameFailing<T>(T? notExpected, T? actual)
=> object.ReferenceEquals(notExpected, actual);
[DoesNotReturn]
private static void ReportAssertAreNotSameFailed(string userMessage)
=> ReportAssertFailed("Assert.AreNotSame", userMessage);
}