Implement attribute builders

Add attributes to class, constructor, field, method and property
This commit is contained in:
2024-04-10 14:50:26 +02:00
parent b5cdfe0a4d
commit 673f8eb478
23 changed files with 2028 additions and 1030 deletions
+133 -134
View File
@@ -3,174 +3,173 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class AccessorBuilderTests
{
[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 =
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;
private readonly AccessorBuilder builder;
public AccessorBuilderTests()
public AccessorBuilderTests()
{
this.builder = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.GetAccessorDeclaration);
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsNullArgumentException()
{
var action = () =>
{
this.builder = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.GetAccessorDeclaration);
}
this.builder.WithModifier(null);
};
[TestMethod]
public void WithModifier_ModifierNull_ThrowsNullArgumentException()
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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithModifier("public");
this.builder.WithBody(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifiers(null);
};
this.builder.WithArrowExpression(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers("public static");
[TestMethod]
public void WithArrowExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArrowExpression("this.field;");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
[TestMethod]
public void Build_ReturnsAccessorDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<AccessorDeclarationSyntax>();
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody("return this.field;");
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier("public").Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(PublicGet);
}
[TestMethod]
public void WithArrowExpression_ExpressionNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithArrowExpression(null);
};
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers("public static").Build().ToFullString();
action.Should().Throw<ArgumentNullException>();
}
result.Should().Be(PublicStaticGet);
}
[TestMethod]
public void WithArrowExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArrowExpression("this.field;");
[TestMethod]
public void SetAccessor_ReturnsExpectedString()
{
var result = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.SetAccessorDeclaration).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(Set);
}
[TestMethod]
public void Build_ReturnsAccessorDeclarationSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody("return this.field;").Build().ToFullString();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<AccessorDeclarationSyntax>();
}
result.Should().Be(GetBodyField);
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier("public").Build().ToFullString();
[TestMethod]
public void WithArrowExpression_ReturnsExpectedString()
{
var result = this.builder.WithArrowExpression("this.field;").Build().ToFullString();
result.Should().Be(PublicGet);
}
result.Should().Be(GetExpressionField);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers("public static").Build().ToFullString();
[TestMethod]
public void WithBody_OverridesWithArrowExpression_ReturnsExpectedString()
{
var result = this.builder
.WithArrowExpression("this field;")
.WithBody("return this.field;")
.Build()
.ToFullString();
result.Should().Be(PublicStaticGet);
}
result.Should().Be(GetBodyField);
}
[TestMethod]
public void SetAccessor_ReturnsExpectedString()
{
var result = new AccessorBuilder(Microsoft.CodeAnalysis.CSharp.SyntaxKind.SetAccessorDeclaration).Build().ToFullString();
[TestMethod]
public void WithArrowExpression_OverridesWithBody_ReturnsExpectedString()
{
var result = this.builder
.WithBody("return this.field;")
.WithArrowExpression("this.field;")
.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);
}
result.Should().Be(GetExpressionField);
}
}
+436
View File
@@ -0,0 +1,436 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests;
[TestClass]
public class AttributeBuilderTests
{
private const string AttributeName = "SomeAttribute";
private const string PropertyName = "SomeProperty";
private const string StringValue = "SomeString";
private const int IntValue = 1;
private const uint UintValue = 1U;
private const long LongValue = 1L;
private const ulong UlongValue = 1UL;
private const short ShortValue = 1;
private const ushort UshortValue = 1;
private const float FloatValue = 1.5f;
private const double DoubleValue = 1.5;
private const byte ByteValue = 1;
private const char CharValue = 'c';
private const bool BoolTrueValue = true;
private const bool BoolFalseValue = false;
private const string AttributeWithPropertyNameAndStringValue = $"{AttributeName}({PropertyName} = \"{StringValue}\")";
private const string AttributeWithPropertyNameAndIntValue = $"{AttributeName}({PropertyName} = 1)";
private const string AttributeWithPropertyNameAndUintValue = $"{AttributeName}({PropertyName} = 1U)";
private const string AttributeWithPropertyNameAndLongValue = $"{AttributeName}({PropertyName} = 1L)";
private const string AttributeWithPropertyNameAndUlongValue = $"{AttributeName}({PropertyName} = 1UL)";
private const string AttributeWithPropertyNameAndShortValue = $"{AttributeName}({PropertyName} = 1)";
private const string AttributeWithPropertyNameAndUshortValue = $"{AttributeName}({PropertyName} = 1)";
private const string AttributeWithPropertyNameAndFloatValue = $"{AttributeName}({PropertyName} = 1.5F)";
private const string AttributeWithPropertyNameAndDoubleValue = $"{AttributeName}({PropertyName} = 1.5)";
private const string AttributeWithPropertyNameAndByteValue = $"{AttributeName}({PropertyName} = 1)";
private const string AttributeWithPropertyNameAndCharValue = $"{AttributeName}({PropertyName} = 'c')";
private const string AttributeWithPropertyNameAndBoolTrueValue = $"{AttributeName}({PropertyName} = true)";
private const string AttributeWithPropertyNameAndBoolFalseValue = $"{AttributeName}({PropertyName} = false)";
private const string AttributeWithPropertyNameAndNullValue = $"{AttributeName}({PropertyName} = null)";
private const string AttributeWithStringValue = $"{AttributeName}(\"{StringValue}\")";
private const string AttributeWithIntValue = $"{AttributeName}(1)";
private const string AttributeWithUintValue = $"{AttributeName}(1U)";
private const string AttributeWithLongValue = $"{AttributeName}(1L)";
private const string AttributeWithUlongValue = $"{AttributeName}(1UL)";
private const string AttributeWithShortValue = $"{AttributeName}(1)";
private const string AttributeWithUshortValue = $"{AttributeName}(1)";
private const string AttributeWithFloatValue = $"{AttributeName}(1.5F)";
private const string AttributeWithDoubleValue = $"{AttributeName}(1.5)";
private const string AttributeWithByteValue = $"{AttributeName}(1)";
private const string AttributeWithCharValue = $"{AttributeName}('c')";
private const string AttributeWithBoolTrueValue = $"{AttributeName}(true)";
private const string AttributeWithBoolFalseValue = $"{AttributeName}(false)";
private const string AttributeWithNullValue = $"{AttributeName}(null)";
private readonly AttributeBuilder attributeBuilder = SyntaxBuilder.CreateAttribute(AttributeName);
[TestMethod]
public void WithArgumentBool_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, false);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentString_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, "Hello");
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentInt_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentFloat_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1f);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentDouble_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1d);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentLong_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1L);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentUint_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1U);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentUlong_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 1UL);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentShort_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, (short)1);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentUshort_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, (ushort)1);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentByte_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, (byte)1);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentChar_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithArgument(null, 'c');
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithNullArgument_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
attributeBuilder.WithNullArgument(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgumentString_NullValue_ThrowsArgumentNullException()
{
var action = () =>
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
attributeBuilder.WithArgument("SomeProperty", (string)null);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgument_WithPropertyNameAndStringValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, StringValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndStringValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndIntValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, IntValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndIntValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndFloatValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, FloatValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndFloatValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndDoubleValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, DoubleValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndDoubleValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndLongValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, LongValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndLongValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndUintValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, UintValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndUintValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndUlongValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, UlongValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndUlongValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndShortValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, ShortValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndShortValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndUshortValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, UshortValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndUshortValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndByteValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, ByteValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndByteValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndBoolTrueValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, BoolTrueValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndBoolTrueValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndBoolFalseValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, BoolFalseValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndBoolFalseValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndCharValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(PropertyName, CharValue).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndCharValue);
}
[TestMethod]
public void WithArgument_WithPropertyNameAndNullValue_ReturnsExpected()
{
var result = attributeBuilder.WithNullArgument(PropertyName).Build().ToFullString();
result.Should().Be(AttributeWithPropertyNameAndNullValue);
}
[TestMethod]
public void WithArgument_WithStringValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(StringValue).Build().ToFullString();
result.Should().Be(AttributeWithStringValue);
}
[TestMethod]
public void WithArgument_WithIntValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(IntValue).Build().ToFullString();
result.Should().Be(AttributeWithIntValue);
}
[TestMethod]
public void WithArgument_WithFloatValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(FloatValue).Build().ToFullString();
result.Should().Be(AttributeWithFloatValue);
}
[TestMethod]
public void WithArgument_WithDoubleValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(DoubleValue).Build().ToFullString();
result.Should().Be(AttributeWithDoubleValue);
}
[TestMethod]
public void WithArgument_WithLongValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(LongValue).Build().ToFullString();
result.Should().Be(AttributeWithLongValue);
}
[TestMethod]
public void WithArgument_WithUintValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(UintValue).Build().ToFullString();
result.Should().Be(AttributeWithUintValue);
}
[TestMethod]
public void WithArgument_WithUlongValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(UlongValue).Build().ToFullString();
result.Should().Be(AttributeWithUlongValue);
}
[TestMethod]
public void WithArgument_WithShortValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(ShortValue).Build().ToFullString();
result.Should().Be(AttributeWithShortValue);
}
[TestMethod]
public void WithArgument_WithUshortValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(UshortValue).Build().ToFullString();
result.Should().Be(AttributeWithUshortValue);
}
[TestMethod]
public void WithArgument_WithByteValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(ByteValue).Build().ToFullString();
result.Should().Be(AttributeWithByteValue);
}
[TestMethod]
public void WithArgument_WithBoolTrueValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(BoolTrueValue).Build().ToFullString();
result.Should().Be(AttributeWithBoolTrueValue);
}
[TestMethod]
public void WithArgument_WithBoolFalseValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(BoolFalseValue).Build().ToFullString();
result.Should().Be(AttributeWithBoolFalseValue);
}
[TestMethod]
public void WithArgument_WithCharValue_ReturnsExpected()
{
var result = attributeBuilder.WithArgument(CharValue).Build().ToFullString();
result.Should().Be(AttributeWithCharValue);
}
[TestMethod]
public void WithArgument_WithNullValue_ReturnsExpected()
{
var result = attributeBuilder.WithNullArgument().Build().ToFullString();
result.Should().Be(AttributeWithNullValue);
}
}
+46 -47
View File
@@ -3,65 +3,64 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class BaseConstructorBuilderTests
{
[TestClass]
public class BaseConstructorBuilderTests
private const string EmptyBase = ": base()";
private const string TestBase = ": base(test)";
private readonly BaseConstructorBuilder builder;
public BaseConstructorBuilderTests()
{
private const string EmptyBase = ": base()";
private const string TestBase = ": base(test)";
this.builder = new BaseConstructorBuilder();
}
private readonly BaseConstructorBuilder builder;
public BaseConstructorBuilderTests()
[TestMethod]
public void WithArgument_NullArgument_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder = new BaseConstructorBuilder();
}
this.builder.WithArgument(null);
};
[TestMethod]
public void WithArgument_NullArgument_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithArgument(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithArgument_ValidArgument_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArgument("test");
[TestMethod]
public void WithArgument_ValidArgument_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithArgument("test");
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void Build_ReturnsConstructorInitializerSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void Build_ReturnsConstructorInitializerSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorInitializerSyntax>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorInitializerSyntax>();
}
[TestMethod]
public void EmptyBaseConstructor_ReturnsExpectedString()
{
var result = this.builder.Build().ToFullString();
[TestMethod]
public void EmptyBaseConstructor_ReturnsExpectedString()
{
var result = this.builder.Build().ToFullString();
result.Should().Be(EmptyBase);
}
result.Should().Be(EmptyBase);
}
[TestMethod]
public void WithArgument_ShouldReturnExpectedString()
{
var result = this.builder
.WithArgument("test")
.Build()
.ToFullString();
[TestMethod]
public void WithArgument_ShouldReturnExpectedString()
{
var result = this.builder
.WithArgument("test")
.Build()
.ToFullString();
result.Should().Be(TestBase);
}
result.Should().Be(TestBase);
}
}
+128 -129
View File
@@ -3,178 +3,177 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class ClassBuilderTests
{
[TestClass]
public class ClassBuilderTests
{
private const string Name = "Test";
private const string Base = "Base";
private const string PublicClass =
private const string Name = "Test";
private const string Base = "Base";
private const string PublicClass =
@"public class Test
{
}";
private const string PublicStaticClass =
private const string PublicStaticClass =
@"public static class Test
{
}";
private const string ClassWithBase =
private const string ClassWithBase =
@"class Test : Base
{
}";
private readonly ClassBuilder builder;
private readonly ClassBuilder builder;
public ClassBuilderTests()
public ClassBuilderTests()
{
this.builder = new ClassBuilder(Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
this.builder = new ClassBuilder(Name);
}
_ = new ClassBuilder(null);
};
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new ClassBuilder(null);
};
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithModifier("public");
this.builder.WithField(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
[TestMethod]
public void WithProperty_NullPropertyBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithModifiers(null);
};
this.builder.WithProperty(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
[TestMethod]
public void WithMethod_NullMethodBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
var returnedBuilder = this.builder.WithModifiers("public static");
this.builder.WithMethod(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithField_NullFieldBuilder_ThrowsArgumentNullException()
[TestMethod]
public void WithConstructor_NullConstructorBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithField(null);
};
this.builder.WithConstructor(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithProperty_NullPropertyBuilder_ThrowsArgumentNullException()
[TestMethod]
public void WithBaseClass_NullBaseClass_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithProperty(null);
};
this.builder.WithBaseClass(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithMethod_NullMethodBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithMethod(null);
};
[TestMethod]
public void WithBaseClass_BaseClassValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBaseClass(Base);
action.Should().Throw<ArgumentNullException>();
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().BeOfType<ClassBuilder>();
}
[TestMethod]
public void WithConstructor_NullConstructorBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithConstructor(null);
};
[TestMethod]
public void Build_ReturnsClassDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ClassDeclarationSyntax>();
}
[TestMethod]
public void WithBaseClass_NullBaseClass_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBaseClass(null);
};
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier("public").Build().ToFullString();
action.Should().Throw<ArgumentNullException>();
}
result.Should().Be(PublicClass);
}
[TestMethod]
public void WithBaseClass_BaseClassValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBaseClass(Base);
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers("public static").Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().BeOfType<ClassBuilder>();
}
result.Should().Be(PublicStaticClass);
}
[TestMethod]
public void Build_ReturnsClassDeclarationSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void WithBaseClass_ReturnsExpectedString()
{
var result = this.builder
.WithBaseClass(Base)
.Build()
.ToFullString();
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);
}
[TestMethod]
public void WithBaseClass_ReturnsExpectedString()
{
var result = this.builder
.WithBaseClass(Base)
.Build()
.ToFullString();
result.Should().Be(ClassWithBase);
}
result.Should().Be(ClassWithBase);
}
}
+157 -158
View File
@@ -3,221 +3,220 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class ConstructorBuilderTests
{
[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 =
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 =
private const string PublicSealedEmptyConstructor =
@"public sealed Test()
{
}";
private const string ConstructorWithParameter =
private const string ConstructorWithParameter =
@"Test(string someString)
{
}";
private const string ConstructorWithParameterWithDefault =
private const string ConstructorWithParameterWithDefault =
@"Test(string someString = null)
{
}";
private const string ConstructorWithBody =
private const string ConstructorWithBody =
@"Test()
{
this.someString = someString;
}";
private const string ConstructorWithModifiersBodyAndParameter =
private const string ConstructorWithModifiersBodyAndParameter =
@"public public sealed Test(string someString = null)
{
this.someString = someString;
}";
private readonly ConstructorBuilder builder;
private readonly ConstructorBuilder builder;
public ConstructorBuilderTests()
public ConstructorBuilderTests()
{
this.builder = new ConstructorBuilder(Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
this.builder = new ConstructorBuilder(Name);
}
_ = new ConstructorBuilder(null);
};
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new ConstructorBuilder(null);
};
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithModifier(Public);
this.builder.WithBase(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
[TestMethod]
public void WithParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithModifiers(null);
};
this.builder.WithParameter(ParameterType, null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
this.builder.WithParameter(null, ParameterName);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithBase_NullBase_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithBase(null);
};
this.builder.WithBody(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(ParameterType, null);
};
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody(Body);
action.Should().Throw<ArgumentNullException>();
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithParameter(null, ParameterName);
};
[TestMethod]
public void Build_ReturnsConstructorDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorDeclarationSyntax>();
}
[TestMethod]
public void WithParameter_ParameterValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithParameter(ParameterType, ParameterName);
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(PublicEmptyConstructor);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
action.Should().Throw<ArgumentNullException>();
}
result.Should().Be(PublicSealedEmptyConstructor);
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody(Body);
[TestMethod]
public void WithParameter_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(ConstructorWithParameter);
}
[TestMethod]
public void Build_ReturnsConstructorDeclarationSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void WithParameter_WithDefault_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName, Null).Build().ToFullString();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<ConstructorDeclarationSyntax>();
}
result.Should().Be(ConstructorWithParameterWithDefault);
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody(Body).Build().ToFullString();
result.Should().Be(PublicEmptyConstructor);
}
result.Should().Be(ConstructorWithBody);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
[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(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);
}
result.Should().Be(ConstructorWithModifiersBodyAndParameter);
}
}
+80 -81
View File
@@ -3,107 +3,106 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class FieldBuilderTests
{
[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()
{
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;";
this.builder = new FieldBuilder(FieldType, FieldName);
}
private readonly FieldBuilder builder;
public FieldBuilderTests()
[TestMethod]
public void Constructor_NullFieldType_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder = new FieldBuilder(FieldType, FieldName);
}
_ = new FieldBuilder(null, FieldName);
};
[TestMethod]
public void Constructor_NullFieldType_ThrowsArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullFieldName_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new FieldBuilder(null, FieldName);
};
_ = new FieldBuilder(FieldType, null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullFieldName_ThrowsArgumentNullException()
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new FieldBuilder(FieldType, null);
};
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifier(Public);
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
[TestMethod]
public void Build_ReturnsFieldDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<FieldDeclarationSyntax>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(PublicField);
}
[TestMethod]
public void Build_ReturnsFieldDeclarationSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
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);
}
result.Should().Be(PublicSealedField);
}
}
+244 -245
View File
@@ -3,315 +3,314 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class MethodBuilderTests
{
[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 EmptyMethodWithThisParameter = "string Test(this 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 =
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 EmptyMethodWithThisParameter = "string Test(this 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 =
private const string PublicSealedMethodWithBodyAndParameter =
@"public sealed string Test(string someString = null)
{
this.someString = someString;
}";
private readonly MethodBuilder builder;
private readonly MethodBuilder builder;
public MethodBuilderTests()
public MethodBuilderTests()
{
this.builder = new MethodBuilder(ReturnType, Name);
}
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
{
var action = () =>
{
this.builder = new MethodBuilder(ReturnType, Name);
}
_ = new MethodBuilder(ReturnType, null);
};
[TestMethod]
public void Constructor_NullName_ThrowArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullReturnType_ThrowArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new MethodBuilder(ReturnType, null);
};
_ = new MethodBuilder(null, Name);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullReturnType_ThrowArgumentNullException()
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new MethodBuilder(null, Name);
};
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithModifier(Public);
this.builder.WithParameter(ParameterType, null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithModifiers(null);
};
this.builder.WithParameter(null, ParameterName);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
[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 WithThisParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
var returnedBuilder = this.builder.WithModifiers(PublicSealed);
this.builder.WithThisParameter(ParameterType, null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterNameNull_ThrowsArgumentNullException()
[TestMethod]
public void WithThisParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithParameter(ParameterType, null);
};
this.builder.WithThisParameter(null, ParameterName);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterTypeNull_ThrowsArgumentNullException()
[TestMethod]
public void WithThisParameter_ParameterValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithThisParameter(ParameterType, ParameterName);
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
this.builder.WithParameter(null, ParameterName);
};
this.builder.WithBody(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithParameter_ParameterValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithParameter(ParameterType, ParameterName);
this.builder.WithExpression(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithThisParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithThisParameter(ParameterType, null);
};
[TestMethod]
public void WithExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithExpression(Expression);
action.Should().Throw<ArgumentNullException>();
}
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithThisParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithThisParameter(null, ParameterName);
};
[TestMethod]
public void Build_ReturnsMethodDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<MethodDeclarationSyntax>();
}
[TestMethod]
public void WithThisParameter_ParameterValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithThisParameter(ParameterType, ParameterName);
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(PublicEmptyMethod);
}
[TestMethod]
public void WithBody_BodyNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBody(null);
};
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
action.Should().Throw<ArgumentNullException>();
}
result.Should().Be(PublicSealedEmptyMethod);
}
[TestMethod]
public void WithBody_BodyValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBody(Body);
[TestMethod]
public void WithParameter_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(EmptyMethodWithParameter);
}
[TestMethod]
public void WithExpression_ExpressionNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithExpression(null);
};
[TestMethod]
public void WithParameter_WithDefault_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName, Null).Build().ToFullString();
action.Should().Throw<ArgumentNullException>();
}
result.Should().Be(EmptyMethodWithParameterWithDefault);
}
[TestMethod]
public void WithExpression_ExpressionValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithExpression(Expression);
[TestMethod]
public void WithThisParameter_ReturnsExpectedString()
{
var result = this.builder.WithThisParameter(ParameterType, ParameterName).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(EmptyMethodWithThisParameter);
}
[TestMethod]
public void Build_ReturnsMethodDeclarationSyntax()
{
var syntax = this.builder.Build();
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
var result = this.builder.WithBody(Body).Build().ToFullString();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<MethodDeclarationSyntax>();
}
result.Should().Be(MethodWithBody);
}
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
[TestMethod]
public void WithArrowExpression_ReturnsExpectedString()
{
var result = this.builder.WithExpression(Expression).Build().ToFullString();
result.Should().Be(PublicEmptyMethod);
}
result.Should().Be(MethodWithArrowExpression);
}
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicSealed).Build().ToFullString();
[TestMethod]
public void WithBody_OverwritesWithExpression_ReturnsExpectedString()
{
var result = this.builder
.WithExpression(Expression)
.WithBody(Body)
.Build()
.ToFullString();
result.Should().Be(PublicSealedEmptyMethod);
}
result.Should().Be(MethodWithBody);
}
[TestMethod]
public void WithParameter_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName).Build().ToFullString();
[TestMethod]
public void WithExpression_OverwritesWithBody_ReturnsExpectedString()
{
var result = this.builder
.WithBody(Body)
.WithExpression(Expression)
.Build()
.ToFullString();
result.Should().Be(EmptyMethodWithParameter);
}
result.Should().Be(MethodWithArrowExpression);
}
[TestMethod]
public void WithParameter_WithDefault_ReturnsExpectedString()
{
var result = this.builder.WithParameter(ParameterType, ParameterName, Null).Build().ToFullString();
[TestMethod]
public void WithBody_WithModifiers_WithParameter_ReturnsExpectedString()
{
var result = this.builder
.WithBody(Body)
.WithModifiers(PublicSealed)
.WithParameter(ParameterType, ParameterName, Null)
.Build()
.ToFullString();
result.Should().Be(EmptyMethodWithParameterWithDefault);
}
result.Should().Be(PublicSealedMethodWithBodyAndParameter);
}
[TestMethod]
public void WithThisParameter_ReturnsExpectedString()
{
var result = this.builder.WithThisParameter(ParameterType, ParameterName).Build().ToFullString();
[TestMethod]
public void WithExpression_WithModifiers_WithParameter_ReturnsExpectedString()
{
var result = this.builder
.WithExpression(Expression)
.WithModifiers(PublicSealed)
.WithParameter(ParameterType, ParameterName, Null)
.Build()
.ToFullString();
result.Should().Be(EmptyMethodWithThisParameter);
}
[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);
}
result.Should().Be(PublicSealedMethodWithParameterAndExpression);
}
}
+60 -61
View File
@@ -3,84 +3,83 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class NamespaceBuilderTests
{
[TestClass]
public class NamespaceBuilderTests
{
private const string Using = "System";
private const string Namespace = "Test";
private const string NamespaceWithUsing =
private const string Using = "System";
private const string Namespace = "Test";
private const string NamespaceWithUsing =
@"namespace Test
{
using System;
}";
private readonly NamespaceBuilder builder;
private readonly NamespaceBuilder builder;
public NamespaceBuilderTests()
public NamespaceBuilderTests()
{
this.builder = new NamespaceBuilder(Namespace);
}
[TestMethod]
public void Constructor_NullNamespace_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder = new NamespaceBuilder(Namespace);
}
_ = new NamespaceBuilder(null);
};
[TestMethod]
public void Constructor_NullNamespace_ThrowsArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithUsing_NullUsing_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new NamespaceBuilder(null);
};
this.builder.WithUsing(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithUsing_NullUsing_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithUsing(null);
};
this.builder.WithClass(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithUsing_ValidUsing_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithUsing(Using);
[TestMethod]
public void Build_ReturnsNamespaceDeclarationSyntax()
{
var syntax = this.builder.Build();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<NamespaceDeclarationSyntax>();
}
[TestMethod]
public void WithClass_NullClassBuilder_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithClass(null);
};
[TestMethod]
public void WithUsing_ReturnsExpectedString()
{
var result = this.builder
.WithUsing(Using)
.Build()
.ToFullString();
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);
}
result.Should().Be(NamespaceWithUsing);
}
}
+90 -91
View File
@@ -3,118 +3,117 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests
namespace Sybil.Tests;
[TestClass]
public class PropertyBuilderTests
{
[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()
{
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 { }";
this.builder = new PropertyBuilder(TypeName, PropertyName);
}
private readonly PropertyBuilder builder;
public PropertyBuilderTests()
[TestMethod]
public void Constructor_NullPropertyType_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder = new PropertyBuilder(TypeName, PropertyName);
}
_ = new PropertyBuilder(null, PropertyName);
};
[TestMethod]
public void Constructor_NullPropertyType_ThrowsArgumentNullException()
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullPropertyName_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new PropertyBuilder(null, PropertyName);
};
_ = new PropertyBuilder(TypeName, null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Constructor_NullPropertyName_ThrowsArgumentNullException()
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
{
var action = () =>
{
var action = () =>
{
_ = new PropertyBuilder(TypeName, null);
};
this.builder.WithModifier(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierNull_ThrowsArgumentNullException()
[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 = () =>
{
var action = () =>
{
this.builder.WithModifier(null);
};
this.builder.WithModifiers(null);
};
action.Should().Throw<ArgumentNullException>();
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifier_ModifierValid_ReturnsBuilder()
[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 = () =>
{
var returnedBuilder = this.builder.WithModifier(Public);
this.builder.WithAccessor(null);
};
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithModifiers_ModifiersNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithModifiers(null);
};
[TestMethod]
public void Build_ReturnsPropertyDeclarationSyntax()
{
var syntax = this.builder.Build();
action.Should().Throw<ArgumentNullException>();
}
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<PropertyDeclarationSyntax>();
}
[TestMethod]
public void WithModifiers_ModifiersValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithModifiers(PublicStatic);
[TestMethod]
public void WithModifier_ReturnsExpectedString()
{
var result = this.builder.WithModifier(Public).Build().ToFullString();
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
result.Should().Be(PublicProperty);
}
[TestMethod]
public void WithAccessor_NullAccessor_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithAccessor(null);
};
[TestMethod]
public void WithModifiers_ReturnsExpectedString()
{
var result = this.builder.WithModifiers(PublicStatic).Build().ToFullString();
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);
}
result.Should().Be(PublicStaticProperty);
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>