Skip to content

Commit

Permalink
Implemented basic struct builder (#9)
Browse files Browse the repository at this point in the history
* Basic struct builder implementation
* Simple tests for struct builder
  • Loading branch information
jvbsl authored Dec 21, 2021
1 parent 8788466 commit c5bb827
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/VaVare.Tests/Builders/StructBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Formatting;
using NUnit.Framework;
using VaVare.Builders;
using VaVare.Models;
using VaVare.Models.Properties;

namespace VaVare.Tests.Builders
{
[TestFixture]
public class StructBuilderTests
{
private const string TypeName = "TestStruct";
private StructBuilder _structBuilder;

[SetUp]
public void SetUp()
{
_structBuilder = new StructBuilder(TypeName, "MyNamespace");
}

[Test]
public void Build_WhenGivenClassName_CodeShouldContainClassName()
{
Assert.That(_structBuilder.Build().ToString().Contains(TypeName), Is.True);
}

[Test]
public void Build_WhenGivenNamespace_CodeShouldContainNamespace()
{
Assert.That(_structBuilder.Build().ToString().Contains("MyNamespace"), Is.True);
}

[Test]
public void Build_WhenGivenField_CodeShouldContainField()
{
var withField = _structBuilder
.WithFields(new Field("myField", typeof(int), new List<Modifiers>() { Modifiers.Public })).Build();
Assert.That(withField.ToString().Contains("publicintmyField;"), Is.True);
}

[Test]
public void Build_WhenGivenAttributes_CodeShouldContainAttributes()
{
var withAttrs = _structBuilder.WithAttributes(new Attribute("MyAttribute")).Build();
Assert.That(withAttrs.ToString().Contains("[MyAttribute]"), Is.True);
}


[Test]
public void Build_WhenGivenProperty_CodeShouldContainProperty()
{
var withProps = _structBuilder
.WithProperties(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet)).Build();
Assert.That(withProps.ToString().Contains("intMyProperty{get;set;}"), Is.True);
}

[Test]
public void Build_WhenGivenUsing_CodeShouldContainUsing()
{
Assert.That(_structBuilder.WithUsings("some.namespace").Build().ToString().Contains("some.namespace"), Is.True);
}


[Test]
public void Build_WhenGivenModifiers_CodeShouldContainModifiers()
{
var withModifiers = _structBuilder.WithModifiers(Modifiers.Public, Modifiers.Abstract).Build();
Assert.That(withModifiers.ToString().Contains($"publicabstractstruct{TypeName}"), Is.True);
}

[Test]
public void Build_WhenGivenInheritance_CodeShouldContainInheritance()
{
Assert.That(_structBuilder.ThatInheritFrom(typeof(int)).Build().ToString().Contains($"{TypeName}:int"), Is.True);
}
}
}
61 changes: 61 additions & 0 deletions src/VaVare/Builders/StructBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using VaVare.Builders.Base;
using VaVare.Builders.BuildMembers;
using VaVare.Generators.Class;
using VaVare.Models;

namespace VaVare.Builders
{
/// <summary>
/// Provides a builder to generate a struct.
/// </summary>
public class StructBuilder : TypeBuilderBase<StructBuilder>
{
/// <summary>
/// Initializes a new instance of the <see cref="StructBuilder"/> class.
/// </summary>
/// <param name="name">Name of the struct.</param>
/// <param name="namespace">Name of the struct namespace.</param>
public StructBuilder(string name, string @namespace)
: base(name, @namespace)
{
}

/// <summary>
/// Add struct fields.
/// </summary>
/// <param name="fields">A set of wanted fields.</param>
/// <returns>The current struct builder.</returns>
public StructBuilder WithFields(params Field[] fields)
{
return With(new FieldBuildMember(fields.Select(FieldGenerator.Create)));
}

/// <summary>
/// Add struct fields.
/// </summary>
/// <param name="fields">An array of already struct fields.</param>
/// <returns>The current struct builder</returns>
public StructBuilder WithFields(params FieldDeclarationSyntax[] fields)
{
return With(new FieldBuildMember(fields));
}

/// <summary>
/// Add struct constructor.
/// </summary>
/// <param name="constructor">An already generated constructor.</param>
/// <returns>The current struct builder.</returns>
public StructBuilder WithConstructor(params ConstructorDeclarationSyntax[] constructor)
{
return With(new ConstructorBuildMember(constructor));
}

protected override TypeDeclarationSyntax BuildBase()
{
return SyntaxFactory.StructDeclaration(Name).WithBaseList(CreateBaseList()).WithModifiers(CreateModifiers());
}
}
}

0 comments on commit c5bb827

Please sign in to comment.