public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireNamespace(string method) { Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System Imports NSubstitute Imports NSubstitute.ExceptionExtensions Imports MyOtherNamespace Namespace MyOtherNamespace Public Class FooBarBar Public Property Bar As Integer Default Public ReadOnly Property Item(ByVal x As Integer) As Integer Get Return 0 End Get End Property Public Function FooBar() As Integer Return 1 End Function End Class End Namespace Namespace MyNamespace Public Class Foo Public Property Bar As Integer Default Public ReadOnly Property Item(ByVal x As Integer) As Integer Get Return 0 End Get End Property Public Function FooBar() As Integer Return 1 End Function End Class Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {method}(substitute(1)) {method}(substitute.Bar) {method}(substitute.FooBar()) Dim substituteFooBarBar = NSubstitute.Substitute.[For](Of FooBarBar)() {method}([|substituteFooBarBar(1)|]) {method}([|substituteFooBarBar.Bar|]) {method}([|substituteFooBarBar.FooBar()|]) End Sub End Class End Namespace "; var textParserResult = TextParser.GetSpans(source); var diagnosticMessages = new[] { "Member Item can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", "Member Bar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireGenericType() { Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = @"Imports System Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo(Of T) Public Property Bar As Integer Default Public ReadOnly Property Item(ByVal x As Integer) As Integer Get Return 0 End Get End Property Public Function FooBar() As Integer Return 1 End Function End Class Public Class FooBarBar(Of T) Public Property Bar As Integer Default Public ReadOnly Property Item(ByVal x As Integer) As Integer Get Return 0 End Get End Property Public Function FooBar() As Integer Return 1 End Function End Class Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo(Of Integer))() substitute(1).ThrowsForAnyArgs(New Exception()) substitute.Bar.ThrowsForAnyArgs(New Exception()) substitute.FooBar().ThrowsForAnyArgs(New Exception()) Dim substituteFooBarBar = NSubstitute.Substitute.[For](Of FooBarBar(Of Integer))() substituteFooBarBar(1).ThrowsForAnyArgs(New Exception()) substituteFooBarBar.Bar.ThrowsForAnyArgs(New Exception()) substituteFooBarBar.FooBar().ThrowsForAnyArgs(New Exception()) End Sub End Class End Namespace "; var expectedDiagnostic = new[] { new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, 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(41, 13) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, 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(42, 13) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(43, 13) } } }; await VerifyDiagnostic(source, expectedDiagnostic); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireNamespace() { Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = @"Imports NSubstitute Imports NSubstitute.ReturnsExtensions Imports MyOtherNamespace Namespace MyOtherNamespace Public Class FooBarBar Public Property Bar As FooBarBar Default Public ReadOnly Property Item(ByVal x As Integer) As FooBarBar Get Return Nothing End Get End Property Public Function FooBar() As FooBarBar Return Nothing End Function End Class End Namespace Namespace MyNamespace Public Class Foo Public Property Bar As FooBarBar Default Public ReadOnly Property Item(ByVal x As Integer) As FooBarBar Get Return Nothing End Get End Property Public Function FooBar() As FooBarBar Return Nothing End Function End Class Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substitute(1)) ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substitute.Bar) ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substitute.FooBar()) Dim substituteFooBarBar = NSubstitute.Substitute.[For](Of FooBarBar)() ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substituteFooBarBar(1)) ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substituteFooBarBar.Bar) ReturnsExtensions.ReturnsNullForAnyArgs(Of FooBarBar)(substituteFooBarBar.FooBar()) End Sub End Class End Namespace "; var expectedDiagnostic = new[] { new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, 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(43, 67) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, 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(44, 67) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(45, 67) } } }; await VerifyDiagnostic(source, expectedDiagnostic); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireNamespace(string method) { Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using NSubstitute; using NSubstitute.ReturnsExtensions; namespace MyOtherNamespace {{ public class FooBarBar {{ public object Bar {{ get; set; }} public object this[int x] => 0; public object FooBar() {{ return 1; }} }} }} namespace MyNamespace {{ using MyOtherNamespace; public class Foo {{ public object Bar {{ get; set; }} public object this[int x] => 0; public object FooBar() {{ return 1; }} }} public class FooTests {{ public void Test() {{ var substitute = NSubstitute.Substitute.For<Foo>(); {method}(substitute[1]); {method}(substitute.Bar); {method}(substitute.FooBar()); var substituteFooBarBar = NSubstitute.Substitute.For<FooBarBar>(); {method}([|substituteFooBarBar[1]|]); {method}([|substituteFooBarBar.Bar|]); {method}([|substituteFooBarBar.FooBar()|]); }} }} }}"; var textParserResult = TextParser.GetSpans(source); var diagnosticMessages = new[] { "Member this[] can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", "Member Bar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireNamespace() { Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = @"using NSubstitute; namespace MyOtherNamespace { public class FooBarBar { public int Bar { get; set; } public int this[int x] => 0; public int FooBar() { return 1; } } } namespace MyNamespace { using MyOtherNamespace; public class Foo { public int Bar { get; set; } public int this[int x] => 0; public int FooBar() { return 1; } } public class FooTests { public void Test() { var substitute = NSubstitute.Substitute.For<Foo>(); substitute[1].ReturnsForAnyArgs<int>(1); substitute.Bar.ReturnsForAnyArgs<int>(1); substitute.FooBar().ReturnsForAnyArgs<int>(1); var substituteFooBarBar = NSubstitute.Substitute.For<FooBarBar>(); substituteFooBarBar[1].ReturnsForAnyArgs<int>(1); substituteFooBarBar.Bar.ReturnsForAnyArgs<int>(1); substituteFooBarBar.FooBar().ReturnsForAnyArgs<int>(1); } } }"; var expectedDiagnostic = new[] { new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member this[] can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(39, 13) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member Bar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(40, 13) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(41, 13) } } }; await VerifyDiagnostic(source, expectedDiagnostic); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireGenericType() { Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = @"using System; using NSubstitute; using NSubstitute.ExceptionExtensions; namespace MyNamespace { public class Foo<T> { public int Bar { get; set; } public int this[int x] => 0; public int FooBar() { return 1; } } public class FooBarBar<T> { public int Bar { get;set; } public int this[int x] => 0; public int FooBar() { return 1; } } public class FooTests { public void Test() { var substitute = NSubstitute.Substitute.For<Foo<int>>(); ExceptionExtensions.Throws<Exception>(substitute[1]); ExceptionExtensions.Throws<Exception>(substitute.Bar); ExceptionExtensions.Throws<Exception>(substitute.FooBar()); var substituteFooBarBar = NSubstitute.Substitute.For<FooBarBar<int>>(); ExceptionExtensions.Throws<Exception>(substituteFooBarBar[1]); ExceptionExtensions.Throws<Exception>(substituteFooBarBar.Bar); ExceptionExtensions.Throws<Exception>(substituteFooBarBar.FooBar()); } } }"; var expectedDiagnostic = new[] { new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member this[] can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(37, 51) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member Bar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(38, 51) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(39, 51) } } }; await VerifyDiagnostic(source, expectedDiagnostic); }
public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingMembersFromEntireType() { Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = @"using NSubstitute; using NSubstitute.ReturnsExtensions; namespace MyNamespace { public class Foo { public object Bar { get; set; } public object this[int x] => new object(); public object FooBar() { return new object(); } } public class FooBarBar { public object Bar { get;set; } public object this[int x] => new object(); public object FooBar() { return new object(); } } public class FooTests { public void Test() { var substitute = NSubstitute.Substitute.For<Foo>(); ReturnsExtensions.ReturnsNullForAnyArgs(substitute[1]); ReturnsExtensions.ReturnsNullForAnyArgs(substitute.Bar); ReturnsExtensions.ReturnsNullForAnyArgs(substitute.FooBar()); var substituteFooBarBar = NSubstitute.Substitute.For<FooBarBar>(); ReturnsExtensions.ReturnsNullForAnyArgs(substituteFooBarBar[1]); ReturnsExtensions.ReturnsNullForAnyArgs(substituteFooBarBar.Bar); ReturnsExtensions.ReturnsNullForAnyArgs(substituteFooBarBar.FooBar()); } } }"; var expectedDiagnostic = new[] { new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member this[] can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(36, 53) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member Bar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(37, 53) } }, new DiagnosticResult { Id = DiagnosticIdentifiers.NonVirtualSetupSpecification, Severity = DiagnosticSeverity.Warning, Message = "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.", Locations = new[] { new DiagnosticResultLocation(38, 53) } } }; await VerifyDiagnostic(source, expectedDiagnostic); }