Skip to content

Commit 544532a

Browse files
authored
Use the Xunit assert library instead of CoreCLRTestLibrary for most asserts (#61226)
Asserts not provided by Xunit are provided by an AssertExtensions class. Tests that reference System.Private.Corelib directly will use a polyfill implementation based off the old CoreCLRTestLibrary asserts. All assert methods provided by CoreCLRTestLibrary have been changed to follow Xunit conventions.
1 parent aefb0fc commit 544532a

File tree

252 files changed

+5963
-6379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

252 files changed

+5963
-6379
lines changed

src/tests/Common/Assert.cs

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// This file is used to provide some basic Assert functionality for assemblies that directly reference System.Private.CoreLib
5+
// and not the ref pack.
6+
7+
// Note: Exception messages call ToString instead of Name to avoid MissingMetadataException when just outputting basic info
8+
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Threading.Tasks;
12+
13+
namespace Xunit
14+
{
15+
/// <summary>
16+
/// A collection of helper classes to test various conditions within
17+
/// unit tests. If the condition being tested is not met, an exception
18+
/// is thrown.
19+
/// </summary>
20+
public static class Assert
21+
{
22+
/// <summary>
23+
/// Asserts that the given delegate throws an <see cref="Exception"/> of type <typeparam name="T" />.
24+
/// </summary>
25+
/// <param name="action">
26+
/// The delagate of type <see cref="Action"/> to execute.
27+
/// </param>
28+
/// <param name="message">
29+
/// A <see cref="String"/> containing additional information for when the assertion fails.
30+
/// </param>
31+
/// <returns>
32+
/// The thrown <see cref="Exception"/>.
33+
/// </returns>
34+
/// <exception cref="AssertFailedException">
35+
/// <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
36+
/// </exception>
37+
public static T Throws<T>(Action action) where T : Exception
38+
{
39+
Exception exception = RunWithCatch(action);
40+
41+
if (exception == null)
42+
Assert.True(false, $"Expected '{typeof(T)}' to be thrown.");
43+
44+
if (exception is not T)
45+
Assert.True(false, $"Expected '{typeof(T)}' to be thrown, however '{exception.GetType()}' was thrown.");
46+
47+
return (T)exception;
48+
}
49+
50+
/// <summary>
51+
/// Tests whether the specified condition is true and throws an exception
52+
/// if the condition is false.
53+
/// </summary>
54+
/// <param name="condition">The condition the test expects to be true.</param>
55+
/// <param name="message">
56+
/// The message to include in the exception when <paramref name="condition"/>
57+
/// is false. The message is shown in test results.
58+
/// </param>
59+
/// <exception cref="AssertFailedException">
60+
/// Thrown if <paramref name="condition"/> is false.
61+
/// </exception>
62+
public static void True(bool condition, string message = "")
63+
{
64+
if (!condition)
65+
{
66+
Assert.HandleFail("Assert.True", message);
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Tests whether the specified condition is false and throws an exception
72+
/// if the condition is true.
73+
/// </summary>
74+
/// <param name="condition">The condition the test expects to be false.</param>
75+
/// <param name="message">
76+
/// The message to include in the exception when <paramref name="condition"/>
77+
/// is true. The message is shown in test results.
78+
/// </param>
79+
/// <exception cref="AssertFailedException">
80+
/// Thrown if <paramref name="condition"/> is true.
81+
/// </exception>
82+
public static void False(bool condition, string message = "")
83+
{
84+
if (condition)
85+
{
86+
Assert.HandleFail("Assert.False", message);
87+
}
88+
}
89+
90+
/// <summary>
91+
/// Tests whether the specified object is null and throws an exception
92+
/// if it is not.
93+
/// </summary>
94+
/// <param name="value">The object the test expects to be null.</param>
95+
/// <param name="message">
96+
/// The message to include in the exception when <paramref name="value"/>
97+
/// is not null. The message is shown in test results.
98+
/// </param>
99+
/// <exception cref="AssertFailedException">
100+
/// Thrown if <paramref name="value"/> is not null.
101+
/// </exception>
102+
public static void Null(object value)
103+
{
104+
if (value != null)
105+
{
106+
Assert.HandleFail("Assert.Null", "");
107+
}
108+
}
109+
110+
/// <summary>
111+
/// Tests whether the specified object is non-null and throws an exception
112+
/// if it is null.
113+
/// </summary>
114+
/// <param name="value">The object the test expects not to be null.</param>
115+
/// <param name="message">
116+
/// The message to include in the exception when <paramref name="value"/>
117+
/// is null. The message is shown in test results.
118+
/// </param>
119+
/// <exception cref="AssertFailedException">
120+
/// Thrown if <paramref name="value"/> is null.
121+
/// </exception>
122+
public static void NotNull(object value)
123+
{
124+
if (value == null)
125+
{
126+
Assert.HandleFail("Assert.NotNull", "");
127+
}
128+
}
129+
130+
/// <summary>
131+
/// Tests whether the expected object is equal to the actual object and
132+
/// throws an exception if it is not.
133+
/// </summary>
134+
/// <param name="notExpected">Expected object.</param>
135+
/// <param name="actual">Actual object.</param>
136+
/// <param name="message">Message to display upon failure.</param>
137+
public static void Equal<T>(T expected, T actual)
138+
{
139+
if (!Object.Equals(expected, actual))
140+
{
141+
Assert.HandleFail("Assert.Equal", "");
142+
}
143+
}
144+
145+
/// <summary>
146+
/// Tests whether the expected object is equal to the actual object and
147+
/// throws an exception if it is not.
148+
/// </summary>
149+
/// <param name="notExpected">Expected object.</param>
150+
/// <param name="actual">Actual object.</param>
151+
/// <param name="message">Message to display upon failure.</param>
152+
public static void Same(object expected, object actual)
153+
{
154+
const string EXPECTED_MSG = @"Expected: [{1}]. Actual: [{2}]. {0}";
155+
156+
if (!Object.ReferenceEquals(expected, actual))
157+
{
158+
Assert.HandleFail("Assert.Same", "");
159+
}
160+
}
161+
162+
/// <summary>
163+
/// Tests whether the expected object is equal to the actual object and
164+
/// throws an exception if it is not.
165+
/// </summary>
166+
/// <param name="notExpected">Expected object.</param>
167+
/// <param name="actual">Actual object.</param>
168+
/// <param name="message">Message to display upon failure.</param>
169+
public static void Equal<T>(T expected, T actual, string format, params Object[] args)
170+
{
171+
Equal<T>(expected, actual);
172+
}
173+
174+
/// <summary>
175+
/// Tests whether the expected object is equal to the actual object and
176+
/// throws an exception if it is not.
177+
/// </summary>
178+
/// </summary>
179+
/// <param name="notExpected">Expected object that we do not want it to be.</param>
180+
/// <param name="actual">Actual object.</param>
181+
/// <param name="message">Message to display upon failure.</param>
182+
public static void NotEqual<T>(T notExpected, T actual)
183+
{
184+
if (Object.Equals(notExpected, actual))
185+
{
186+
Assert.HandleFail("Assert.NotEqual", "");
187+
}
188+
}
189+
190+
/// <summary>
191+
/// Helper function that creates and throws an exception.
192+
/// </summary>
193+
/// <param name="assertionName">name of the assertion throwing an exception.</param>
194+
/// <param name="message">message describing conditions for assertion failure.</param>
195+
/// <param name="parameters">The parameters.</param>=
196+
internal static void HandleFail(string assertionName, string message)
197+
{
198+
throw new XunitException(assertionName + ": " + message);
199+
}
200+
201+
202+
[Obsolete("Did you mean to call Assert.Equal()")]
203+
public static new bool Equals(Object o1, Object o2)
204+
{
205+
Assert.True(false, "Don't call this.");
206+
throw new Exception();
207+
}
208+
209+
private static Exception RunWithCatch(Action action)
210+
{
211+
try
212+
{
213+
action();
214+
return null;
215+
}
216+
catch (Exception ex)
217+
{
218+
return ex;
219+
}
220+
}
221+
}
222+
223+
/// <summary>
224+
/// Exception raised by the Assert on Fail
225+
/// </summary>
226+
public class XunitException : Exception
227+
{
228+
public XunitException(string message)
229+
: base(message)
230+
{
231+
}
232+
233+
public XunitException()
234+
: base()
235+
{
236+
}
237+
}
238+
}

0 commit comments

Comments
 (0)