Skip to content

Commit

Permalink
Refactor tests to simplify and inline test cases (#57)
Browse files Browse the repository at this point in the history
* Remove #pragmas from test data

* Combine AbstractClass.Good.cs and AbstractClass.Bad.cs

* Split AbstractClass tests up per-test case

* Remove dead code from AbstractClass tests

* Inline AbstractClass test data into test

* Move duplicated AbstractClass test

* Split AsAcceptOnlyInterface tests up per-test case

* Remove dead code from AsAcceptOnlyInterface tests

* Inline AsAcceptOnlyInterface test data into tests

* Split CallbackSignatureShouldMatchMockedMethod tests up per-test case

* Inline CallbackSignatureShouldMatchMockedMethod test data into tests

* Simplify names for ConstructorArgumentsShouldMatch tests

* Split ConstructorArgumentsShouldMatch tests up per-test case

* Inline ConstructorArgumentsShouldMatch test data into tests

* Simplify names for NoConstructorArgumentsForInterfaceMock tests

* Split NoConstructorArgumentsForInterfaceMock_1

* Inline NoConstructorAgrumentsForInterfaceMock_1

* Add TODOs for questionable test practices in AbstractClass tests

* Split NoConstructorArgumentsForInterfaceMock_2

* Inline NoConstructorAgrumentsForInterfaceMock_2

* Split NoMethodsInPropertySetup

* Inline NoMethodsInPropertySetup

* Simplify names for NoSealedClassMocks

* Split NoSealedClassMocks

* Inline NoSealedClassMocks

* Simplify SetupOnForOverridableMemebers

* Split SetupOnlyForOverridableMembers tests

* Simplify SetupOnForOverridableMemebers tests

* Inline SetupOnlyforOverridableMemebers

* Simplify SetupShouldNotIncludeAsyncResult

* Split SetupShouldNotIncludeAsyncResult tests

* Inline SetupShouldNotIncludeAsyncResult tests
  • Loading branch information
MattKotsenas authored Jun 5, 2024
1 parent d82c8ca commit b33a321
Show file tree
Hide file tree
Showing 74 changed files with 1,778 additions and 1,065 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Diagnostic 1
Id: Moq1002
Location: SourceFile(Test0.cs[608..614))
Highlight: ("42")
Lines: var mock = new Mock<AbstractGenericClassWithCtor<object>>("42");
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 2
Id: Moq1002
Location: SourceFile(Test0.cs[788..798))
Highlight: ("42", 42)
Lines: var mock1 = new Mock<AbstractGenericClassWithCtor<object>>("42", 42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 3
Id: Moq1002
Location: SourceFile(Test0.cs[934..938))
Highlight: (42)
Lines: var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Diagnostic 1
Id: Moq1002
Location: SourceFile(Test0.cs[526..532))
Highlight: ("42")
Lines: var mock = new Mock<AbstractClassWithCtor>("42");
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 2
Id: Moq1002
Location: SourceFile(Test0.cs[691..701))
Highlight: ("42", 42)
Lines: var mock1 = new Mock<AbstractClassWithCtor>("42", 42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 3
Id: Moq1002
Location: SourceFile(Test0.cs[822..826))
Highlight: (42)
Lines: var mock2 = new Mock<AbstractClassDefaultCtor>(42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
229 changes: 224 additions & 5 deletions Source/Moq.Analyzers.Test/AbstractClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,234 @@ namespace Moq.Analyzers.Test;

public class AbstractClassTests : DiagnosticVerifier
{
// TODO: Review use of `.As<>()` in the test cases. It is not clear what purpose it serves.
[Fact]
public Task ShouldPassIfGoodParametersAndFailOnTypeMismatch()
public Task ShouldFailOnGenericTypesWithMismatchArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
File.ReadAllText("Data/AbstractClass.Good.cs"),
File.ReadAllText("Data/AbstractClass.Bad.cs")
]
));
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.GenericMistmatchArgs;
internal abstract class AbstractGenericClassDefaultCtor<T>
{
protected AbstractGenericClassDefaultCtor()
{
}
}
internal abstract class AbstractGenericClassWithCtor<T>
{
protected AbstractGenericClassWithCtor(int a)
{
}
protected AbstractGenericClassWithCtor(int a, string b)
{
}
}
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");
// 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);
// The class has a ctor but does not take any arguments
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42);
}
}
"""
]));
}

[Fact]
public Task ShouldPassOnGenericTypesWithNoArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.GenericNoArgs;
internal abstract class AbstractGenericClassDefaultCtor<T>
{
protected AbstractGenericClassDefaultCtor()
{
}
}
internal class MyUnitTests
{
private void TestForBaseGenericNoArgs()
{
var mock = new Mock<AbstractGenericClassDefaultCtor<object>>();
mock.As<AbstractGenericClassDefaultCtor<object>>();
var mock1 = new Mock<AbstractGenericClassDefaultCtor<object>>();
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(MockBehavior.Default);
}
}
"""
]));
}

[Fact]
public Task ShouldFailOnMismatchArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.MismatchArgs;
internal abstract class AbstractClassDefaultCtor
{
protected AbstractClassDefaultCtor()
{
}
}
internal abstract class AbstractClassWithCtor
{
protected AbstractClassWithCtor(int a)
{
}
protected AbstractClassWithCtor(int a, string b)
{
}
}
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");
// The class has a ctor with two arguments [Int32, String], but they are passed in reverse order
var mock1 = new Mock<AbstractClassWithCtor>("42", 42);
// The class has a ctor but does not take any arguments
var mock2 = new Mock<AbstractClassDefaultCtor>(42);
}
}
"""
]));
}

[Fact]
public Task ShouldPassWithNoArgs()
{
return Verify(VerifyCSharpDiagnostic(
[
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.NoArgs;
internal abstract class AbstractClassDefaultCtor
{
protected AbstractClassDefaultCtor()
{
}
}
internal class MyUnitTests
{
// Base case that we can handle abstract types
private void TestForBaseNoArgs()
{
var mock = new Mock<AbstractClassDefaultCtor>();
mock.As<AbstractClassDefaultCtor>();
}
}
"""
]));
}

[Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")]
public Task ShouldFailWithArgsNonePassed()
{
return Verify(VerifyCSharpDiagnostic(
[
"""
namespace Moq.Analyzers.Test.Data.AbstractClass.WithArgsNonePassed;
internal abstract class AbstractClassWithCtor
{
protected AbstractClassWithCtor(int a)
{
}
protected AbstractClassWithCtor(int a, string b)
{
}
}
internal class MyUnitTests
{
// This is syntatically not allowed by C#, but you can do it with Moq
private void TestForBaseWithArgsNonePassed()
{
var mock = new Mock<AbstractClassWithCtor>();
mock.As<AbstractClassWithCtor>();
var mock2 = new Mock<AbstractClassDefaultCtor>(MockBehavior.Default);
}
}
"""
]));
}

[Fact]
public Task ShouldPassWithArgsPassed()
{
return Verify(VerifyCSharpDiagnostic(
[
"""
namespace Moq.Analyzers.Test.DataAbstractClass.WithArgsPassed;
internal abstract class AbstractGenericClassWithCtor<T>
{
protected AbstractGenericClassWithCtor(int a)
{
}
protected AbstractGenericClassWithCtor(int a, string b)
{
}
}
internal abstract class AbstractClassWithCtor
{
protected AbstractClassWithCtor(int a)
{
}
protected AbstractClassWithCtor(int a, string b)
{
}
}
internal class MyUnitTests
{
private void TestForBaseWithArgsPassed()
{
var mock = new Mock<AbstractClassWithCtor>(42);
var mock2 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42);
var mock3 = new Mock<AbstractClassWithCtor>(42, "42");
var mock4 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42, "42");
var mock5 = new Mock<AbstractGenericClassWithCtor<object>>(42);
var mock6 = new Mock<AbstractGenericClassWithCtor<object>>(MockBehavior.Default, 42);
}
}
"""
]));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Diagnostic 1
Id: Moq1300
Location: SourceFile(Test0.cs[292..307))
Highlight: BaseSampleClass
Lines: mock.As<BaseSampleClass>();
Severity: Error
Message: Mock.As() should take interfaces only

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Diagnostic 1
Id: Moq1300
Location: SourceFile(Test0.cs[433..443))
Highlight: OtherClass
Lines: mock.As<OtherClass>();
Severity: Error
Message: Mock.As() should take interfaces only

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Loading

0 comments on commit b33a321

Please sign in to comment.