Skip to content

Commit 78fa087

Browse files
authored
Improve Assert.AreSame message for nulls (#7426)
2 parents ff258ec + 0a796d6 commit 78fa087

16 files changed

Lines changed: 303 additions & 12 deletions

src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ internal void ComputeAssertion(string expectedExpression, string actualExpressio
8080
public readonly struct AssertAreNotSameInterpolatedStringHandler<TArgument>
8181
{
8282
private readonly StringBuilder? _builder;
83+
private readonly TArgument? _notExpected;
84+
private readonly TArgument? _actual;
8385

8486
public AssertAreNotSameInterpolatedStringHandler(int literalLength, int formattedCount, TArgument? notExpected, TArgument? actual, out bool shouldAppend)
8587
{
88+
_notExpected = notExpected;
89+
_actual = actual;
8690
shouldAppend = IsAreNotSameFailing(notExpected, actual);
8791
if (shouldAppend)
8892
{
@@ -95,7 +99,7 @@ internal void ComputeAssertion(string notExpectedExpression, string actualExpres
9599
if (_builder is not null)
96100
{
97101
_builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionTwoParametersMessage, "notExpected", notExpectedExpression, "actual", actualExpression) + " ");
98-
ThrowAssertAreNotSameFailed(_builder.ToString());
102+
ThrowAssertAreNotSameFailed(_notExpected, _actual, _builder.ToString());
99103
}
100104
}
101105

@@ -187,14 +191,22 @@ private static bool IsAreSameFailing<T>(T? expected, T? actual)
187191
[DoesNotReturn]
188192
private static void ThrowAssertAreSameFailed<T>(T? expected, T? actual, string userMessage)
189193
{
190-
string finalMessage = userMessage;
191-
if (expected is ValueType && actual is ValueType)
192-
{
193-
finalMessage = string.Format(
194+
string finalMessage = expected is null
195+
? string.Format(
194196
CultureInfo.CurrentCulture,
195-
FrameworkMessages.AreSameGivenValues,
196-
userMessage);
197-
}
197+
FrameworkMessages.AreSameExpectedIsNull,
198+
userMessage)
199+
: actual is null
200+
? string.Format(
201+
CultureInfo.CurrentCulture,
202+
FrameworkMessages.AreSameActualIsNull,
203+
userMessage)
204+
: expected is ValueType && actual is ValueType
205+
? string.Format(
206+
CultureInfo.CurrentCulture,
207+
FrameworkMessages.AreSameGivenValues,
208+
userMessage)
209+
: userMessage;
198210

199211
ThrowAssertFailed("Assert.AreSame", finalMessage);
200212
}
@@ -240,14 +252,23 @@ public static void AreNotSame<T>(T? notExpected, T? actual, string? message = ""
240252
{
241253
if (IsAreNotSameFailing(notExpected, actual))
242254
{
243-
ThrowAssertAreNotSameFailed(BuildUserMessageForNotExpectedExpressionAndActualExpression(message, notExpectedExpression, actualExpression));
255+
ThrowAssertAreNotSameFailed(notExpected, actual, BuildUserMessageForNotExpectedExpressionAndActualExpression(message, notExpectedExpression, actualExpression));
244256
}
245257
}
246258

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

250262
[DoesNotReturn]
251-
private static void ThrowAssertAreNotSameFailed(string userMessage)
252-
=> ThrowAssertFailed("Assert.AreNotSame", userMessage);
263+
private static void ThrowAssertAreNotSameFailed<T>(T? notExpected, T? actual, string userMessage)
264+
{
265+
string finalMessage = notExpected is null && actual is null
266+
? string.Format(
267+
CultureInfo.CurrentCulture,
268+
FrameworkMessages.AreNotSameBothNull,
269+
userMessage)
270+
: userMessage;
271+
272+
ThrowAssertFailed("Assert.AreNotSame", finalMessage);
273+
}
253274
}

src/TestFramework/TestFramework/Resources/FrameworkMessages.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,18 @@
162162
<data name="AreNotEqualDeltaFailMsg" xml:space="preserve">
163163
<value>Expected a difference greater than &lt;{3}&gt; between expected value &lt;{1}&gt; and actual value &lt;{2}&gt;. {0}</value>
164164
</data>
165+
<data name="AreSameExpectedIsNull" xml:space="preserve">
166+
<value>Expected is &lt;null&gt;. {0}</value>
167+
</data>
168+
<data name="AreSameActualIsNull" xml:space="preserve">
169+
<value>Actual is &lt;null&gt;. {0}</value>
170+
</data>
165171
<data name="AreSameGivenValues" xml:space="preserve">
166172
<value>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</value>
167173
</data>
174+
<data name="AreNotSameBothNull" xml:space="preserve">
175+
<value>Both values are &lt;null&gt;. {0}</value>
176+
</data>
168177
<data name="BothCollectionsEmpty" xml:space="preserve">
169178
<value>Both collections are empty. {0}</value>
170179
</data>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Očekáván rozdíl, který je větší jak &lt;{3}&gt; mezi očekávanou hodnotou &lt;{1}&gt; a aktuální hodnotou &lt;{2}&gt;. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">Nevkládejte hodnotu typů do AreSame(). Hodnoty převedené do typu Object už nebudou nikdy stejné. Zvažte použití AreEqual(). {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Es wurde eine Differenz größer als &lt;{3}&gt; zwischen dem erwarteten Wert &lt;{1}&gt; und dem tatsächlichen Wert &lt;{2}&gt; erwartet. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">Übergeben Sie keine Werttypen an AreSame(). In ein Objekt konvertierte Werte sind niemals identisch. Verwenden Sie stattdessen AreEqual(). {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Se esperaba una diferencia mayor que &lt;{3}&gt; entre el valor esperado &lt;{1}&gt; y el valor actual &lt;{2}&gt;. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">No pase tipos de valor a AreSame(). Los valores convertidos a Object no serán nunca iguales. Considere el uso de AreEqual(). {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Différence attendue supérieure à &lt;{3}&gt; comprise entre la valeur attendue &lt;{1}&gt; et la valeur réelle &lt;{2}&gt;. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">Ne passez pas de types valeur à AreSame(). Les valeurs converties en Object ne seront plus jamais les mêmes. Si possible, utilisez AreEqual(). {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Prevista una differenza maggiore di &lt;{3}&gt; tra il valore previsto &lt;{1}&gt; e il valore effettivo &lt;{2}&gt;. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">Non passare tipi valore a AreSame(). I valori convertiti in Object non saranno mai uguali. Usare AreEqual(). {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">指定する値 &lt;{1}&gt; と実際の値 &lt;{2}&gt; との間には、&lt;{3}&gt; を超える差が必要です。{0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">AreSame() には値型を渡すことはできません。オブジェクトに変換された値が同じにはなりません。AreEqual() を使用することを検討してください。{0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">예상 값 &lt;{1}&gt;과(와) 실제 값 &lt;{2}&gt;의 차이가 &lt;{3}&gt;보다 커야 합니다. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">AreSame()에 값 형식을 전달하면 안 됩니다. Object로 변환된 값은 동일한 값으로 간주되지 않습니다. AreEqual()을 사용해 보세요. {0}</target>

src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
<target state="translated">Oczekiwano różnicy większej niż &lt;{3}&gt; pomiędzy oczekiwaną wartością &lt;{1}&gt; a rzeczywistą wartością &lt;{2}&gt;. {0}</target>
7979
<note></note>
8080
</trans-unit>
81+
<trans-unit id="AreNotSameBothNull">
82+
<source>Both values are &lt;null&gt;. {0}</source>
83+
<target state="new">Both values are &lt;null&gt;. {0}</target>
84+
<note></note>
85+
</trans-unit>
86+
<trans-unit id="AreSameActualIsNull">
87+
<source>Actual is &lt;null&gt;. {0}</source>
88+
<target state="new">Actual is &lt;null&gt;. {0}</target>
89+
<note></note>
90+
</trans-unit>
91+
<trans-unit id="AreSameExpectedIsNull">
92+
<source>Expected is &lt;null&gt;. {0}</source>
93+
<target state="new">Expected is &lt;null&gt;. {0}</target>
94+
<note></note>
95+
</trans-unit>
8196
<trans-unit id="AreSameGivenValues">
8297
<source>Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}</source>
8398
<target state="translated">Nie przekazuj typów wartości do metody AreSame(). Wartości przekonwertowane na typ Object nigdy nie będą takie same. Rozważ użycie metody AreEqual(). {0}</target>

0 commit comments

Comments
 (0)