diff --git a/Sybil.IntegrationTests/FlowTests.cs b/Sybil.IntegrationTests/FlowTests.cs index 77bf053..8cad274 100644 --- a/Sybil.IntegrationTests/FlowTests.cs +++ b/Sybil.IntegrationTests/FlowTests.cs @@ -11,7 +11,7 @@ namespace Sybil.IntegrationTests { using System; - public sealed class TestClass + public sealed class TestClass : BaseTestClass { TestClass(string fieldString) : base(fieldString) { @@ -35,6 +35,7 @@ namespace Sybil.IntegrationTests .WithUsing("System") .WithClass( SyntaxBuilder.CreateClass("TestClass") + .WithBaseClass("BaseTestClass") .WithModifiers("public sealed") .WithConstructor( SyntaxBuilder.CreateConstructor("TestClass") diff --git a/Sybil.UnitTests/ClassBuilderTests.cs b/Sybil.UnitTests/ClassBuilderTests.cs index 347de2e..49746e4 100644 --- a/Sybil.UnitTests/ClassBuilderTests.cs +++ b/Sybil.UnitTests/ClassBuilderTests.cs @@ -9,6 +9,7 @@ namespace Sybil.Tests public class ClassBuilderTests { private const string Name = "Test"; + private const string Base = "Base"; private const string PublicClass = @"public class Test { @@ -16,6 +17,10 @@ namespace Sybil.Tests private const string PublicStaticClass = @"public static class Test { +}"; + private const string ClassWithBase = +@"class Test : Base +{ }"; private readonly ClassBuilder builder; @@ -118,6 +123,25 @@ namespace Sybil.Tests action.Should().Throw(); } + [TestMethod] + public void WithBaseClass_NullBaseClass_ThrowsArgumentNullException() + { + var action = () => + { + this.builder.WithBaseClass(null); + }; + + action.Should().Throw(); + } + + [TestMethod] + public void WithBaseClass_BaseClassValid_ReturnsBuilder() + { + var returnedBuilder = this.builder.WithBaseClass(Base); + + returnedBuilder.Should().NotBeNull().And.Subject.Should().BeOfType(); + } + [TestMethod] public void Build_ReturnsClassDeclarationSyntax() { @@ -141,5 +165,16 @@ namespace Sybil.Tests result.Should().Be(PublicStaticClass); } + + [TestMethod] + public void WithBaseClass_ReturnsExpectedString() + { + var result = this.builder + .WithBaseClass(Base) + .Build() + .ToFullString(); + + result.Should().Be(ClassWithBase); + } } } \ No newline at end of file diff --git a/Sybil/ClassBuilder.cs b/Sybil/ClassBuilder.cs index 76ea2c0..5b36cba 100644 --- a/Sybil/ClassBuilder.cs +++ b/Sybil/ClassBuilder.cs @@ -24,6 +24,16 @@ namespace Sybil this.ClassDeclaration = SyntaxFactory.ClassDeclaration(className); } + public ClassBuilder WithBaseClass(string baseClass) + { + _ = string.IsNullOrWhiteSpace(baseClass) ? throw new ArgumentNullException(nameof(baseClass)) : baseClass; + + this.ClassDeclaration = this.ClassDeclaration.AddBaseListTypes( + SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseClass))); + + return this; + } + public ClassBuilder WithModifier(string modifier) { _ = string.IsNullOrWhiteSpace(modifier) ? throw new ArgumentNullException(nameof(modifier)) : modifier; diff --git a/Sybil/Sybil.csproj b/Sybil/Sybil.csproj index b80bd0c..5eb13e5 100644 --- a/Sybil/Sybil.csproj +++ b/Sybil/Sybil.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.1.2 + 0.2.0