From d25b13c9e242dea86f40e879e3a93adc6c544ad8 Mon Sep 17 00:00:00 2001 From: NeVeSpl Date: Sat, 23 Mar 2024 10:20:26 +0100 Subject: [PATCH] #113 - expose IProperty.GetMethod and IProperty.SetMethod and --- NTypewriter.CodeModel.Roslyn/Property.cs | 3 +++ .../NTypewriter.CodeModel.Tests.csproj | 12 +++++++++ .../GetMethod_IsPublic/expectedResult.txt | 10 ++++++++ .../Property/GetMethod_IsPublic/inputCode.cs | 25 +++++++++++++++++++ .../GetMethod_IsPublic/inputTemplate.tsnt | 8 ++++++ .../Property/PropertyRenderingTests.cs | 12 +++++++++ .../SetMethod_IsPublic/expectedResult.txt | 10 ++++++++ .../Property/SetMethod_IsPublic/inputCode.cs | 25 +++++++++++++++++++ .../SetMethod_IsPublic/inputTemplate.tsnt | 8 ++++++ NTypewriter.CodeModel/IProperty.cs | 10 ++++++++ 10 files changed, 123 insertions(+) create mode 100644 NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/expectedResult.txt create mode 100644 NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputCode.cs create mode 100644 NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputTemplate.tsnt create mode 100644 NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/expectedResult.txt create mode 100644 NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputCode.cs create mode 100644 NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputTemplate.tsnt diff --git a/NTypewriter.CodeModel.Roslyn/Property.cs b/NTypewriter.CodeModel.Roslyn/Property.cs index 0ccac8e..768e0af 100644 --- a/NTypewriter.CodeModel.Roslyn/Property.cs +++ b/NTypewriter.CodeModel.Roslyn/Property.cs @@ -12,6 +12,9 @@ internal sealed class Property : SymbolBase, IProperty public bool IsReadOnly => symbol.IsReadOnly; public bool IsSealed => symbol.IsSealed; + public IMethod GetMethod => symbol.GetMethod != null ? Method.Create(symbol.GetMethod) : null; + public IMethod SetMethod => symbol.SetMethod != null ? Method.Create(symbol.SetMethod) : null; + private Property(IPropertySymbol symbol) : base (symbol) { diff --git a/NTypewriter.CodeModel.Tests/NTypewriter.CodeModel.Tests.csproj b/NTypewriter.CodeModel.Tests/NTypewriter.CodeModel.Tests.csproj index e1cf209..1aeaad3 100644 --- a/NTypewriter.CodeModel.Tests/NTypewriter.CodeModel.Tests.csproj +++ b/NTypewriter.CodeModel.Tests/NTypewriter.CodeModel.Tests.csproj @@ -43,6 +43,8 @@ + + @@ -151,6 +153,10 @@ + + + + @@ -332,6 +338,12 @@ + + + + + + diff --git a/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/expectedResult.txt b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/expectedResult.txt new file mode 100644 index 0000000..9cfd951 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/expectedResult.txt @@ -0,0 +1,10 @@ +PropGetSet true +PropGet true +PropGetInternalSet true +PropGetPrivateSet true +PropGetProtectedSet true +PropGetInit true +PropRequiredGetSet false +PropRequiredGetInit false +ProtectedPropGetSet false +InternalPropGetSet false diff --git a/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputCode.cs b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputCode.cs new file mode 100644 index 0000000..ec6b4c7 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputCode.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NTypewriter.Tests.CodeModel +{ + + + + class SampleClass + { + public int PropGetSet { get; set; } + public int PropGet { get; } + public int PropGetInternalSet { get; internal set; } + public int PropGetPrivateSet { get; private set; } + public int PropGetProtectedSet { get; protected set; } + public int PropGetInit { get; init; } + public required string PropRequiredGetSet { get; set; } + public required string PropRequiredGetInit { get; init; } + protected int ProtectedPropGetSet { get; set; } + internal int InternalPropGetSet { get; set; } + } +} diff --git a/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputTemplate.tsnt b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputTemplate.tsnt new file mode 100644 index 0000000..896f0e8 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/GetMethod_IsPublic/inputTemplate.tsnt @@ -0,0 +1,8 @@ +{{- capture captured}} +{{- for class in data.Classes | Symbols.WhereNamespaceStartsWith "NTypewriter.Tests.CodeModel"}} +{{- for property in class.Properties }} +{{- property.Name | String.PadRight 21 }}{{ (property.GetMethod?.IsPublic )}} +{{ end }} +{{- end }} +{{- end }} +{{- Save captured "Some name" }} \ No newline at end of file diff --git a/NTypewriter.CodeModel.Tests/Property/PropertyRenderingTests.cs b/NTypewriter.CodeModel.Tests/Property/PropertyRenderingTests.cs index 2bc2958..7a879f6 100644 --- a/NTypewriter.CodeModel.Tests/Property/PropertyRenderingTests.cs +++ b/NTypewriter.CodeModel.Tests/Property/PropertyRenderingTests.cs @@ -20,6 +20,18 @@ public async Task IsWriteOnly() await RunTestForProperty(); } + [TestMethod] + public async Task GetMethod_IsPublic() + { + await RunTestForProperty(); + } + + [TestMethod] + public async Task SetMethod_IsPublic() + { + await RunTestForProperty(); + } + [TestMethod] public async Task Type() diff --git a/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/expectedResult.txt b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/expectedResult.txt new file mode 100644 index 0000000..fc84708 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/expectedResult.txt @@ -0,0 +1,10 @@ +PropGetSet true +PropGet +PropGetInternalSet false +PropGetPrivateSet false +PropGetProtectedSet false +PropGetInit true +PropRequiredGetSet false +PropRequiredGetInit false +ProtectedPropGetSet false +InternalPropGetSet false diff --git a/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputCode.cs b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputCode.cs new file mode 100644 index 0000000..ec6b4c7 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputCode.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NTypewriter.Tests.CodeModel +{ + + + + class SampleClass + { + public int PropGetSet { get; set; } + public int PropGet { get; } + public int PropGetInternalSet { get; internal set; } + public int PropGetPrivateSet { get; private set; } + public int PropGetProtectedSet { get; protected set; } + public int PropGetInit { get; init; } + public required string PropRequiredGetSet { get; set; } + public required string PropRequiredGetInit { get; init; } + protected int ProtectedPropGetSet { get; set; } + internal int InternalPropGetSet { get; set; } + } +} diff --git a/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputTemplate.tsnt b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputTemplate.tsnt new file mode 100644 index 0000000..bdbeb97 --- /dev/null +++ b/NTypewriter.CodeModel.Tests/Property/SetMethod_IsPublic/inputTemplate.tsnt @@ -0,0 +1,8 @@ +{{- capture captured}} +{{- for class in data.Classes | Symbols.WhereNamespaceStartsWith "NTypewriter.Tests.CodeModel"}} +{{- for property in class.Properties }} +{{- property.Name | String.PadRight 21 }}{{ (property.SetMethod?.IsPublic )}} +{{ end }} +{{- end }} +{{- end }} +{{- Save captured "Some name" }} \ No newline at end of file diff --git a/NTypewriter.CodeModel/IProperty.cs b/NTypewriter.CodeModel/IProperty.cs index 133d1aa..798a4b3 100644 --- a/NTypewriter.CodeModel/IProperty.cs +++ b/NTypewriter.CodeModel/IProperty.cs @@ -33,5 +33,15 @@ public interface IProperty : ISymbolBase /// The type of the property. /// IType Type { get; } + + /// + /// Getter + /// + IMethod GetMethod { get; } + + /// + /// Setter + /// + IMethod SetMethod { get; } } } \ No newline at end of file