|
| 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 | +} |
0 commit comments