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 Error CS0426 : The type name 'TestEnum' does not exist in the type 'Foo' #62

Merged
merged 1 commit into from
Jun 4, 2023
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
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