Skip to content

Commit

Permalink
0.10.0 (#35)
Browse files Browse the repository at this point in the history
* Added DateTimeOffset to type generator

* Created multiple new modifiers

* Added support for field initialization
  • Loading branch information
MilleBo authored Jul 3, 2019
1 parent 0bed2b2 commit b69021c
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Testura.Code.Tests/Generators/Class/FieldGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
27 changes: 26 additions & 1 deletion src/Testura.Code/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,32 @@ public enum Modifiers
/// <summary>
/// Generate with the <c>virtual</c> modifier.
/// </summary>
Virtual
Virtual,

/// <summary>
/// Generate with the <c>override</c> modifier.
/// </summary>
Override,

/// <summary>
/// Generate with the <c>readonly</c> modifier.
/// </summary>
Readonly,

/// <summary>
/// Generate with the <c>async</c> modifier.
/// </summary>
Async,

/// <summary>
/// Generate with the <c>sealed</c> modifier.
/// </summary>
Sealed,

/// <summary>
/// Generate with the <c>new</c> modifier.
/// </summary>
New
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Testura.Code/Generators/Class/FieldGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
25 changes: 20 additions & 5 deletions src/Testura.Code/Generators/Common/ModifierGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
namespace Testura.Code.Generators.Common
{
/// <summary>
/// Provivdes the functionality to generate modifiers (public, protected, etc).
/// Provides the functionality to generate modifiers (public, protected, etc).
/// </summary>
public static class ModifierGenerator
{
/// <summary>
/// Create the syntax for modifier(s) to class, method, fields or properties.
/// </summary>
/// <param name="modifierses">Modifiers to create.</param>
/// <param name="modifiers">Modifiers to create.</param>
/// <returns>The declared syntax list.</returns>
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<SyntaxToken>();
foreach (var modifierse in modifierses)
foreach (var modifierse in modifiers)
{
switch (modifierse)
{
Expand All @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Testura.Code/Generators/Common/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/Testura.Code/Models/Field.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Testura.Code.Models
{
Expand All @@ -15,11 +16,13 @@ public class Field
/// <param name="type">Type of the field.</param>
/// <param name="modifiers">The fields modifiers.</param>
/// <param name="attributes">The fields attributes.</param>
/// <param name="initializeWith">Expression used to initialize field</param>
public Field(
string name,
Type type,
IEnumerable<Modifiers> modifiers = null,
IEnumerable<Attribute> attributes = null)
IEnumerable<Attribute> attributes = null,
ExpressionSyntax initializeWith = null)
{
if (name == null)
{
Expand All @@ -35,6 +38,7 @@ public Field(
Type = type;
Modifiers = modifiers;
Attributes = attributes;
InitializeWith = initializeWith;
}

/// <summary>
Expand All @@ -56,5 +60,10 @@ public Field(
/// Gets or sets the attributes of the field.
/// </summary>
public IEnumerable<Attribute> Attributes { get; set; }

/// <summary>
/// Gets or sets expression used to assign field
/// </summary>
public ExpressionSyntax InitializeWith { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Testura.Code/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit b69021c

Please sign in to comment.