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
20 changes: 20 additions & 0 deletions CodeBlocker.Test/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,26 @@ public Widget()
Render(constructor));
}

[TestMethod]
public void AConstructorCanChainToThisWithNoArguments()
{
ConstructorTemplate constructor = new()
{
Name = "Widget",
Keywords = { "public" },
Parameters = { new ParameterTemplate { Type = "int", Name = "count" } },
ChainsToThis = true,
};

Assert.AreEqual(
"""
public Widget(int count)
: this() { }

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

#endregion

#region OperatorTemplate
Expand Down
10 changes: 6 additions & 4 deletions CodeBlocker/Templates/ConstructorTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ public class ConstructorTemplate : MemberTemplate
public Collection<ParameterTemplate> Parameters { get; } = [];

/// <summary>
/// Gets the arguments passed to the base constructor, written verbatim. Empty omits the
/// <c>: base(...)</c> clause entirely.
/// Gets the arguments passed to the constructor initialiser, written verbatim. Empty omits the
/// clause when <see cref="ChainsToThis"/> is <see langword="false"/> and emits <c>: this()</c>
/// when <see cref="ChainsToThis"/> is <see langword="true"/>.
/// </summary>
public Collection<string> BaseParameters { get; } = [];

/// <summary>
/// Gets or sets a value indicating whether the initialiser chains to <c>this</c> rather than
/// <c>base</c>.
/// <c>base</c>. When <see langword="true"/>, an initialiser is always emitted, including the
/// zero-argument form <c>: this()</c>.
/// </summary>
public bool ChainsToThis { get; set; }

Expand Down Expand Up @@ -49,7 +51,7 @@ public override void WriteTo(CodeBlocker codeBlocker)

private void WriteInitialiserTo(CodeBlocker codeBlocker)
{
if (BaseParameters.Count == 0)
if (!ChainsToThis && BaseParameters.Count == 0)
{
return;
}
Expand Down