Add support for base class (#4)

This commit is contained in:
2022-03-24 10:08:35 +01:00
committed by GitHub
parent 96099becdd
commit 0f71c3bdff
4 changed files with 48 additions and 2 deletions
+2 -1
View File
@@ -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")
+35
View File
@@ -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<ArgumentNullException>();
}
[TestMethod]
public void WithBaseClass_NullBaseClass_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithBaseClass(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithBaseClass_BaseClassValid_ReturnsBuilder()
{
var returnedBuilder = this.builder.WithBaseClass(Base);
returnedBuilder.Should().NotBeNull().And.Subject.Should().BeOfType<ClassBuilder>();
}
[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);
}
}
}
+10
View File
@@ -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;
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.2</Version>
<Version>0.2.0</Version>
</PropertyGroup>
<PropertyGroup>