From 689fa0b79d89b32086c1943e9ce61f779bc2f820 Mon Sep 17 00:00:00 2001 From: Michel Zehnder Date: Mon, 10 Jun 2024 13:47:17 +0200 Subject: [PATCH] Improve display of null and empty string values --- .../Attributes/DataSource/DataRowAttribute.cs | 24 +++++++++++--- .../Parameterized tests/DataRowTests.cs | 31 ++++++++++--------- .../TestId.DefaultStrategy.cs | 4 +-- .../TestId.FullyQualifiedStrategy.cs | 4 +-- .../Parameterized tests/DataRowTests.cs | 28 ++++++++--------- .../Attributes/DataRowAttributeTests.cs | 6 ++-- 6 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs b/src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs index 737d2e7f23..701fc453e2 100644 --- a/src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs +++ b/src/TestFramework/TestFramework/Attributes/DataSource/DataRowAttribute.cs @@ -13,6 +13,11 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class DataRowAttribute : Attribute, ITestDataSource { + /// + /// String inlining for empty string with quotation marks. + /// + private static readonly string EmptyStringWithQuotationMarks = "\"\""; + /// /// Initializes a new instance of the class. /// @@ -95,15 +100,26 @@ public DataRowAttribute(params object?[]? data) /// 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) + { + return str; + } + + return string.IsNullOrEmpty(str) ? EmptyStringWithQuotationMarks : str; } // We need to box the object here so that we can support value types diff --git a/test/IntegrationTests/MSTest.IntegrationTests/Parameterized tests/DataRowTests.cs b/test/IntegrationTests/MSTest.IntegrationTests/Parameterized tests/DataRowTests.cs index 2073218ab7..06f90dae8c 100644 --- a/test/IntegrationTests/MSTest.IntegrationTests/Parameterized tests/DataRowTests.cs +++ b/test/IntegrationTests/MSTest.IntegrationTests/Parameterized tests/DataRowTests.cs @@ -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; @@ -139,7 +140,7 @@ public void DataRowsShouldSerializeEnumsProperly() // Assert VerifyE2E.TestsPassed( testResults, - "DataRowEnums ()", + "DataRowEnums (null)", "DataRowEnums (Alfa)", "DataRowEnums (Beta)", "DataRowEnums (Gamma)"); @@ -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)", @@ -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])", diff --git a/test/IntegrationTests/MSTest.IntegrationTests/TestId.DefaultStrategy.cs b/test/IntegrationTests/MSTest.IntegrationTests/TestId.DefaultStrategy.cs index 82483e0bfa..461710dc6d 100644 --- a/test/IntegrationTests/MSTest.IntegrationTests/TestId.DefaultStrategy.cs +++ b/test/IntegrationTests/MSTest.IntegrationTests/TestId.DefaultStrategy.cs @@ -45,8 +45,8 @@ public void TestIdUniqueness_DataRowString_DefaultStrategy() VerifyE2E.FailedTestCount(testResults, 0); VerifyE2E.TestsPassed( testResults, - "DataRowStringTests ()", - "DataRowStringTests ()", + "DataRowStringTests (null)", + "DataRowStringTests (\"\")", "DataRowStringTests ( )", "DataRowStringTests ( )"); diff --git a/test/IntegrationTests/MSTest.IntegrationTests/TestId.FullyQualifiedStrategy.cs b/test/IntegrationTests/MSTest.IntegrationTests/TestId.FullyQualifiedStrategy.cs index 2a273ca46a..819f9e2630 100644 --- a/test/IntegrationTests/MSTest.IntegrationTests/TestId.FullyQualifiedStrategy.cs +++ b/test/IntegrationTests/MSTest.IntegrationTests/TestId.FullyQualifiedStrategy.cs @@ -45,8 +45,8 @@ public void TestIdUniqueness_DataRowString_FullyQualifiedStrategy() VerifyE2E.FailedTestCount(testResults, 0); VerifyE2E.TestsPassed( testResults, - "DataRowStringTests ()", - "DataRowStringTests ()", + "DataRowStringTests (null)", + "DataRowStringTests (\"\")", "DataRowStringTests ( )", "DataRowStringTests ( )"); diff --git a/test/IntegrationTests/MSTest.VstestConsoleWrapper.IntegrationTests/Parameterized tests/DataRowTests.cs b/test/IntegrationTests/MSTest.VstestConsoleWrapper.IntegrationTests/Parameterized tests/DataRowTests.cs index cc96420e16..a23fc1f420 100644 --- a/test/IntegrationTests/MSTest.VstestConsoleWrapper.IntegrationTests/Parameterized tests/DataRowTests.cs +++ b/test/IntegrationTests/MSTest.VstestConsoleWrapper.IntegrationTests/Parameterized tests/DataRowTests.cs @@ -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)", @@ -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])", diff --git a/test/UnitTests/TestFramework.UnitTests/Attributes/DataRowAttributeTests.cs b/test/UnitTests/TestFramework.UnitTests/Attributes/DataRowAttributeTests.cs index 5a13e56c1b..2620505fd5 100644 --- a/test/UnitTests/TestFramework.UnitTests/Attributes/DataRowAttributeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Attributes/DataRowAttributeTests.cs @@ -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()