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
78 changes: 78 additions & 0 deletions CodeBlocker.Test/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ public void ParameterEscapesALoneSurrogateInAQuotedDefaultValue()
}
}

[TestMethod]
public void ParameterWritesAQuotedEmptyDefaultValue() =>
Assert.AreEqual(
"string separator = \"\"",
Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "", DefaultValueIsQuoted = true }));

[TestMethod]
public void ParameterWithAnEmptyUnquotedDefaultValueHasNone() =>
Assert.AreEqual("string separator", Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "" }));

[TestMethod]
public void AQuotedEmptyDefaultKeepsAParameterOptional()
{
MethodTemplate method = new()
{
Type = "void",
Name = "M",
Keywords = { "public" },
BodyFactory = _ => { },
Parameters =
{
new ParameterTemplate { Type = "int", Name = "count", DefaultValue = "0" },
new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "", DefaultValueIsQuoted = true },
},
};

Assert.AreEqual("public void M(int count = 0, string separator = \"\") { }\n", Render(method));
}

[TestMethod]
public void ParameterKeepsItsAttributesOnTheDeclarationLine()
{
Expand Down Expand Up @@ -102,6 +131,12 @@ public void FieldEscapesAQuotedInitialiser() =>
"private string p = \"C:\\\\temp\\\\\\\"x\\\"\";\n",
Render(new FieldTemplate { Type = "string", Name = "p", Keywords = { "private" }, DefaultValue = "C:\\temp\\\"x\"", DefaultValueIsQuoted = true }));

[TestMethod]
public void FieldWritesAQuotedEmptyInitialiser() =>
Assert.AreEqual(
"public const string Empty = \"\";\n",
Render(new FieldTemplate { Type = "string", Name = "Empty", Keywords = { "public", "const" }, DefaultValue = "", DefaultValueIsQuoted = true }));

[TestMethod]
public void FieldCommentsAndAttributesGoOnTheirOwnLines()
{
Expand Down Expand Up @@ -190,6 +225,49 @@ public void AShorthandPropertyKeepsItsInitialiser()
Assert.AreEqual("public int Count { get; set; } = 7;\n", Render(property));
}

[TestMethod]
public void AShorthandPropertyKeepsAQuotedEmptyInitialiser()
{
PropertyTemplate property = new()
{
Type = "string",
Name = "Prefix",
Keywords = { "public" },
Getter = AccessorTemplate.Auto(),
Setter = AccessorTemplate.Auto(),
DefaultValue = "",
DefaultValueIsQuoted = true,
};

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

[TestMethod]
public void ABracedPropertyKeepsAQuotedEmptyInitialiser()
{
PropertyTemplate property = new()
{
Type = "string",
Name = "Prefix",
Keywords = { "public" },
Getter = AccessorTemplate.Auto(),
Setter = new AccessorTemplate { Modifier = "private" },
DefaultValue = "",
DefaultValueIsQuoted = true,
};

Assert.AreEqual(
"""
public string Prefix
{
get;
private set;
} = "";

""".ReplaceLineEndings("\n"),
Render(property));
}

[TestMethod]
public void AnAccessorModifierForcesTheBracedForm()
{
Expand Down
6 changes: 2 additions & 4 deletions CodeBlocker/Templates/PropertyTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ private void WriteShorthand(CodeBlocker codeBlocker)

codeBlocker.Write(" }");
WriteDefaultValueTo(codeBlocker);
codeBlocker.WriteLine(DefaultValueIsSet ? ";" : string.Empty);
codeBlocker.WriteLine(HasDefaultValue ? ";" : string.Empty);
}

private bool DefaultValueIsSet => !string.IsNullOrEmpty(DefaultValue);

private void WriteAccessorList(CodeBlocker codeBlocker)
{
codeBlocker.WriteLine();
Expand All @@ -145,6 +143,6 @@ private void WriteAccessorList(CodeBlocker codeBlocker)
// Scope would not allow.
codeBlocker.Write("}");
WriteDefaultValueTo(codeBlocker);
codeBlocker.WriteLine(DefaultValueIsSet ? ";" : string.Empty);
codeBlocker.WriteLine(HasDefaultValue ? ";" : string.Empty);
}
}
13 changes: 10 additions & 3 deletions CodeBlocker/Templates/TemplateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public abstract class TemplateBase
public string Type { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the initialiser or default value. Empty means no initialiser is written.
/// 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>.
/// </summary>
public string DefaultValue { get; set; } = string.Empty;

Expand All @@ -33,6 +34,12 @@ public abstract class TemplateBase
/// </summary>
public bool DefaultValueIsQuoted { get; set; }

/// <summary>
/// Gets a value indicating whether an initialiser is written: <see cref="DefaultValue"/> is not
/// empty, or it is quoted, where empty raw text is the empty string literal.
/// </summary>
private protected bool HasDefaultValue => DefaultValueIsQuoted || !string.IsNullOrEmpty(DefaultValue);

/// <summary>
/// Gets the attributes to write before the declaration, each without its square brackets.
/// </summary>
Expand Down Expand Up @@ -90,15 +97,15 @@ internal virtual void Validate()
}

/// <summary>
/// Writes <c> = value</c> when <see cref="DefaultValue"/> is set, as an escaped string literal
/// Writes <c> = value</c> when <see cref="HasDefaultValue"/> is set, as an escaped string literal
/// when <see cref="DefaultValueIsQuoted"/> is set.
/// </summary>
/// <param name="codeBlocker">The <see cref="CodeBlocker"/> to write to.</param>
protected void WriteDefaultValueTo(CodeBlocker codeBlocker)
{
Ensure.NotNull(codeBlocker);

if (string.IsNullOrEmpty(DefaultValue))
if (!HasDefaultValue)
{
return;
}
Expand Down
Loading