Skip to content

Commit

Permalink
Merge pull request #613 from manfred-brands/Issue607_StaticClasses
Browse files Browse the repository at this point in the history
Also look at static methods for NUnit1032
  • Loading branch information
mikkelbu authored Oct 20, 2023
2 parents 1d21b00 + 570d596 commit 42f733f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -830,5 +830,55 @@ private void B(string one, bool keepGoing)

RoslynAssert.Valid(analyzer, testCode);
}

[TestCase("")]
[TestCase("static")]
public void StaticFieldsNeedDisposingInOneTimeTearDown(string modifier)
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@"
public {modifier} class StaticFields
{{
private static readonly IDisposable? ↓field = new DummyDisposable();
[Test]
public {modifier} void TestMethod()
{{
Assert.That(field, Is.Not.Null);
}}
{DummyDisposable}
}}
");

RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode);
}

[TestCase("")]
[TestCase("static")]
public void StaticFieldsAreDisposed(string modifier)
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@"
public {modifier} class StaticFields
{{
private static readonly IDisposable? field = new DummyDisposable();
[OneTimeTearDown]
public {modifier} void OneTimeTearDown()
{{
field?.Dispose();
}}
[Test]
public {modifier} void TestMethod()
{{
Assert.That(field, Is.Not.Null);
}}
{DummyDisposable}
}}
");

RoslynAssert.Valid(analyzer, testCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static void AnalyzeDisposableFields(SyntaxNodeAnalysisContext context)
ImmutableHashSet<string> disposeMethods = StandardDisposeMethods;

#if NETSTANDARD2_0_OR_GREATER
// Are there any additional methods configured that are considers Dispose Methods
// Are there any additional methods configured that are considered Dispose Methods
// e.g. DisposeIfDisposeable or Release
AnalyzerConfigOptions options = context.Options.AnalyzerConfigOptionsProvider.GetOptions(classDeclaration.SyntaxTree);
if (options.TryGetValue("dotnet_diagnostic.NUnit1032.additional_dispose_methods", out string? value))
Expand All @@ -127,7 +127,7 @@ private static void AnalyzeDisposableFields(SyntaxNodeAnalysisContext context)
Parameters parameters = new(model, typeSymbol, disposeMethods, symbolNames);

ImmutableArray<ISymbol> members = typeSymbol.GetMembers();
var methods = members.OfType<IMethodSymbol>().Where(m => !m.IsStatic).ToArray();
var methods = members.OfType<IMethodSymbol>().ToArray();
var oneTimeTearDownMethods = methods.Where(m => HasAttribute(m, NUnitFrameworkConstants.NameOfOneTimeTearDownAttribute)).ToImmutableHashSet<IMethodSymbol>(SymbolEqualityComparer.Default);
var oneTimeSetUpMethods = methods.Where(m => m.MethodKind == MethodKind.Constructor || HasAttribute(m, NUnitFrameworkConstants.NameOfOneTimeSetUpAttribute)).ToImmutableHashSet<IMethodSymbol>(SymbolEqualityComparer.Default);
var setUpMethods = methods.Where(m => HasAttribute(m, NUnitFrameworkConstants.NameOfSetUpAttribute)).ToImmutableHashSet<IMethodSymbol>(SymbolEqualityComparer.Default);
Expand Down

0 comments on commit 42f733f

Please sign in to comment.