Skip to content

Commit

Permalink
Refactoring of the unit tests to use Moq library from NuGet packages (f…
Browse files Browse the repository at this point in the history
…ixes #23).
  • Loading branch information
GillesTourreau committed Jun 24, 2024
1 parent da2f494 commit 27ed2fe
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace PosInformatique.Moq.Analyzers.Tests
{
using System.Threading.Tasks;
using Xunit;
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<
AsMustBeUsedWithInterfaceAnalyzer,
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
using Verifier = MoqCSharpAnalyzerVerifier<AsMustBeUsedWithInterfaceAnalyzer>;

public class AsMustBeUsedWithInterfaceAnalyzerTest
{
Expand Down Expand Up @@ -38,9 +36,9 @@ public class C
public interface I
{
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand All @@ -67,9 +65,9 @@ public class C
public class C2
{
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand Down Expand Up @@ -105,9 +103,9 @@ public class OtherClass
{
public void GenericMethodNotAs<T>() { }
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand Down Expand Up @@ -140,7 +138,7 @@ public void As<TInterface>() { }
}
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerWithNoMoqLibraryAsync(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace PosInformatique.Moq.Analyzers.Tests
{
using System.Threading.Tasks;
using Xunit;
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<
CallBackDelegateMustMatchMockedMethodAnalyzer,
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
using Verifier = MoqCSharpAnalyzerVerifier<CallBackDelegateMustMatchMockedMethodAnalyzer>;

public class CallBackDelegateMustMatchMockedMethodAnalyzerTest
{
Expand All @@ -31,46 +29,30 @@ public void TestMethod()
mock1.Setup(m => m.TestMethod())
.Callback(() => { })
.Throws()
.Callback(() => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethod(default))
.Callback((string x) => { })
.Throws()
.Callback((string x) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethod(default, default))
.Callback((string x, int y) => { })
.Throws()
.Callback((string x, int y) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestGenericMethod(1234))
.Callback((int x) => { })
.Throws()
.Callback((int x) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
.Callback((object x) => { })
.Throws()
.Callback((object x) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethodReturn())
.Callback(() => { })
.Throws()
.Callback(() => { })
.Returns(1234);
mock1.Setup(m => m.TestMethodReturn(default))
.Callback((string x) => { })
.Throws()
.Callback((string x) => { })
.Returns(1234);
mock1.Setup(m => m.TestMethodReturn(default, default))
.Callback((string x, int y) => { })
.Throws()
.Callback((string x, int y) => { })
.Returns(1234);
mock1.Setup(m => m.TestMethod(default))
.Callback()
.Throws()
.Callback();
// Special case add a Verify() method inside the Callback() method.
var innerMock = new Mock<I>();
Expand All @@ -95,10 +77,9 @@ public interface I
int TestMethodReturn(string a, int b);
}
}
" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand All @@ -118,47 +99,33 @@ public void TestMethod()
mock1.Setup(m => m.TestMethod())
.Callback([|(int too, int much, int parameters)|] => { })
.Throws()
.Callback([|(int too, int much, int parameters)|] => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethod(default))
.Callback([|()|] => { })
.Throws()
.Callback([|()|] => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethod(default))
.Callback(([|int otherType|]) => { })
.Throws()
.Callback(([|int otherType|]) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethod(default))
.Callback([|(int too, int much, int parameters)|] => { })
.Throws()
.Callback([|(int too, int much, int parameters)|] => { });
.Throws(new Exception());
mock1.Setup(m => m.TestGenericMethod(1234))
.Callback(([|string x|]) => { })
.Throws()
.Callback(([|string x|]) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
.Callback(([|string x|]) => { })
.Throws()
.Callback(([|string x|]) => { });
.Throws(new Exception());
mock1.Setup(m => m.TestMethodReturn())
.Callback([|(int too, int much, int parameters)|] => { })
.Throws()
.Callback([|(int too, int much, int parameters)|] => { })
.Returns(1234);
mock1.Setup(m => m.TestMethodReturn(default))
.Callback([|()|] => { })
.Throws()
.Callback([|()|] => { })
.Returns(1234);
mock1.Setup(m => m.TestMethodReturn(default))
.Callback(([|int otherType|]) => { })
.Throws()
.Callback(([|int otherType|]) => { })
.Returns(1234);
mock1.Setup(m => m.TestMethodReturn(default))
.Callback([|(int too, int much, int parameters)|] => { })
.Throws()
.Callback([|(int too, int much, int parameters)|] => { })
.Returns(1234);
}
Expand All @@ -180,10 +147,9 @@ public interface I
int TestMethodReturn(string a, int b);
}
}
" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand Down Expand Up @@ -223,7 +189,7 @@ public void Setup(Action<T> act) { }
public enum MockBehavior { Strict, Loose }
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerWithNoMoqLibraryAsync(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
namespace PosInformatique.Moq.Analyzers.Tests
{
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<
ConstructorArgumentCannotBePassedForInterfaceAnalyzer,
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
using Verifier = MoqCSharpAnalyzerVerifier<ConstructorArgumentCannotBePassedForInterfaceAnalyzer>;

public class ConstructorArgumentCannotBePassedForInterfaceAnalyzerTest
{
Expand All @@ -33,9 +30,9 @@ public void TestMethod()
public interface I
{
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand All @@ -57,17 +54,15 @@ public void TestMethod()
public interface I
{
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task Interface_WithoutBehaviorStrict()
{
var context = new CSharpAnalyzerTest<ConstructorArgumentCannotBePassedForInterfaceAnalyzer, DefaultVerifier>();

context.TestCode = @"
var source = @"
namespace ConsoleApplication1
{
using Moq;
Expand All @@ -83,21 +78,19 @@ public void TestMethod()
public interface I
{
}
}" + MoqLibrary.Code;

context.ExpectedDiagnostics.Add(new DiagnosticResult(ConstructorArgumentCannotBePassedForInterfaceAnalyzer.Rule)
.WithLocation(0).WithArguments("1")
.WithLocation(1).WithArguments("2"));
}";

await context.RunAsync();
await Verifier.VerifyAnalyzerAsync(
source,
new DiagnosticResult(ConstructorArgumentCannotBePassedForInterfaceAnalyzer.Rule)
.WithLocation(0).WithArguments("1")
.WithLocation(1).WithArguments("2"));
}

[Fact]
public async Task Interface_WithBehaviorStrict()
{
var context = new CSharpAnalyzerTest<ConstructorArgumentCannotBePassedForInterfaceAnalyzer, DefaultVerifier>();

context.TestCode = @"
var source = @"
namespace ConsoleApplication1
{
using Moq;
Expand All @@ -113,13 +106,13 @@ public void TestMethod()
public interface I
{
}
}" + MoqLibrary.Code;

context.ExpectedDiagnostics.Add(new DiagnosticResult(ConstructorArgumentCannotBePassedForInterfaceAnalyzer.Rule)
.WithLocation(0).WithArguments("1")
.WithLocation(1).WithArguments("2"));
}";

await context.RunAsync();
await Verifier.VerifyAnalyzerAsync(
source,
new DiagnosticResult(ConstructorArgumentCannotBePassedForInterfaceAnalyzer.Rule)
.WithLocation(0).WithArguments("1")
.WithLocation(1).WithArguments("2"));
}

[Fact]
Expand All @@ -141,9 +134,9 @@ public void TestMethod()
public abstract class C
{
}
}" + MoqLibrary.Code;
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerAsync(source);
}

[Fact]
Expand Down Expand Up @@ -177,7 +170,7 @@ public Mock(MockBehavior _, int a, int b, int c) { }
public enum MockBehavior { Strict, Loose }
}";

await Verify.VerifyAnalyzerAsync(source);
await Verifier.VerifyAnalyzerWithNoMoqLibraryAsync(source);
}
}
}
Loading

0 comments on commit 27ed2fe

Please sign in to comment.