Skip to content

Commit

Permalink
Fix bug: interface with get only property generates setter (#40)
Browse files Browse the repository at this point in the history
* Add failing unit test for interface with get only property.

* Fix bug when interface property has get-only, but still generates setter. By checking if interface had getter or setter before adding it to generated decorator abstract class.

* Add additional property get/set combinations for unit test.
  • Loading branch information
CodingFlow authored Nov 2, 2023
1 parent 8b89c89 commit 356a422
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
13 changes: 13 additions & 0 deletions DecoratorGenerator.UnitTests/ILionProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DecoratorGenerator;

namespace SampleLibrary;

[Decorate]
public interface ILionProperties
{
string Name { get; }

string Color { set; }

int Age { get; set; }
}
19 changes: 19 additions & 0 deletions DecoratorGenerator.UnitTests/LionPropertiesDecorator.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <auto-generated/>
namespace SampleLibrary;

public abstract class LionPropertiesDecorator : ILionProperties
{
private ILionProperties lionProperties;

protected LionPropertiesDecorator(ILionProperties lionProperties) {
this.lionProperties = lionProperties;
}

public virtual string Name { get => lionProperties.Name; }

public virtual string Color { set => lionProperties.Color = value; }

public virtual int Age { get => lionProperties.Age; set => lionProperties.Age = value; }


}
22 changes: 22 additions & 0 deletions DecoratorGenerator.UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ public async Task OneInterface() {
}.RunAsync();
}

[Test]
public async Task OneInterface_Properties() {
var source = await ReadCSharpFile<ILionProperties>();
var generated = await ReadCSharpFile<LionPropertiesDecorator>();

await new VerifyCS.Test
{
TestState = {
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
AdditionalReferences =
{
implementationAssembly,
},
Sources = { source },
GeneratedSources =
{
(typeof(Main), "LionPropertiesDecorator.generated.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)),
},
},
}.RunAsync();
}

[Test]
public async Task OneInterface_NestedNamespace() {
var source = await ReadCSharpFile<INested>();
Expand Down
17 changes: 15 additions & 2 deletions DecoratorGenerator/OutputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,30 @@ public abstract class {className} : {@interface.Name}
var displayProperties = properties.Select(property => {
var formattedAccessibility = property.DeclaredAccessibility.ToString().ToLower();
var signature = $@"{formattedAccessibility} virtual {property.Type} {property.Name}";
var call = $@"get => {targetFieldName}.{property.Name}; set => {targetFieldName}.{property.Name} = value;";


var propertyMethods = new List<string>();

if (property.GetMethod != null) {
propertyMethods.Add($@"get => {targetFieldName}.{property.Name};");
}

if (property.SetMethod != null) {
propertyMethods.Add($@"set => {targetFieldName}.{property.Name} = value;");
}

var call = string.Join(" ", propertyMethods);

return (signature, call, string.Empty);
});

return displayProperties;
}

private static IEnumerable<string> FormatDisplayMethods(IEnumerable<(string signature, string call, ITypeSymbol returnType)> displayMethods) {
return displayMethods.Select(method => {
return
$@" {method.signature} {{
$@" {method.signature} {{
{(method.returnType.Name == "Void" ? string.Empty : "return ")}{method.call};
}}";
});
Expand Down

0 comments on commit 356a422

Please sign in to comment.