Skip to content
Merged
143 changes: 143 additions & 0 deletions tests/Moq.Analyzers.Test/MockBehaviorDiagnosticAnalyzerBaseTests.cs
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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Moq.Analyzers.Test/MockBehaviorDiagnosticAnalyzerBaseTests.cs#L1

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.
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);
}
Comment thread
rjmurillo-bot marked this conversation as resolved.

[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();
}
}
""";
Comment thread
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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Moq.Analyzers.Test/MockBehaviorDiagnosticAnalyzerBaseTests.cs#L133

Remove the 'Async' suffix to the name of this method.
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);
}
}
Loading