-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGFIX IDISP004 vs ApplicationInsights LoggerFactory. Fix #90.
- Loading branch information
1 parent
035d1ab
commit 0361611
Showing
10 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
IDisposableAnalyzers.NetCoreTests/Helpers/DisposableTests.IsCreation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace IDisposableAnalyzers.NetCoreTests.Helpers | ||
{ | ||
using System.Threading; | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using NUnit.Framework; | ||
|
||
internal partial class DisposableTests | ||
{ | ||
internal class IsCreation | ||
{ | ||
[TestCase("Microsoft.Extensions.Logging.ApplicationInsightsLoggerFactoryExtensions.AddApplicationInsights(((Microsoft.Extensions.Logging.ILoggerFactory)o), null)")] | ||
public void WhiteList(string code) | ||
{ | ||
var testCode = @" | ||
namespace RoslynSandbox | ||
{ | ||
internal class Foo | ||
{ | ||
internal Foo(object o) | ||
{ | ||
var value = PLACEHOLDER; | ||
} | ||
} | ||
}"; | ||
testCode = testCode.AssertReplace("PLACEHOLDER", code); | ||
var syntaxTree = CSharpSyntaxTree.ParseText(testCode); | ||
var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); | ||
var semanticModel = compilation.GetSemanticModel(syntaxTree); | ||
var value = syntaxTree.FindExpression(code); | ||
Assert.AreEqual(Result.No, Disposable.IsCreation(value, semanticModel, CancellationToken.None)); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
IDisposableAnalyzers.NetCoreTests/Helpers/DisposableTests.IsPotentiallyAssignableTo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace IDisposableAnalyzers.NetCoreTests.Helpers | ||
{ | ||
using System.Threading; | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using NUnit.Framework; | ||
|
||
internal partial class DisposableTests | ||
{ | ||
internal class IsPotentiallyAssignableTo | ||
{ | ||
[TestCase("new string(' ', 1)", false)] | ||
[TestCase("new System.Text.StringBuilder()", false)] | ||
[TestCase("new System.IO.MemoryStream()", true)] | ||
[TestCase("(Microsoft.Extensions.Logging.ILoggerFactory)o", true)] | ||
public void Expression(string code, bool expected) | ||
{ | ||
var testCode = @" | ||
namespace RoslynSandbox | ||
{ | ||
internal class Foo | ||
{ | ||
internal Foo(object o) | ||
{ | ||
var value = PLACEHOLDER; | ||
} | ||
} | ||
}"; | ||
testCode = testCode.AssertReplace("PLACEHOLDER", code); | ||
var syntaxTree = CSharpSyntaxTree.ParseText(testCode); | ||
var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); | ||
var semanticModel = compilation.GetSemanticModel(syntaxTree); | ||
var value = syntaxTree.FindEqualsValueClause(code).Value; | ||
Assert.AreEqual(expected, Disposable.IsPotentiallyAssignableFrom(value, semanticModel, CancellationToken.None)); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...leAnalyzers.NetCoreTests/IDISP004DontIgnoreReturnValueOfTypeIDisposableTests/HappyPath.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ReSharper disable InconsistentNaming | ||
#pragma warning disable SA1203 // Constants must appear before fields | ||
namespace IDisposableAnalyzers.NetCoreTests.IDISP004DontIgnoreReturnValueOfTypeIDisposableTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
[TestFixture(typeof(IDISP004DontIgnoreReturnValueOfTypeIDisposable))] | ||
[TestFixture(typeof(ObjectCreationAnalyzer))] | ||
internal partial class HappyPath<T> | ||
where T : DiagnosticAnalyzer, new() | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new T(); | ||
|
||
[Test] | ||
public void ILoggerFactoryAddApplicationInsights() | ||
{ | ||
var testCode = @" | ||
namespace RoslynSandbox | ||
{ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
public class Foo | ||
{ | ||
public void Configure( | ||
IApplicationBuilder app, | ||
IHostingEnvironment env, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning); | ||
} | ||
} | ||
}"; | ||
AnalyzerAssert.Valid(Analyzer, testCode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace IDisposableAnalyzers | ||
{ | ||
using System; | ||
using Gu.Roslyn.AnalyzerExtensions; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal static class MethodSymbolExt | ||
{ | ||
[Obsolete("Move to Gu.Extensions")] | ||
internal static bool TryGetThisParameter(this IMethodSymbol method, out IParameterSymbol parameter) | ||
{ | ||
if (method.IsExtensionMethod) | ||
{ | ||
if (method.ReducedFrom is IMethodSymbol reduced) | ||
{ | ||
return reduced.Parameters.TryFirst(out parameter); | ||
} | ||
|
||
return method.Parameters.TryFirst(out parameter); | ||
} | ||
|
||
parameter = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters