Skip to content

Commit

Permalink
Fix Error CS0426 : The type name 'TestEnum' does not exist in the typ…
Browse files Browse the repository at this point in the history
…e 'Foo' (#62)

Occurs when there's a class that matches the namespace name

Fixes #61
  • Loading branch information
andrewlock authored Jun 4, 2023
1 parent 2f222ec commit 3479831
Show file tree
Hide file tree
Showing 16 changed files with 1,320 additions and 856 deletions.
116 changes: 59 additions & 57 deletions src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using Foo;
using Xunit;

namespace NetEscapades.EnumGenerators.IntegrationTests;

public class EnumInFooExtensionsTests : ExtensionTests<EnumInFoo>
{
public static TheoryData<EnumInFoo> ValidEnumValues() => new()
{
EnumInFoo.First,
EnumInFoo.Second,
(EnumInFoo)3,
};

public static TheoryData<string> ValuesToParse() => new()
{
"First",
"Second",
"2nd",
"2ND",
"first",
"SECOND",
"3",
"267",
"-267",
"2147483647",
"3000000000",
"Fourth",
"Fifth",
};

protected override string ToStringFast(EnumInFoo value) => value.ToStringFast();
protected override bool IsDefined(EnumInFoo value) => EnumInFooExtensions.IsDefined(value);
protected override bool IsDefined(string name, bool allowMatchingMetadataAttribute) => EnumInFooExtensions.IsDefined(name, allowMatchingMetadataAttribute: false);
#if READONLYSPAN
protected override bool IsDefined(in ReadOnlySpan<char> name, bool allowMatchingMetadataAttribute) => EnumInFooExtensions.IsDefined(name, allowMatchingMetadataAttribute: false);
#endif
protected override bool TryParse(string name, out EnumInFoo parsed, bool ignoreCase, bool allowMatchingMetadataAttribute)
=> EnumInFooExtensions.TryParse(name, out parsed, ignoreCase);
#if READONLYSPAN
protected override bool TryParse(in ReadOnlySpan<char> name, out EnumInFoo parsed, bool ignoreCase, bool allowMatchingMetadataAttribute)
=> EnumInFooExtensions.TryParse(name, out parsed, ignoreCase);
#endif

[Theory]
[MemberData(nameof(ValidEnumValues))]
public void GeneratesToStringFast(EnumInFoo value) => GeneratesToStringFastTest(value);

[Theory]
[MemberData(nameof(ValidEnumValues))]
public void GeneratesIsDefined(EnumInFoo value) => GeneratesIsDefinedTest(value);

[Theory]
[MemberData(nameof(ValuesToParse))]
public void GeneratesIsDefinedUsingName(string name) => GeneratesIsDefinedTest(name, allowMatchingMetadataAttribute: false);

#if READONLYSPAN
[Theory]
[MemberData(nameof(ValuesToParse))]
public void GeneratesIsDefinedUsingNameAsSpan(string name) => GeneratesIsDefinedTest(name.AsSpan(), allowMatchingMetadataAttribute: false);
#endif

[Theory]
[MemberData(nameof(ValuesToParse))]
public void GeneratesTryParse(string name) => GeneratesTryParseTest(name, ignoreCase: false, allowMatchingMetadataAttribute: false);

#if READONLYSPAN
[Theory]
[MemberData(nameof(ValuesToParse))]
public void GeneratesTryParseUsingSpan(string name) => GeneratesTryParseTest(name.AsSpan(), ignoreCase: false, allowMatchingMetadataAttribute: false);
#endif

[Theory]
[MemberData(nameof(ValuesToParse))]
public void GeneratesTryParseIgnoreCase(string name) => GeneratesTryParseTest(name, ignoreCase: true, allowMatchingMetadataAttribute: false);

[Fact]
public void GeneratesGetValues() => GeneratesGetValuesTest(EnumInFooExtensions.GetValues());

[Fact]
public void GeneratesGetNames() => base.GeneratesGetNamesTest(EnumInFooExtensions.GetNames());
}
20 changes: 20 additions & 0 deletions tests/NetEscapades.EnumGenerators.IntegrationTests/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ public enum EnumInSystem
}
}

namespace Foo
{
using NetEscapades.EnumGenerators;

// causes Error CS0426 : The type name 'TestEnum' does not exist in the type 'Foo'.
// workaround is to use global prefix

public class Foo
{
}

[EnumExtensions]
public enum EnumInFoo
{
First = 0,
Second = 1,
Third = 2,
}
}

namespace NetEscapades.EnumGenerators.IntegrationTests
{
[EnumExtensions]
Expand Down
23 changes: 23 additions & 0 deletions tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,27 @@ public enum MyEnum
.DisableRequireUniquePrefix()
.UseDirectory("Snapshots");
}

[Fact]
public Task CanHandleNamespaceAndClassNameAreTheSame()
{
const string input = @"using NetEscapades.EnumGenerators;
using System.ComponentModel.DataAnnotations;
namespace Foo
{
public class Foo {}
[EnumExtensions]
public enum TestEnum
{
Value1
}
}";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(input);

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

}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 3479831

Please sign in to comment.