Cover classes with unit tests

This commit is contained in:
2022-03-22 12:46:52 +01:00
parent 959c1359e1
commit 30bb35853b
14 changed files with 1214 additions and 8 deletions
+176
View File
@@ -0,0 +1,176 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class AccessorBuilderTests
{
private const string PublicGet = "public get;";
private const string PublicStaticGet = "public static get;";
private const string Set = "set;";
private const string GetExpressionField = "get => this.field ;";
private const string GetBodyField =
@"get
{
return this.field;
}";
private readonly AccessorBuilder builder;
public AccessorBuilderTests()
{
this.builder = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.GetAccessorDeclaration);
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsNullArgumentException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier("public");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers("public static");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody("return this.field;");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithArrowExpression_ExpressionNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithArrowExpression(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArrowExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArrowExpression("this.field;");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsAccessorDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<AccessorDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier("public").Build().ToFullString();
result.Should().Be(PublicGet);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers("public static").Build().ToFullString();
result.Should().Be(PublicStaticGet);
}
[TestMethod]
public void SetAccessor_ReturnsExpectedString()
{
var result = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.SetAccessorDeclaration).Build().ToFullString();
result.Should().Be(Set);
}
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody("return this.field;").Build().ToFullString();
result.Should().Be(GetBodyField);
}
[TestMethod]
public void WithArrowExpression_ReturnsExpectedString()
{
var result = this.builder.WithArrowExpression("this.field;").Build().ToFullString();
result.Should().Be(GetExpressionField);
}
[TestMethod]
public void WithBody_OverridesWithArrowExpression_ReturnsExpectedString()
{
var result = this.builder
.WithArrowExpression("this field;")
.WithBody("return this.field;")
.Build()
.ToFullString();
result.Should().Be(GetBodyField);
}
[TestMethod]
public void WithArrowExpression_OverridesWithBody_ReturnsExpectedString()
{
var result = this.builder
.WithBody("return this.field;")
.WithArrowExpression("this.field;")
.Build()
.ToFullString();
result.Should().Be(GetExpressionField);
}
}
}
@@ -0,0 +1,67 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class BaseConstructorBuilderTests
{
private const string EmptyBase = ": base()";
private const string TestBase = ": base(test)";
private readonly BaseConstructorBuilder builder;
public BaseConstructorBuilderTests()
{
this.builder = new BaseConstructorBuilder();
}
[TestMethod]
public void WithArgument_NullArgument_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithArgument(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgument_ValidArgument_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArgument("test");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsConstructorInitializerSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorInitializerSyntax>();
}
[TestMethod]
public void EmptyBaseConstructor_ReturnsExpectedString()
{
var result = this.builder.Build().ToFullString();
result.Should().Be(EmptyBase);
}
[TestMethod]
public void WithArgument_ShouldReturnExpectedString()
{
var result = this.builder
.WithArgument("test")
.Build()
.ToFullString();
result.Should().Be(TestBase);
}
}
}
+145
View File
@@ -0,0 +1,145 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class ClassBuilderTests
{
private const string Name = "Test";
private const string PublicClass =
@"public class Test
{
}";
private const string PublicStaticClass =
@"public static class Test
{
}";
private readonly ClassBuilder builder;
public ClassBuilderTests()
{
this.builder = new ClassBuilder(Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
_ = new ClassBuilder(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier("public");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers("public static");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithField_NullFieldBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithField(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithProperty_NullPropertyBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithProperty(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithMethod_NullMethodBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithMethod(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithConstructor_NullConstructorBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithConstructor(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Build_ReturnsClassDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ClassDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier("public").Build().ToFullString();
result.Should().Be(PublicClass);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers("public static").Build().ToFullString();
result.Should().Be(PublicStaticClass);
}
}
}
+223
View File
@@ -0,0 +1,223 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class ConstructorBuilderTests
{
private const string Public = "public";
private const string PublicSealed = "public sealed";
private const string Null = "null";
private const string ParameterType = "string";
private const string ParameterName = "someString";
private const string Name = "Test";
private const string Body = "this.someString = someString;";
private const string PublicEmptyConstructor =
@"public Test()
{
}";
private const string PublicSealedEmptyConstructor =
@"public sealed Test()
{
}";
private const string ConstructorWithParameter =
@"Test(string someString)
{
}";
private const string ConstructorWithParameterWithDefault =
@"Test(string someString = null)
{
}";
private const string ConstructorWithBody =
@"Test()
{
this.someString = someString;
}";
private const string ConstructorWithModifiersBodyAndParameter =
@"public public sealed Test(string someString = null)
{
this.someString = someString;
}";
private readonly ConstructorBuilder builder;
public ConstructorBuilderTests()
{
this.builder = new ConstructorBuilder(Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
_ = new ConstructorBuilder(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier(Public);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBase_NullBase_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBase(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(ParameterType, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(null, ParameterName);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithParameter(ParameterType, ParameterName);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody(Body);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsConstructorDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
result.Should().Be(PublicEmptyConstructor);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
result.Should().Be(PublicSealedEmptyConstructor);
}
[TestMethod]
public void WithParameter_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName).Build().ToFullString();
result.Should().Be(ConstructorWithParameter);
}
[TestMethod]
public void WithParameter_WithDefault_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName, Null).Build().ToFullString();
result.Should().Be(ConstructorWithParameterWithDefault);
}
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody(Body).Build().ToFullString();
result.Should().Be(ConstructorWithBody);
}
[TestMethod]
public void WithBody_WithParameter_WithModifier_WithModifiers_ReturnsExpectedString()
{
var result = this.builder
.WithBody(Body)
.WithParameter(ParameterType, ParameterName, Null)
.WithModifier(Public)
.WithModifiers(PublicSealed)
.Build()
.ToFullString();
result.Should().Be(ConstructorWithModifiersBodyAndParameter);
}
}
}
+109
View File
@@ -0,0 +1,109 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class FieldBuilderTests
{
private const string Public = "public";
private const string PublicSealed = "public sealed";
private const string FieldName = "someString";
private const string FieldType = "string";
private const string PublicField = "public string someString;";
private const string PublicSealedField = "public sealed string someString;";
private readonly FieldBuilder builder;
public FieldBuilderTests()
{
this.builder = new FieldBuilder(FieldType, FieldName);
}
[TestMethod]
public void Constructor_NullFieldType_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new FieldBuilder(null, FieldName);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullFieldName_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new FieldBuilder(FieldType, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier(Public);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsFieldDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<FieldDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
result.Should().Be(PublicField);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
result.Should().Be(PublicSealedField);
}
}
}
+278
View File
@@ -0,0 +1,278 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class MethodBuilderTests
{
private const string Public = "public";
private const string PublicSealed = "public sealed";
private const string Null = "null";
private const string ParameterType = "string";
private const string ParameterName = "someString";
private const string Name = "Test";
private const string ReturnType = "string";
private const string Expression = "this.someString;";
private const string Body = "this.someString = someString;";
private const string PublicEmptyMethod = "public string Test() => throw new NotImplementedException() ;";
private const string PublicSealedEmptyMethod = "public sealed string Test() => throw new NotImplementedException() ;";
private const string EmptyMethodWithParameter = "string Test(string someString) => throw new NotImplementedException() ;";
private const string EmptyMethodWithParameterWithDefault = "string Test(string someString = null) => throw new NotImplementedException() ;";
private const string MethodWithArrowExpression = "string Test() => this.someString ;";
private const string PublicSealedMethodWithParameterAndExpression = "public sealed string Test(string someString = null) => this.someString ;";
private const string MethodWithBody =
@"string Test()
{
this.someString = someString;
}";
private const string PublicSealedMethodWithBodyAndParameter =
@"public sealed string Test(string someString = null)
{
this.someString = someString;
}";
private readonly MethodBuilder builder;
public MethodBuilderTests()
{
this.builder = new MethodBuilder(ReturnType, Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
_ = new MethodBuilder(ReturnType, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullReturnType_ThrowArgumentNullException()
{
var action = () =>
{
_ = new MethodBuilder(null, Name);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier(Public);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(ParameterType, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(null, ParameterName);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithParameter(ParameterType, ParameterName);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody(Body);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithExpression_ExpressionNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithExpression(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithExpression(Expression);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsMethodDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<MethodDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
result.Should().Be(PublicEmptyMethod);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
result.Should().Be(PublicSealedEmptyMethod);
}
[TestMethod]
public void WithParameter_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName).Build().ToFullString();
result.Should().Be(EmptyMethodWithParameter);
}
[TestMethod]
public void WithParameter_WithDefault_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName, Null).Build().ToFullString();
result.Should().Be(EmptyMethodWithParameterWithDefault);
}
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody(Body).Build().ToFullString();
result.Should().Be(MethodWithBody);
}
[TestMethod]
public void WithArrowExpression_ReturnsExpectedString()
{
var result = this.builder.WithExpression(Expression).Build().ToFullString();
result.Should().Be(MethodWithArrowExpression);
}
[TestMethod]
public void WithBody_OverwritesWithExpression_ReturnsExpectedString()
{
var result = this.builder
.WithExpression(Expression)
.WithBody(Body)
.Build()
.ToFullString();
result.Should().Be(MethodWithBody);
}
[TestMethod]
public void WithExpression_OverwritesWithBody_ReturnsExpectedString()
{
var result = this.builder
.WithBody(Body)
.WithExpression(Expression)
.Build()
.ToFullString();
result.Should().Be(MethodWithArrowExpression);
}
[TestMethod]
public void WithBody_WithModifiers_WithParameter_ReturnsExpectedString()
{
var result = this.builder
.WithBody(Body)
.WithModifiers(PublicSealed)
.WithParameter(ParameterType, ParameterName, Null)
.Build()
.ToFullString();
result.Should().Be(PublicSealedMethodWithBodyAndParameter);
}
[TestMethod]
public void WithExpression_WithModifiers_WithParameter_ReturnsExpectedString()
{
var result = this.builder
.WithExpression(Expression)
.WithModifiers(PublicSealed)
.WithParameter(ParameterType, ParameterName, Null)
.Build()
.ToFullString();
result.Should().Be(PublicSealedMethodWithParameterAndExpression);
}
}
}
+86
View File
@@ -0,0 +1,86 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class NamespaceBuilderTests
{
private const string Using = "System";
private const string Namespace = "Test";
private const string NamespaceWithUsing =
@"namespace Test
{
using System;
}";
private readonly NamespaceBuilder builder;
public NamespaceBuilderTests()
{
this.builder = new NamespaceBuilder(Namespace);
}
[TestMethod]
public void Constructor_NullNamespace_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new NamespaceBuilder(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithUsing_NullUsing_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithUsing(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithUsing_ValidUsing_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithUsing(Using);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithClass_NullClassBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithClass(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Build_ReturnsNamespaceDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<NamespaceDeclarationSyntax>();
}
[TestMethod]
public void WithUsing_ReturnsExpectedString()
{
var result = this.builder
.WithUsing(Using)
.Build()
.ToFullString();
result.Should().Be(NamespaceWithUsing);
}
}
}
+120
View File
@@ -0,0 +1,120 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
{
[TestClass]
public class PropertyBuilderTests
{
private const string Public = "public";
private const string PublicStatic = "public static";
private const string TypeName = "string";
private const string PropertyName = "SomeString";
private const string PublicProperty = "public string SomeString { }";
private const string PublicStaticProperty = "public static string SomeString { }";
private readonly PropertyBuilder builder;
public PropertyBuilderTests()
{
this.builder = new PropertyBuilder(TypeName, PropertyName);
}
[TestMethod]
public void Constructor_NullPropertyType_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new PropertyBuilder(null, PropertyName);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new PropertyBuilder(TypeName, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier(Public);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicStatic);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithAccessor_NullAccessor_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithAccessor(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Build_ReturnsPropertyDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<PropertyDeclarationSyntax>();
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
result.Should().Be(PublicProperty);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicStatic).Build().ToFullString();
result.Should().Be(PublicStaticProperty);
}
}
}
+3
View File
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("Sybil.UnitTests")]
+1 -1
View File
@@ -25,7 +25,7 @@ namespace Sybil
public ConstructorInitializerSyntax Build()
{
return ConstructorInitializerSyntax.NormalizeWhitespace();
return this.ConstructorInitializerSyntax.NormalizeWhitespace();
}
}
}
-2
View File
@@ -9,7 +9,6 @@ namespace Sybil
{
public sealed class ClassBuilder : IBuilder<ClassDeclarationSyntax>
{
private readonly string className;
private ClassDeclarationSyntax ClassDeclaration { get; set; }
private readonly List<ConstructorBuilder> Constructors = new List<ConstructorBuilder>();
@@ -22,7 +21,6 @@ namespace Sybil
{
_ = string.IsNullOrWhiteSpace(className) ? throw new ArgumentNullException(nameof(className)) : className;
this.className = className;
this.ClassDeclaration = SyntaxFactory.ClassDeclaration(className);
}
+2
View File
@@ -23,6 +23,8 @@ namespace Sybil
public ConstructorBuilder WithBase(BaseConstructorBuilder baseConstructorBuilder)
{
_ = baseConstructorBuilder ?? throw new ArgumentNullException(nameof(baseConstructorBuilder));
this.baseConstructorBuilder = baseConstructorBuilder;
return this;
}
+2 -5
View File
@@ -31,6 +31,8 @@ namespace Sybil
public NamespaceBuilder WithClass(ClassBuilder classBuilder)
{
_ = classBuilder ?? throw new ArgumentNullException(nameof(classBuilder));
this.classBuilders.Add(classBuilder);
return this;
@@ -42,10 +44,5 @@ namespace Sybil
.AddMembers(this.classBuilders.Select(c => c.Build()).ToArray())
.NormalizeWhitespace();
}
public static NamespaceBuilder Create(string @namespace)
{
return new NamespaceBuilder(@namespace);
}
}
}
+2
View File
@@ -41,6 +41,8 @@ namespace Sybil
}
public PropertyBuilder WithAccessor(AccessorBuilder accessorBuilder)
{
_ = accessorBuilder ?? throw new ArgumentNullException(nameof(accessorBuilder));
this.accessors.Add(accessorBuilder);
return this;