Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions CodeBlocker.Test/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,34 @@
"string separator = \"\"",
Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "", DefaultValueIsQuoted = true }));

[TestMethod]
public void ParameterWritesAQuotedNullDefaultValueAsEmpty() =>
// ktsu-dev/CodeBlocker#209: a null quoted default threw NullReferenceException mid-declaration.
Assert.AreEqual(
"string separator = \"\"",
Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = null!, DefaultValueIsQuoted = true }));

[TestMethod]
public void AQuotedNullDefaultKeepsAMethodWhole()
{
ClassTemplate type = new()
{
Name = "C",
Members =
{
new MethodTemplate
{
Type = "void",
Name = "M",
BodyFactory = _ => { },
Parameters = { new ParameterTemplate { Type = "string", Name = "s", DefaultValue = null!, DefaultValueIsQuoted = true } },
},
},
};

StringAssert.Contains(Render(type), "void M(string s = \"\") { }");

Check warning on line 217 in CodeBlocker.Test/TemplateTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'StringAssert.Contains'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_CodeBlocker&issues=AaHQZwsbD1ZcMAb9H2KC&open=AaHQZwsbD1ZcMAb9H2KC&pullRequest=210
}

[TestMethod]
public void ParameterWithAnEmptyUnquotedDefaultValueHasNone() =>
Assert.AreEqual("string separator", Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "" }));
Expand Down Expand Up @@ -246,6 +274,13 @@
"public const string Empty = \"\";\n",
Render(new FieldTemplate { Type = "string", Name = "Empty", Keywords = { "public", "const" }, DefaultValue = "", DefaultValueIsQuoted = true }));

[TestMethod]
public void FieldWritesAQuotedNullInitialiserAsEmpty() =>
// ktsu-dev/CodeBlocker#209.
Assert.AreEqual(
"private string label = \"\";\n",
Render(new FieldTemplate { Type = "string", Name = "label", Keywords = { "private" }, DefaultValue = null!, DefaultValueIsQuoted = true }));

[TestMethod]
public void FieldCommentsAndAttributesGoOnTheirOwnLines()
{
Expand Down Expand Up @@ -351,6 +386,24 @@
Assert.AreEqual("public string Prefix { get; set; } = \"\";\n", Render(property));
}

[TestMethod]
public void APropertyWritesAQuotedNullInitialiserAsEmpty()
{
// ktsu-dev/CodeBlocker#209.
PropertyTemplate property = new()
{
Type = "string",
Name = "Prefix",
Keywords = { "public" },
Getter = AccessorTemplate.Auto(),
Setter = AccessorTemplate.Auto(),
DefaultValue = null!,
DefaultValueIsQuoted = true,
};

Assert.AreEqual("public string Prefix { get; set; } = \"\";\n", Render(property));
}

[TestMethod]
public void ABracedPropertyKeepsAQuotedEmptyInitialiser()
{
Expand Down
3 changes: 2 additions & 1 deletion CodeBlocker/Templates/TemplateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class TemplateBase
/// <summary>
/// Gets or sets the initialiser or default value. Empty means no initialiser is written, unless
/// <see cref="DefaultValueIsQuoted"/> is set, in which case it is the empty string <c>""</c>.
/// A <see langword="null"/> value is read as empty.
/// </summary>
public string DefaultValue { get; set; } = string.Empty;

Expand Down Expand Up @@ -115,7 +116,7 @@ protected void WriteDefaultValueTo(CodeBlocker codeBlocker)
}

codeBlocker.Write(" = ");
codeBlocker.Write(DefaultValueIsQuoted ? TemplateRendering.QuoteStringLiteral(DefaultValue) : DefaultValue);
codeBlocker.Write(DefaultValueIsQuoted ? TemplateRendering.QuoteStringLiteral(DefaultValue ?? string.Empty) : DefaultValue);
}
}

Expand Down
Loading