diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs index c703d78..38558c2 100644 --- a/CodeBlocker.Test/TemplateTests.cs +++ b/CodeBlocker.Test/TemplateTests.cs @@ -189,6 +189,34 @@ public void ParameterWritesAQuotedEmptyDefaultValue() => "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 = \"\") { }"); + } + [TestMethod] public void ParameterWithAnEmptyUnquotedDefaultValueHasNone() => Assert.AreEqual("string separator", Render(new ParameterTemplate { Type = "string", Name = "separator", DefaultValue = "" })); @@ -246,6 +274,13 @@ public void FieldWritesAQuotedEmptyInitialiser() => "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() { @@ -351,6 +386,24 @@ public void AShorthandPropertyKeepsAQuotedEmptyInitialiser() 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() { diff --git a/CodeBlocker/Templates/TemplateBase.cs b/CodeBlocker/Templates/TemplateBase.cs index a962c5b..513bef9 100644 --- a/CodeBlocker/Templates/TemplateBase.cs +++ b/CodeBlocker/Templates/TemplateBase.cs @@ -24,6 +24,7 @@ public abstract class TemplateBase /// /// Gets or sets the initialiser or default value. Empty means no initialiser is written, unless /// is set, in which case it is the empty string "". + /// A value is read as empty. /// public string DefaultValue { get; set; } = string.Empty; @@ -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); } }