forked from Testura/Testura.Code
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented basic struct builder (#9)
* Basic struct builder implementation * Simple tests for struct builder
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |