Skip to content

Commit d28e47b

Browse files
committed
Format enum now has the same visibility as EnumClass
Fixes #3
1 parent 119d779 commit d28e47b

File tree

8 files changed

+228
-25
lines changed

8 files changed

+228
-25
lines changed

Src/FastEnum.Tests.CodeGen/BugTests.cs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,48 @@ public class BugTests
1010
[Fact]
1111
public void TestUlongBug()
1212
{
13-
string code = """
14-
[FastEnum]
15-
public enum TestEnum : ulong
16-
{
17-
None = 0,
18-
Max = ulong.MaxValue
19-
}
20-
""";
13+
const string code = """
14+
[FastEnum]
15+
public enum TestEnum : ulong
16+
{
17+
None = 0,
18+
Max = ulong.MaxValue
19+
}
20+
""";
21+
2122
Assert.NotEmpty(TestHelper.GetGeneratedOutput<EnumGenerator>(code));
2223
}
2324

2425
[Fact]
2526
public void TestNegativeValueBug()
2627
{
27-
string code = """
28-
[FastEnum]
29-
public enum TestEnum : long
30-
{
31-
None = 0,
32-
Min = long.MinValue
33-
}
34-
""";
28+
const string code = """
29+
[FastEnum]
30+
public enum TestEnum : long
31+
{
32+
None = 0,
33+
Min = long.MinValue
34+
}
35+
""";
3536

3637
Assert.NotEmpty(TestHelper.GetGeneratedOutput<EnumGenerator>(code));
3738
}
39+
40+
[Fact]
41+
public async Task TestIssue3()
42+
{
43+
const string code = """
44+
[FastEnum(EnumsClassVisibility = Visibility.Internal, ExtensionClassVisibility = Visibility.Internal)]
45+
public enum TestEnum
46+
{
47+
None = 0,
48+
Value
49+
}
50+
""";
51+
52+
await Verify(TestHelper.GetGeneratedOutput<EnumGenerator>(code))
53+
.UseFileName(nameof(TestIssue3))
54+
.UseDirectory("Issues")
55+
.DisableDiff();
56+
}
3857
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// <auto-generated />
2+
#nullable enable
3+
using System;
4+
5+
[Flags]
6+
internal enum TestEnumFormat : byte
7+
{
8+
None = 0,
9+
Name = 1,
10+
Value = 2,
11+
Default = Name | Value
12+
}
13+
// <auto-generated />
14+
#nullable enable
15+
using System;
16+
17+
internal static partial class Enums
18+
{
19+
internal static partial class TestEnum
20+
{
21+
public const int MemberCount = 2;
22+
public const bool IsFlagEnum = false;
23+
24+
public static string[] GetMemberNames() => _names ??= new string[] {
25+
"None",
26+
"Value"
27+
};
28+
29+
public static global::TestEnum[] GetMemberValues() => _values ??= new global::TestEnum[] {
30+
global::TestEnum.None,
31+
global::TestEnum.Value
32+
};
33+
34+
public static Int32[] GetUnderlyingValues() => _underlyingValues ??= new Int32[] {
35+
0,
36+
1
37+
};
38+
39+
public static bool TryParse(string value, out global::TestEnum result, TestEnumFormat format = TestEnumFormat.Default, StringComparison comparison = StringComparison.Ordinal)
40+
{
41+
if (format.HasFlag(TestEnumFormat.Name))
42+
{
43+
if (value.Equals("None", comparison))
44+
{
45+
result = global::TestEnum.None;
46+
return true;
47+
}
48+
49+
if (value.Equals("Value", comparison))
50+
{
51+
result = global::TestEnum.Value;
52+
return true;
53+
}
54+
}
55+
if (format.HasFlag(TestEnumFormat.Value))
56+
{
57+
if (value.Equals("0", comparison))
58+
{
59+
result = global::TestEnum.None;
60+
return true;
61+
}
62+
63+
if (value.Equals("1", comparison))
64+
{
65+
result = global::TestEnum.Value;
66+
return true;
67+
}
68+
}
69+
result = default;
70+
return false;
71+
}
72+
73+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
74+
public static bool TryParse(ReadOnlySpan<char> value, out global::TestEnum result, TestEnumFormat format = TestEnumFormat.Default, StringComparison comparison = StringComparison.Ordinal)
75+
{
76+
if (format.HasFlag(TestEnumFormat.Name))
77+
{
78+
if (value.Equals("None", comparison))
79+
{
80+
result = global::TestEnum.None;
81+
return true;
82+
}
83+
84+
if (value.Equals("Value", comparison))
85+
{
86+
result = global::TestEnum.Value;
87+
return true;
88+
}
89+
}
90+
if (format.HasFlag(TestEnumFormat.Value))
91+
{
92+
if (value.Equals("0", comparison))
93+
{
94+
result = global::TestEnum.None;
95+
return true;
96+
}
97+
98+
if (value.Equals("1", comparison))
99+
{
100+
result = global::TestEnum.Value;
101+
return true;
102+
}
103+
}
104+
result = default;
105+
return false;
106+
}
107+
108+
public static global::TestEnum Parse(ReadOnlySpan<char> value, TestEnumFormat format = TestEnumFormat.Default, StringComparison comparison = StringComparison.Ordinal)
109+
{
110+
if (!TryParse(value, out global::TestEnum result, format, comparison))
111+
throw new ArgumentOutOfRangeException($"Invalid value: {value.ToString()}");
112+
113+
return result;
114+
}
115+
#endif
116+
117+
public static global::TestEnum Parse(string value, TestEnumFormat format = TestEnumFormat.Default, StringComparison comparison = StringComparison.Ordinal)
118+
{
119+
if (!TryParse(value, out global::TestEnum result, format, comparison))
120+
throw new ArgumentOutOfRangeException($"Invalid value: {value}");
121+
122+
return result;
123+
}
124+
125+
public static bool IsDefined(global::TestEnum input)
126+
{
127+
Int32[] _isDefinedValues = GetUnderlyingValues();
128+
129+
for (int i = 0; i < _isDefinedValues.Length; i++)
130+
{
131+
if (_isDefinedValues[i] == (Int32)input)
132+
return true;
133+
}
134+
135+
return false;
136+
}
137+
138+
private static string[]? _names;
139+
private static global::TestEnum[]? _values;
140+
private static Int32[]? _underlyingValues;
141+
142+
}
143+
}
144+
// <auto-generated />
145+
#nullable enable
146+
using System;
147+
using System.Diagnostics.CodeAnalysis;
148+
149+
internal static partial class TestEnumExtensions
150+
{
151+
public static string GetString(this global::TestEnum value) => value switch
152+
{
153+
global::TestEnum.None => "None",
154+
global::TestEnum.Value => "Value",
155+
_ => value.ToString()
156+
};
157+
158+
public static bool TryGetUnderlyingValue(this global::TestEnum value, out Int32 underlyingValue)
159+
{
160+
switch (value)
161+
{
162+
case global::TestEnum.None:
163+
underlyingValue = 0;
164+
return true;
165+
case global::TestEnum.Value:
166+
underlyingValue = 1;
167+
return true;
168+
}
169+
underlyingValue = default;
170+
return false;
171+
}
172+
173+
public static Int32 GetUnderlyingValue(this global::TestEnum value)
174+
{
175+
if (!TryGetUnderlyingValue(value, out Int32 underlyingValue))
176+
throw new ArgumentOutOfRangeException($"Invalid value: {value}");
177+
178+
return underlyingValue;
179+
}
180+
}

Src/FastEnum.Tests.CodeGen/Resources/EnumTransform.input.verified.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44

55
[Flags]
6-
public enum MyEnum1Format : byte
6+
internal enum MyEnum1Format : byte
77
{
88
None = 0,
99
Name = 1,
@@ -152,7 +152,7 @@ internal static partial class MyEnum1Extensions
152152
using System;
153153

154154
[Flags]
155-
public enum MyEnum2Format : byte
155+
internal enum MyEnum2Format : byte
156156
{
157157
None = 0,
158158
Name = 1,
@@ -301,7 +301,7 @@ internal static partial class MyEnum2Extensions
301301
using System;
302302

303303
[Flags]
304-
public enum MyEnum3Format : byte
304+
internal enum MyEnum3Format : byte
305305
{
306306
None = 0,
307307
Name = 1,
@@ -450,7 +450,7 @@ internal static partial class MyEnum3Extensions
450450
using System;
451451

452452
[Flags]
453-
public enum MyEnum4Format : byte
453+
internal enum MyEnum4Format : byte
454454
{
455455
None = 0,
456456
Name = 1,

Src/FastEnum.Tests.CodeGen/Resources/InternalEnum.input.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using System;
55
namespace Some.Namespace.Here;
66

77
[Flags]
8-
public enum MyEnumFormat : byte
8+
internal enum MyEnumFormat : byte
99
{
1010
None = 0,
1111
Name = 1,

Src/FastEnum.Tests.CodeGen/Resources/NestedType.input.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using System;
55
namespace Some.Namespace.Here;
66

77
[Flags]
8-
public enum MyEnumFormat : byte
8+
internal enum MyEnumFormat : byte
99
{
1010
None = 0,
1111
Name = 1,

Src/FastEnum.Tests.CodeGen/Resources/OmitValue.input.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44

55
[Flags]
6-
public enum MyEnumFormat : byte
6+
internal enum MyEnumFormat : byte
77
{
88
None = 0,
99
Name = 1,

Src/FastEnum.Tests.CodeGen/Resources/VisibilityOverride.input.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44

55
[Flags]
6-
public enum MyPublicEnumFormat : byte
6+
internal enum MyPublicEnumFormat : byte
77
{
88
None = 0,
99
Name = 1,

Src/FastEnum/Generators/EnumFormatCode.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using Genbox.FastEnum.Data;
2+
using Microsoft.CodeAnalysis;
23

34
namespace Genbox.FastEnum.Generators;
45

56
internal static class EnumFormatCode
67
{
78
internal static string Generate(EnumSpec es)
89
{
10+
FastEnumData op = es.Data;
11+
912
string? ns = es.Data.EnumsClassNamespace ?? es.Namespace; //We use the same namespace as the Enums class
1013
string cn = es.Data.EnumNameOverride ?? es.Name;
14+
string vi = op.EnumsClassVisibility == Visibility.Inherit ? (es.AccessChain[0] == Accessibility.Public ? "public" : "internal") : op.EnumsClassVisibility.ToString().ToLowerInvariant();
1115

1216
string res = $$"""
1317
using System;
1418
{{(ns != null ? "\nnamespace " + ns + ";\n" : null)}}
1519
[Flags]
16-
public enum {{cn}}Format : byte
20+
{{vi}} enum {{cn}}Format : byte
1721
{
1822
None = 0,
1923
Name = 1,

0 commit comments

Comments
 (0)