Skip to content
75 changes: 40 additions & 35 deletions src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ internal void ComputeAssertion(string expectedExpression, string actualExpressio
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionTwoParametersMessage, "expected", expectedExpression, "actual", actualExpression) + " ");
ReportAssertAreSameFailed(_expected, _actual, _builder.ToString());
ReportAssertAreSameFailed(_expected, _actual, _builder.ToString(), expectedExpression, actualExpression);
}
}

Expand Down Expand Up @@ -98,8 +97,7 @@ internal void ComputeAssertion(string notExpectedExpression, string actualExpres
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionTwoParametersMessage, "notExpected", notExpectedExpression, "actual", actualExpression) + " ");
ReportAssertAreNotSameFailed(_notExpected, _actual, _builder.ToString());
ReportAssertAreNotSameFailed(_notExpected, _actual, _builder.ToString(), notExpectedExpression, actualExpression);
}
}

Expand Down Expand Up @@ -181,34 +179,37 @@ public static void AreSame<T>(T? expected, T? actual, string? message = "", [Cal
return;
}

string userMessage = BuildUserMessageForExpectedExpressionAndActualExpression(message, expectedExpression, actualExpression);
ReportAssertAreSameFailed(expected, actual, userMessage);
ReportAssertAreSameFailed(expected, actual, message, expectedExpression, actualExpression);
}

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)
private static void ReportAssertAreSameFailed<T>(T? expected, T? actual, string? userMessage, string expectedExpression, string actualExpression)
{
string finalMessage = expected is null
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreSameExpectedIsNull,
userMessage)
: actual is null
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreSameActualIsNull,
userMessage)
: expected is ValueType && actual is ValueType
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreSameGivenValues,
userMessage)
: userMessage;

ReportAssertFailed("Assert.AreSame", finalMessage);
StructuredAssertionMessage msg = new("Expected both values to refer to the same object.");

if (expected is ValueType && actual is ValueType)
{
msg.WithAdditionalSummaryLine("Do not pass value types to AreSame \u2014 value types are boxed on each call, so references will never be the same.");
Comment thread
Evangelink marked this conversation as resolved.
}

msg.WithUserMessage(userMessage);

if (expected is not ValueType || actual is not ValueType)
Comment thread
Evangelink marked this conversation as resolved.
{
string expectedText = expected is null ? "null" : $"{expected.GetType()} (hash: 0x{RuntimeHelpers.GetHashCode(expected):X})";
string actualText = actual is null ? "null" : $"{actual.GetType()} (hash: 0x{RuntimeHelpers.GetHashCode(actual):X})";
EvidenceBlock evidence = EvidenceBlock.Create()
Comment thread
Evangelink marked this conversation as resolved.
.AddLine("expected:", expectedText)
.AddLine("actual:", actualText);
msg.WithEvidence(evidence).WithExpectedAndActual(expectedText, actualText);
Comment thread
Evangelink marked this conversation as resolved.
}

msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.AreSame", expectedExpression, actualExpression, "<expected>", "<actual>"));

ReportAssertFailed(msg);
}

/// <inheritdoc cref="AreNotSame{T}(T, T, string?, string, string)" />
Expand Down Expand Up @@ -252,23 +253,27 @@ public static void AreNotSame<T>(T? notExpected, T? actual, string? message = ""
{
if (IsAreNotSameFailing(notExpected, actual))
{
ReportAssertAreNotSameFailed(notExpected, actual, BuildUserMessageForNotExpectedExpressionAndActualExpression(message, notExpectedExpression, actualExpression));
ReportAssertAreNotSameFailed(notExpected, actual, message, notExpectedExpression, actualExpression);
}
}

private static bool IsAreNotSameFailing<T>(T? notExpected, T? actual)
=> object.ReferenceEquals(notExpected, actual);

[DoesNotReturn]
private static void ReportAssertAreNotSameFailed<T>(T? notExpected, T? actual, string userMessage)
private static void ReportAssertAreNotSameFailed<T>(T? notExpected, T? actual, string? userMessage, string notExpectedExpression, string actualExpression)
{
string finalMessage = notExpected is null && actual is null
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreNotSameBothNull,
userMessage)
: userMessage;

ReportAssertFailed("Assert.AreNotSame", finalMessage);
StructuredAssertionMessage msg = new("Expected values to refer to different objects.");

msg.WithAdditionalSummaryLine(
notExpected is null && actual is null
? "Both values are null."
: "Both values refer to the same object.");

msg.WithUserMessage(userMessage);

msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.AreNotSame", notExpectedExpression, actualExpression, "<notExpected>", "<actual>"));

ReportAssertFailed(msg);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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;
Expand Down Expand Up @@ -38,8 +38,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsExactInstanceOfTypeFailed(_value, _expectedType, _builder.ToString());
ReportAssertIsExactInstanceOfTypeFailed(_value, _expectedType, _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -98,8 +97,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsExactInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString());
ReportAssertIsExactInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -160,8 +158,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsNotExactInstanceOfTypeFailed(_value, _wrongType, _builder.ToString());
ReportAssertIsNotExactInstanceOfTypeFailed(_value, _wrongType, _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -220,8 +217,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsNotExactInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString());
ReportAssertIsNotExactInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -291,7 +287,7 @@ public static void IsExactInstanceOfType([NotNull] object? value, [NotNull] Type
{
if (IsExactInstanceOfTypeFailing(value, expectedType))
{
ReportAssertIsExactInstanceOfTypeFailed(value, expectedType, BuildUserMessageForValueExpression(message, valueExpression));
ReportAssertIsExactInstanceOfTypeFailed(value, expectedType, message, valueExpression);
}
}

Expand Down Expand Up @@ -329,18 +325,25 @@ private static bool IsExactInstanceOfTypeFailing([NotNullWhen(false)] object? va
=> expectedType is null || value is null || value.GetType() != expectedType;

[DoesNotReturn]
private static void ReportAssertIsExactInstanceOfTypeFailed(object? value, Type? expectedType, string userMessage)
private static void ReportAssertIsExactInstanceOfTypeFailed(object? value, Type? expectedType, string? userMessage, string valueExpression)
{
string finalMessage = expectedType is not null
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.IsExactInstanceOfFailMsg,
userMessage,
expectedType.ToString(),
value?.GetType().ToString() ?? "null")
: userMessage;

ReportAssertFailed("Assert.IsExactInstanceOfType", finalMessage);
StructuredAssertionMessage msg = expectedType is null
? new("Cannot check type because the expected type argument is null.")
: new($"Expected value to be exactly of type {expectedType.Name}.");
msg.WithUserMessage(userMessage);

if (expectedType is not null)
{
string actualTypeText = value?.GetType().ToString() ?? "null";
EvidenceBlock evidence = EvidenceBlock.Create()
.AddLine("expected type:", expectedType.ToString())
.AddLine(value is null ? "actual:" : "actual type:", actualTypeText);
msg.WithEvidence(evidence)
.WithExpectedAndActual(expectedType.ToString(), actualTypeText);
}

msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsExactInstanceOfType", valueExpression, "<value>"));
ReportAssertFailed(msg);
}

/// <summary>
Expand Down Expand Up @@ -371,7 +374,7 @@ public static void IsNotExactInstanceOfType(object? value, [NotNull] Type? wrong
{
if (IsNotExactInstanceOfTypeFailing(value, wrongType))
{
ReportAssertIsNotExactInstanceOfTypeFailed(value, wrongType, BuildUserMessageForValueExpression(message, valueExpression));
ReportAssertIsNotExactInstanceOfTypeFailed(value, wrongType, message, valueExpression);
}
}

Expand Down Expand Up @@ -403,19 +406,24 @@ private static bool IsNotExactInstanceOfTypeFailing(object? value, [NotNullWhen(
(value is not null && value.GetType() == wrongType);

[DoesNotReturn]
private static void ReportAssertIsNotExactInstanceOfTypeFailed(object? value, Type? wrongType, string userMessage)
private static void ReportAssertIsNotExactInstanceOfTypeFailed(object? value, Type? wrongType, string? userMessage, string valueExpression)
{
string finalMessage = userMessage;
StructuredAssertionMessage msg = wrongType is null
? new("Cannot check type because the not-expected type argument is null.")
: new($"Expected value to not be exactly of type {wrongType.Name}.");
msg.WithUserMessage(userMessage);

if (wrongType is not null)
{
finalMessage = string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.IsNotExactInstanceOfFailMsg,
userMessage,
wrongType.ToString(),
value!.GetType().ToString());
string actualTypeText = value?.GetType().ToString() ?? "null";
EvidenceBlock evidence = EvidenceBlock.Create()
.AddLine("not expected type:", wrongType.ToString())
Comment thread
Evangelink marked this conversation as resolved.
.AddLine("actual type:", actualTypeText);
msg.WithEvidence(evidence)
.WithExpectedAndActual(wrongType.ToString(), actualTypeText);
}

ReportAssertFailed("Assert.IsNotExactInstanceOfType", finalMessage);
msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsNotExactInstanceOfType", valueExpression, "<value>"));
ReportAssertFailed(msg);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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;
Expand Down Expand Up @@ -38,8 +38,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsInstanceOfTypeFailed(_value, _expectedType, _builder.ToString());
ReportAssertIsInstanceOfTypeFailed(_value, _expectedType, _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -98,8 +97,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString());
ReportAssertIsInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -160,8 +158,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsNotInstanceOfTypeFailed(_value, _wrongType, _builder.ToString());
ReportAssertIsNotInstanceOfTypeFailed(_value, _wrongType, _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -220,8 +217,7 @@ internal void ComputeAssertion(string valueExpression)
{
if (_builder is not null)
{
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "value", valueExpression) + " ");
ReportAssertIsNotInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString());
ReportAssertIsNotInstanceOfTypeFailed(_value, typeof(TArg), _builder.ToString(), valueExpression);
}
}

Expand Down Expand Up @@ -292,7 +288,7 @@ public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? exp
{
if (IsInstanceOfTypeFailing(value, expectedType))
{
ReportAssertIsInstanceOfTypeFailed(value, expectedType, BuildUserMessageForValueExpression(message, valueExpression));
ReportAssertIsInstanceOfTypeFailed(value, expectedType, message, valueExpression);
}
}

Expand Down Expand Up @@ -331,18 +327,25 @@ private static bool IsInstanceOfTypeFailing([NotNullWhen(false)] object? value,
=> expectedType == null || value == null || !expectedType.IsInstanceOfType(value);

[DoesNotReturn]
private static void ReportAssertIsInstanceOfTypeFailed(object? value, Type? expectedType, string userMessage)
private static void ReportAssertIsInstanceOfTypeFailed(object? value, Type? expectedType, string? userMessage, string valueExpression)
{
string finalMessage = expectedType is not null
? string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.IsInstanceOfFailMsg,
userMessage,
expectedType.ToString(),
value?.GetType().ToString() ?? "null")
: userMessage;

ReportAssertFailed("Assert.IsInstanceOfType", finalMessage);
StructuredAssertionMessage msg = expectedType is null
? new("Cannot check type because the expected type argument is null.")
: new($"Expected value to be of type {expectedType.Name} (or derived).");
msg.WithUserMessage(userMessage);

if (expectedType is not null)
{
string actualTypeText = value?.GetType().ToString() ?? "null";
EvidenceBlock evidence = EvidenceBlock.Create()
.AddLine("expected type:", $"{expectedType} (or derived)")
.AddLine(value is null ? "actual:" : "actual type:", actualTypeText);
msg.WithEvidence(evidence)
.WithExpectedAndActual($"{expectedType} (or derived)", actualTypeText);
}

msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsInstanceOfType", valueExpression, "<value>"));
ReportAssertFailed(msg);
}

/// <summary>
Expand Down Expand Up @@ -374,7 +377,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType,
{
if (IsNotInstanceOfTypeFailing(value, wrongType))
{
ReportAssertIsNotInstanceOfTypeFailed(value, wrongType, BuildUserMessageForValueExpression(message, valueExpression));
ReportAssertIsNotInstanceOfTypeFailed(value, wrongType, message, valueExpression);
}
}

Expand Down Expand Up @@ -407,19 +410,24 @@ private static bool IsNotInstanceOfTypeFailing(object? value, [NotNullWhen(false
(value is not null && wrongType.IsInstanceOfType(value));

[DoesNotReturn]
private static void ReportAssertIsNotInstanceOfTypeFailed(object? value, Type? wrongType, string userMessage)
private static void ReportAssertIsNotInstanceOfTypeFailed(object? value, Type? wrongType, string? userMessage, string valueExpression)
{
string finalMessage = userMessage;
StructuredAssertionMessage msg = wrongType is null
? new("Cannot check type because the not-expected type argument is null.")
: new($"Expected value to not be of type {wrongType.Name} (or derived).");
msg.WithUserMessage(userMessage);

if (wrongType is not null)
{
finalMessage = string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.IsNotInstanceOfFailMsg,
userMessage,
wrongType.ToString(),
value!.GetType().ToString());
string actualTypeText = value?.GetType().ToString() ?? "null";
EvidenceBlock evidence = EvidenceBlock.Create()
.AddLine("not expected type:", $"{wrongType} (or derived)")
Comment thread
Evangelink marked this conversation as resolved.
.AddLine("actual type:", actualTypeText);
msg.WithEvidence(evidence)
.WithExpectedAndActual($"{wrongType} (or derived)", actualTypeText);
}

ReportAssertFailed("Assert.IsNotInstanceOfType", finalMessage);
msg.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsNotInstanceOfType", valueExpression, "<value>"));
ReportAssertFailed(msg);
}
}
Loading