diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs index 3c01f84..33d1c12 100644 --- a/CodeBlocker.Test/TemplateTests.cs +++ b/CodeBlocker.Test/TemplateTests.cs @@ -1552,6 +1552,95 @@ public void ASourceFileWithAnEmptyExpressionBodyWritesNothing() Assert.AreEqual(string.Empty, codeBlocker.ToString()); } + [TestMethod] + public void ANullMethodParameterIsRefusedBeforeTheClassIsWritten() + { + MethodTemplate method = new() { Name = "M", Type = "void", BodyFactory = _ => { } }; + method.Parameters.Add(null!); + ClassTemplate type = new() { Name = "C", Members = { method } }; + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException exception = + Assert.ThrowsExactly(() => codeBlocker.AddClass(type)); + Assert.Contains("'M'", exception.Message); + Assert.Contains("Parameters", exception.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullPositionalParameterIsRefusedBeforeTheRecordIsWritten() + { + ClassTemplate record = new() { Name = "R", Kind = TypeKind.Record }; + record.PositionalParameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException exception = + Assert.ThrowsExactly(() => codeBlocker.AddClass(record)); + Assert.Contains("'R'", exception.Message); + Assert.Contains("PositionalParameters", exception.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullConstructorParameterIsRefusedBeforeTheClassIsWritten() + { + ConstructorTemplate constructor = new() { Name = "C" }; + constructor.Parameters.Add(null!); + ClassTemplate type = new() { Name = "C", Members = { constructor } }; + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + Assert.ThrowsExactly(() => codeBlocker.AddClass(type)); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullOperatorParameterIsRefusedBeforeTheOperatorIsWritten() + { + OperatorTemplate op = new() { Type = "Money", Symbol = "-", Keywords = { "public", "static" } }; + op.Parameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + codeBlocker.WriteLine("int a;"); + + Assert.ThrowsExactly(() => op.WriteTo(codeBlocker)); + Assert.AreEqual("int a;\n", codeBlocker.ToString()); + } + + [TestMethod] + public void ANullMethodParameterIsRefusedWhenTheMethodIsWrittenOnItsOwn() + { + MethodTemplate method = new() { Name = "M", Type = "void", BodyFactory = _ => { } }; + method.Parameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + codeBlocker.WriteLine("int a;"); + + Assert.ThrowsExactly(() => method.WriteTo(codeBlocker)); + Assert.AreEqual("int a;\n", codeBlocker.ToString()); + } + + [TestMethod] + public void ANullMemberOrNestedClassIsRefusedWithTheTypeNamed() + { + ClassTemplate withNullMember = new() { Name = "A" }; + withNullMember.Members.Add(null!); + ClassTemplate withNullNested = new() { Name = "B" }; + withNullNested.NestedClasses.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException memberException = + Assert.ThrowsExactly(() => codeBlocker.AddClass(withNullMember)); + Assert.Contains("'A'", memberException.Message); + InvalidOperationException nestedException = + Assert.ThrowsExactly(() => codeBlocker.AddClass(withNullNested)); + Assert.Contains("'B'", nestedException.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + [TestMethod] public void AnInvalidMemberOfANestedClassLeavesTheOuterClassUnwritten() { diff --git a/CodeBlocker/Templates/ClassTemplate.cs b/CodeBlocker/Templates/ClassTemplate.cs index f359d45..30289d6 100644 --- a/CodeBlocker/Templates/ClassTemplate.cs +++ b/CodeBlocker/Templates/ClassTemplate.cs @@ -64,7 +64,9 @@ public class ClassTemplate : TemplateBase /// , or the kind of a nested type, is not a known kind. Nothing is written. /// /// - /// A member, or a member of a nested type, would be refused when written. Nothing is written. + /// A member, or a member of a nested type, would be refused when written, or + /// , or holds a + /// entry. Nothing is written. /// public override void WriteTo(CodeBlocker codeBlocker) { @@ -107,6 +109,14 @@ internal override void Validate(CodeBlocker codeBlocker) base.Validate(codeBlocker); _ = TypeKindKeywords.For(Kind); + // Null entries are refused here rather than found while writing: a null member or nested + // type would throw a bare NullReferenceException, and a null positional parameter would + // throw after "record R" is already written. + string owner = $"Type '{Name}'"; + TemplateRendering.EnsureNoNullEntries(Members, nameof(Members), owner); + TemplateRendering.EnsureNoNullEntries(NestedClasses, nameof(NestedClasses), owner); + TemplateRendering.EnsureNoNullEntries(PositionalParameters, nameof(PositionalParameters), owner); + foreach (MemberTemplate member in Members) { member.Validate(codeBlocker); diff --git a/CodeBlocker/Templates/ConstructorTemplate.cs b/CodeBlocker/Templates/ConstructorTemplate.cs index 05438c2..82d1f3c 100644 --- a/CodeBlocker/Templates/ConstructorTemplate.cs +++ b/CodeBlocker/Templates/ConstructorTemplate.cs @@ -39,16 +39,27 @@ public class ConstructorTemplate : MemberTemplate /// /// The to write to. /// is . + /// + /// holds a entry. Nothing is written. + /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); + Validate(codeBlocker); base.WriteTo(codeBlocker); TemplateRendering.WriteParameterList(codeBlocker, Parameters); WriteInitialiserTo(codeBlocker); TemplateRendering.WriteBody(codeBlocker, BodyFactory); } + /// + internal override void Validate(CodeBlocker codeBlocker) + { + base.Validate(codeBlocker); + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Constructor '{Name}'"); + } + private void WriteInitialiserTo(CodeBlocker codeBlocker) { List arguments = TemplateRendering.NonBlank(BaseParameters); diff --git a/CodeBlocker/Templates/MethodTemplate.cs b/CodeBlocker/Templates/MethodTemplate.cs index 70b4f91..37a201d 100644 --- a/CodeBlocker/Templates/MethodTemplate.cs +++ b/CodeBlocker/Templates/MethodTemplate.cs @@ -39,14 +39,25 @@ public class MethodTemplate : MemberTemplate /// /// The to write to. /// is . + /// + /// holds a entry. Nothing is written. + /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); + Validate(codeBlocker); base.WriteTo(codeBlocker); TemplateRendering.WriteTypeParameterList(codeBlocker, TypeParameters); TemplateRendering.WriteParameterList(codeBlocker, Parameters); TemplateRendering.WriteConstraints(codeBlocker, Constraints); TemplateRendering.WriteBody(codeBlocker, BodyFactory); } + + /// + internal override void Validate(CodeBlocker codeBlocker) + { + base.Validate(codeBlocker); + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Method '{Name}'"); + } } diff --git a/CodeBlocker/Templates/OperatorTemplate.cs b/CodeBlocker/Templates/OperatorTemplate.cs index 70a0fdd..251d2c1 100644 --- a/CodeBlocker/Templates/OperatorTemplate.cs +++ b/CodeBlocker/Templates/OperatorTemplate.cs @@ -60,14 +60,16 @@ public class OperatorTemplate : MemberTemplate /// The to write to. /// is . /// - /// is not an value. Nothing is written. + /// is not an value, or + /// holds a entry. Nothing is written. /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); - // Worked out before anything is written, so an unknown kind cannot leave a half-written - // declaration behind. + // Checked before anything is written, so an unknown kind or a null parameter cannot leave a + // half-written declaration behind. + Validate(codeBlocker); string signature = Signature; // Not through the MemberTemplate implementation: an operator's signature is its symbol and @@ -83,7 +85,8 @@ public override void WriteTo(CodeBlocker codeBlocker) internal override void Validate(CodeBlocker codeBlocker) { base.Validate(codeBlocker); - _ = Signature; + string signature = Signature; + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Operator '{signature}'"); } private string Signature => Kind switch diff --git a/CodeBlocker/Templates/TemplateRendering.cs b/CodeBlocker/Templates/TemplateRendering.cs index d257133..138b62b 100644 --- a/CodeBlocker/Templates/TemplateRendering.cs +++ b/CodeBlocker/Templates/TemplateRendering.cs @@ -200,6 +200,24 @@ private static bool IsLoneSurrogate(string value, int index) return false; } + /// + /// Throws if a template's collection holds a entry, which would otherwise + /// only be found partway through writing the declaration that lists it. + /// + /// The entry type. + /// The collection to check. + /// The collection's property name, for the message. + /// The template that holds the collection, for the message. + /// An entry is . + internal static void EnsureNoNullEntries(IEnumerable entries, string collectionName, string owner) + where T : class + { + if (entries.Any(entry => entry is null)) + { + throw new InvalidOperationException($"{owner} has a null entry in {collectionName}."); + } + } + /// /// Drops the , empty and whitespace entries from a list of verbatim /// fragments, so that a generator can pass condition ? "IFoo" : "" without the blank