From ce50bdd0a1a8b6f21946a94cbf71bd373581758a Mon Sep 17 00:00:00 2001 From: Steve Wilford Date: Wed, 22 May 2024 12:23:58 +0100 Subject: [PATCH] Add support for static get-only properties --- src/CodeGenHelpers/PropertyBuilder.cs | 11 +++-- .../Tests/PropertyTests.cs | 46 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/CodeGenHelpers/PropertyBuilder.cs b/src/CodeGenHelpers/PropertyBuilder.cs index 9694c52..394e37c 100644 --- a/src/CodeGenHelpers/PropertyBuilder.cs +++ b/src/CodeGenHelpers/PropertyBuilder.cs @@ -118,7 +118,7 @@ public PropertyBuilder WithAccessModifier(Accessibility accessModifier) AccessModifier = accessModifier; return this; } - + public PropertyBuilder Override(bool @override = true) { _override = @override; @@ -128,7 +128,11 @@ public PropertyBuilder Override(bool @override = true) public PropertyBuilder MakeStatic() { IsStatic = true; - FieldTypeValue = FieldType.Const; + if (string.IsNullOrEmpty(_getterExpression)) + { + FieldTypeValue = FieldType.Const; + } + return this; } @@ -167,6 +171,7 @@ public ClassBuilder UseAutoProps(Accessibility? setterAccessibility) public PropertyBuilder WithGetterExpression(string expression) { _getterExpression = expression; + FieldTypeValue = FieldType.Property; return this; } @@ -260,7 +265,7 @@ void IBuilder.Write(in CodeWriter writer) { FieldType.Const => $"{AccessModifier.Code()}{isNew} const {type} {name}", FieldType.ReadOnly => $"{AccessModifier.Code()}{isNew}{_static} readonly {type} {name}", - _ => additionalModifier is null + _ => additionalModifier is null ? $"{AccessModifier.Code()}{isNew}{_static} {type} {name}" : $"{AccessModifier.Code()} {additionalModifier} {type} {name}" }).Trim(); diff --git a/tests/CodeGenHelpers.Tests/Tests/PropertyTests.cs b/tests/CodeGenHelpers.Tests/Tests/PropertyTests.cs index 2ad34ad..f939b8b 100644 --- a/tests/CodeGenHelpers.Tests/Tests/PropertyTests.cs +++ b/tests/CodeGenHelpers.Tests/Tests/PropertyTests.cs @@ -174,6 +174,52 @@ partial class SampleClass AreEqual(expected, builder); } + [Fact] + public void AddsGetterExpressionToStaticProperty() + { + var builder = CodeBuilder.Create("AwesomeApp") + .AddClass("SampleClass") + .AddProperty("Test") + .MakePublicProperty() + .MakeStatic() + .SetType("string") + .WithGetterExpression("\"test\"") + .Class; + + var expected = @"namespace AwesomeApp +{ + partial class SampleClass + { + public static string Test => ""test""; + } +} +"; + AreEqual(expected, builder); + } + + [Fact] + public void AddsStaticToPropertyWithGetterExpression() + { + var builder = CodeBuilder.Create("AwesomeApp") + .AddClass("SampleClass") + .AddProperty("Test") + .MakePublicProperty() + .WithGetterExpression("\"test\"") + .MakeStatic() + .SetType("string") + .Class; + + var expected = @"namespace AwesomeApp +{ + partial class SampleClass + { + public static string Test => ""test""; + } +} +"; + AreEqual(expected, builder); + } + private void AreEqual(string expected, ClassBuilder builder) { var expectedOutput = $@"//------------------------------------------------------------------------------