Skip to content

Commit

Permalink
[GH-11] - adding tests for code action titles
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Jun 24, 2018
1 parent 3757f09 commit 88458a6
Show file tree
Hide file tree
Showing 26 changed files with 366 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace NSubstitute.Analyzers.CSharp.CodeFixProviders
{
[ExportCodeFixProvider(LanguageNames.CSharp)]
internal class DefaultSuppressDiagnosticsCodeFixProvider : AbstractSuppressDiagnosticsCodeFixProvider
internal class NonVirtualSetupSuppressDiagnosticsCodeFixProvider : AbstractNonVirtualSetupSuppressDiagnosticsCodeFixProvider
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Immutable;

namespace NSubstitute.Analyzers.Shared.CodeFixProviders
{
internal class AbstractNonVirtualSetupSuppressDiagnosticsCodeFixProvider : AbstractSuppressDiagnosticsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticIdentifiers.NonVirtualSetupSpecification);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,8 +14,6 @@ namespace NSubstitute.Analyzers.Shared.CodeFixProviders
{
internal abstract class AbstractSuppressDiagnosticsCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticIdentifiers.NonVirtualSetupSpecification);

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var project = context.Document.Project;
Expand All @@ -39,7 +36,6 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

var root = await context.Document.GetSyntaxRootAsync();
var model = await context.Document.GetSemanticModelAsync();
var i = 1;
foreach (var diagnostic in context.Diagnostics.Where(diagnostic => FixableDiagnosticIds.Contains(diagnostic.Id)))
{
var syntaxNode = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
Expand All @@ -50,13 +46,33 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
CreateCodeFixTitle(diagnostic, innerSymbol),
cancellationToken => GetTransformedSolutionAsync(context, diagnostic, settingsFile, innerSymbol),
(i++).ToString()),
cancellationToken => GetTransformedSolutionAsync(context, diagnostic, settingsFile, innerSymbol)),
diagnostic);
}
}
}

protected virtual IEnumerable<ISymbol> GetSuppressibleSymbol(ISymbol symbol)
{
if (symbol == null)
{
yield break;
}

yield return symbol;

if (!(symbol is ITypeSymbol))
{
yield return symbol.ContainingType;
yield return symbol.ContainingType.ContainingNamespace;
}

if (symbol is ITypeSymbol)
{
yield return symbol.ContainingNamespace;
}
}

private static string CreateCodeFixTitle(Diagnostic diagnostic, ISymbol innerSymbol)
{
var prefix = GetSymbolTitlePrefix(innerSymbol);
Expand All @@ -67,15 +83,15 @@ private static string GetSymbolTitlePrefix(ISymbol innerSymbol)
{
switch (innerSymbol)
{
case IMethodSymbol methodSymbol:
case IMethodSymbol _:
return "method";
case IPropertySymbol propertySymbol when propertySymbol.IsIndexer:
return "indexer";
case IPropertySymbol propertySymbol:
case IPropertySymbol _:
return "property";
case ITypeSymbol typeSymbol:
case ITypeSymbol _:
return "class";
case INamespaceSymbol namespaceSymbol:
case INamespaceSymbol _:
return "namespace";
default:
return string.Empty;
Expand All @@ -85,12 +101,11 @@ private static string GetSymbolTitlePrefix(ISymbol innerSymbol)
private Task<Solution> GetTransformedSolutionAsync(CodeFixContext context, Diagnostic diagnostic, TextDocument settingsFile, ISymbol symbol)
{
var project = context.Document.Project;
var solution = project.Solution;

var options = GetUpdatedAnalyzersOptions(context, diagnostic, symbol);

project = project.RemoveAdditionalDocument(settingsFile.Id);
solution = project.Solution;
var solution = project.Solution;

var newDocumentId = settingsFile.Id ?? DocumentId.CreateNewId(project.Id);

Expand Down Expand Up @@ -135,26 +150,5 @@ private static TextDocument GetSettingsFile(Project project)
return project.AdditionalDocuments.SingleOrDefault(document =>
document.Name.Equals(AnalyzersSettings.AnalyzerFileName, StringComparison.CurrentCultureIgnoreCase));
}

private IEnumerable<ISymbol> GetSuppressibleSymbol(ISymbol symbol)
{
if (symbol == null)
{
yield break;
}

yield return symbol;

if (!(symbol is ITypeSymbol))
{
yield return symbol.ContainingType;
yield return symbol.ContainingType.ContainingNamespace;
}

if (symbol is ITypeSymbol)
{
yield return symbol.ContainingNamespace;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace NSubstitute.Analyzers.VisualBasic.CodeFixProviders
{
[ExportCodeFixProvider(LanguageNames.VisualBasic)]
internal class DefaultSuppressDiagnosticsCodeFixProvider : AbstractSuppressDiagnosticsCodeFixProvider
internal class NonVirtualSetupSuppressDiagnosticsCodeFixProvider : AbstractNonVirtualSetupSuppressDiagnosticsCodeFixProvider
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders;

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests
{
public abstract class CSharpCodeFixActionsVerifier : CodeFixCodeActionsVerifier
{
protected override string Language { get; } = LanguageNames.CSharp;

protected override CompilationOptions GetCompilationOptions()
{
return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using NSubstitute.Analyzers.CSharp.CodeFixProviders;
using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers;
using Xunit;

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class NonVirtualSetupSuppressDiagnosticsCodeFixActionsTests : CSharpCodeFixActionsVerifier
{
[Fact]
public async Task CreatesCorrectCodeFixActions_ForIndexer()
{
var source = @"using NSubstitute;
namespace MyNamespace
{
public class Foo
{
public int this[int x] => 0;
}
public class FooTests
{
public void Test()
{
var substitute = NSubstitute.Substitute.For<Foo>();
substitute[1].Returns(1);
}
}
}";
await VerifyCodeActions(source, new[]
{
"Suppress NS001 for indexer this[] in nsubstitute.json",
"Suppress NS001 for class Foo in nsubstitute.json",
"Suppress NS001 for namespace MyNamespace in nsubstitute.json"
});
}

[Fact]
public async Task CreatesCorrectCodeFixActions_ForProperty()
{
var source = @"using NSubstitute;
namespace MyNamespace
{
public class Foo
{
public int Bar { get; }
}
public class FooTests
{
public void Test()
{
var substitute = NSubstitute.Substitute.For<Foo>();
substitute.Bar.Returns(1);
}
}
}";
await VerifyCodeActions(source, new[]
{
"Suppress NS001 for property Bar in nsubstitute.json",
"Suppress NS001 for class Foo in nsubstitute.json",
"Suppress NS001 for namespace MyNamespace in nsubstitute.json"
});
}

[Fact]
public async Task CreatesCorrectCodeFixActions_ForMethod()
{
var source = @"using NSubstitute;
namespace MyNamespace
{
public class Foo
{
public int Bar()
{
return 2;
}
}
public class FooTests
{
public void Test()
{
var substitute = NSubstitute.Substitute.For<Foo>();
substitute.Bar().Returns(1);
}
}
}";
await VerifyCodeActions(source, new[]
{
"Suppress NS001 for method Bar in nsubstitute.json",
"Suppress NS001 for class Foo in nsubstitute.json",
"Suppress NS001 for namespace MyNamespace in nsubstitute.json"
});
}

protected override DiagnosticAnalyzer GetDiagnosticAnalyzer()
{
return new NonVirtualSetupAnalyzer();
}

protected override CodeFixProvider GetCodeFixProvider()
{
return new NonVirtualSetupSuppressDiagnosticsCodeFixProvider();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public abstract class DefaultSuppressDiagnosticsCodeFixProviderVerifier : CSharpSuppressDiagnosticSettingsVerifier, INonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixVerifier
public abstract class NonVirtualSetupSuppressDiagnosticsCodeFixVerifier : CSharpSuppressDiagnosticSettingsVerifier, INonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
[Fact]
public abstract Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod();
Expand Down Expand Up @@ -38,7 +38,7 @@ protected override DiagnosticAnalyzer GetDiagnosticAnalyzer()

protected override CodeFixProvider GetCodeFixProvider()
{
return new DefaultSuppressDiagnosticsCodeFixProvider();
return new NonVirtualSetupSuppressDiagnosticsCodeFixProvider();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.
NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsAsExtensionMethodTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsAsExtensionMethodTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsAsExtensionMethodWithGenericTypeSpecifiedTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsAsExtensionMethodWithGenericTypeSpecifiedTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsAsOrdinaryMethodTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsAsOrdinaryMethodTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsAsOrdinaryMethodWithGenericTypeSpecifiedTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsAsOrdinaryMethodWithGenericTypeSpecifiedTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsForAnyArgsAsExtensionMethodTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsForAnyArgsAsExtensionMethodTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsForAnyArgsAsExtensionMethodWithGenericTypeSpecifiedTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsForAnyArgsAsExtensionMethodWithGenericTypeSpecifiedTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsForAnyArgsAsOrdinaryMethodTests : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsForAnyArgsAsOrdinaryMethodTests : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonVirtualSetupAnalyzerSuppressDiagnosticsCodeFixProviderTests
{
public class ReturnsForAnyArgsAsOrdinaryMethodWithGenericTypeSpecified : DefaultSuppressDiagnosticsCodeFixProviderVerifier
public class ReturnsForAnyArgsAsOrdinaryMethodWithGenericTypeSpecified : NonVirtualSetupSuppressDiagnosticsCodeFixVerifier
{
public override async Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod()
{
Expand Down
Loading

0 comments on commit 88458a6

Please sign in to comment.