|
1 | | -// Copyright (c) Microsoft Corporation. All rights reserved. |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | 4 | using System.ComponentModel; |
@@ -38,8 +38,7 @@ internal void ComputeAssertion(string valueExpression) |
38 | 38 | { |
39 | 39 | if (_builder is not null) |
40 | 40 | { |
41 | | - _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " "); |
42 | | - ReportAssertIsInstanceOfTypeFailed(_value, _expectedType, _builder.ToString()); |
| 41 | + ReportAssertIsInstanceOfTypeFailed(_value, _expectedType, _builder.ToString(), valueExpression); |
43 | 42 | } |
44 | 43 | } |
45 | 44 |
|
@@ -98,8 +97,7 @@ internal void ComputeAssertion(string valueExpression) |
98 | 97 | { |
99 | 98 | if (_builder is not null) |
100 | 99 | { |
101 | | - _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " "); |
102 | | - ReportAssertIsInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString()); |
| 100 | + ReportAssertIsInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression); |
103 | 101 | } |
104 | 102 | } |
105 | 103 |
|
@@ -160,8 +158,7 @@ internal void ComputeAssertion(string valueExpression) |
160 | 158 | { |
161 | 159 | if (_builder is not null) |
162 | 160 | { |
163 | | - _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " "); |
164 | | - ReportAssertIsNotInstanceOfTypeFailed(_value, _wrongType, _builder.ToString()); |
| 161 | + ReportAssertIsNotInstanceOfTypeFailed(_value, _wrongType, _builder.ToString(), valueExpression); |
165 | 162 | } |
166 | 163 | } |
167 | 164 |
|
@@ -220,8 +217,7 @@ internal void ComputeAssertion(string valueExpression) |
220 | 217 | { |
221 | 218 | if (_builder is not null) |
222 | 219 | { |
223 | | - _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " "); |
224 | | - ReportAssertIsNotInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString()); |
| 220 | + ReportAssertIsNotInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression); |
225 | 221 | } |
226 | 222 | } |
227 | 223 |
|
@@ -292,7 +288,7 @@ public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? exp |
292 | 288 | { |
293 | 289 | if (IsInstanceOfTypeFailing(value, expectedType)) |
294 | 290 | { |
295 | | - ReportAssertIsInstanceOfTypeFailed(value, expectedType, BuildUserMessageForValueExpression(message, valueExpression)); |
| 291 | + ReportAssertIsInstanceOfTypeFailed(value, expectedType, message, valueExpression); |
296 | 292 | } |
297 | 293 | } |
298 | 294 |
|
@@ -331,18 +327,25 @@ private static bool IsInstanceOfTypeFailing([NotNullWhen(false)] object? value, |
331 | 327 | => expectedType == null || value == null || !expectedType.IsInstanceOfType(value); |
332 | 328 |
|
333 | 329 | [DoesNotReturn] |
334 | | - private static void ReportAssertIsInstanceOfTypeFailed(object? value, Type? expectedType, string userMessage) |
| 330 | + private static void ReportAssertIsInstanceOfTypeFailed(object? value, Type? expectedType, string? userMessage, string valueExpression) |
335 | 331 | { |
336 | | - string finalMessage = expectedType is not null |
337 | | - ? string.Format( |
338 | | - CultureInfo.CurrentCulture, |
339 | | - FrameworkMessages.IsInstanceOfFailMsg, |
340 | | - userMessage, |
341 | | - expectedType.ToString(), |
342 | | - value?.GetType().ToString() ?? "null") |
343 | | - : userMessage; |
344 | | - |
345 | | - ReportAssertFailed("Assert.IsInstanceOfType", finalMessage); |
| 332 | + StructuredAssertionMessage msg = expectedType is null |
| 333 | + ? new("Cannot check type because the expected type argument is null.") |
| 334 | + : new($"Expected value to be of type {expectedType.Name} (or derived)."); |
| 335 | + msg.WithUserMessage(userMessage); |
| 336 | + |
| 337 | + if (expectedType is not null) |
| 338 | + { |
| 339 | + string actualTypeText = value?.GetType().ToString() ?? "null"; |
| 340 | + EvidenceBlock evidence = EvidenceBlock.Create() |
| 341 | + .AddLine("expected type:", $"{expectedType} (or derived)") |
| 342 | + .AddLine(value is null ? "actual:" : "actual type:", actualTypeText); |
| 343 | + msg.WithEvidence(evidence) |
| 344 | + .WithExpectedAndActual($"{expectedType} (or derived)", actualTypeText); |
| 345 | + } |
| 346 | + |
| 347 | + msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsInstanceOfType", valueExpression, "<value>")); |
| 348 | + ReportAssertFailed(msg); |
346 | 349 | } |
347 | 350 |
|
348 | 351 | /// <summary> |
@@ -374,7 +377,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, |
374 | 377 | { |
375 | 378 | if (IsNotInstanceOfTypeFailing(value, wrongType)) |
376 | 379 | { |
377 | | - ReportAssertIsNotInstanceOfTypeFailed(value, wrongType, BuildUserMessageForValueExpression(message, valueExpression)); |
| 380 | + ReportAssertIsNotInstanceOfTypeFailed(value, wrongType, message, valueExpression); |
378 | 381 | } |
379 | 382 | } |
380 | 383 |
|
@@ -407,19 +410,24 @@ private static bool IsNotInstanceOfTypeFailing(object? value, [NotNullWhen(false |
407 | 410 | (value is not null && wrongType.IsInstanceOfType(value)); |
408 | 411 |
|
409 | 412 | [DoesNotReturn] |
410 | | - private static void ReportAssertIsNotInstanceOfTypeFailed(object? value, Type? wrongType, string userMessage) |
| 413 | + private static void ReportAssertIsNotInstanceOfTypeFailed(object? value, Type? wrongType, string? userMessage, string valueExpression) |
411 | 414 | { |
412 | | - string finalMessage = userMessage; |
| 415 | + StructuredAssertionMessage msg = wrongType is null |
| 416 | + ? new("Cannot check type because the not-expected type argument is null.") |
| 417 | + : new($"Expected value to not be of type {wrongType.Name} (or derived)."); |
| 418 | + msg.WithUserMessage(userMessage); |
| 419 | + |
413 | 420 | if (wrongType is not null) |
414 | 421 | { |
415 | | - finalMessage = string.Format( |
416 | | - CultureInfo.CurrentCulture, |
417 | | - FrameworkMessages.IsNotInstanceOfFailMsg, |
418 | | - userMessage, |
419 | | - wrongType.ToString(), |
420 | | - value!.GetType().ToString()); |
| 422 | + string actualTypeText = value?.GetType().ToString() ?? "null"; |
| 423 | + EvidenceBlock evidence = EvidenceBlock.Create() |
| 424 | + .AddLine("not expected type:", $"{wrongType} (or derived)") |
| 425 | + .AddLine("actual type:", actualTypeText); |
| 426 | + msg.WithEvidence(evidence) |
| 427 | + .WithExpectedAndActual($"{wrongType} (or derived)", actualTypeText); |
421 | 428 | } |
422 | 429 |
|
423 | | - ReportAssertFailed("Assert.IsNotInstanceOfType", finalMessage); |
| 430 | + msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsNotInstanceOfType", valueExpression, "<value>")); |
| 431 | + ReportAssertFailed(msg); |
424 | 432 | } |
425 | 433 | } |
0 commit comments