diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodTests.cs new file mode 100644 index 00000000..e731f3d8 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodTests.cs @@ -0,0 +1,636 @@ +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Shared; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using Xunit; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonVirtualSetupWhenAnalyzerTests +{ + public class WhenAsOrdinaryMethodTests : NonVirtualSetupWhenDiagnosticVerifier + { + [Theory] + [InlineData("Sub(sb) sb.Bar()", 14, 58)] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()", 14, 79)] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub", + 15, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb()")] + [InlineData(@"Function(ByVal [sub] As Func(Of Integer)) [sub]()")] + [InlineData( + @"Sub(sb As Func(Of Integer)) + sb() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string whenAction) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()", 22, 58)] + [InlineData(@"Function(ByVal [sub] As Foo2) [sub].Bar()", 22, 80)] + [InlineData( + @"Sub(sb As Foo2) + sb.Bar() + End Sub", + 23, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar() As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As IFoo) [sub].Bar()")] + [InlineData( + @"Sub(sb As IFoo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar() As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + ReadOnly Property Bar As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar(Of Integer)()")] + [InlineData(@"Function(ByVal [sub] As IFoo(Of Integer)) [sub].Bar(Of Integer)()")] + [InlineData( + @"Sub(sb As IFoo(Of Integer)) + sb.Bar(Of Integer)() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + + Function Bar(Of T)() As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo (Of Integer))() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb(1) + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default Property Item(ByVal i As Integer) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string whenAction) + { + var source = $@"Imports System.Runtime.CompilerServices + +Namespace NSubstitute + Public Class Foo + Public Function Bar() As Integer + Return 1 + End Function + End Class + + Module SubstituteExtensions + + Function [When](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T + Return Nothing + End Function + End Module + + Public Class FooTests + Public Sub Test() + Dim substitute As Foo = Nothing + SubstituteExtensions.[When](substitute, {whenAction}, 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub", + 13, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub", + 14, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public Overridable Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb(1) + End Sub", + 19, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb(1) + End Sub", + 20, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + + Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Throw New System.NotImplementedException + End Get + End Property + End Class + + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(19, 27) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs new file mode 100644 index 00000000..f7644a4a --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -0,0 +1,636 @@ +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Shared; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using Xunit; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonVirtualSetupWhenAnalyzerTests +{ + public class WhenAsOrdinaryMethodWithGenericTypeSpecifiedTests : NonVirtualSetupWhenDiagnosticVerifier + { + [Theory] + [InlineData("Sub(sb) sb.Bar()", 14, 66)] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()", 14, 87)] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub", + 15, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.When(Of Foo2)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb()")] + [InlineData(@"Function(ByVal [sub] As Func(Of Integer)) [sub]()")] + [InlineData( + @"Sub(sb As Func(Of Integer)) + sb() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string whenAction) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + SubstituteExtensions.When(Of Func(Of Integer))(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()", 22, 67)] + [InlineData(@"Function(ByVal [sub] As Foo2) [sub].Bar()", 22, 89)] + [InlineData( + @"Sub(sb As Foo2) + sb.Bar() + End Sub", + 23, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.When(Of Foo2)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar() As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As IFoo) [sub].Bar()")] + [InlineData( + @"Sub(sb As IFoo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar() As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + ReadOnly Property Bar As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar(Of Integer)()")] + [InlineData(@"Function(ByVal [sub] As IFoo(Of Integer)) [sub].Bar(Of Integer)()")] + [InlineData( + @"Sub(sb As IFoo(Of Integer)) + sb.Bar(Of Integer)() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + + Function Bar(Of T)() As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo (Of Integer))() + SubstituteExtensions.When(Of IFoo (Of Integer))(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb(1) + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default Property Item(ByVal i As Integer) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.When(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string whenAction) + { + var source = $@"Imports System.Runtime.CompilerServices + +Namespace NSubstitute + Public Class Foo + Public Function Bar() As Integer + Return 1 + End Function + End Class + + Module SubstituteExtensions + + Function [When](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T + Return Nothing + End Function + End Module + + Public Class FooTests + Public Sub Test() + Dim substitute As Foo = Nothing + SubstituteExtensions.[When](Of Foo)(substitute, {whenAction}, 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub", + 13, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub", + 14, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public Overridable Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb(1) + End Sub", + 19, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb(1) + End Sub", + 20, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + + Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Throw New System.NotImplementedException + End Get + End Property + End Class + + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(19, 27) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.When(Of Foo)(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsExtensionMethodTests.cs new file mode 100644 index 00000000..9fcc0b63 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsExtensionMethodTests.cs @@ -0,0 +1,636 @@ +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Shared; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using Xunit; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonVirtualSetupWhenAnalyzerTests +{ + public class WhenForAnyArgsAsExtensionMethodTests : NonVirtualSetupWhenDiagnosticVerifier + { + [Theory] + [InlineData("Sub(sb) sb.Bar()", 14, 47)] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()", 14, 68)] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub", + 15, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb()")] + [InlineData(@"Function(ByVal [sub] As Func(Of Integer)) [sub]()")] + [InlineData( + @"Sub(sb As Func(Of Integer)) + sb() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string whenAction) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()", 22, 47)] + [InlineData(@"Function(ByVal [sub] As Foo2) [sub].Bar()", 22, 69)] + [InlineData( + @"Sub(sb As Foo2) + sb.Bar() + End Sub", + 23, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar() As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As IFoo) [sub].Bar()")] + [InlineData( + @"Sub(sb As IFoo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar() As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + ReadOnly Property Bar As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar(Of Integer)()")] + [InlineData(@"Function(ByVal [sub] As IFoo(Of Integer)) [sub].Bar(Of Integer)()")] + [InlineData( + @"Sub(sb As IFoo(Of Integer)) + sb.Bar(Of Integer)() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + + Function Bar(Of T)() As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo (Of Integer))() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb(1) + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default Property Item(ByVal i As Integer) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string whenAction) + { + var source = $@"Imports System.Runtime.CompilerServices + +Namespace NSubstitute + Public Class Foo + Public Function Bar() As Integer + Return 1 + End Function + End Class + + Module SubstituteExtensions + + Function WhenForAnyArgs(Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T + Return Nothing + End Function + End Module + + Public Class FooTests + Public Sub Test() + Dim substitute As Foo = Nothing + substitute.WhenForAnyArgs({whenAction}, 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub", + 13, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub", + 14, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public Overridable Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb(1) + End Sub", + 19, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb(1) + End Sub", + 20, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + + Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Throw New System.NotImplementedException + End Get + End Property + End Class + + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs({whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs(AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(19, 27) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.WhenForAnyArgs(AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodTests.cs new file mode 100644 index 00000000..dc847ca6 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodTests.cs @@ -0,0 +1,636 @@ +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Shared; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using Xunit; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonVirtualSetupWhenAnalyzerTests +{ + public class WhenForAnyArgsAsOrdinaryMethodTests : NonVirtualSetupWhenDiagnosticVerifier + { + [Theory] + [InlineData("Sub(sb) sb.Bar()", 14, 68)] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()", 14, 89)] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub", + 15, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb()")] + [InlineData(@"Function(ByVal [sub] As Func(Of Integer)) [sub]()")] + [InlineData( + @"Sub(sb As Func(Of Integer)) + sb() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string whenAction) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()", 22, 68)] + [InlineData(@"Function(ByVal [sub] As Foo2) [sub].Bar()", 22, 90)] + [InlineData( + @"Sub(sb As Foo2) + sb.Bar() + End Sub", + 23, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar() As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As IFoo) [sub].Bar()")] + [InlineData( + @"Sub(sb As IFoo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar() As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + ReadOnly Property Bar As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar(Of Integer)()")] + [InlineData(@"Function(ByVal [sub] As IFoo(Of Integer)) [sub].Bar(Of Integer)()")] + [InlineData( + @"Sub(sb As IFoo(Of Integer)) + sb.Bar(Of Integer)() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + + Function Bar(Of T)() As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo (Of Integer))() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb(1) + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default Property Item(ByVal i As Integer) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string whenAction) + { + var source = $@"Imports System.Runtime.CompilerServices + +Namespace NSubstitute + Public Class Foo + Public Function Bar() As Integer + Return 1 + End Function + End Class + + Module SubstituteExtensions + + Function [WhenForAnyArgs](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T + Return Nothing + End Function + End Module + + Public Class FooTests + Public Sub Test() + Dim substitute As Foo = Nothing + SubstituteExtensions.[WhenForAnyArgs](substitute, {whenAction}, 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub", + 13, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub", + 14, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public Overridable Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb(1) + End Sub", + 19, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb(1) + End Sub", + 20, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + + Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Throw New System.NotImplementedException + End Get + End Property + End Class + + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(19, 27) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs new file mode 100644 index 00000000..d1ead803 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonVirtualSetupWhenAnalyzerTests/WhenForAnyArgsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -0,0 +1,636 @@ +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Shared; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using Xunit; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonVirtualSetupWhenAnalyzerTests +{ + public class WhenForAnyArgsAsOrdinaryMethodWithGenericTypeSpecifiedTests : NonVirtualSetupWhenDiagnosticVerifier + { + [Theory] + [InlineData("Sub(sb) sb.Bar()", 14, 76)] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()", 14, 97)] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub", + 15, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.WhenForAnyArgs(Of Foo2)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb()")] + [InlineData(@"Function(ByVal [sub] As Func(Of Integer)) [sub]()")] + [InlineData( + @"Sub(sb As Func(Of Integer)) + sb() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string whenAction) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + SubstituteExtensions.WhenForAnyArgs(Of Func(Of Integer))(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()", 22, 77)] + [InlineData(@"Function(ByVal [sub] As Foo2) [sub].Bar()", 22, 99)] + [InlineData( + @"Sub(sb As Foo2) + sb.Bar() + End Sub", + 23, + 17)] + public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + SubstituteExtensions.WhenForAnyArgs(Of Foo2)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar() As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As IFoo) [sub].Bar()")] + [InlineData( + @"Sub(sb As IFoo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar() As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i +1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + ReadOnly Property Bar As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar(Of Integer)()")] + [InlineData(@"Function(ByVal [sub] As IFoo(Of Integer)) [sub].Bar(Of Integer)()")] + [InlineData( + @"Sub(sb As IFoo(Of Integer)) + sb.Bar(Of Integer)() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + + Function Bar(Of T)() As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo (Of Integer))() + SubstituteExtensions.WhenForAnyArgs(Of IFoo (Of Integer))(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As IFoo) + Dim x = sb(1) + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default Property Item(ByVal i As Integer) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + SubstituteExtensions.WhenForAnyArgs(Of IFoo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData("Sub(sb) sb.Bar()")] + [InlineData(@"Function(ByVal [sub] As Foo) [sub].Bar()")] + [InlineData( + @"Sub(sb As Foo) + sb.Bar() + End Sub")] + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string whenAction) + { + var source = $@"Imports System.Runtime.CompilerServices + +Namespace NSubstitute + Public Class Foo + Public Function Bar() As Integer + Return 1 + End Function + End Class + + Module SubstituteExtensions + + Function [WhenForAnyArgs](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T + Return Nothing + End Function + End Module + + Public Class FooTests + Public Sub Test() + Dim substitute As Foo = Nothing + SubstituteExtensions.[WhenForAnyArgs](Of Foo)(substitute, {whenAction}, 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub", + 13, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub", + 14, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public ReadOnly Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb.Bar + End Sub")] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb.Bar + End Sub")] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string whenAction) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public Overridable Property Bar As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source); + } + + [Theory] + [InlineData( + @"Sub(sb As Foo) + Dim x = sb(1) + End Sub", + 19, + 25)] + [InlineData( + @"Sub(sb As Foo) + Dim x as Integer + x = sb(1) + End Sub", + 20, + 21)] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string whenAction, int expectedLine, int expectedColumn) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + + Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Throw New System.NotImplementedException + End Get + End Property + End Class + + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 1 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,{whenAction}).[Do](Sub(callInfo) i = i + 1) + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(expectedLine, expectedColumn) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + var expectedDiagnostic = new DiagnosticResult + { + Id = DiagnosticIdentifiers.NonVirtualWhenSetupSpecification, + Severity = DiagnosticSeverity.Warning, + Message = "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", + Locations = new[] + { + new DiagnosticResultLocation(19, 27) + } + }; + + await VerifyDiagnostic(source, expectedDiagnostic); + } + + [Fact] + public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularFunction() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar() As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim i As Integer = 0 + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + SubstituteExtensions.WhenForAnyArgs(Of Foo)(substitute,AddressOf SubstituteCall).[Do](Sub(callInfo) i = i + 1) + substitute.Bar() + End Sub + + Private Sub SubstituteCall(ByVal [sub] As Foo) + Dim objBarr = [sub].Bar() + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source); + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied functions not supported in VB")] + public override Task ReportsDiagnostics_WhenSettingValueForNonVirtualMember_InRegularExpressionBodiedFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InLocalFunction() + { + return Task.CompletedTask; + } + + [Fact(Skip = "Expression bodied local functions not supported in VB")] + public override Task ReportsNoDiagnostics_WhenSettingValueForVirtualMember_InExpressionBodiedLocalFunction() + { + return Task.CompletedTask; + } + } +} \ No newline at end of file