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

Replace our custom test harness with Microsoft.CodeAnalysis testing harness #65

Merged
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
14 changes: 12 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
<Import Project="build/targets/reproducible/Packages.props" />
<Import Project="build/targets/tests/Packages.props" />
<Import Project="build/targets/codeanalysis/Packages.props" />
<ItemGroup Label="Public APIs">
<!--
These items impact our compatibility with SDK versions, so follow
to strike a balance between compatibility and modernity.

These are the two main documents to reference how the Public API impacts users:
- https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md
- https://learn.microsoft.com/en-us/visualstudio/productinfo/vs-servicing
-->

<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="GetPackFromProject" Version="1.0.6" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.6.1" />
<PackageVersion Include="Moq" Version="4.8.2" />
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.133" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

Install ["Moq.Analyzers" NuGet package](https://www.nuget.org/packages/Moq.Analyzers) into test projects using Moq.

You must use an in-support version of the .NET SDK (i.e. 6+).

## Contributions are welcome!

Moq.Analyzers continues to evolve and add new features. Any help will be appreciated. You can report issues, develop new features, improve the documention, or do other cool stuff.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

71 changes: 26 additions & 45 deletions Source/Moq.Analyzers.Test/AbstractClassTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System.IO;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Moq.Analyzers.Test.Helpers;
using Xunit;

namespace Moq.Analyzers.Test;

public class AbstractClassTests : DiagnosticVerifier
public class AbstractClassTests : DiagnosticVerifier<ConstructorArgumentsShouldMatchAnalyzer>
{
// TODO: Review use of `.As<>()` in the test cases. It is not clear what purpose it serves.
[Fact]
public Task ShouldFailOnGenericTypesWithMismatchArgs()
public async Task ShouldFailOnGenericTypesWithMismatchArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.GenericMistmatchArgs;

Expand All @@ -39,24 +38,22 @@ internal class MyUnitTests
private void TestBadWithGeneric()
{
// The class has a constructor that takes an Int32 but passes a String
var mock = new Mock<AbstractGenericClassWithCtor<object>>("42");
var mock = new Mock<AbstractGenericClassWithCtor<object>>{|Moq1002:("42")|};

// The class has a ctor with two arguments [Int32, String], but they are passed in reverse order
var mock1 = new Mock<AbstractGenericClassWithCtor<object>>("42", 42);
var mock1 = new Mock<AbstractGenericClassWithCtor<object>>{|Moq1002:("42", 42)|};

// The class has a ctor but does not take any arguments
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42);
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>{|Moq1002:(42)|};
}
}
"""
]));
""");
}

[Fact]
public Task ShouldPassOnGenericTypesWithNoArgs()
public async Task ShouldPassOnGenericTypesWithNoArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.GenericNoArgs;

Expand All @@ -79,15 +76,13 @@ private void TestForBaseGenericNoArgs()
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(MockBehavior.Default);
}
}
"""
]));
""");
}

[Fact]
public Task ShouldFailOnMismatchArgs()
public async Task ShouldFailOnMismatchArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.MismatchArgs;

Expand All @@ -114,24 +109,22 @@ internal class MyUnitTests
private void TestBad()
{
// The class has a ctor that takes an Int32 but passes a String
var mock = new Mock<AbstractClassWithCtor>("42");
var mock = new Mock<AbstractClassWithCtor>{|Moq1002:("42")|};

// The class has a ctor with two arguments [Int32, String], but they are passed in reverse order
var mock1 = new Mock<AbstractClassWithCtor>("42", 42);
var mock1 = new Mock<AbstractClassWithCtor>{|Moq1002:("42", 42)|};

// The class has a ctor but does not take any arguments
var mock2 = new Mock<AbstractClassDefaultCtor>(42);
var mock2 = new Mock<AbstractClassDefaultCtor>{|Moq1002:(42)|};
}
}
"""
]));
""");
}

[Fact]
public Task ShouldPassWithNoArgs()
public async Task ShouldPassWithNoArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.NoArgs;

Expand All @@ -151,15 +144,13 @@ private void TestForBaseNoArgs()
mock.As<AbstractClassDefaultCtor>();
}
}
"""
]));
""");
}

[Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")]
public Task ShouldFailWithArgsNonePassed()
public async Task ShouldFailWithArgsNonePassed()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.WithArgsNonePassed;

Expand All @@ -181,19 +172,15 @@ private void TestForBaseWithArgsNonePassed()
{
var mock = new Mock<AbstractClassWithCtor>();
mock.As<AbstractClassWithCtor>();

var mock2 = new Mock<AbstractClassDefaultCtor>(MockBehavior.Default);
}
}
"""
]));
""");
}

[Fact]
public Task ShouldPassWithArgsPassed()
public async Task ShouldPassWithArgsPassed()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
namespace Moq.Analyzers.Test.DataAbstractClass.WithArgsPassed;

Expand Down Expand Up @@ -233,12 +220,6 @@ private void TestForBaseWithArgsPassed()
var mock6 = new Mock<AbstractGenericClassWithCtor<object>>(MockBehavior.Default, 42);
}
}
"""
]));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new ConstructorArgumentsShouldMatchAnalyzer();
""");
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

45 changes: 16 additions & 29 deletions Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Moq.Analyzers.Test.Helpers;
using Xunit;

namespace Moq.Analyzers.Test;

public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier
public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier<AsShouldBeUsedOnlyForInterfaceAnalyzer>
{
[Fact]
public Task ShouldFailWhenUsingAsWithAbstractClass()
public async Task ShouldFailWhenUsingAsWithAbstractClass()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
using Moq;

Expand All @@ -28,18 +27,16 @@ internal class MyUnitTests
private void TestBadAsForAbstractClass()
{
var mock = new Mock<BaseSampleClass>();
mock.As<BaseSampleClass>();
mock.As<{|Moq1300:BaseSampleClass|}>();
}
}
"""
]));
""");
}

[Fact]
public Task ShouldFailWhenUsingAsWithConcreteClass()
public async Task ShouldFailWhenUsingAsWithConcreteClass()
{
return Verify(VerifyCSharpDiagnostic(
[
await VerifyCSharpDiagnostic(
"""
using Moq;

Expand All @@ -66,18 +63,16 @@ internal class MyUnitTests
private void TestBadAsForNonAbstractClass()
{
var mock = new Mock<BaseSampleClass>();
mock.As<OtherClass>();
mock.As<{|Moq1300:OtherClass|}>();
}
}
"""
]));
""");
}

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

Expand All @@ -101,15 +96,13 @@ private void TestOkAsForInterface()
mock.As<ISampleInterface>();
}
}
"""
]));
""");
}

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

Expand All @@ -135,12 +128,6 @@ private void TestOkAsForInterfaceWithConfiguration()
.Returns(10);
}
}
"""
]));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new AsShouldBeUsedOnlyForInterfaceAnalyzer();
""");
}
}
Loading
Loading