From e3613173aa314ee5c31d71801072499e67f86297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 17 Jun 2026 01:42:29 +0200 Subject: [PATCH 1/2] test: add edge case tests for DuplicateDataRowAnalyzer (MSTEST0042) Cover four untested branches in TypedConstantArrayComparer: - TypedConstantKind.Enum (object.Equals fallback): duplicate and different enum args - TypedConstantKind.Type/typeof (object.Equals fallback): duplicate and different Type args - IsNull && IsNull path within nested array element comparison - Asymmetric IsNull || IsNull path in nested array element comparison Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../DuplicateDataRowAnalyzerTests.cs | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs index 6e2d7c31ae..ba760e5d3f 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs @@ -402,4 +402,136 @@ public static void TestMethod(int x, int y) NumberOfFixAllInProjectIterations = 2, }.RunAsync(); } + + [TestMethod] + public async Task WhenEnumArgumentIsDuplicated_Diagnostic() + { + string code = """ + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(ConsoleColor.Red)] + [[|DataRow(ConsoleColor.Red)|]] + public static void TestMethod(object c) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenEnumArgumentsAreDifferent_NoDiagnostic() + { + string code = """ + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(ConsoleColor.Red)] + [DataRow(ConsoleColor.Blue)] + public static void TestMethod(object c) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenTypeArgumentIsDuplicated_Diagnostic() + { + string code = """ + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(typeof(int))] + [[|DataRow(typeof(int))|]] + public static void TestMethod(Type t) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenTypeArgumentsAreDifferent_NoDiagnostic() + { + string code = """ + using System; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(typeof(int))] + [DataRow(typeof(string))] + public static void TestMethod(Type t) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenArrayContainsNullElement_SameContent_Diagnostic() + { + // Tests the IsNull && IsNull path within a nested array element comparison. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(null, 1)] + [[|DataRow(null, 1)|]] + public static void TestMethod(object x, int y) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenArrayFirstElementNullDiffersFromNonNull_NoDiagnostic() + { + // Tests the asymmetric IsNull || IsNull path: one null element vs one non-null element. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [DataRow(null, 1)] + [DataRow(1, 1)] + public static void TestMethod(object x, int y) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } } From 51aff24a6e8273e33a21a514b87f5fd8d9e56c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 17 Jun 2026 09:39:47 +0200 Subject: [PATCH 2/2] test: make null-element DataRow tests actually exercise IsNull branches The asymmetric test used [DataRow(null, 1)] vs [DataRow(1, 1)], whose first ctor args have different TypedConstant.Type (object vs int). AreTypedConstantEquals short-circuits on that type mismatch and never reaches the IsNull || IsNull guard. Switch both null-element tests to typed string[] arrays so the null and non-null elements share the same element type (string), genuinely exercising the nested-array IsNull && IsNull and IsNull || IsNull paths the test names describe. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../DuplicateDataRowAnalyzerTests.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs index ba760e5d3f..1371f88aa7 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/DuplicateDataRowAnalyzerTests.cs @@ -495,6 +495,8 @@ public static void TestMethod(Type t) public async Task WhenArrayContainsNullElement_SameContent_Diagnostic() { // Tests the IsNull && IsNull path within a nested array element comparison. + // Using a typed string[] ensures both null elements share the same element type (string), + // so the comparer reaches the IsNull && IsNull guard instead of short-circuiting on a type mismatch. string code = """ using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -502,9 +504,9 @@ public async Task WhenArrayContainsNullElement_SameContent_Diagnostic() public class MyTestClass { [TestMethod] - [DataRow(null, 1)] - [[|DataRow(null, 1)|]] - public static void TestMethod(object x, int y) + [DataRow(new string[] { null })] + [[|DataRow(new string[] { null })|]] + public static void TestMethod(string[] x) { } } @@ -517,6 +519,8 @@ public static void TestMethod(object x, int y) public async Task WhenArrayFirstElementNullDiffersFromNonNull_NoDiagnostic() { // Tests the asymmetric IsNull || IsNull path: one null element vs one non-null element. + // Using a typed string[] ensures both elements share the same element type (string), + // so the comparer reaches the IsNull || IsNull guard instead of short-circuiting on a type mismatch. string code = """ using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -524,9 +528,9 @@ public async Task WhenArrayFirstElementNullDiffersFromNonNull_NoDiagnostic() public class MyTestClass { [TestMethod] - [DataRow(null, 1)] - [DataRow(1, 1)] - public static void TestMethod(object x, int y) + [DataRow(new string[] { null })] + [DataRow(new string[] { "a" })] + public static void TestMethod(string[] x) { } }