Skip to content
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

Refactor unit tests: data driven tests and leverage Microsoft.CodeAnalysis.Testing patterns #76

Merged
merged 16 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 0 additions & 220 deletions Source/Moq.Analyzers.Test/AbstractClassTests.cs

This file was deleted.

118 changes: 22 additions & 96 deletions Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
using Verifier = Moq.Analyzers.Test.Helpers.AnalyzerVerifier<Moq.Analyzers.AsShouldBeUsedOnlyForInterfaceAnalyzer>;

namespace Moq.Analyzers.Test;

public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier<AsShouldBeUsedOnlyForInterfaceAnalyzer>
public class AsAcceptOnlyInterfaceAnalyzerTests
{
[Fact]
public async Task ShouldFailWhenUsingAsWithAbstractClass()
public static IEnumerable<object[]> TestData()
{
await VerifyCSharpDiagnostic(
"""
using Moq;

namespace AsAcceptOnlyInterface.TestBadAsForAbstractClass;

public abstract class BaseSampleClass
{
public int Calculate() => 0;
}

internal class MyUnitTests
{
private void TestBadAsForAbstractClass()
{
var mock = new Mock<BaseSampleClass>();
mock.As<{|Moq1300:BaseSampleClass|}>();
}
}
""");
foreach (var @namespace in new[] { string.Empty, "namespace MyNamespace;" })
{
// TODO: .As<BaseSampleClass> and .As<SampleClass> feels redundant
yield return [@namespace, """new Mock<BaseSampleClass>().As<{|Moq1300:BaseSampleClass|}>();"""];
yield return [@namespace, """new Mock<BaseSampleClass>().As<{|Moq1300:SampleClass|}>();"""];
yield return [@namespace, """new Mock<SampleClass>().As<ISampleInterface>();"""];
// TODO: Testing with .Setup() and .Returns() seems unnecessary.
yield return [@namespace, """new Mock<SampleClass>().As<ISampleInterface>().Setup(x => x.Calculate(It.IsAny<int>(), It.IsAny<int>())).Returns(10);"""];
}
}

[Fact]
public async Task ShouldFailWhenUsingAsWithConcreteClass()
[Theory]
[MemberData(nameof(TestData))]
public async Task ShouldAnalyzeAs(string @namespace, string mock)
{
await VerifyCSharpDiagnostic(
"""
using Moq;

namespace AsAcceptOnlyInterface.TestBadAsForNonAbstractClass;
await Verifier.VerifyAnalyzerAsync(
$$"""
{{@namespace}}

public interface ISampleInterface
{
Expand All @@ -46,80 +35,17 @@ public abstract class BaseSampleClass
public int Calculate() => 0;
}

public class OtherClass
{

public int Calculate() => 0;
}

internal class MyUnitTests
{
private void TestBadAsForNonAbstractClass()
{
var mock = new Mock<BaseSampleClass>();
mock.As<{|Moq1300:OtherClass|}>();
}
}
""");
}

[Fact]
public async Task ShouldPassWhenUsingAsWithInterface()
{
await VerifyCSharpDiagnostic(
"""
using Moq;

namespace AsAcceptOnlyInterface.TestOkAsForInterface;

public interface ISampleInterface
{
int Calculate(int a, int b);
}

public class SampleClass
{
public int Calculate() => 0;
}

internal class MyUnitTests
{
private void TestOkAsForInterface()
{
var mock = new Mock<SampleClass>();
mock.As<ISampleInterface>();
}
}
""");
}

[Fact]
public async Task ShouldPassWhenUsingAsWithInterfaceWithSetup()
{
await VerifyCSharpDiagnostic(
"""
using Moq;

namespace AsAcceptOnlyInterface.TestOkAsForInterfaceWithConfiguration;

public interface ISampleInterface
{
int Calculate(int a, int b);
}

public class SampleClass
{
public int Calculate() => 0;
}

internal class MyUnitTests
internal class UnitTest
{
private void TestOkAsForInterfaceWithConfiguration()
private void Test()
{
var mock = new Mock<SampleClass>();
mock.As<ISampleInterface>()
.Setup(x => x.Calculate(It.IsAny<int>(), It.IsAny<int>()))
.Returns(10);
{{mock}}
}
}
""");
Expand Down
Loading
Loading