Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for fully qualified namespace #34

Merged
merged 2 commits into from
Jan 7, 2025
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
1 change: 1 addition & 0 deletions MockMe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "wiki", "wiki", "{4723EF4B-98A5-4F3D-BEB9-0632A3939D4D}"
ProjectSection(SolutionItems) = preProject
wiki\AdvancedUsage.md = wiki\AdvancedUsage.md
wiki\CommonErrors.md = wiki\CommonErrors.md
wiki\HowDoesItWork.md = wiki\HowDoesItWork.md
wiki\QuickStart.md = wiki\QuickStart.md
EndProjectSection
Expand Down
21 changes: 18 additions & 3 deletions src/MockMe.Generator/MockStoreGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
methodDeclarations.Collect()
);

#if DEBUG
//System.Diagnostics.Debugger.Launch();
#endif
context.RegisterSourceOutput(
compilationAndMethods,
(ctx, source) =>
Expand Down Expand Up @@ -161,10 +163,23 @@ ImmutableArray<InvocationExpressionSyntax> methods
HashSet<ITypeSymbol> usedSymbols = [];
foreach (var method in methods.Distinct())
{
if (method.Expression is not MemberAccessExpressionSyntax memberAccessExpression)
{
continue;
}

var identifierName =
memberAccessExpression.Expression as IdentifierNameSyntax
?? (memberAccessExpression.Expression as MemberAccessExpressionSyntax)?.Name
as IdentifierNameSyntax;

if (identifierName is null)
{
continue;
}

if (
method.Expression is MemberAccessExpressionSyntax memberAccess
&& memberAccess.Expression is IdentifierNameSyntax identifierName
&& identifierName.Identifier.Text == StoreClassName
identifierName.Identifier.Text == StoreClassName
&& method.ArgumentList.Arguments.Count == 1
&& method.ArgumentList.Arguments[0].Expression
is DefaultExpressionSyntax defaultExpression
Expand Down
33 changes: 33 additions & 0 deletions tests/MockMe.Tests/TestWithFullyQualifiedNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace MockMe.Tests;

public class NewClass
{
public string Name => throw new NotImplementedException();

public double GetRandomNum() => throw new NotImplementedException();

public Task ExpensiveOp(int num1, int num2) => throw new NotImplementedException();
}

public class TestWithFullyQualifiedNamespace
{
[Fact]
public void TestWithMockMe()
{
#pragma warning disable IDE0002 // name can be simplified
var mock = MockMe.Mock.Me(default(NewClass));
#pragma warning restore IDE0002

mock.Setup.Name.Get().Returns("New Class Name");
mock.Setup.GetRandomNum().Returns(99.0);

NewClass newClass = mock;

Assert.Equal("New Class Name", newClass.Name);
Assert.Equal(99.0, newClass.GetRandomNum());
}
}
21 changes: 21 additions & 0 deletions wiki/CommonErrors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Common Errors

> If you run into an error, check here first. If you don't see the error that you are experiencing, or if the recommended resolution doesn't work for you, then please [open an issue](https://github.com/connorivy/MockMe/issues/).

## Cannot convert from 'MyTypeToMock' to 'MockMe.DummyClass'

#### How to resolve
You need to trigger the source generators. This can be done is a couple ways,
1. Save the current file. (This will USUALLY be enough)
2. If that didn't work, the build the test project.

#### Why it occurs
This error occurs when you initially write `Mock.Me(default(MyTypeToMock))` and the source generator has yet to trigger. There is an initial overload of Mock.Me that just serves the purpose of helping to fill in intellisense. The correct method, the one that will create a mock of your object, will not exist until the source generators have created it.

## Mock object is missing methods or properties that exist in the mocked class

#### How to resolve
Rebuild (build alone does not always suffice) the test project.

#### Why it occurs
Even though it looks like the class member doesn't exist, it does. The issue is that intellisense can be a bit slow to pick up all the members on the source generated classes.
Loading