Skip to content

Commit 786d75d

Browse files
authored
Split Assert class per group of feature (#1238)
* make assert partial * applied comments * remove line * partial assert tests * remove * remove * applied PR's comments
1 parent 6d07ad1 commit 786d75d

15 files changed

+3648
-3434
lines changed

src/TestFramework/MSTest.Core/Assertions/Assert.AreEqual.cs

+1,591
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#if !NETCOREAPP3_0_OR_GREATER && !NET6_0_OR_GREATER
5+
#define HIDE_MESSAGELESS_IMPLEMENTATION
6+
#endif
7+
8+
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
using System;
11+
using System.Diagnostics.CodeAnalysis;
12+
using System.Globalization;
13+
using System.Reflection;
14+
using System.Runtime.CompilerServices;
15+
using System.Threading.Tasks;
16+
17+
/// <summary>
18+
/// A collection of helper classes to test various conditions within
19+
/// unit tests. If the condition being tested is not met, an exception
20+
/// is thrown.
21+
/// </summary>
22+
public sealed partial class Assert
23+
{
24+
#if HIDE_MESSAGELESS_IMPLEMENTATION
25+
/// <summary>
26+
/// Tests whether the specified objects both refer to the same object and
27+
/// throws an exception if the two inputs do not refer to the same object.
28+
/// </summary>
29+
/// <param name="expected">
30+
/// The first object to compare. This is the value the test expects.
31+
/// </param>
32+
/// <param name="actual">
33+
/// The second object to compare. This is the value produced by the code under test.
34+
/// </param>
35+
/// <exception cref="AssertFailedException">
36+
/// Thrown if <paramref name="expected"/> does not refer to the same object
37+
/// as <paramref name="actual"/>.
38+
/// </exception>
39+
public static void AreSame(object expected, object actual)
40+
{
41+
AreSame(expected, actual, string.Empty, null);
42+
}
43+
#endif
44+
45+
/// <summary>
46+
/// Tests whether the specified objects both refer to the same object and
47+
/// throws an exception if the two inputs do not refer to the same object.
48+
/// </summary>
49+
/// <param name="expected">
50+
/// The first object to compare. This is the value the test expects.
51+
/// </param>
52+
/// <param name="actual">
53+
/// The second object to compare. This is the value produced by the code under test.
54+
/// </param>
55+
/// <param name="message">
56+
/// The message to include in the exception when <paramref name="actual"/>
57+
/// is not the same as <paramref name="expected"/>. The message is shown
58+
/// in test results.
59+
/// </param>
60+
/// <exception cref="AssertFailedException">
61+
/// Thrown if <paramref name="expected"/> does not refer to the same object
62+
/// as <paramref name="actual"/>.
63+
/// </exception>
64+
public static void AreSame(object expected, object actual, [CallerArgumentExpression("actual")] string message = null)
65+
{
66+
AreSame(expected, actual, message, null);
67+
}
68+
69+
/// <summary>
70+
/// Tests whether the specified objects both refer to the same object and
71+
/// throws an exception if the two inputs do not refer to the same object.
72+
/// </summary>
73+
/// <param name="expected">
74+
/// The first object to compare. This is the value the test expects.
75+
/// </param>
76+
/// <param name="actual">
77+
/// The second object to compare. This is the value produced by the code under test.
78+
/// </param>
79+
/// <param name="message">
80+
/// The message to include in the exception when <paramref name="actual"/>
81+
/// is not the same as <paramref name="expected"/>. The message is shown
82+
/// in test results.
83+
/// </param>
84+
/// <param name="parameters">
85+
/// An array of parameters to use when formatting <paramref name="message"/>.
86+
/// </param>
87+
/// <exception cref="AssertFailedException">
88+
/// Thrown if <paramref name="expected"/> does not refer to the same object
89+
/// as <paramref name="actual"/>.
90+
/// </exception>
91+
public static void AreSame(object expected, object actual, [CallerArgumentExpression("actual")] string message = null, params object[] parameters)
92+
{
93+
if (!ReferenceEquals(expected, actual))
94+
{
95+
string userMessage = BuildUserMessage(message, parameters);
96+
string finalMessage = userMessage;
97+
98+
if (expected is ValueType valExpected)
99+
{
100+
if (actual is ValueType valActual)
101+
{
102+
finalMessage = string.Format(
103+
CultureInfo.CurrentCulture,
104+
FrameworkMessages.AreSameGivenValues,
105+
userMessage);
106+
}
107+
}
108+
109+
ThrowAssertFailed("Assert.AreSame", finalMessage);
110+
}
111+
}
112+
113+
#if HIDE_MESSAGELESS_IMPLEMENTATION
114+
/// <summary>
115+
/// Tests whether the specified objects refer to different objects and
116+
/// throws an exception if the two inputs refer to the same object.
117+
/// </summary>
118+
/// <param name="notExpected">
119+
/// The first object to compare. This is the value the test expects not
120+
/// to match <paramref name="actual"/>.
121+
/// </param>
122+
/// <param name="actual">
123+
/// The second object to compare. This is the value produced by the code under test.
124+
/// </param>
125+
/// <exception cref="AssertFailedException">
126+
/// Thrown if <paramref name="notExpected"/> refers to the same object
127+
/// as <paramref name="actual"/>.
128+
/// </exception>
129+
public static void AreNotSame(object notExpected, object actual)
130+
{
131+
AreNotSame(notExpected, actual, string.Empty, null);
132+
}
133+
#endif
134+
135+
/// <summary>
136+
/// Tests whether the specified objects refer to different objects and
137+
/// throws an exception if the two inputs refer to the same object.
138+
/// </summary>
139+
/// <param name="notExpected">
140+
/// The first object to compare. This is the value the test expects not
141+
/// to match <paramref name="actual"/>.
142+
/// </param>
143+
/// <param name="actual">
144+
/// The second object to compare. This is the value produced by the code under test.
145+
/// </param>
146+
/// <param name="message">
147+
/// The message to include in the exception when <paramref name="actual"/>
148+
/// is the same as <paramref name="notExpected"/>. The message is shown in
149+
/// test results.
150+
/// </param>
151+
/// <exception cref="AssertFailedException">
152+
/// Thrown if <paramref name="notExpected"/> refers to the same object
153+
/// as <paramref name="actual"/>.
154+
/// </exception>
155+
public static void AreNotSame(object notExpected, object actual, [CallerArgumentExpression("actual")] string message = null)
156+
{
157+
AreNotSame(notExpected, actual, message, null);
158+
}
159+
160+
/// <summary>
161+
/// Tests whether the specified objects refer to different objects and
162+
/// throws an exception if the two inputs refer to the same object.
163+
/// </summary>
164+
/// <param name="notExpected">
165+
/// The first object to compare. This is the value the test expects not
166+
/// to match <paramref name="actual"/>.
167+
/// </param>
168+
/// <param name="actual">
169+
/// The second object to compare. This is the value produced by the code under test.
170+
/// </param>
171+
/// <param name="message">
172+
/// The message to include in the exception when <paramref name="actual"/>
173+
/// is the same as <paramref name="notExpected"/>. The message is shown in
174+
/// test results.
175+
/// </param>
176+
/// <param name="parameters">
177+
/// An array of parameters to use when formatting <paramref name="message"/>.
178+
/// </param>
179+
/// <exception cref="AssertFailedException">
180+
/// Thrown if <paramref name="notExpected"/> refers to the same object
181+
/// as <paramref name="actual"/>.
182+
/// </exception>
183+
public static void AreNotSame(object notExpected, object actual, [CallerArgumentExpression("actual")] string message = null, params object[] parameters)
184+
{
185+
if (ReferenceEquals(notExpected, actual))
186+
{
187+
ThrowAssertFailed("Assert.AreNotSame", BuildUserMessage(message, parameters));
188+
}
189+
}
190+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
5+
6+
using System;
7+
using System.Diagnostics.CodeAnalysis;
8+
using System.Globalization;
9+
using System.Reflection;
10+
using System.Runtime.CompilerServices;
11+
using System.Threading.Tasks;
12+
13+
/// <summary>
14+
/// A collection of helper classes to test various conditions within
15+
/// unit tests. If the condition being tested is not met, an exception
16+
/// is thrown.
17+
/// </summary>
18+
public sealed partial class Assert
19+
{
20+
/// <summary>
21+
/// Throws an AssertFailedException.
22+
/// </summary>
23+
/// <exception cref="AssertFailedException">
24+
/// Always thrown.
25+
/// </exception>
26+
[DoesNotReturn]
27+
public static void Fail()
28+
{
29+
Fail(string.Empty, null);
30+
}
31+
32+
/// <summary>
33+
/// Throws an AssertFailedException.
34+
/// </summary>
35+
/// <param name="message">
36+
/// The message to include in the exception. The message is shown in
37+
/// test results.
38+
/// </param>
39+
/// <exception cref="AssertFailedException">
40+
/// Always thrown.
41+
/// </exception>
42+
[DoesNotReturn]
43+
public static void Fail(string message)
44+
{
45+
Fail(message, null);
46+
}
47+
48+
/// <summary>
49+
/// Throws an AssertFailedException.
50+
/// </summary>
51+
/// <param name="message">
52+
/// The message to include in the exception. The message is shown in
53+
/// test results.
54+
/// </param>
55+
/// <param name="parameters">
56+
/// An array of parameters to use when formatting <paramref name="message"/>.
57+
/// </param>
58+
/// <exception cref="AssertFailedException">
59+
/// Always thrown.
60+
/// </exception>
61+
[DoesNotReturn]
62+
public static void Fail(string message, params object[] parameters)
63+
{
64+
ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using System.Globalization;
6+
7+
/// <summary>
8+
/// A collection of helper classes to test various conditions within
9+
/// unit tests. If the condition being tested is not met, an exception
10+
/// is thrown.
11+
/// </summary>
12+
public sealed partial class Assert
13+
{
14+
/// <summary>
15+
/// Throws an AssertInconclusiveException.
16+
/// </summary>
17+
/// <exception cref="AssertInconclusiveException">
18+
/// Always thrown.
19+
/// </exception>
20+
public static void Inconclusive()
21+
{
22+
Inconclusive(string.Empty, null);
23+
}
24+
25+
/// <summary>
26+
/// Throws an AssertInconclusiveException.
27+
/// </summary>
28+
/// <param name="message">
29+
/// The message to include in the exception. The message is shown in
30+
/// test results.
31+
/// </param>
32+
/// <exception cref="AssertInconclusiveException">
33+
/// Always thrown.
34+
/// </exception>
35+
public static void Inconclusive(string message)
36+
{
37+
Inconclusive(message, null);
38+
}
39+
40+
/// <summary>
41+
/// Throws an AssertInconclusiveException.
42+
/// </summary>
43+
/// <param name="message">
44+
/// The message to include in the exception. The message is shown in
45+
/// test results.
46+
/// </param>
47+
/// <param name="parameters">
48+
/// An array of parameters to use when formatting <paramref name="message"/>.
49+
/// </param>
50+
/// <exception cref="AssertInconclusiveException">
51+
/// Always thrown.
52+
/// </exception>
53+
public static void Inconclusive(string message, params object[] parameters)
54+
{
55+
string userMessage = BuildUserMessage(message, parameters);
56+
throw new AssertInconclusiveException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, "Assert.Inconclusive", userMessage));
57+
}
58+
}

0 commit comments

Comments
 (0)