Add support for "this" parameter (#5)

This commit is contained in:
2022-03-24 11:26:04 +01:00
committed by GitHub
parent 0f71c3bdff
commit c1ae1da60c
3 changed files with 57 additions and 2 deletions
+39
View File
@@ -20,6 +20,7 @@ namespace Sybil.Tests
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 ;";
@@ -131,6 +132,36 @@ namespace Sybil.Tests
returnedBuilder.Should().NotBeNull().And.Subject.Should().Be(this.builder);
}
[TestMethod]
public void WithThisParameter_ParameterNameNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithThisParameter(ParameterType, null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithThisParameter_ParameterTypeNull_ThrowsArgumentNullException()
{
var action = () =>
{
this.builder.WithThisParameter(null, ParameterName);
};
action.Should().Throw<ArgumentNullException>();
}
[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()
{
@@ -209,6 +240,14 @@ namespace Sybil.Tests
result.Should().Be(EmptyMethodWithParameterWithDefault);
}
[TestMethod]
public void WithThisParameter_ReturnsExpectedString()
{
var result = this.builder.WithThisParameter(ParameterType, ParameterName).Build().ToFullString();
result.Should().Be(EmptyMethodWithThisParameter);
}
[TestMethod]
public void WithBody_ReturnsExpectedString()
{
+17 -1
View File
@@ -53,7 +53,7 @@ namespace Sybil
var parameter = SyntaxFactory.Parameter(
SyntaxFactory.List<AttributeListSyntax>(),
new SyntaxTokenList(),
SyntaxFactory.TokenList(),
type: SyntaxFactory.ParseTypeName(parameterType),
identifier: SyntaxFactory.ParseToken(parameterName),
@default: equalsValueClauseSyntax);
@@ -62,6 +62,22 @@ namespace Sybil
return this;
}
public MethodBuilder WithThisParameter(string parameterType, string parameterName)
{
_ = string.IsNullOrWhiteSpace(parameterName) ? throw new ArgumentNullException(nameof(parameterName)) : parameterName;
_ = string.IsNullOrWhiteSpace(parameterType) ? throw new ArgumentNullException(nameof(parameterType)) : parameterType;
var parameter = SyntaxFactory.Parameter(
SyntaxFactory.List<AttributeListSyntax>(),
SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ThisKeyword)),
type: SyntaxFactory.ParseTypeName(parameterType),
identifier: SyntaxFactory.ParseToken(parameterName),
@default: null);
this.MethodDeclarationSyntax = this.MethodDeclarationSyntax.AddParameterListParameters(parameter);
return this;
}
public MethodBuilder WithBody(string body)
{
this.BlockBody = SyntaxFactory.Block(
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.2.0</Version>
<Version>0.3.0</Version>
</PropertyGroup>
<PropertyGroup>