Skip to content
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
6 changes: 0 additions & 6 deletions Source/Mockolate.Analyzers/MockabilityAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ private static bool IsMockable(ITypeSymbol typeSymbol, [NotNullWhen(false)] out
return false;
}

if (typeSymbol.IsStatic)
{
reason = "type is static";
return false;
}

if (typeSymbol.TypeKind != TypeKind.Interface &&
typeSymbol.TypeKind != TypeKind.Class &&
typeSymbol.TypeKind != TypeKind.Delegate)
Expand Down
76 changes: 73 additions & 3 deletions Tests/Mockolate.Analyzers.Tests/MockabilityAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ public void MyTest()
"""
);

[Fact]
public async Task WhenMockingArray_ShouldBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
$$"""
using System;

{{GeneratedPrefix}}

namespace MyNamespace
{
public interface IMyInterface
{
}

public class MyClass
{
public void MyTest()
{
Mockolate.Mock.Create<{|#0:IMyInterface[]|}>();
}
}
}
""",
Verifier.Diagnostic(Rules.MockabilityRule)
.WithLocation(0)
.WithArguments("MyNamespace.IMyInterface[]", "type kind 'Array' is not supported")
);

[Fact]
public async Task WhenMockingClass_ShouldNotBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
Expand Down Expand Up @@ -119,11 +147,14 @@ public interface IGlobalInterface
void DoSomething();
}

public class MyClass
namespace MyNamespace
{
public void MyTest()
public class MyClass
{
Mockolate.Mock.Create<{|#0:IGlobalInterface|}>();
public void MyTest()
{
Mockolate.Mock.Create<{|#0:IGlobalInterface|}>();
}
}
}
""",
Expand Down Expand Up @@ -158,6 +189,45 @@ public void MyTest()
"""
);

[Fact]
public async Task WhenMockingMockGeneratorWithoutAttributes_ShouldNotBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
$$"""
using System;

{{GeneratedPrefix}}

namespace MyNamespace
{
public class MyClass
{
public void MyTest()
{
Mockolate.MyMock.Create();
}
}
}
namespace Mockolate
{
public interface IGlobalInterface
{
void DoSomething();
}

[AttributeUsage(AttributeTargets.Method)]
internal class MockGeneratorAttribute : Attribute
{
}

public static class MyMock
{
[MockGenerator]
public static IGlobalInterface Create() => default!;
}
}
"""
);

[Fact]
public async Task WhenMockingRecord_ShouldBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
Expand Down
32 changes: 32 additions & 0 deletions Tests/Mockolate.Analyzers.Tests/WrappabilityAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ public void MyTest()
.WithArguments("MyNamespace.MyBaseClass", "only interface types can be wrapped")
);

[Fact]
public async Task WhenWrappingGenericInterface_ShouldNotBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
$$"""
using System;

{{GeneratedPrefix}}

namespace MyNamespace
{
public interface IMyInterface<T>
{
void DoSomething(T value);
}

public class MyImplementation : IMyInterface<int>
{
public void DoSomething(int value) { }
}

public class MyClass
{
public void MyTest()
{
MyImplementation instance = new MyImplementation();
Mockolate.Mock.Wrap<IMyInterface<int>>(instance);
}
}
}
"""
);

[Fact]
public async Task WhenWrappingInterface_ShouldNotBeFlagged() => await Verifier
.VerifyAnalyzerAsync(
Expand Down
Loading