Skip to content

Commit

Permalink
Merge branch 'GH-73-NS1001-higlights-wrong-member' into GH-70-fix-pro…
Browse files Browse the repository at this point in the history
…viders-for-internal-member-substitution
  • Loading branch information
tpodolak committed Feb 23, 2019
2 parents b06e298 + 317a6c8 commit c58db62
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 26 deletions.
8 changes: 8 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 1.0.6 (21 February 2019)

- [#71](https://github.com/nsubstitute/NSubstitute.Analyzers/issues/71) - Incorrect NS4000 when using typeof expression

### 1.0.5 (09 February 2019)

- [#61](https://github.com/nsubstitute/NSubstitute.Analyzers/issues/61) - CallInfo analysis for AndDoes method

### 1.0.4 (03 January 2019)

- [#57](https://github.com/nsubstitute/NSubstitute.Analyzers/issues/57) - Incorrect NS3004 for derived types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
base.VisitInvocationExpression(node);
}

public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
}

public override void VisitStructDeclaration(StructDeclarationSyntax node)
{
}

public override void DefaultVisit(SyntaxNode node)
{
VisitRelatedSymbols(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void AnalyzeInvocation(SyntaxNodeAnalysisContext syntaxNodeContext)
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptorsProvider.NonVirtualReceivedSetupSpecification,
invocationExpression.GetLocation(),
GetSymbolLocation(parentNode, symbolInfo.Symbol),
symbolInfo.Symbol.Name);

syntaxNodeContext.ReportDiagnostic(diagnostic);
Expand All @@ -86,7 +86,7 @@ private void AnalyzeInvocation(SyntaxNodeAnalysisContext syntaxNodeContext)
{
var diagnostic = Diagnostic.Create(
DiagnosticDescriptorsProvider.InternalSetupSpecification,
invocationExpression.GetLocation(),
GetSymbolLocation(parentNode, symbolInfo.Symbol),
symbolInfo.Symbol.Name);

syntaxNodeContext.ReportDiagnostic(diagnostic);
Expand Down Expand Up @@ -117,5 +117,12 @@ private SyntaxNode GetKnownParent(SyntaxNode receivedSyntaxNode)

return null;
}

private Location GetSymbolLocation(SyntaxNode syntaxNode, ISymbol symbol)
{
var actualNode = symbol is IMethodSymbol _ ? syntaxNode.Parent : syntaxNode;

return actualNode.GetLocation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
base.VisitInvocationExpression(node);
}

public override void VisitClassBlock(ClassBlockSyntax node)
{
}

public override void VisitStructureBlock(StructureBlockSyntax node)
{
}

public override void DefaultVisit(SyntaxNode node)
{
VisitRelatedSymbols(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
[|substitute.{method}()|].Bar();
[|substitute.{method}().Bar()|];
}}
}}
}}";
Expand Down Expand Up @@ -138,7 +138,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
[|substitute.{method}()|].Bar();
[|substitute.{method}().Bar()|];
}}
}}
}}";
Expand Down Expand Up @@ -327,7 +327,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|substitute.{method}()|].Bar;
var x = [|substitute.{method}().Bar|];
}}
}}
}}";
Expand Down Expand Up @@ -374,7 +374,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|substitute.{method}()|][1];
var x = [|substitute.{method}()[1]|];
}}
}}
}}";
Expand Down Expand Up @@ -457,7 +457,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|substitute.{method}()|]{call};
var x = [|substitute.{method}(){call}|];
}}
}}
}}";
Expand Down Expand Up @@ -531,7 +531,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|substitute.{method}()|]{call};
var x = [|substitute.{method}(){call}|];
}}
}}
}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
[|{method}(substitute)|].Bar();
[|{method}(substitute).Bar()|];
}}
}}
}}";
Expand Down Expand Up @@ -154,7 +154,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
[|{method}(substitute)|].Bar();
[|{method}(substitute).Bar()|];
}}
}}
}}";
Expand Down Expand Up @@ -351,7 +351,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|{method}(substitute)|].Bar;
var x = [|{method}(substitute).Bar|];
}}
}}
}}";
Expand Down Expand Up @@ -398,7 +398,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|{method}(substitute)|][1];
var x = [|{method}(substitute)[1]|];
}}
}}
}}";
Expand Down Expand Up @@ -481,7 +481,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|{method}(substitute)|]{call};
var x = [|{method}(substitute){call}|];
}}
}}
}}";
Expand Down Expand Up @@ -555,7 +555,7 @@ public class FooTests
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
var x = [|{method}(substitute)|]{call};
var x = [|{method}(substitute){call}|];
}}
}}
}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public abstract class ReEntrantReturnsSetupDiagnosticVerifier : CSharpDiagnostic
[InlineData]
public abstract Task ReportsDiagnostic_WhenUsingReEntrantReturns_AcrossMultipleFiles(string method);

[CombinatoryTheory]
[InlineData("Foo")]
[InlineData("FooBar")]
public abstract Task ReportsNoDiagnostic_WhenUsed_WithTypeofExpression(string method, string type);

protected override DiagnosticAnalyzer GetDiagnosticAnalyzer()
{
return new ReEntrantSetupAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,49 @@ public static int ReturnThis()

await VerifyDiagnostics(new[] { source, secondSource }, Descriptor, $"{plainMethodName}() is set with a method that itself calls {plainMethodName}. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => FooBar.ReturnThis()).");
}

[CombinatoryData("Returns", "Returns<object>", "ReturnsForAnyArgs", "ReturnsForAnyArgs<object>")]
public override async Task ReportsNoDiagnostic_WhenUsed_WithTypeofExpression(string method, string type)
{
var source = $@"using NSubstitute;
namespace MyNamespace
{{
public class Foo
{{
public object FooBar()
{{
return null;
}}
public object Bar()
{{
var sub = Substitute.For<Foo>();
sub.FooBar().Returns(1);
return null;
}}
}}
public struct FooBar
{{
public object Bar()
{{
var sub = Substitute.For<Foo>();
sub.FooBar().Returns(1);
return null;
}}
}}
public class FooTests
{{
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
substitute.FooBar().{method}(typeof({type}));
}}
}}
}}";
await VerifyNoDiagnostic(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,49 @@ public static int ReturnThis()

await VerifyDiagnostics(new[] { source, secondSource }, Descriptor, $"{plainMethodName}() is set with a method that itself calls {plainMethodName}. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => FooBar.ReturnThis()).");
}

[CombinatoryData("SubstituteExtensions.Returns", "SubstituteExtensions.Returns<object>", "SubstituteExtensions.ReturnsForAnyArgs", "SubstituteExtensions.ReturnsForAnyArgs<object>")]
public override async Task ReportsNoDiagnostic_WhenUsed_WithTypeofExpression(string method, string type)
{
var source = $@"using NSubstitute;
namespace MyNamespace
{{
public class Foo
{{
public object FooBar()
{{
return null;
}}
public object Bar()
{{
var sub = Substitute.For<Foo>();
sub.FooBar().Returns(1);
return null;
}}
}}
public struct FooBar
{{
public object Bar()
{{
var sub = Substitute.For<Foo>();
sub.FooBar().Returns(1);
return null;
}}
}}
public class FooTests
{{
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
{method}(substitute.FooBar(), typeof({type}));
}}
}}
}}";
await VerifyNoDiagnostic(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public interface IReEntrantReturnsSetupDiagnosticVerifier
Task ReportsNoDiagnostic_WhenReEntrantSubstituteNotUsed(string method, string firstReturn, string secondReturn);

Task ReportsDiagnostic_WhenUsingReEntrantReturns_AcrossMultipleFiles(string method);

Task ReportsNoDiagnostic_WhenUsed_WithTypeofExpression(string method, string type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For](Of Foo)()
[|substitute.{method}()|].Bar()
[|substitute.{method}().Bar()|]
End Sub
End Class
End Namespace
Expand Down Expand Up @@ -133,7 +133,7 @@ Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For](Of Foo2)()
[|substitute.{method}()|].Bar()
[|substitute.{method}().Bar()|]
End Sub
End Class
End Namespace
Expand Down Expand Up @@ -324,7 +324,7 @@ Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.For(Of Foo)
Dim x As Integer = [|substitute.{method}()|].Bar
Dim x As Integer = [|substitute.{method}().Bar|]
End Sub
End Class
End Namespace";
Expand Down Expand Up @@ -383,7 +383,7 @@ Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.For(Of Foo)
Dim x As Integer = [|substitute.{method}()|](1)
Dim x As Integer = [|substitute.{method}()(1)|]
End Sub
End Class
End Namespace";
Expand Down Expand Up @@ -457,7 +457,7 @@ End Class
Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For](Of Foo)()
Dim x = [|substitute.{method}()|]{call}
Dim x = [|substitute.{method}(){call}|]
End Sub
End Class
End Namespace";
Expand Down Expand Up @@ -523,7 +523,7 @@ End Class
Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For](Of Foo)()
Dim x = [|substitute.{method}()|]{call}
Dim x = [|substitute.{method}(){call}|]
End Sub
End Class
End Namespace";
Expand Down
Loading

0 comments on commit c58db62

Please sign in to comment.