-
Notifications
You must be signed in to change notification settings - Fork 4
test: add direct unit tests for MockBehaviorDiagnosticAnalyzerBase #1038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
15d2d26
68e09ea
2f8d304
9195c08
5ea2dd1
50b49bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| using Microsoft.CodeAnalysis.Testing; | ||
|
Check warning on line 1 in tests/Moq.Analyzers.Test/MockBehaviorDiagnosticAnalyzerBaseTests.cs
|
||
| using ExplicitVerifier = Moq.Analyzers.Test.Helpers.AnalyzerVerifier<Moq.Analyzers.SetExplicitMockBehaviorAnalyzer>; | ||
| using StrictVerifier = Moq.Analyzers.Test.Helpers.AnalyzerVerifier<Moq.Analyzers.SetStrictMockBehaviorAnalyzer>; | ||
|
|
||
| namespace Moq.Analyzers.Test; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="MockBehaviorDiagnosticAnalyzerBase"/> shared logic, exercised through | ||
| /// its concrete subclasses <see cref="SetExplicitMockBehaviorAnalyzer"/> and | ||
| /// <see cref="SetStrictMockBehaviorAnalyzer"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These tests target the base class branches: IsMockReferenced() guard, | ||
| /// AnalyzeObjectCreation type guards, and AnalyzeInvocation method guards. | ||
| /// Subclass-specific tests live in SetExplicitMockBehaviorAnalyzerTests and | ||
| /// SetStrictMockBehaviorAnalyzerTests. | ||
| /// | ||
| /// Not tested: MockBehavior-is-null branch (line 89-92 of MockBehaviorDiagnosticAnalyzerBase). | ||
| /// This guard requires a compilation where Moq.Mock resolves but Moq.MockBehavior does not. | ||
| /// That scenario cannot occur with any real Moq assembly. | ||
| /// </remarks> | ||
| public class MockBehaviorDiagnosticAnalyzerBaseTests | ||
| { | ||
| public static IEnumerable<object[]> MoqReferenceAssemblyGroups() | ||
| { | ||
| return new object[][] | ||
| { | ||
| [ReferenceAssemblyCatalog.Net80WithOldMoq], | ||
| [ReferenceAssemblyCatalog.Net80WithNewMoq], | ||
| }; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldNotReport_WhenMoqIsNotReferenced() | ||
| { | ||
| const string source = """ | ||
| public class Foo | ||
| { | ||
| private void Test() { } | ||
| } | ||
| """; | ||
|
|
||
| // CompilerDiagnostics.None suppresses CS0246 from the global using Moq added by the test infrastructure. | ||
| await VerifyBothAnalyzersAsync(source, ReferenceAssemblyCatalog.Net80, CompilerDiagnostics.None); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(MoqReferenceAssemblyGroups))] | ||
| public async Task ShouldNotReport_WhenObjectCreationIsNotMockType(string referenceAssemblyGroup) | ||
| { | ||
| const string source = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| internal class UnitTest | ||
| { | ||
| private void Test() | ||
| { | ||
| var list = new List<int>(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| await VerifyBothAnalyzersAsync(source, referenceAssemblyGroup); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(MoqReferenceAssemblyGroups))] | ||
| public async Task ShouldNotReport_WhenInvocationIsNotMockOf(string referenceAssemblyGroup) | ||
| { | ||
| const string source = """ | ||
| using System; | ||
|
|
||
| internal class UnitTest | ||
| { | ||
| private void Test() | ||
| { | ||
| Console.WriteLine("not a mock"); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| await VerifyBothAnalyzersAsync(source, referenceAssemblyGroup); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(MoqReferenceAssemblyGroups))] | ||
| public async Task ShouldNotReport_WhenNonMockObjectCreatedWithMoqReferenced(string referenceAssemblyGroup) | ||
| { | ||
| // Moq is referenced but the object creation is not Mock<T> or MockRepository. | ||
| // Exercises the type guard in AnalyzeObjectCreation. | ||
| const string source = """ | ||
| using Moq; | ||
|
|
||
| internal class UnitTest | ||
| { | ||
| private void Test() | ||
| { | ||
| var obj = new object(); | ||
| } | ||
| } | ||
| """; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| await VerifyBothAnalyzersAsync(source, referenceAssemblyGroup); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(MoqReferenceAssemblyGroups))] | ||
| public async Task ShouldNotReport_WhenMoqInvocationIsNotMockOf(string referenceAssemblyGroup) | ||
| { | ||
| // Moq is referenced, invocation exists on a mock, but it is not Mock.Of<T>(). | ||
| // Exercises the invocation method guard in AnalyzeInvocation. | ||
| const string source = """ | ||
| using Moq; | ||
|
|
||
| public interface ISample | ||
| { | ||
| void Method(); | ||
| } | ||
|
|
||
| internal class UnitTest | ||
| { | ||
| private void Test() | ||
| { | ||
| var mock = new Mock<ISample>(MockBehavior.Strict); | ||
| mock.Setup(x => x.Method()); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| await VerifyBothAnalyzersAsync(source, referenceAssemblyGroup); | ||
| } | ||
|
|
||
| private static async Task VerifyBothAnalyzersAsync( | ||
|
Check notice on line 133 in tests/Moq.Analyzers.Test/MockBehaviorDiagnosticAnalyzerBaseTests.cs
|
||
| string source, | ||
| string referenceAssemblyGroup, | ||
| CompilerDiagnostics? compilerDiagnostics = null) | ||
| { | ||
| await ExplicitVerifier.VerifyAnalyzerAsync( | ||
| source, referenceAssemblyGroup, configFileName: null, configContent: null, compilerDiagnostics).ConfigureAwait(false); | ||
| await StrictVerifier.VerifyAnalyzerAsync( | ||
| source, referenceAssemblyGroup, configFileName: null, configContent: null, compilerDiagnostics).ConfigureAwait(false); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.