Skip to content

Commit

Permalink
Improve display of null and empty string values
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelZ committed Jun 10, 2024
1 parent dbec6ce commit 689fa0b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class DataRowAttribute : Attribute, ITestDataSource
{
/// <summary>
/// String inlining for empty string with quotation marks.
/// </summary>
private static readonly string EmptyStringWithQuotationMarks = "\"\"";

/// <summary>
/// Initializes a new instance of the <see cref="DataRowAttribute"/> class.
/// </summary>
Expand Down Expand Up @@ -95,15 +100,26 @@ public DataRowAttribute(params object?[]? data)
/// </summary>
private static string? GetObjectString(object? obj)
{
if (TestIdGenerationStrategy != TestIdGenerationStrategy.FullyQualified)
{
return obj?.ToString();
}

if (obj == null)
{
return null;
return "null";
}

if (TestIdGenerationStrategy != TestIdGenerationStrategy.FullyQualified
|| !obj.GetType().IsArray)
if (!obj.GetType().IsArray)
{
return obj.ToString();
string? str = obj.ToString();

if (obj is not string)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)

Check failure on line 117 in src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs#L117

src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs(117,13): error IDE0046: (NETCORE_ENGINEERING_TELEMETRY=Build) 'if' statement can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0046)
{
return str;
}

return string.IsNullOrEmpty(str) ? EmptyStringWithQuotationMarks : str;
}

// We need to box the object here so that we can support value types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// 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 Microsoft.MSTestV2.CLIAutomation;
Expand Down Expand Up @@ -139,7 +140,7 @@ public void DataRowsShouldSerializeEnumsProperly()
// Assert
VerifyE2E.TestsPassed(
testResults,
"DataRowEnums ()",
"DataRowEnums (null)",
"DataRowEnums (Alfa)",
"DataRowEnums (Beta)",
"DataRowEnums (Gamma)");
Expand Down Expand Up @@ -202,35 +203,35 @@ public void ExecuteDataRowTests_Enums()
"DataRowEnum_ULong (Alfa)",
"DataRowEnum_ULong (Beta)",
"DataRowEnum_ULong (Gamma)",
"DataRowEnums_Nullable_SByte ()",
"DataRowEnums_Nullable_SByte (null)",
"DataRowEnums_Nullable_SByte (Alfa)",
"DataRowEnums_Nullable_SByte (Beta)",
"DataRowEnums_Nullable_SByte (Gamma)",
"DataRowEnums_Nullable_Byte ()",
"DataRowEnums_Nullable_Byte (null)",
"DataRowEnums_Nullable_Byte (Alfa)",
"DataRowEnums_Nullable_Byte (Beta)",
"DataRowEnums_Nullable_Byte (Gamma)",
"DataRowEnums_Nullable_Short ()",
"DataRowEnums_Nullable_Short (null)",
"DataRowEnums_Nullable_Short (Alfa)",
"DataRowEnums_Nullable_Short (Beta)",
"DataRowEnums_Nullable_Short (Gamma)",
"DataRowEnums_Nullable_UShort ()",
"DataRowEnums_Nullable_UShort (null)",
"DataRowEnums_Nullable_UShort (Alfa)",
"DataRowEnums_Nullable_UShort (Beta)",
"DataRowEnums_Nullable_UShort (Gamma)",
"DataRowEnums_Nullable_Int ()",
"DataRowEnums_Nullable_Int (null)",
"DataRowEnums_Nullable_Int (Alfa)",
"DataRowEnums_Nullable_Int (Beta)",
"DataRowEnums_Nullable_Int (Gamma)",
"DataRowEnums_Nullable_UInt ()",
"DataRowEnums_Nullable_UInt (null)",
"DataRowEnums_Nullable_UInt (Alfa)",
"DataRowEnums_Nullable_UInt (Beta)",
"DataRowEnums_Nullable_UInt (Gamma)",
"DataRowEnums_Nullable_Long ()",
"DataRowEnums_Nullable_Long (null)",
"DataRowEnums_Nullable_Long (Alfa)",
"DataRowEnums_Nullable_Long (Beta)",
"DataRowEnums_Nullable_Long (Gamma)",
"DataRowEnums_Nullable_ULong ()",
"DataRowEnums_Nullable_ULong (null)",
"DataRowEnums_Nullable_ULong (Alfa)",
"DataRowEnums_Nullable_ULong (Beta)",
"DataRowEnums_Nullable_ULong (Gamma)",
Expand Down Expand Up @@ -285,13 +286,13 @@ public void ExecuteDataRowTests_Regular()
"DataRowTestMixed (2,10,10,10,10,10,10,10,10)",
"DataRowTestMixed (3,10,10,10,10,10,10,10,10)",
"DataRowTestMixed (4,10,10,10,10,10,10,10,10)",
"NullValueInData (john.doe@example.com,abc123,)",
"NullValueInData (john.doe@example.com,abc123,null)",
"NullValueInData (john.doe@example.com,abc123,/unit/test)",
"NullValue ()",
"OneStringArray ([])",
"TwoStringArrays ([],[1.4,message])",
"OneObjectArray ([,1])",
"TwoObjectArrays ([,1],[3])",
"NullValue (null)",
"OneStringArray ([\"\"])",
"TwoStringArrays ([\"\"],[1.4,message])",
"OneObjectArray ([\"\",1])",
"TwoObjectArrays ([\"\",1],[3])",
"ThreeObjectArrays ([1],[2],[3])",
"FourObjectArrays ([1],[2],[3],[4])",
"FiveObjectArrays ([1],[2],[3],[4],[5])",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void TestIdUniqueness_DataRowString_DefaultStrategy()
VerifyE2E.FailedTestCount(testResults, 0);
VerifyE2E.TestsPassed(
testResults,
"DataRowStringTests ()",
"DataRowStringTests ()",
"DataRowStringTests (null)",
"DataRowStringTests (\"\")",
"DataRowStringTests ( )",
"DataRowStringTests ( )");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void TestIdUniqueness_DataRowString_FullyQualifiedStrategy()
VerifyE2E.FailedTestCount(testResults, 0);
VerifyE2E.TestsPassed(
testResults,
"DataRowStringTests ()",
"DataRowStringTests ()",
"DataRowStringTests (null)",
"DataRowStringTests (\"\")",
"DataRowStringTests ( )",
"DataRowStringTests ( )");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,35 +136,35 @@ public void ExecuteDataRowTests_Enums()
"DataRowEnum_ULong (Alfa)",
"DataRowEnum_ULong (Beta)",
"DataRowEnum_ULong (Gamma)",
"DataRowEnums_Nullable_SByte ()",
"DataRowEnums_Nullable_SByte (null)",
"DataRowEnums_Nullable_SByte (Alfa)",
"DataRowEnums_Nullable_SByte (Beta)",
"DataRowEnums_Nullable_SByte (Gamma)",
"DataRowEnums_Nullable_Byte ()",
"DataRowEnums_Nullable_Byte (null)",
"DataRowEnums_Nullable_Byte (Alfa)",
"DataRowEnums_Nullable_Byte (Beta)",
"DataRowEnums_Nullable_Byte (Gamma)",
"DataRowEnums_Nullable_Short ()",
"DataRowEnums_Nullable_Short (null)",
"DataRowEnums_Nullable_Short (Alfa)",
"DataRowEnums_Nullable_Short (Beta)",
"DataRowEnums_Nullable_Short (Gamma)",
"DataRowEnums_Nullable_UShort ()",
"DataRowEnums_Nullable_UShort (null)",
"DataRowEnums_Nullable_UShort (Alfa)",
"DataRowEnums_Nullable_UShort (Beta)",
"DataRowEnums_Nullable_UShort (Gamma)",
"DataRowEnums_Nullable_Int ()",
"DataRowEnums_Nullable_Int (null)",
"DataRowEnums_Nullable_Int (Alfa)",
"DataRowEnums_Nullable_Int (Beta)",
"DataRowEnums_Nullable_Int (Gamma)",
"DataRowEnums_Nullable_UInt ()",
"DataRowEnums_Nullable_UInt (null)",
"DataRowEnums_Nullable_UInt (Alfa)",
"DataRowEnums_Nullable_UInt (Beta)",
"DataRowEnums_Nullable_UInt (Gamma)",
"DataRowEnums_Nullable_Long ()",
"DataRowEnums_Nullable_Long (null)",
"DataRowEnums_Nullable_Long (Alfa)",
"DataRowEnums_Nullable_Long (Beta)",
"DataRowEnums_Nullable_Long (Gamma)",
"DataRowEnums_Nullable_ULong ()",
"DataRowEnums_Nullable_ULong (null)",
"DataRowEnums_Nullable_ULong (Alfa)",
"DataRowEnums_Nullable_ULong (Beta)",
"DataRowEnums_Nullable_ULong (Gamma)",
Expand Down Expand Up @@ -214,13 +214,13 @@ public void ExecuteRegular_DataRowTests()
"DataRowTestMixed (2,10,10,10,10,10,10,10,10)",
"DataRowTestMixed (3,10,10,10,10,10,10,10,10)",
"DataRowTestMixed (4,10,10,10,10,10,10,10,10)",
"NullValueInData (john.doe@example.com,abc123,)",
"NullValueInData (john.doe@example.com,abc123,null)",
"NullValueInData (john.doe@example.com,abc123,/unit/test)",
"NullValue ()",
"OneStringArray ([])",
"TwoStringArrays ([],[1.4,message])",
"OneObjectArray ([,1])",
"TwoObjectArrays ([,1],[3])",
"NullValue (null)",
"OneStringArray ([\"\"])",
"TwoStringArrays ([\"\"],[1.4,message])",
"OneObjectArray ([\"\",1])",
"TwoObjectArrays ([\"\",1],[3])",
"ThreeObjectArrays ([1],[2],[3])",
"FourObjectArrays ([1],[2],[3],[4])",
"FiveObjectArrays ([1],[2],[3],[4],[5])",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public void GetDisplayNameShouldReturnAppropriateName()
string[] data2 = ["First", null, "Second"];

string displayName = dataRowAttribute.GetDisplayName(testMethodInfo, data);
Verify(displayName == "DataRowTestMethod (First,Second,)");
Verify(displayName == "DataRowTestMethod (First,Second,null)");

displayName = dataRowAttribute.GetDisplayName(testMethodInfo, data1);
Verify(displayName == "DataRowTestMethod (,First,Second)");
Verify(displayName == "DataRowTestMethod (null,First,Second)");

displayName = dataRowAttribute.GetDisplayName(testMethodInfo, data2);
Verify(displayName == "DataRowTestMethod (First,,Second)");
Verify(displayName == "DataRowTestMethod (First,null,Second)");
}

public void GetDisplayNameShouldReturnSpecifiedDisplayName()
Expand Down

0 comments on commit 689fa0b

Please sign in to comment.