Skip to content

Commit 41b7545

Browse files
authored
Fix: ensure generated abstract class access modifier matches interface (internal or public). (#97)
1 parent d4a8422 commit 41b7545

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

DecoratorGenerator.UnitTests/Tests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using SampleLibrary.Deep.Nesteds;
66
using System.Reflection;
77
using System.Text;
8+
using TestLibrary;
89
using VerifyCS = DecoratorGenerator.UnitTests.CSharpSourceGeneratorVerifier<DecoratorGenerator.Main>;
910

1011
namespace DecoratorGenerator.UnitTests;
@@ -41,6 +42,29 @@ public async Task OneInterface() {
4142
}.RunAsync();
4243
}
4344

45+
[Test]
46+
public async Task OneInterface_Internal() {
47+
var source = await ReadCSharpFile<IInternalType>(true);
48+
var generated = await ReadCSharpFile<InternalTypeDecorator>(true);
49+
50+
await new VerifyCS.Test
51+
{
52+
TestState = {
53+
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
54+
AdditionalReferences =
55+
{
56+
implementationAssembly,
57+
GetAssembly("TestLibrary")
58+
},
59+
Sources = { source },
60+
GeneratedSources =
61+
{
62+
(typeof(Main), "InternalTypeDecorator.generated.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)),
63+
},
64+
},
65+
}.RunAsync();
66+
}
67+
4468
[Test]
4569
public async Task OneInterface_Properties() {
4670
var source = await ReadCSharpFile<ILionProperties>(true);

DecoratorGenerator/OutputGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace DecoratorGenerator
88
internal static class OutputGenerator
99
{
1010
public static (string source, string className) GenerateOutputs(INamedTypeSymbol @interface) {
11+
var interfaceAccessModifier = @interface.DeclaredAccessibility.ToString().ToLower();
1112
var className = $"{@interface.Name.Substring(1)}Decorator";
1213
var formattedConstraintTypes = FormatInterfaceConstraintTypes(@interface);
1314
var formattedConstraints = CreateFormattedConstraints(@interface.TypeParameters);
@@ -25,7 +26,7 @@ public static (string source, string className) GenerateOutputs(INamedTypeSymbol
2526
#nullable restore
2627
namespace {@interface.ContainingNamespace.ToDisplayString()};
2728
28-
public abstract class {className}{formattedConstraintTypes} : {@interface.Name}{formattedConstraintTypes}{(formattedConstraints != string.Empty ? $@" {formattedConstraints}" : string.Empty)}
29+
{interfaceAccessModifier} abstract class {className}{formattedConstraintTypes} : {@interface.Name}{formattedConstraintTypes}{(formattedConstraints != string.Empty ? $@" {formattedConstraints}" : string.Empty)}
2930
{{
3031
private {@interface.Name}{formattedConstraintTypes} {targetFieldName};
3132

TestLibrary/IInternalType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using DecoratorGenerator;
2+
3+
namespace TestLibrary;
4+
5+
[Decorate]
6+
internal interface IInternalType
7+
{
8+
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <auto-generated/>
2+
#nullable restore
3+
namespace TestLibrary;
4+
5+
internal abstract class InternalTypeDecorator : IInternalType
6+
{
7+
private IInternalType internalType;
8+
9+
protected InternalTypeDecorator(IInternalType internalType) {
10+
this.internalType = internalType;
11+
}
12+
13+
14+
15+
16+
}

0 commit comments

Comments
 (0)