diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs index 63dc3df..4b809ef 100644 --- a/CodeBlocker.Test/TemplateTests.cs +++ b/CodeBlocker.Test/TemplateTests.cs @@ -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 diff --git a/CodeBlocker/Templates/ConstructorTemplate.cs b/CodeBlocker/Templates/ConstructorTemplate.cs index 92bdba7..4915a51 100644 --- a/CodeBlocker/Templates/ConstructorTemplate.cs +++ b/CodeBlocker/Templates/ConstructorTemplate.cs @@ -14,14 +14,16 @@ public class ConstructorTemplate : MemberTemplate public Collection Parameters { get; } = []; /// - /// Gets the arguments passed to the base constructor, written verbatim. Empty omits the - /// : base(...) clause entirely. + /// Gets the arguments passed to the constructor initialiser, written verbatim. Empty omits the + /// clause when is and emits : this() + /// when is . /// public Collection BaseParameters { get; } = []; /// /// Gets or sets a value indicating whether the initialiser chains to this rather than - /// base. + /// base. When , an initialiser is always emitted, including the + /// zero-argument form : this(). /// public bool ChainsToThis { get; set; } @@ -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; }