Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ce30878
Remove #pragmas from test data
MattKotsenas Jun 4, 2024
319ff4a
Combine AbstractClass.Good.cs and AbstractClass.Bad.cs
MattKotsenas Jun 4, 2024
e448369
Split AbstractClass tests up per-test case
MattKotsenas Jun 5, 2024
33d2b08
Remove dead code from AbstractClass tests
MattKotsenas Jun 5, 2024
735f015
Inline AbstractClass test data into test
MattKotsenas Jun 5, 2024
6104a0e
Move duplicated AbstractClass test
MattKotsenas Jun 5, 2024
2aa7ab3
Split AsAcceptOnlyInterface tests up per-test case
MattKotsenas Jun 5, 2024
ef07098
Remove dead code from AsAcceptOnlyInterface tests
MattKotsenas Jun 5, 2024
ea75457
Inline AsAcceptOnlyInterface test data into tests
MattKotsenas Jun 5, 2024
5ddb662
Split CallbackSignatureShouldMatchMockedMethod tests up per-test case
MattKotsenas Jun 5, 2024
64fb31e
Inline CallbackSignatureShouldMatchMockedMethod test data into tests
MattKotsenas Jun 5, 2024
b1a0819
Simplify names for ConstructorArgumentsShouldMatch tests
MattKotsenas Jun 5, 2024
9c77ff4
Split ConstructorArgumentsShouldMatch tests up per-test case
MattKotsenas Jun 5, 2024
203c546
Inline ConstructorArgumentsShouldMatch test data into tests
MattKotsenas Jun 5, 2024
597d8de
Simplify names for NoConstructorArgumentsForInterfaceMock tests
MattKotsenas Jun 5, 2024
b1db62d
Split NoConstructorArgumentsForInterfaceMock_1
MattKotsenas Jun 5, 2024
37c30c4
Inline NoConstructorAgrumentsForInterfaceMock_1
MattKotsenas Jun 5, 2024
c9e1e30
Add TODOs for questionable test practices in AbstractClass tests
MattKotsenas Jun 5, 2024
66a3a3b
Split NoConstructorArgumentsForInterfaceMock_2
MattKotsenas Jun 5, 2024
5047560
Inline NoConstructorAgrumentsForInterfaceMock_2
MattKotsenas Jun 5, 2024
2462e7f
Split NoMethodsInPropertySetup
MattKotsenas Jun 5, 2024
dccc740
Inline NoMethodsInPropertySetup
MattKotsenas Jun 5, 2024
f2922b6
Simplify names for NoSealedClassMocks
MattKotsenas Jun 5, 2024
8870a56
Split NoSealedClassMocks
MattKotsenas Jun 5, 2024
c9e24c4
Inline NoSealedClassMocks
MattKotsenas Jun 5, 2024
fb5741c
Simplify SetupOnForOverridableMemebers
MattKotsenas Jun 5, 2024
d020985
Split SetupOnlyForOverridableMembers tests
MattKotsenas Jun 5, 2024
558905e
Simplify SetupOnForOverridableMemebers tests
MattKotsenas Jun 5, 2024
51b70a9
Inline SetupOnlyforOverridableMemebers
MattKotsenas Jun 5, 2024
11bde89
Simplify SetupShouldNotIncludeAsyncResult
MattKotsenas Jun 5, 2024
0c118b6
Split SetupShouldNotIncludeAsyncResult tests
MattKotsenas Jun 5, 2024
db0b3cb
Inline SetupShouldNotIncludeAsyncResult tests
MattKotsenas Jun 5, 2024
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
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