Skip to content

Commit

Permalink
Migrate from ApprovalTests to Verify (#25)
Browse files Browse the repository at this point in the history
* Do the minimal, mechanical conversion from ApprovalTests to Verify

* Enable ImplicitUsings, C# 12, and Nullable
  • Loading branch information
MattKotsenas authored May 31, 2024
1 parent edfc0e3 commit 9d37487
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 729 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
</PropertyGroup>

<Import Project="build/targets/artifacts/Artifacts.props" />
<Import Project="build/targets/compiler/Compiler.props" />
<Import Project="build/targets/tests/Tests.props" />
</Project>
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project>
<Import Project="build/targets/artifacts/Artifacts.targets" />
<Import Project="build/targets/compiler/Compiler.targets" />
<Import Project="build/targets/tests/Tests.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Diagnostic 1
Id: Moq1300
Location: SourceFile(Test0.cs[1376..1391))
Highlight: BaseSampleClass
Lines: mock.As<BaseSampleClass>();
Severity: Error
Message: Mock.As() should take interfaces only
Diagnostic 2
Id: Moq1300
Location: SourceFile(Test0.cs[1541..1551))
Highlight: OtherClass
Lines: mock.As<OtherClass>();
Severity: Error
Message: Mock.As() should take interfaces only
Diagnostic 1
Id: Moq1300
Location: SourceFile(Test0.cs[1376..1391))
Highlight: BaseSampleClass
Lines: mock.As<BaseSampleClass>();
Severity: Error
Message: Mock.As() should take interfaces only

Diagnostic 2
Id: Moq1300
Location: SourceFile(Test0.cs[1541..1551))
Highlight: OtherClass
Lines: mock.As<OtherClass>();
Severity: Error
Message: Mock.As() should take interfaces only

Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
namespace Moq.Analyzers.Test
{
using System.IO;
using ApprovalTests;
using ApprovalTests.Reporters;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;

[UseReporter(typeof(DiffReporter))]
public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier
{
[Fact]
public void ShouldPassIfGoodParameters()
public Task ShouldPassIfGoodParameters()
{
Approvals.Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/AsAcceptOnlyInterface.cs")));
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/AsAcceptOnlyInterface.cs")));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
Diagnostic 1
Id: Moq1100
Location: SourceFile(Test0.cs[1120..1127))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((int i) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.
Diagnostic 2
Id: Moq1100
Location: SourceFile(Test0.cs[1201..1223))
Highlight: (string s1, string s2)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s1, string s2) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.
Diagnostic 3
Id: Moq1100
Location: SourceFile(Test0.cs[1336..1355))
Highlight: (string s1, int i1)
Lines: mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((string s1, int i1) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.
Diagnostic 4
Id: Moq1100
Location: SourceFile(Test0.cs[1435..1442))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((int i) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.
Diagnostic 1
Id: Moq1100
Location: SourceFile(Test0.cs[1120..1127))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((int i) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 2
Id: Moq1100
Location: SourceFile(Test0.cs[1201..1223))
Highlight: (string s1, string s2)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s1, string s2) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 3
Id: Moq1100
Location: SourceFile(Test0.cs[1336..1355))
Highlight: (string s1, int i1)
Lines: mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((string s1, int i1) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 4
Id: Moq1100
Location: SourceFile(Test0.cs[1435..1442))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((int i) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
namespace Moq.Analyzers.Test
{
using System.IO;
using ApprovalTests;
using ApprovalTests.Reporters;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;

[UseReporter(typeof(DiffReporter))]
public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : DiagnosticVerifier
{
[Fact]
public void ShouldPassIfGoodParameters()
public Task ShouldPassIfGoodParameters()
{
Approvals.Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/CallbackSignatureShouldMatchMockedMethod.cs")));
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/CallbackSignatureShouldMatchMockedMethod.cs")));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
#pragma warning disable SA1402 // File may only contain a single class
#pragma warning disable SA1502 // Element must not be on a single line
#pragma warning disable SA1602 // Undocumented enum values
#pragma warning disable SA1649 // File name must match first type name
#pragma warning disable RCS1021 // Simplify lambda expression
#pragma warning disable RCS1163 // Unused parameter
#pragma warning disable RCS1213 // Remove unused member declaration
#pragma warning disable IDE0051 // Unused private member
#pragma warning disable IDE0059 // Unnecessary value assignment
#pragma warning disable IDE0060 // Unused parameter
namespace CallbackSignatureShouldMatchMockedMethod
{
using System;
using System.Collections.Generic;
using Moq;
internal interface IFoo
{
int Do(string s);
int Do(int i, string s, DateTime dt);
int Do(List<string> l);
}
internal class MyUnitTests
{
private void TestBadCallbacks()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}
private void TestGoodSetupAndParameterlessCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback(() => { });
}
private void TestGoodSetupAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}
private void TestGoodSetupAndReturnsAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns(0).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns(0).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns(0).Callback((List<string> l) => { });
}
private void MyGoodSetupAndReturns()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns((string s) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns((int i, string s, DateTime dt) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns((List<string> l) => { return 0; });
}
}
#pragma warning disable SA1402 // File may only contain a single class
#pragma warning disable SA1502 // Element must not be on a single line
#pragma warning disable SA1602 // Undocumented enum values
#pragma warning disable SA1649 // File name must match first type name
#pragma warning disable RCS1021 // Simplify lambda expression
#pragma warning disable RCS1163 // Unused parameter
#pragma warning disable RCS1213 // Remove unused member declaration
#pragma warning disable IDE0051 // Unused private member
#pragma warning disable IDE0059 // Unnecessary value assignment
#pragma warning disable IDE0060 // Unused parameter
namespace CallbackSignatureShouldMatchMockedMethod
{
using System;
using System.Collections.Generic;
using Moq;

internal interface IFoo
{
int Do(string s);

int Do(int i, string s, DateTime dt);

int Do(List<string> l);
}

internal class MyUnitTests
{
private void TestBadCallbacks()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}

private void TestGoodSetupAndParameterlessCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback(() => { });
}

private void TestGoodSetupAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}

private void TestGoodSetupAndReturnsAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns(0).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns(0).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns(0).Callback((List<string> l) => { });
}

private void MyGoodSetupAndReturns()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns((string s) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns((int i, string s, DateTime dt) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns((List<string> l) => { return 0; });
}
}
}
Loading

0 comments on commit 9d37487

Please sign in to comment.