From b69021c3c1a586019c84413c38b548fb8407381b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mille=20Bostr=C3=B6m?= Date: Wed, 3 Jul 2019 18:43:38 +0200 Subject: [PATCH] 0.10.0 (#35) * Added DateTimeOffset to type generator * Created multiple new modifiers * Added support for field initialization --- .../Generators/Class/FieldGeneratorTests.cs | 10 +++++++ .../Common/ModifierGeneratorTests.cs | 5 ++++ src/Testura.Code/Enums.cs | 27 ++++++++++++++++++- .../Generators/Class/FieldGenerator.cs | 9 ++++++- .../Generators/Common/ModifierGenerator.cs | 25 +++++++++++++---- .../Generators/Common/TypeGenerator.cs | 6 +++++ src/Testura.Code/Models/Field.cs | 11 +++++++- src/Testura.Code/Properties/AssemblyInfo.cs | 4 +-- 8 files changed, 87 insertions(+), 10 deletions(-) diff --git a/src/Testura.Code.Tests/Generators/Class/FieldGeneratorTests.cs b/src/Testura.Code.Tests/Generators/Class/FieldGeneratorTests.cs index 64d4f66..887ab40 100644 --- a/src/Testura.Code.Tests/Generators/Class/FieldGeneratorTests.cs +++ b/src/Testura.Code.Tests/Generators/Class/FieldGeneratorTests.cs @@ -1,7 +1,11 @@ using System.Collections.Generic; +using Microsoft.CodeAnalysis.CSharp; using NUnit.Framework; using Testura.Code.Generators.Class; +using Testura.Code.Generators.Common; using Testura.Code.Models; +using Testura.Code.Models.References; +using Testura.Code.Models.Types; using Assert = NUnit.Framework.Assert; namespace Testura.Code.Tests.Generators.Class @@ -15,6 +19,12 @@ public void Create_WhenCreatingField_ShouldGenerateCorrectCode() Assert.AreEqual("intmyField;", FieldGenerator.Create(new Field("myField", typeof(int))).ToString()); } + [Test] + public void Create_WhenCreatingFieldWithInitializer_ShouldGenerateCorrectCode() + { + Assert.AreEqual("ILogger_logger=LoggerService.Logger();", FieldGenerator.Create(new Field("_logger", CustomType.Create("ILogger"), initializeWith: ReferenceGenerator.Create(new VariableReference("LoggerService", new MethodReference( "Logger"))))).ToString()); + } + [Test] public void Create_WhenCreatingFieldWithGenericType_ShouldGenerateCorrectCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/ModifierGeneratorTests.cs b/src/Testura.Code.Tests/Generators/Common/ModifierGeneratorTests.cs index 4b419ef..5f0c074 100644 --- a/src/Testura.Code.Tests/Generators/Common/ModifierGeneratorTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/ModifierGeneratorTests.cs @@ -11,6 +11,11 @@ public class ModifierGeneratorTests [TestCase(Modifiers.Public, "public")] [TestCase(Modifiers.Static, "static")] [TestCase(Modifiers.Virtual, "virtual")] + [TestCase(Modifiers.Override, "override")] + [TestCase(Modifiers.Readonly, "readonly")] + [TestCase(Modifiers.Async, "async")] + [TestCase(Modifiers.Sealed, "sealed")] + [TestCase(Modifiers.New, "new")] public void Create_WhenCreatingWithModifier_ShouldGenerateCode(Modifiers modifier, string expected) { Assert.AreEqual(expected, ModifierGenerator.Create(modifier).ToString()); diff --git a/src/Testura.Code/Enums.cs b/src/Testura.Code/Enums.cs index c309782..87a733d 100644 --- a/src/Testura.Code/Enums.cs +++ b/src/Testura.Code/Enums.cs @@ -64,7 +64,32 @@ public enum Modifiers /// /// Generate with the virtual modifier. /// - Virtual + Virtual, + + /// + /// Generate with the override modifier. + /// + Override, + + /// + /// Generate with the readonly modifier. + /// + Readonly, + + /// + /// Generate with the async modifier. + /// + Async, + + /// + /// Generate with the sealed modifier. + /// + Sealed, + + /// + /// Generate with the new modifier. + /// + New } /// diff --git a/src/Testura.Code/Generators/Class/FieldGenerator.cs b/src/Testura.Code/Generators/Class/FieldGenerator.cs index 4db2984..8995dd2 100644 --- a/src/Testura.Code/Generators/Class/FieldGenerator.cs +++ b/src/Testura.Code/Generators/Class/FieldGenerator.cs @@ -24,7 +24,14 @@ public static FieldDeclarationSyntax Create(Field field) throw new ArgumentNullException(nameof(field)); } - var fieldDeclaration = FieldDeclaration(VariableDeclaration(TypeGenerator.Create(field.Type), SeparatedList(new[] { VariableDeclarator(Identifier(field.Name)) }))); + var variableDeclarator = VariableDeclarator(Identifier(field.Name)); + + if (field.InitializeWith != null) + { + variableDeclarator = variableDeclarator.WithInitializer(EqualsValueClause(field.InitializeWith)); + } + + var fieldDeclaration = FieldDeclaration(VariableDeclaration(TypeGenerator.Create(field.Type), SeparatedList(new[] { variableDeclarator}))); if (field.Modifiers != null) { diff --git a/src/Testura.Code/Generators/Common/ModifierGenerator.cs b/src/Testura.Code/Generators/Common/ModifierGenerator.cs index 38b2d2c..18b621c 100644 --- a/src/Testura.Code/Generators/Common/ModifierGenerator.cs +++ b/src/Testura.Code/Generators/Common/ModifierGenerator.cs @@ -6,24 +6,24 @@ namespace Testura.Code.Generators.Common { /// - /// Provivdes the functionality to generate modifiers (public, protected, etc). + /// Provides the functionality to generate modifiers (public, protected, etc). /// public static class ModifierGenerator { /// /// Create the syntax for modifier(s) to class, method, fields or properties. /// - /// Modifiers to create. + /// Modifiers to create. /// The declared syntax list. - public static SyntaxTokenList Create(params Modifiers[] modifierses) + public static SyntaxTokenList Create(params Modifiers[] modifiers) { - if (modifierses.Length == 0) + if (modifiers.Length == 0) { return SyntaxFactory.TokenList(); } var tokens = new List(); - foreach (var modifierse in modifierses) + foreach (var modifierse in modifiers) { switch (modifierse) { @@ -42,6 +42,21 @@ public static SyntaxTokenList Create(params Modifiers[] modifierses) case Modifiers.Virtual: tokens.Add(SyntaxFactory.Token(SyntaxKind.VirtualKeyword)); break; + case Modifiers.Async: + tokens.Add(SyntaxFactory.Token(SyntaxKind.AsyncKeyword)); + break; + case Modifiers.Override: + tokens.Add(SyntaxFactory.Token(SyntaxKind.OverrideKeyword)); + break; + case Modifiers.Readonly: + tokens.Add(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)); + break; + case Modifiers.Sealed: + tokens.Add(SyntaxFactory.Token(SyntaxKind.SealedKeyword)); + break; + case Modifiers.New: + tokens.Add(SyntaxFactory.Token(SyntaxKind.NewKeyword)); + break; default: throw new ArgumentOutOfRangeException(); } diff --git a/src/Testura.Code/Generators/Common/TypeGenerator.cs b/src/Testura.Code/Generators/Common/TypeGenerator.cs index a282586..25750a8 100644 --- a/src/Testura.Code/Generators/Common/TypeGenerator.cs +++ b/src/Testura.Code/Generators/Common/TypeGenerator.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Testura.Code.Models.Types; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +using DateTimeOffset = System.DateTimeOffset; namespace Testura.Code.Generators.Common { @@ -134,6 +135,11 @@ private static TypeSyntax CheckPredefinedTypes(Type type) typeSyntax = IdentifierName("TimeSpan"); } + if (type == typeof(DateTimeOffset)) + { + typeSyntax = IdentifierName("DateTimeOffset"); + } + if (type.IsEnum) { typeSyntax = IdentifierName(type.Name); diff --git a/src/Testura.Code/Models/Field.cs b/src/Testura.Code/Models/Field.cs index 3b5e825..933666e 100644 --- a/src/Testura.Code/Models/Field.cs +++ b/src/Testura.Code/Models/Field.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Testura.Code.Models { @@ -15,11 +16,13 @@ public class Field /// Type of the field. /// The fields modifiers. /// The fields attributes. + /// Expression used to initialize field public Field( string name, Type type, IEnumerable modifiers = null, - IEnumerable attributes = null) + IEnumerable attributes = null, + ExpressionSyntax initializeWith = null) { if (name == null) { @@ -35,6 +38,7 @@ public Field( Type = type; Modifiers = modifiers; Attributes = attributes; + InitializeWith = initializeWith; } /// @@ -56,5 +60,10 @@ public Field( /// Gets or sets the attributes of the field. /// public IEnumerable Attributes { get; set; } + + /// + /// Gets or sets expression used to assign field + /// + public ExpressionSyntax InitializeWith { get; set; } } } diff --git a/src/Testura.Code/Properties/AssemblyInfo.cs b/src/Testura.Code/Properties/AssemblyInfo.cs index 52ef0d8..1cba564 100644 --- a/src/Testura.Code/Properties/AssemblyInfo.cs +++ b/src/Testura.Code/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.9.0.0")] -[assembly: AssemblyFileVersion("0.9.0.0")] +[assembly: AssemblyVersion("0.10.0.0")] +[assembly: AssemblyFileVersion("0.10.0.0")]