diff --git a/DecoratorGenerator.UnitTests/Tests.cs b/DecoratorGenerator.UnitTests/Tests.cs index 6d42358..8a90f30 100644 --- a/DecoratorGenerator.UnitTests/Tests.cs +++ b/DecoratorGenerator.UnitTests/Tests.cs @@ -134,6 +134,29 @@ public async Task OneInterface_NestedNamespace() { }.RunAsync(); } + [Test] + public async Task OneInterface_OutParameter() { + var source = await ReadCSharpFile(true); + var generated = await ReadCSharpFile(true); + + await new VerifyCS.Test + { + TestState = { + ReferenceAssemblies = ReferenceAssemblies.Net.Net90, + AdditionalReferences = + { + implementationAssembly, + GetAssembly("TestLibrary") + }, + Sources = { source }, + GeneratedSources = + { + (typeof(Main), "OutParameterDecorator.generated.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)), + }, + }, + }.RunAsync(); + } + [Test] public async Task TwoInterfaces() { var sourceOne = await ReadCSharpFile(true); diff --git a/DecoratorGenerator/OutputGenerator.cs b/DecoratorGenerator/OutputGenerator.cs index d69989a..2c21a07 100644 --- a/DecoratorGenerator/OutputGenerator.cs +++ b/DecoratorGenerator/OutputGenerator.cs @@ -58,12 +58,24 @@ private static string FormatInterfaceConstraintTypes(INamedTypeSymbol @interface var displayMethods = methods.Select(method => { var typeParametersStrings = method.TypeParameters.Select(t => t.ToDisplayString()); - var parametersStrings = method.Parameters.Select(p => $@"{p.Type} {p.Name}"); + var parametersStrings = method.Parameters.Select(p => { + var modifier = p.RefKind == RefKind.None + ? string.Empty + : $"{p.RefKind.ToString().ToLower()} "; + + return $@"{modifier}{p.Type} {p.Name}"; + }); var formattedAccessibility = (method.ReturnType.DeclaredAccessibility != Accessibility.NotApplicable ? method.ReturnType.DeclaredAccessibility : Accessibility.Public).ToString().ToLower(); var formattedGenericTypeParameters = method.IsGenericMethod ? $@"<{string.Join(", ", typeParametersStrings)}>" : string.Empty; var formattedConstraints = CreateFormattedConstraints(method.TypeParameters); var signature = $@"{formattedAccessibility} virtual {method.ReturnType} {method.Name}{formattedGenericTypeParameters}({string.Join(", ", parametersStrings)}){(formattedConstraints != string.Empty ? $@" {formattedConstraints}" : string.Empty)}"; - var callParameters = $@"{string.Join(", ", method.Parameters.Select(p => p.Name))}"; + var callParameters = $@"{string.Join(", ", method.Parameters.Select(p => { + var modifier = p.RefKind == RefKind.None + ? string.Empty + : $"{p.RefKind.ToString().ToLower()} "; + + return $"{modifier}{p.Name}"; + }))}"; var call = $@"{targetFieldName}.{method.Name}{(method.IsGenericMethod ? $@"<{string.Join(", ", typeParametersStrings)}>" : string.Empty)}({callParameters})"; @@ -123,8 +135,7 @@ private static string CreateFormattedConstraints(ImmutableArray FormatDisplayMethods(IEnumerable<(string signature, string call, ITypeSymbol returnType)> displayMethods) { return displayMethods.Select(method => { - return - $@" {method.signature} {{ + return $@" {method.signature} {{ {(method.returnType.Name == "Void" ? string.Empty : "return ")}{method.call}; }}"; }); diff --git a/SampleLibrary/SampleLibrary.csproj b/SampleLibrary/SampleLibrary.csproj index 4c4c10e..9326220 100644 --- a/SampleLibrary/SampleLibrary.csproj +++ b/SampleLibrary/SampleLibrary.csproj @@ -1,7 +1,7 @@  - net6.0 + net10.0 enable enable diff --git a/TestLibrary/IOutParameter.cs b/TestLibrary/IOutParameter.cs new file mode 100644 index 0000000..e689a3a --- /dev/null +++ b/TestLibrary/IOutParameter.cs @@ -0,0 +1,9 @@ +using DecoratorGenerator; + +namespace SampleLibrary; + +[Decorate] +public interface IOutParameter +{ + bool VerifySomething(string input, out string output); +} diff --git a/TestLibrary/OutParameterDecorator.generated.cs b/TestLibrary/OutParameterDecorator.generated.cs new file mode 100644 index 0000000..1d4957c --- /dev/null +++ b/TestLibrary/OutParameterDecorator.generated.cs @@ -0,0 +1,18 @@ +// +#nullable restore +namespace SampleLibrary; + +public abstract class OutParameterDecorator : IOutParameter +{ + private IOutParameter outParameter; + + protected OutParameterDecorator(IOutParameter outParameter) { + this.outParameter = outParameter; + } + + + + public virtual bool VerifySomething(string input, out string output) { + return outParameter.VerifySomething(input, out output); + } +}