Skip to content

Commit d18b0b4

Browse files
Evangelinkgithub-actions[bot]Copilot
authored
[code-simplifier] Simplify RedundantTestMethodAttributeAnalyzer named-argument lookups (#11304)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4d3477c commit d18b0b4

2 files changed

Lines changed: 105 additions & 20 deletions

File tree

‎src/Analyzers/MSTest.Analyzers/RedundantTestMethodAttributeAnalyzer.cs‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,8 @@ private static bool IsConditionIgnoreMessageRedundant(
251251
|| !HasNonEmptyConditionIgnoreMessage(methodAttribute));
252252

253253
private static bool HasNonEmptyConditionIgnoreMessage(AttributeData attribute)
254-
{
255-
foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments)
256-
{
257-
if (argument.Key == "IgnoreMessage")
258-
{
259-
return argument.Value.Value is string { Length: > 0 };
260-
}
261-
}
262-
263-
return true;
264-
}
254+
=> !TryGetNamedArgument(attribute, "IgnoreMessage", out TypedConstant constant)
255+
|| constant.Value is string { Length: > 0 };
265256

266257
private static bool TryGetConditionMode(AttributeData? attribute, out bool includeMode)
267258
{
@@ -311,16 +302,40 @@ private static bool TryGetRetrySettings(
311302
}
312303

313304
private static int GetNamedIntArgument(AttributeData attribute, string name)
305+
=> TryGetNamedArgument(
306+
attribute,
307+
name,
308+
out TypedConstant constant,
309+
valuePredicate: static constant => constant.Value is int)
310+
&& constant.Value is int value
311+
? value
312+
: 0;
313+
314+
private static bool TryGetNamedArgument(
315+
AttributeData attribute,
316+
string name,
317+
out TypedConstant constant,
318+
bool returnLastMatch = false,
319+
Func<TypedConstant, bool>? valuePredicate = null)
314320
{
315-
foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments)
321+
constant = default;
322+
bool found = false;
323+
324+
foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments.Where(argument => argument.Key == name))
316325
{
317-
if (argument.Key == name && argument.Value.Value is int value)
326+
if (valuePredicate is null || valuePredicate(argument.Value))
318327
{
319-
return value;
328+
constant = argument.Value;
329+
found = true;
330+
331+
if (!returnLastMatch)
332+
{
333+
return true;
334+
}
320335
}
321336
}
322337

323-
return 0;
338+
return found;
324339
}
325340

326341
private static bool IsIgnoreRedundant(AttributeData methodAttribute, AttributeData? classAttribute)
@@ -345,12 +360,9 @@ private static bool TryGetIgnoreMessage(AttributeData? attribute, out string? me
345360
_ => null,
346361
};
347362

348-
foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments)
363+
if (TryGetNamedArgument(attribute, "IgnoreMessage", out TypedConstant constant, returnLastMatch: true))
349364
{
350-
if (argument.Key == "IgnoreMessage")
351-
{
352-
message = argument.Value.Value as string;
353-
}
365+
message = constant.Value as string;
354366
}
355367

356368
return true;

‎test/UnitTests/MSTest.Analyzers.UnitTests/RedundantTestMethodAttributeAnalyzerTests.cs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using Microsoft.CodeAnalysis.Testing;
5+
46
using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
57
MSTest.Analyzers.RedundantTestMethodAttributeAnalyzer,
68
MSTest.Analyzers.RedundantTestMethodAttributeFixer>;
@@ -550,6 +552,50 @@ await VerifyCS.VerifyCodeFixAsync(
550552
fixedCode);
551553
}
552554

555+
[TestMethod]
556+
public async Task WhenClassRetryHasMalformedDuplicateDelay_UsesFirstIntegerValue()
557+
{
558+
string code = """
559+
using Microsoft.VisualStudio.TestTools.UnitTesting;
560+
561+
[TestClass]
562+
[Retry(3, MillisecondsDelayBetweenRetries = "bad", MillisecondsDelayBetweenRetries = 100)]
563+
public sealed class MyTestClass
564+
{
565+
[TestMethod]
566+
[{|#0:Retry(3, MillisecondsDelayBetweenRetries = 100)|}]
567+
public void TestMethod()
568+
{
569+
}
570+
}
571+
""";
572+
573+
string fixedCode = """
574+
using Microsoft.VisualStudio.TestTools.UnitTesting;
575+
576+
[TestClass]
577+
[Retry(3, MillisecondsDelayBetweenRetries = "bad", MillisecondsDelayBetweenRetries = 100)]
578+
public sealed class MyTestClass
579+
{
580+
[TestMethod]
581+
public void TestMethod()
582+
{
583+
}
584+
}
585+
""";
586+
587+
var test = new VerifyCS.Test
588+
{
589+
TestCode = code,
590+
FixedCode = fixedCode,
591+
CompilerDiagnostics = CompilerDiagnostics.None,
592+
};
593+
test.ExpectedDiagnostics.Add(
594+
VerifyCS.Diagnostic().WithLocation(0).WithArguments("[Retry]", "TestMethod"));
595+
596+
await test.RunAsync();
597+
}
598+
553599
[TestMethod]
554600
public async Task WhenRetrySettingsDifferFromClassSettings_NoDiagnostic()
555601
{
@@ -668,6 +714,33 @@ await VerifyCS.VerifyCodeFixAsync(
668714
fixedCode);
669715
}
670716

717+
[TestMethod]
718+
public async Task WhenClassIgnoreHasDuplicateNamedMessages_UsesLastMessage()
719+
{
720+
string code = """
721+
using Microsoft.VisualStudio.TestTools.UnitTesting;
722+
723+
[TestClass]
724+
[Ignore(IgnoreMessage = "Class reason", IgnoreMessage = "")]
725+
public sealed class MyTestClass
726+
{
727+
[TestMethod]
728+
[Ignore("Method reason")]
729+
public void TestMethod()
730+
{
731+
}
732+
}
733+
""";
734+
735+
var test = new VerifyCS.Test
736+
{
737+
TestCode = code,
738+
CompilerDiagnostics = CompilerDiagnostics.None,
739+
};
740+
741+
await test.RunAsync();
742+
}
743+
671744
[TestMethod]
672745
public async Task WhenMethodIgnoreProvidesOnlyMessage_NoDiagnostic()
673746
{

0 commit comments

Comments
 (0)