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

Fix interceptors for enums in global namespace #113

Merged
merged 2 commits into from
Oct 23, 2024
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
4 changes: 2 additions & 2 deletions src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ static file class EnumInterceptors
sb.AppendLine(
$$"""
public static string {{toIntercept.ExtensionTypeName}}ToString(this global::System.Enum value)
=> global::{{toIntercept.EnumNamespace}}.{{toIntercept.ExtensionTypeName}}.ToStringFast((global::{{toIntercept.FullyQualifiedName}})value);
=> global::{{toIntercept.EnumNamespace}}{{(string.IsNullOrEmpty(toIntercept.EnumNamespace) ? "" : ".")}}{{toIntercept.ExtensionTypeName}}.ToStringFast((global::{{toIntercept.FullyQualifiedName}})value);

""");
}
Expand All @@ -968,7 +968,7 @@ static file class EnumInterceptors
sb.AppendLine(
$$"""
public static bool {{toIntercept.ExtensionTypeName}}HasFlag(this global::System.Enum value, global::System.Enum flag)
=> global::{{toIntercept.EnumNamespace}}.{{toIntercept.ExtensionTypeName}}.HasFlagFast((global::{{toIntercept.FullyQualifiedName}})value, (global::{{toIntercept.FullyQualifiedName}})flag);
=> global::{{toIntercept.EnumNamespace}}{{(string.IsNullOrEmpty(toIntercept.EnumNamespace) ? "" : ".")}}{{toIntercept.ExtensionTypeName}}.HasFlagFast((global::{{toIntercept.FullyQualifiedName}})value, (global::{{toIntercept.FullyQualifiedName}})flag);

""");
}
Expand Down
85 changes: 85 additions & 0 deletions tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,91 @@ public void MyTest()
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task CanInterceptEnumInGlobalNamespace()
{
const string input =
"""
using NetEscapades.EnumGenerators;
using System;

[EnumExtensions]
[Flags]
internal enum MyEnum
{
First = 1,
Second = 2,
Third = 4,
}

internal class InnerClass
{
public MyEnum _field = default;
public MyEnum Property {get;set;} = default;
public void MyTest()
{
var myValue = MyEnum.Second;
var var1 = myValue.HasFlag(MyEnum.First);
var var2 = MyEnum.Second.HasFlag(myValue);
var var3 = Property.HasFlag(MyEnum.First);
var var4 = _field.HasFlag(MyEnum.First);
var var5 = myValue.ToString();
}
}
""";
var (diagnostics, output) =
TestHelpers.GetGeneratedTrees<EnumGenerator, TrackingNames>(new(_analyzerOpts, _features, input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task CanInterceptEnumInDifferentNamespace()
{
const string input =
"""
using NetEscapades.EnumGenerators;
using System;

namespace Foo
{
[EnumExtensions(ExtensionClassNamespace = "Bar", ExtensionClassName = "SomethingElse")]
[Flags]
internal enum MyEnum
{
First = 1,
Second = 2,
Third = 4,
}
}

namespace Baz
{
using Foo;
internal class InnerClass
{
public MyEnum _field = default;
public MyEnum Property {get;set;} = default;
public void MyTest()
{
var myValue = MyEnum.Second;
var var1 = myValue.HasFlag(MyEnum.First);
var var2 = MyEnum.Second.HasFlag(myValue);
var var3 = Property.HasFlag(MyEnum.First);
var var4 = _field.HasFlag(MyEnum.First);
var var5 = myValue.ToString();
}
}
}
""";
var (diagnostics, output) =
TestHelpers.GetGeneratedTrees<EnumGenerator, TrackingNames>(new(_analyzerOpts, _features, input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task CanInterceptMultipleEnumsToString()
{
Expand Down
Loading
Loading