Skip to content

Commit

Permalink
Added support for get and set modifiers and added the protected modif…
Browse files Browse the repository at this point in the history
…ier. (#54)
  • Loading branch information
MilleBo authored Jan 16, 2020
1 parent da7aa49 commit 144abd2
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 31 deletions.
12 changes: 12 additions & 0 deletions src/Testura.Code.Tests/Generators/Class/PropertyGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public void Create_WhenCreatingAutoPropertyWithGetAndSet_ShouldHaveBothGetAndSet
Assert.AreEqual("intMyProperty{get;set;}", PropertyGenerator.Create(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet)).ToString());
}

[Test]
public void Create_WhenCreatingPropertyWithGetModifiers_ShouldHaveGetModifiers()
{
Assert.AreEqual("intMyProperty{protectedget;set;}", PropertyGenerator.Create(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet, getModifiers: new List<Modifiers> { Modifiers.Protected})).ToString());
}

[Test]
public void Create_WhenCreatingPropertyWithSetModifiers_ShouldHaveGetModifiers()
{
Assert.AreEqual("intMyProperty{get;privateinternalset;}", PropertyGenerator.Create(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet, setModifiers: new List<Modifiers> { Modifiers.Private, Modifiers.Internal })).ToString());
}

[Test]
public void Create_WhenCreatingAutoPropertyWithAttribute_ShouldHaveAttribute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ModifierGeneratorTests
[TestCase(Modifiers.Async, "async")]
[TestCase(Modifiers.Sealed, "sealed")]
[TestCase(Modifiers.New, "new")]
[TestCase(Modifiers.Protected, "protected")]
public void Create_WhenCreatingWithModifier_ShouldGenerateCode(Modifiers modifier, string expected)
{
Assert.AreEqual(expected, ModifierGenerator.Create(modifier).ToString());
Expand Down
5 changes: 5 additions & 0 deletions src/Testura.Code/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public enum Modifiers
/// Generate with the <c>Internal</c> modifier.
/// </summary>
Internal,

/// <summary>
/// Generate with the <c>Protected</c> modifier.
/// </summary>
Protected
}

/// <summary>
Expand Down
38 changes: 26 additions & 12 deletions src/Testura.Code/Generators/Class/PropertyGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Testura.Code.Generators.Common;
Expand All @@ -26,13 +28,13 @@ public static PropertyDeclarationSyntax Create(Property property)
}

PropertyDeclarationSyntax propertyDeclaration;
if (property is AutoProperty)
if (property is AutoProperty autoProperty)
{
propertyDeclaration = CreateAutoProperty((AutoProperty)property);
propertyDeclaration = CreateAutoProperty(autoProperty);
}
else if (property is BodyProperty)
else if (property is BodyProperty bodyProperty)
{
propertyDeclaration = CreateBodyProperty((BodyProperty)property);
propertyDeclaration = CreateBodyProperty(bodyProperty);
}
else
{
Expand All @@ -56,12 +58,11 @@ private static PropertyDeclarationSyntax CreateAutoProperty(AutoProperty propert
{
var propertyDeclaration = PropertyDeclaration(
TypeGenerator.Create(property.Type), Identifier(property.Name))
.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).
WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.GetAccessorDeclaration, null, property.GetModifiers));

if (property.PropertyType == PropertyTypes.GetAndSet)
{
propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).
WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.SetAccessorDeclaration, null, property.SetModifiers));
}

return propertyDeclaration;
Expand All @@ -71,16 +72,29 @@ private static PropertyDeclarationSyntax CreateBodyProperty(BodyProperty propert
{
var propertyDeclaration = PropertyDeclaration(
TypeGenerator.Create(property.Type), Identifier(property.Name))
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithBody(property.GetBody));
.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.GetAccessorDeclaration, property.GetBody, property.GetModifiers));

if (property.SetBody != null)
{
propertyDeclaration =
propertyDeclaration.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithBody(property.SetBody));
propertyDeclaration.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.SetAccessorDeclaration, property.SetBody, property.SetModifiers));
}

return propertyDeclaration;
}

private static AccessorDeclarationSyntax CreateAccessDeclaration(SyntaxKind kind, BlockSyntax body, IEnumerable<Modifiers> modifiers)
{
var accessDeclaration = AccessorDeclaration(kind);

if (modifiers != null)
{
accessDeclaration = accessDeclaration.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
}

accessDeclaration = body != null ? accessDeclaration.WithBody(body) : accessDeclaration.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));

return accessDeclaration;
}
}
}
3 changes: 3 additions & 0 deletions src/Testura.Code/Generators/Common/ModifierGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static SyntaxTokenList Create(params Modifiers[] modifiers)
case Modifiers.Internal:
tokens.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword));
break;
case Modifiers.Protected:
tokens.Add(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
12 changes: 8 additions & 4 deletions src/Testura.Code/Models/Properties/AutoProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ public class AutoProperty : Property
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="type">The property type.</param>
/// <param name="propertyType">What kind of of property to geneate (get or get/set).</param>
/// <param name="modifiers">The properpty modifiers.</param>
/// <param name="propertyType">What kind of of property to generate (get or get/set).</param>
/// <param name="modifiers">The property modifiers.</param>
/// <param name="attributes">Attributes on the property.</param>
/// <param name="getModifiers">The get modifiers.</param>
/// <param name="setModifiers">The set modifiers.</param>
public AutoProperty(
string name,
Type type,
PropertyTypes propertyType,
IEnumerable<Code.Modifiers> modifiers = null,
IEnumerable<Attribute> attributes = null)
: base(name, type, modifiers, attributes)
IEnumerable<Attribute> attributes = null,
IEnumerable<Modifiers> getModifiers = null,
IEnumerable<Modifiers> setModifiers = null)
: base(name, type, modifiers, attributes, getModifiers, setModifiers)
{
PropertyType = propertyType;
}
Expand Down
14 changes: 12 additions & 2 deletions src/Testura.Code/Models/Properties/BodyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ public class BodyProperty : Property
/// <param name="setBody">The generated set body.</param>
/// <param name="modifiers">Modifiers of the property.</param>
/// <param name="attributes">Attributes of the property.</param>
public BodyProperty(string name, Type type, BlockSyntax getBody, BlockSyntax setBody, IEnumerable<Modifiers> modifiers = null, IEnumerable<Attribute> attributes = null)
: base(name, type, modifiers, attributes)
/// <param name="getModifiers">The get modifiers.</param>
/// <param name="setModifiers">The set modifiers.</param>
public BodyProperty(
string name,
Type type,
BlockSyntax getBody,
BlockSyntax setBody,
IEnumerable<Modifiers> modifiers = null,
IEnumerable<Attribute> attributes = null,
IEnumerable<Modifiers> getModifiers = null,
IEnumerable<Modifiers> setModifiers = null)
: base(name, type, modifiers, attributes, getModifiers, setModifiers)
{
if (getBody == null)
{
Expand Down
22 changes: 19 additions & 3 deletions src/Testura.Code/Models/Properties/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ public abstract class Property
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="type">The property type.</param>
/// <param name="modifiers">The properpty modifiers.</param>
/// <param name="modifiers">The property modifiers.</param>
/// <param name="attributes">Attributes on the property.</param>
/// <param name="getModifiers">The get modifiers.</param>
/// <param name="setModifiers">The set modifiers.</param>
protected Property(
string name,
Type type,
IEnumerable<Code.Modifiers> modifiers = null,
IEnumerable<Attribute> attributes = null)
IEnumerable<Modifiers> modifiers = null,
IEnumerable<Attribute> attributes = null,
IEnumerable<Modifiers> getModifiers = null,
IEnumerable<Modifiers> setModifiers = null)
{
if (name == null)
{
Expand All @@ -35,6 +39,8 @@ protected Property(
Type = type;
Modifiers = modifiers;
Attributes = attributes;
GetModifiers = getModifiers;
SetModifiers = setModifiers;
}

/// <summary>
Expand All @@ -56,5 +62,15 @@ protected Property(
/// Gets or sets the attributes of the property.
/// </summary>
public IEnumerable<Attribute> Attributes { get; set; }

/// <summary>
/// Gets or sets the get modifiers.
/// </summary>
public IEnumerable<Modifiers> GetModifiers { get; set; }

/// <summary>
/// Gets or sets the set modifiers.
/// </summary>
public IEnumerable<Modifiers> SetModifiers { get; }
}
}
15 changes: 5 additions & 10 deletions src/Testura.Code/Testura.Code.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.13.0</Version>
<Version>0.14.0</Version>
<Authors>Mille Bostrom</Authors>
<Copyright>Copyright 2016</Copyright>
<Title>$(ProjectName)</Title>
<Description>Testura.Code is a wrapper around the Roslyn API and used for generation, saving and and compiling C# code. It provides methods and helpers to generate classes, methods, statements and expressions.</Description>
<PackageReleaseNotes>
Release 0.13.0:
- Updated ClassBuilder with regions
- Changed so ClassBuilder preserv order when generating
- Added internal and partial modifiers
- Added support for enums
- Added NamespaceBuilder
- Fixed nullable structs

Release 0.14.0:
- Added modifiers for get and set in properties
- Added protected modifier

</PackageReleaseNotes>
<PackageTags>
Expand All @@ -29,7 +24,7 @@
<CodeAnalysisRuleSet>Settings\Testura.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AssemblyVersion>0.13.0.0</AssemblyVersion>
<AssemblyVersion>0.14.0.0</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down

0 comments on commit 144abd2

Please sign in to comment.