Skip to content
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
23 changes: 23 additions & 0 deletions DecoratorGenerator.UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@
}.RunAsync();
}

[Test]
public async Task OneInterface_OutParameter() {
var source = await ReadCSharpFile<IOutParameter>(true);
var generated = await ReadCSharpFile<OutParameterDecorator>(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<IBird>(true);
Expand Down Expand Up @@ -176,7 +199,7 @@
{
implementationAssembly,
GetAssembly("TestLibrary"),
Assembly.GetAssembly(typeof(DynamoDBContext)),

Check warning on line 202 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Possible null reference argument for parameter 'assembly' in 'void MetadataReferenceCollection.Add(Assembly assembly)'.
},
Sources = { wrapperList, source },
GeneratedSources =
Expand Down Expand Up @@ -206,19 +229,19 @@
private static async Task<string> ReadFile(bool isTestLibrary, string searchPattern) {
var currentDirectory = GetCurrentDirectory();

var targetDirectory = isTestLibrary ? GetTestLibraryDirectory(currentDirectory) : currentDirectory;

Check warning on line 232 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Possible null reference argument for parameter 'currentDirectory' in 'DirectoryInfo Tests.GetTestLibraryDirectory(DirectoryInfo currentDirectory)'.

var file = targetDirectory.GetFiles(searchPattern).First();

Check warning on line 234 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

using var fileReader = new StreamReader(file.OpenRead());
return await fileReader.ReadToEndAsync();
}

private static DirectoryInfo? GetCurrentDirectory() {
return Directory.GetParent(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName);

Check warning on line 241 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 241 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.

Check warning on line 241 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.
}

private static DirectoryInfo GetTestLibraryDirectory(DirectoryInfo currentDirectory) {
return currentDirectory.Parent.GetDirectories("TestLibrary").First();

Check warning on line 245 in DecoratorGenerator.UnitTests/Tests.cs

View workflow job for this annotation

GitHub Actions / Unit tests (10.0.x)

Dereference of a possibly null reference.
}
}
19 changes: 15 additions & 4 deletions DecoratorGenerator/OutputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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})";

Expand Down Expand Up @@ -123,8 +135,7 @@ private static string CreateFormattedConstraints(ImmutableArray<ITypeParameterSy

private static IEnumerable<string> 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};
}}";
});
Expand Down
2 changes: 1 addition & 1 deletion SampleLibrary/SampleLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
9 changes: 9 additions & 0 deletions TestLibrary/IOutParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using DecoratorGenerator;

namespace SampleLibrary;

[Decorate]
public interface IOutParameter
{
bool VerifySomething(string input, out string output);
}
18 changes: 18 additions & 0 deletions TestLibrary/OutParameterDecorator.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <auto-generated/>
#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);
}
}
Loading