diff --git a/LICENSE b/LICENSE index 6e3e5eb..99a4191 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Macocian Alexandru Victor +Copyright (c) 2024 Macocian Alexandru Victor Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 4a459a9..38c484b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,84 @@ # Sybil C# Syntax builders following builder pattern + +## Usage + +Sybil supports building of namespaces with classes, constructors, parameters, properties, fields and methods. +Example of creating a namespace with one class with one constructor and base class, multiple fields, properties and methods: +```C# +var namespaceSyntax = SyntaxBuilder.CreateNamespace("TestNamespace") + .WithUsing("System") + .WithClass( + SyntaxBuilder.CreateClass("TestClass") + .WithBaseClass("BaseTestClass") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute") + .WithArgument("SomeIntProperty", 1) + .WithArgument("SomeStringProperty", "Hello") + .WithArgument("SomeEnumProperty", MyEnum.Value) + .WithArgument("SomeTypeProperty", typeof(string)) + .WithNullArgument("SomeNullProperty")) + .WithModifiers("public sealed") + .WithConstructor( + SyntaxBuilder.CreateConstructor("TestClass") + .WithBase( + SyntaxBuilder.CreateBaseConstructor() + .WithArgument("fieldString")) + .WithAttribute( + SyntaxBuilder.CreateAttribute("SomeAttribute5") + .WithNullArgument()) + .WithParameter("string", "fieldString") + .WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();")) + .WithField( + SyntaxBuilder.CreateField("string", "fieldString") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute2")) + .WithModifier("private")) + .WithProperty( + SyntaxBuilder.CreateProperty("string", "PropertyString") + .WithModifier("public") + .WithAccessor( + SyntaxBuilder.CreateSetter() + .WithModifier("private")) + .WithAccessor( + SyntaxBuilder.CreateGetter()) + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute3") + .WithArgument(typeof(string)))) + .WithMethod( + SyntaxBuilder.CreateMethod("string", "GetFieldString") + .WithModifier("public") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute4") + .WithArgument(0.5f)) + .WithBody("return this.fieldString;"))) + .Build(); + + var namespaceString = namespaceSyntax.ToFullString(); +``` + +This generates the following syntax: +```C# +namespace TestNamespace +{ + using System; + + [SomeAttribute(SomeIntProperty = 1, SomeStringProperty = "Hello", SomeEnumProperty = MyEnum.Value, SomeTypeProperty = typeof(String), SomeNullProperty = null)] + public sealed class TestClass : BaseTestClass + { + [SomeAttribute5(null)] + TestClass(string fieldString) : base(fieldString) + { + this.fieldString = fieldString ?? throw new ArgumentNullException(); + } + + [SomeAttribute2] + private string fieldString; + [SomeAttribute3(typeof(String))] + public string PropertyString { private set; get; } + + [SomeAttribute4(0.5F)] + public string GetFieldString() + { + return this.fieldString; + } + } +} +``` \ No newline at end of file diff --git a/Sybil.IntegrationTests/FlowTests.cs b/Sybil.IntegrationTests/FlowTests.cs index 8cad274..025799b 100644 --- a/Sybil.IntegrationTests/FlowTests.cs +++ b/Sybil.IntegrationTests/FlowTests.cs @@ -1,26 +1,32 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Sybil.IntegrationTests.Models; -namespace Sybil.IntegrationTests +namespace Sybil.IntegrationTests; + +[TestClass] +public sealed class FlowTests { - [TestClass] - public sealed class FlowTests - { - private const string ExpectedNamespace = + private const string ExpectedNamespace = @"namespace TestNamespace { using System; + [SomeAttribute(SomeIntProperty = 1, SomeStringProperty = ""Hello"", SomeEnumProperty = MyEnum.Value, SomeTypeProperty = typeof(String), SomeNullProperty = null)] public sealed class TestClass : BaseTestClass { + [SomeAttribute5(null)] TestClass(string fieldString) : base(fieldString) { this.fieldString = fieldString ?? throw new ArgumentNullException(); } + [SomeAttribute2] private string fieldString; + [SomeAttribute3(typeof(String))] public string PropertyString { private set; get; } + [SomeAttribute4(0.5F)] public string GetFieldString() { return this.fieldString; @@ -28,41 +34,54 @@ namespace Sybil.IntegrationTests } }"; - [TestMethod] - public void NewNamespace_GeneratesExpected() - { - var namespaceSyntax = SyntaxBuilder.CreateNamespace("TestNamespace") - .WithUsing("System") - .WithClass( - SyntaxBuilder.CreateClass("TestClass") - .WithBaseClass("BaseTestClass") - .WithModifiers("public sealed") - .WithConstructor( - SyntaxBuilder.CreateConstructor("TestClass") - .WithBase( - SyntaxBuilder.CreateBaseConstructor() - .WithArgument("fieldString")) - .WithParameter("string", "fieldString") - .WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();")) - .WithField( - SyntaxBuilder.CreateField("string", "fieldString") + [TestMethod] + public void NewNamespace_GeneratesExpected() + { + var namespaceSyntax = SyntaxBuilder.CreateNamespace("TestNamespace") + .WithUsing("System") + .WithClass( + SyntaxBuilder.CreateClass("TestClass") + .WithBaseClass("BaseTestClass") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute") + .WithArgument("SomeIntProperty", 1) + .WithArgument("SomeStringProperty", "Hello") + .WithArgument("SomeEnumProperty", MyEnum.Value) + .WithArgument("SomeTypeProperty", typeof(string)) + .WithNullArgument("SomeNullProperty")) + .WithModifiers("public sealed") + .WithConstructor( + SyntaxBuilder.CreateConstructor("TestClass") + .WithBase( + SyntaxBuilder.CreateBaseConstructor() + .WithArgument("fieldString")) + .WithAttribute( + SyntaxBuilder.CreateAttribute("SomeAttribute5") + .WithNullArgument()) + .WithParameter("string", "fieldString") + .WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();")) + .WithField( + SyntaxBuilder.CreateField("string", "fieldString") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute2")) + .WithModifier("private")) + .WithProperty( + SyntaxBuilder.CreateProperty("string", "PropertyString") + .WithModifier("public") + .WithAccessor( + SyntaxBuilder.CreateSetter() .WithModifier("private")) - .WithProperty( - SyntaxBuilder.CreateProperty("string", "PropertyString") - .WithModifier("public") - .WithAccessor( - SyntaxBuilder.CreateSetter() - .WithModifier("private")) - .WithAccessor( - SyntaxBuilder.CreateGetter())) - .WithMethod( - SyntaxBuilder.CreateMethod("string", "GetFieldString") - .WithModifier("public") - .WithBody("return this.fieldString;"))) - .Build(); + .WithAccessor( + SyntaxBuilder.CreateGetter()) + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute3") + .WithArgument(typeof(string)))) + .WithMethod( + SyntaxBuilder.CreateMethod("string", "GetFieldString") + .WithModifier("public") + .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute4") + .WithArgument(0.5f)) + .WithBody("return this.fieldString;"))) + .Build(); - var namespaceString = namespaceSyntax.ToFullString(); - namespaceString.Should().Be(ExpectedNamespace); - } + var namespaceString = namespaceSyntax.ToFullString(); + namespaceString.Should().Be(ExpectedNamespace); } } diff --git a/Sybil.IntegrationTests/Models/MyEnum.cs b/Sybil.IntegrationTests/Models/MyEnum.cs new file mode 100644 index 0000000..ffd9708 --- /dev/null +++ b/Sybil.IntegrationTests/Models/MyEnum.cs @@ -0,0 +1,7 @@ +namespace Sybil.IntegrationTests.Models +{ + public enum MyEnum + { + Value + } +} diff --git a/Sybil.IntegrationTests/Sybil.IntegrationTests.csproj b/Sybil.IntegrationTests/Sybil.IntegrationTests.csproj index f2037c1..4281029 100644 --- a/Sybil.IntegrationTests/Sybil.IntegrationTests.csproj +++ b/Sybil.IntegrationTests/Sybil.IntegrationTests.csproj @@ -1,7 +1,7 @@  - net6.0 + net8.0 enable false diff --git a/Sybil.UnitTests/AccessorBuilderTests.cs b/Sybil.UnitTests/AccessorBuilderTests.cs index fdee58e..04f1839 100644 --- a/Sybil.UnitTests/AccessorBuilderTests.cs +++ b/Sybil.UnitTests/AccessorBuilderTests.cs @@ -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(); + } + + [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } + 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(); - } + 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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/AttributeBuilderTests.cs b/Sybil.UnitTests/AttributeBuilderTests.cs new file mode 100644 index 0000000..cf2835a --- /dev/null +++ b/Sybil.UnitTests/AttributeBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void WithArgumentString_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, "Hello"); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentInt_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentFloat_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1f); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentDouble_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1d); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentLong_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1L); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentUint_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1U); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentUlong_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 1UL); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentShort_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, (short)1); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentUshort_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, (ushort)1); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentByte_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, (byte)1); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithArgumentChar_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithArgument(null, 'c'); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithNullArgument_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => + { + attributeBuilder.WithNullArgument(null); + }; + + action.Should().Throw(); + } + + [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(); + } + + [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); + } +} diff --git a/Sybil.UnitTests/BaseConstructorBuilderTests.cs b/Sybil.UnitTests/BaseConstructorBuilderTests.cs index 243a0a6..d9bfffa 100644 --- a/Sybil.UnitTests/BaseConstructorBuilderTests.cs +++ b/Sybil.UnitTests/BaseConstructorBuilderTests.cs @@ -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(); + } - action.Should().Throw(); - } + [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(); + } - syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); - } + [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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/ClassBuilderTests.cs b/Sybil.UnitTests/ClassBuilderTests.cs index 49746e4..5ac4056 100644 --- a/Sybil.UnitTests/ClassBuilderTests.cs +++ b/Sybil.UnitTests/ClassBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void WithModifier_ModifierNull_ThrowsArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new ClassBuilder(null); - }; + this.builder.WithModifier(null); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + returnedBuilder.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } + 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(); - } + 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(); - } - - [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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/ConstructorBuilderTests.cs b/Sybil.UnitTests/ConstructorBuilderTests.cs index a3229b5..ac23cde 100644 --- a/Sybil.UnitTests/ConstructorBuilderTests.cs +++ b/Sybil.UnitTests/ConstructorBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void WithModifier_ModifierNull_ThrowsArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new ConstructorBuilder(null); - }; + this.builder.WithModifier(null); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + 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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } + 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(); - } + 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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/FieldBuilderTests.cs b/Sybil.UnitTests/FieldBuilderTests.cs index 478e938..9d9fbfc 100644 --- a/Sybil.UnitTests/FieldBuilderTests.cs +++ b/Sybil.UnitTests/FieldBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void Constructor_NullFieldName_ThrowsArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new FieldBuilder(null, FieldName); - }; + _ = new FieldBuilder(FieldType, null); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } - - [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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/MethodBuilderTests.cs b/Sybil.UnitTests/MethodBuilderTests.cs index 96b8c13..b107b48 100644 --- a/Sybil.UnitTests/MethodBuilderTests.cs +++ b/Sybil.UnitTests/MethodBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void Constructor_NullReturnType_ThrowArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new MethodBuilder(ReturnType, null); - }; + _ = new MethodBuilder(null, Name); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + 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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } + 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(); - } + 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(); - } + 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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/NamespaceBuilderTests.cs b/Sybil.UnitTests/NamespaceBuilderTests.cs index ae532d9..426a3ab 100644 --- a/Sybil.UnitTests/NamespaceBuilderTests.cs +++ b/Sybil.UnitTests/NamespaceBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void WithUsing_NullUsing_ThrowsArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new NamespaceBuilder(null); - }; + this.builder.WithUsing(null); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } - - [TestMethod] - public void Build_ReturnsNamespaceDeclarationSyntax() - { - var syntax = this.builder.Build(); - - syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); - } - - [TestMethod] - public void WithUsing_ReturnsExpectedString() - { - var result = this.builder - .WithUsing(Using) - .Build() - .ToFullString(); - - result.Should().Be(NamespaceWithUsing); - } + result.Should().Be(NamespaceWithUsing); } } \ No newline at end of file diff --git a/Sybil.UnitTests/PropertyBuilderTests.cs b/Sybil.UnitTests/PropertyBuilderTests.cs index 8609a9b..a8d4e6b 100644 --- a/Sybil.UnitTests/PropertyBuilderTests.cs +++ b/Sybil.UnitTests/PropertyBuilderTests.cs @@ -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(); + } + + [TestMethod] + public void Constructor_NullPropertyName_ThrowsArgumentNullException() + { + var action = () => { - var action = () => - { - _ = new PropertyBuilder(null, PropertyName); - }; + _ = new PropertyBuilder(TypeName, null); + }; - action.Should().Throw(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); - } + action.Should().Throw(); + } - [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(); + } - [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(); - } + syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); + } - [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(); - } - - [TestMethod] - public void Build_ReturnsPropertyDeclarationSyntax() - { - var syntax = this.builder.Build(); - - syntax.Should().NotBeNull().And.Subject.Should().BeOfType(); - } - - [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); } } \ No newline at end of file diff --git a/Sybil.UnitTests/Sybil.UnitTests.csproj b/Sybil.UnitTests/Sybil.UnitTests.csproj index 1982f8c..1e39f04 100644 --- a/Sybil.UnitTests/Sybil.UnitTests.csproj +++ b/Sybil.UnitTests/Sybil.UnitTests.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable false diff --git a/Sybil/AttributeBuilder.cs b/Sybil/AttributeBuilder.cs new file mode 100644 index 0000000..8331b2c --- /dev/null +++ b/Sybil/AttributeBuilder.cs @@ -0,0 +1,417 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System; + +namespace Sybil +{ + public sealed class AttributeBuilder : IBuilder + { + private AttributeSyntax AttributeSyntax { get; set; } + + internal AttributeBuilder(string attributeName) + { + this.AttributeSyntax = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(attributeName)); + } + + public AttributeBuilder WithArgument(string propertyName, string value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentNullException(nameof(value)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, int value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, float value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, double value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, long value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, uint value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, ulong value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, short value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, ushort value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, byte value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, bool value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(value ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, char value) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithNullArgument(string propertyName) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, Type type) + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.TypeOfExpression(SyntaxFactory.ParseTypeName(type.Name)))); + + return this; + } + + public AttributeBuilder WithArgument(string propertyName, TEnum value) + where TEnum : Enum + { + if (string.IsNullOrWhiteSpace(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.NameEquals(propertyName), + null, + SyntaxFactory.MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.IdentifierName(typeof(TEnum).Name), + SyntaxFactory.IdentifierName(Enum.GetName(typeof(TEnum), value))))); + + return this; + } + + public AttributeBuilder WithArgument(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentNullException(nameof(value)); + } + + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(int value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(float value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(double value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(long value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(uint value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(ulong value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(short value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(ushort value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(byte value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithArgument(bool value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(value ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))); + + return this; + } + + public AttributeBuilder WithArgument(char value) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal(value)))); + + return this; + } + + public AttributeBuilder WithNullArgument() + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + + return this; + } + + public AttributeBuilder WithArgument(Type type) + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.TypeOfExpression( + SyntaxFactory.ParseTypeName(type.Name)))); + + return this; + } + + public AttributeBuilder WithArgument(TEnum value) + where TEnum : Enum + { + this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments( + SyntaxFactory.AttributeArgument( + SyntaxFactory.MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.IdentifierName(typeof(TEnum).Name), + SyntaxFactory.IdentifierName(Enum.GetName(typeof(TEnum), value))))); + + return this; + } + + public AttributeSyntax Build() + { + return this.AttributeSyntax + .NormalizeWhitespace(); + } + } +} diff --git a/Sybil/ClassBuilder.cs b/Sybil/ClassBuilder.cs index 5b36cba..915c9f2 100644 --- a/Sybil/ClassBuilder.cs +++ b/Sybil/ClassBuilder.cs @@ -11,6 +11,7 @@ namespace Sybil { private ClassDeclarationSyntax ClassDeclaration { get; set; } + private readonly List Attributes = new List(); private readonly List Constructors = new List(); private readonly List Fields = new List(); private readonly List Properties = new List(); @@ -80,9 +81,17 @@ namespace Sybil return this; } + public ClassBuilder WithAttribute(AttributeBuilder attributeBuilder) + { + this.Attributes.Add(attributeBuilder ?? throw new ArgumentNullException(nameof(attributeBuilder))); + + return this; + } + public ClassDeclarationSyntax Build() { return this.ClassDeclaration + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.Attributes.Select(p => p.Build()).ToArray()))) .AddMembers(this.Constructors.Select(p => p.Build()).ToArray()) .AddMembers(this.Fields.Select(p => p.Build()).ToArray()) .AddMembers(this.Properties.Select(p => p.Build()).ToArray()) diff --git a/Sybil/ConstructorBuilder.cs b/Sybil/ConstructorBuilder.cs index a2e988c..8b12158 100644 --- a/Sybil/ConstructorBuilder.cs +++ b/Sybil/ConstructorBuilder.cs @@ -2,12 +2,15 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using System; +using System.Collections.Generic; using System.Linq; namespace Sybil { public sealed class ConstructorBuilder : IBuilder { + private readonly List attributes = new List(); + private BaseConstructorBuilder baseConstructorBuilder; private ConstructorDeclarationSyntax ConstructorDeclarationSyntax { get; set; } @@ -28,6 +31,7 @@ namespace Sybil this.baseConstructorBuilder = baseConstructorBuilder; return this; } + public ConstructorBuilder WithModifiers(string modifiers) { _ = string.IsNullOrWhiteSpace(modifiers) ? throw new ArgumentNullException(nameof(modifiers)) : modifiers; @@ -36,6 +40,7 @@ namespace Sybil return this; } + public ConstructorBuilder WithModifier(string modifier) { _ = string.IsNullOrWhiteSpace(modifier) ? throw new ArgumentNullException(nameof(modifier)) : modifier; @@ -44,6 +49,7 @@ namespace Sybil return this; } + public ConstructorBuilder WithParameter(string parameterType, string parameterName, string defaultValue = null) { _ = string.IsNullOrWhiteSpace(parameterName) ? throw new ArgumentNullException(nameof(parameterName)) : parameterName; @@ -66,6 +72,7 @@ namespace Sybil return this; } + public ConstructorBuilder WithBody(string body) { _ = string.IsNullOrWhiteSpace(body) ? throw new ArgumentNullException(nameof(body)) : body; @@ -75,6 +82,14 @@ namespace Sybil return this; } + public ConstructorBuilder WithAttribute(AttributeBuilder attributeBuilder) + { + _ = attributeBuilder ?? throw new ArgumentNullException(nameof(attributeBuilder)); + + this.attributes.Add(attributeBuilder); + return this; + } + public ConstructorDeclarationSyntax Build() { if (this.baseConstructorBuilder is null is false) @@ -82,7 +97,9 @@ namespace Sybil this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithInitializer(this.baseConstructorBuilder.Build()); } - return this.ConstructorDeclarationSyntax.NormalizeWhitespace(); + return this.ConstructorDeclarationSyntax + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.attributes.Select(p => p.Build()).ToArray()))) + .NormalizeWhitespace(); } } } diff --git a/Sybil/FieldBuilder.cs b/Sybil/FieldBuilder.cs index 959a622..be503a2 100644 --- a/Sybil/FieldBuilder.cs +++ b/Sybil/FieldBuilder.cs @@ -2,12 +2,14 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using System; +using System.Collections.Generic; using System.Linq; namespace Sybil { public sealed class FieldBuilder : IBuilder { + private readonly List attributeBuilders = new List(); private FieldDeclarationSyntax FieldDeclarationSyntax { get; set; } internal FieldBuilder( @@ -31,6 +33,7 @@ namespace Sybil return this; } + public FieldBuilder WithModifiers(string modifiers) { _ = string.IsNullOrWhiteSpace(modifiers) ? throw new ArgumentNullException(nameof(modifiers)) : modifiers; @@ -40,9 +43,18 @@ namespace Sybil return this; } + public FieldBuilder WithAttribute(AttributeBuilder attributeBuilder) + { + this.attributeBuilders.Add(attributeBuilder ?? throw new ArgumentNullException(nameof(attributeBuilder))); + + return this; + } + public FieldDeclarationSyntax Build() { - return this.FieldDeclarationSyntax.NormalizeWhitespace(); + return this.FieldDeclarationSyntax + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.attributeBuilders.Select(p => p.Build()).ToArray()))) + .NormalizeWhitespace(); } } } diff --git a/Sybil/MethodBuilder.cs b/Sybil/MethodBuilder.cs index 7ff3c1f..6a6d9c5 100644 --- a/Sybil/MethodBuilder.cs +++ b/Sybil/MethodBuilder.cs @@ -2,12 +2,15 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using System; +using System.Collections.Generic; using System.Linq; namespace Sybil { public sealed class MethodBuilder : IBuilder { + private readonly List attributeBuilders = new List(); + private BlockSyntax BlockBody { get; set; } private ArrowExpressionClauseSyntax ArrowExpression { get; set; } private MethodDeclarationSyntax MethodDeclarationSyntax { get; set; } @@ -31,6 +34,7 @@ namespace Sybil return this; } + public MethodBuilder WithModifiers(string modifiers) { _ = string.IsNullOrWhiteSpace(modifiers) ? throw new ArgumentNullException(nameof(modifiers)) : modifiers; @@ -39,6 +43,7 @@ namespace Sybil return this; } + public MethodBuilder WithParameter(string parameterType, string parameterName, string defaultValue = null) { _ = string.IsNullOrWhiteSpace(parameterName) ? throw new ArgumentNullException(nameof(parameterName)) : parameterName; @@ -62,6 +67,7 @@ namespace Sybil return this; } + public MethodBuilder WithThisParameter(string parameterType, string parameterName) { _ = string.IsNullOrWhiteSpace(parameterName) ? throw new ArgumentNullException(nameof(parameterName)) : parameterName; @@ -78,6 +84,7 @@ namespace Sybil return this; } + public MethodBuilder WithBody(string body) { this.BlockBody = SyntaxFactory.Block( @@ -86,6 +93,7 @@ namespace Sybil return this; } + public MethodBuilder WithExpression(string expression) { this.ArrowExpression = SyntaxFactory.ArrowExpressionClause( @@ -95,14 +103,27 @@ namespace Sybil return this; } + public MethodBuilder WithAttribute(AttributeBuilder attributeBuilder) + { + this.attributeBuilders.Add(attributeBuilder ?? throw new ArgumentNullException(nameof(attributeBuilder))); + + return this; + } + public MethodDeclarationSyntax Build() { if (this.BlockBody is null is false) { - return this.MethodDeclarationSyntax.WithBody(this.BlockBody).NormalizeWhitespace(); + return this.MethodDeclarationSyntax + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.attributeBuilders.Select(p => p.Build()).ToArray()))) + .WithBody(this.BlockBody) + .NormalizeWhitespace(); } - return this.MethodDeclarationSyntax.WithExpressionBody(this.ArrowExpression).NormalizeWhitespace(); + return this.MethodDeclarationSyntax + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.attributeBuilders.Select(p => p.Build()).ToArray()))) + .WithExpressionBody(this.ArrowExpression) + .NormalizeWhitespace(); } } } diff --git a/Sybil/PropertyBuilder.cs b/Sybil/PropertyBuilder.cs index 95a17bd..2dc1926 100644 --- a/Sybil/PropertyBuilder.cs +++ b/Sybil/PropertyBuilder.cs @@ -9,6 +9,7 @@ namespace Sybil { public sealed class PropertyBuilder : IBuilder { + private readonly List attributes = new List(); private readonly List accessors = new List(); private PropertyDeclarationSyntax PropertyDeclarationSyntax { get; set; } @@ -31,6 +32,7 @@ namespace Sybil return this; } + public PropertyBuilder WithModifiers(string modifiers) { _ = string.IsNullOrWhiteSpace(modifiers) ? throw new ArgumentNullException(nameof(modifiers)) : modifiers; @@ -39,6 +41,7 @@ namespace Sybil return this; } + public PropertyBuilder WithAccessor(AccessorBuilder accessorBuilder) { _ = accessorBuilder ?? throw new ArgumentNullException(nameof(accessorBuilder)); @@ -48,9 +51,19 @@ namespace Sybil return this; } + public PropertyBuilder WithAttribute(AttributeBuilder attributeBuilder) + { + this.attributes.Add(attributeBuilder ?? throw new ArgumentNullException(nameof(attributeBuilder))); + + return this; + } + public PropertyDeclarationSyntax Build() { - return this.PropertyDeclarationSyntax.WithAccessorList(this.BuildAccessorList()).NormalizeWhitespace(); + return this.PropertyDeclarationSyntax + .WithAccessorList(this.BuildAccessorList()) + .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(this.attributes.Select(p => p.Build()).ToArray()))) + .NormalizeWhitespace(); } private AccessorListSyntax BuildAccessorList() diff --git a/Sybil/Sybil.csproj b/Sybil/Sybil.csproj index 0b8ae23..0993d2e 100644 --- a/Sybil/Sybil.csproj +++ b/Sybil/Sybil.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.3.0 + 0.4.0 diff --git a/Sybil/SyntaxBuilder.cs b/Sybil/SyntaxBuilder.cs index 5522ab7..298a6a0 100644 --- a/Sybil/SyntaxBuilder.cs +++ b/Sybil/SyntaxBuilder.cs @@ -4,41 +4,15 @@ namespace Sybil { public static class SyntaxBuilder { - public static NamespaceBuilder CreateNamespace(string @namespace) - { - return new NamespaceBuilder(@namespace); - } - public static ClassBuilder CreateClass(string className) - { - return new ClassBuilder(className); - } - public static ConstructorBuilder CreateConstructor(string className) - { - return new ConstructorBuilder(className); - } - public static BaseConstructorBuilder CreateBaseConstructor() - { - return new BaseConstructorBuilder(); - } - public static PropertyBuilder CreateProperty(string typeName, string propertyName) - { - return new PropertyBuilder(typeName, propertyName); - } - public static AccessorBuilder CreateSetter() - { - return new AccessorBuilder(SyntaxKind.SetAccessorDeclaration); - } - public static AccessorBuilder CreateGetter() - { - return new AccessorBuilder(SyntaxKind.GetAccessorDeclaration); - } - public static FieldBuilder CreateField(string fieldType, string fieldName) - { - return new FieldBuilder(fieldType, fieldName); - } - public static MethodBuilder CreateMethod(string returnType, string methodName) - { - return new MethodBuilder(returnType, methodName); - } + public static NamespaceBuilder CreateNamespace(string @namespace) => new NamespaceBuilder(@namespace); + public static ClassBuilder CreateClass(string className) => new ClassBuilder(className); + public static ConstructorBuilder CreateConstructor(string className) => new ConstructorBuilder(className); + public static BaseConstructorBuilder CreateBaseConstructor() => new BaseConstructorBuilder(); + public static PropertyBuilder CreateProperty(string typeName, string propertyName) => new PropertyBuilder(typeName, propertyName); + public static AccessorBuilder CreateSetter() => new AccessorBuilder(SyntaxKind.SetAccessorDeclaration); + public static AccessorBuilder CreateGetter() => new AccessorBuilder(SyntaxKind.GetAccessorDeclaration); + public static FieldBuilder CreateField(string fieldType, string fieldName) => new FieldBuilder(fieldType, fieldName); + public static MethodBuilder CreateMethod(string returnType, string methodName) => new MethodBuilder(returnType, methodName); + public static AttributeBuilder CreateAttribute(string attributeName) => new AttributeBuilder(attributeName); } }