Skip to content

Commit

Permalink
Fix interceptors for enums in global namespace (#113)
Browse files Browse the repository at this point in the history
* Add tests for other namespace situations

* Fix generation in global namspace
  • Loading branch information
andrewlock authored Oct 23, 2024
1 parent 8941586 commit e87b3d2
Show file tree
Hide file tree
Showing 4 changed files with 1,480 additions and 2 deletions.
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

0 comments on commit e87b3d2

Please sign in to comment.