mirror of
https://github.com/AlexMacocian/Sybil.git
synced 2026-07-15 15:19:59 +00:00
Add field initializers
This commit is contained in:
@@ -23,6 +23,7 @@ namespace TestNamespace
|
||||
this.PropertyString = string.Empty;
|
||||
}
|
||||
|
||||
private string someTypeString = $""{typeof(string)}"";
|
||||
[SomeAttribute2]
|
||||
private string fieldString;
|
||||
[SomeAttribute3(typeof(String))]
|
||||
@@ -64,6 +65,10 @@ namespace TestNamespace
|
||||
.WithNullArgument())
|
||||
.WithParameter("string", "fieldString")
|
||||
.WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();\rthis.PropertyString = string.Empty;"))
|
||||
.WithField(
|
||||
SyntaxBuilder.CreateField("string", "someTypeString")
|
||||
.WithModifier("private")
|
||||
.WithInitializer("$\"{typeof(string)}\""))
|
||||
.WithField(
|
||||
SyntaxBuilder.CreateField("string", "fieldString")
|
||||
.WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute2"))
|
||||
|
||||
@@ -14,6 +14,8 @@ public class FieldBuilderTests
|
||||
private const string FieldType = "string";
|
||||
private const string PublicField = "public string someString;";
|
||||
private const string PublicSealedField = "public sealed string someString;";
|
||||
private const string PublicSealedWithInitializer = "public sealed string someString = someValue;";
|
||||
private const string Initializer = "someValue";
|
||||
|
||||
private readonly FieldBuilder builder;
|
||||
|
||||
@@ -105,4 +107,25 @@ public class FieldBuilderTests
|
||||
|
||||
result.Should().Be(PublicSealedField);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WithInitializer_NullInitializer_ThrowsArgumentNullException()
|
||||
{
|
||||
var action = () =>
|
||||
{
|
||||
this.builder.WithInitializer(null);
|
||||
};
|
||||
|
||||
action.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WithInitializer_ReturnsExpectedString()
|
||||
{
|
||||
var result = this.builder.WithModifiers(PublicSealed)
|
||||
.WithInitializer(Initializer).Build().ToFullString();
|
||||
|
||||
|
||||
result.Should().Be(PublicSealedWithInitializer);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,23 @@ namespace Sybil
|
||||
return this;
|
||||
}
|
||||
|
||||
public FieldBuilder WithInitializer(string initializer)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(initializer))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(initializer));
|
||||
}
|
||||
|
||||
var variableDeclaration = this.FieldDeclarationSyntax.Declaration.Variables[0];
|
||||
variableDeclaration = variableDeclaration.WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(initializer)));
|
||||
this.FieldDeclarationSyntax = this.FieldDeclarationSyntax
|
||||
.WithDeclaration(
|
||||
this.FieldDeclarationSyntax.Declaration
|
||||
.WithVariables(SyntaxFactory.SeparatedList<VariableDeclaratorSyntax>(new SyntaxNodeOrToken[] { variableDeclaration })));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public FieldDeclarationSyntax Build()
|
||||
{
|
||||
if (this.attributeBuilders.Count > 0)
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.4.1</Version>
|
||||
<Version>0.5.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
Reference in New Issue
Block a user