|
| 1 | +// Copyright (c) 2023-2026 ktsu-dev contributors |
| 2 | + |
| 3 | +namespace ktsu.Coder.Test.Ast; |
| 4 | + |
| 5 | +using ktsu.Coder.Ast; |
| 6 | +using ktsu.Coder.Graph; |
| 7 | +using ktsu.Coder.Languages; |
| 8 | +using ktsu.Coder.Serialization; |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Tests for <see cref="CompileTimeAssertion"/>: what a generated type promises that the type itself |
| 13 | +/// cannot say. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Only C++ has anything checked before the program runs, so this is the clearest case of the rule |
| 17 | +/// the whole AST follows — say what is true, and let each language say as much of it as it can. The |
| 18 | +/// other three write a comment rather than dropping it, because a file that quietly loses a |
| 19 | +/// guarantee looks exactly like one that still makes it. |
| 20 | +/// </remarks> |
| 21 | +[TestClass] |
| 22 | +public class CompileTimeAssertionTests |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// C++ writes the assertion, with the message on its own line. |
| 26 | + /// </summary> |
| 27 | + [TestMethod] |
| 28 | + public void Cpp_WritesTheAssertion() |
| 29 | + { |
| 30 | + CompileTimeAssertion assertion = new( |
| 31 | + "std::is_trivially_copyable_v<RigidBody>", |
| 32 | + "RigidBody must be trivially copyable: it crosses the C# boundary and the wire as bytes"); |
| 33 | + |
| 34 | + Assert.AreEqual( |
| 35 | + "static_assert(std::is_trivially_copyable_v<RigidBody>,\n" |
| 36 | + + " \"RigidBody must be trivially copyable: it crosses the C# boundary and the wire as bytes\");\n", |
| 37 | + new CppGenerator().Generate(assertion).ReplaceLineEndings("\n")); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// An assertion with nothing to say when it fails is still an assertion. |
| 42 | + /// </summary> |
| 43 | + [TestMethod] |
| 44 | + public void Cpp_WritesAnAssertionWithNoMessage() |
| 45 | + { |
| 46 | + Assert.AreEqual( |
| 47 | + "static_assert(sizeof(Handle) == 8);\n", |
| 48 | + new CppGenerator().Generate(new CompileTimeAssertion("sizeof(Handle) == 8")).ReplaceLineEndings("\n")); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// A message with a quote in it is escaped rather than ending the string early. |
| 53 | + /// </summary> |
| 54 | + [TestMethod] |
| 55 | + public void Cpp_EscapesTheMessage() |
| 56 | + { |
| 57 | + CompileTimeAssertion assertion = new("sizeof(T) == 8", "a \"T\" is eight bytes"); |
| 58 | + |
| 59 | + Assert.Contains("\\\"T\\\"", new CppGenerator().Generate(assertion), StringComparison.Ordinal); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// The other three have nothing checked before the program runs, so they say what was asserted |
| 64 | + /// rather than dropping it. |
| 65 | + /// </summary> |
| 66 | + [TestMethod] |
| 67 | + public void OtherLanguages_SayWhatWasAsserted() |
| 68 | + { |
| 69 | + CompileTimeAssertion assertion = new("std::is_standard_layout_v<RigidBody>", "layout must be stable"); |
| 70 | + |
| 71 | + Assert.Contains( |
| 72 | + "// asserted at build time: std::is_standard_layout_v<RigidBody>", |
| 73 | + new CSharpGenerator().Generate(assertion), |
| 74 | + StringComparison.Ordinal); |
| 75 | + Assert.Contains( |
| 76 | + "# asserted at build time: std::is_standard_layout_v<RigidBody>", |
| 77 | + new PythonGenerator().Generate(assertion), |
| 78 | + StringComparison.Ordinal); |
| 79 | + Assert.Contains( |
| 80 | + "// asserted at build time: std::is_standard_layout_v<RigidBody>", |
| 81 | + new JavaScriptGenerator().Generate(assertion), |
| 82 | + StringComparison.Ordinal); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Assertions about one type stay together, and are separated from the declaration they are about. |
| 87 | + /// </summary> |
| 88 | + /// <remarks> |
| 89 | + /// The same rule the members of a type follow. Two of a kind that say nothing about themselves are |
| 90 | + /// one block; a struct followed by an assertion is two. |
| 91 | + /// </remarks> |
| 92 | + [TestMethod] |
| 93 | + public void Cpp_GroupsAssertionsAboutTheSameType() |
| 94 | + { |
| 95 | + SourceFile file = new("RigidBody.gen.hpp"); |
| 96 | + file.Members.Add(new ClassDeclaration("RigidBody") { Kind = TypeDeclarationKind.Struct }); |
| 97 | + file.Members.Add(new CompileTimeAssertion("std::is_trivially_copyable_v<RigidBody>", "bytes")); |
| 98 | + file.Members.Add(new CompileTimeAssertion("std::is_standard_layout_v<RigidBody>", "offsets")); |
| 99 | + |
| 100 | + string code = new CppGenerator().Generate(file).ReplaceLineEndings("\n"); |
| 101 | + |
| 102 | + Assert.Contains("};\n\nstatic_assert(", code, StringComparison.Ordinal); |
| 103 | + Assert.Contains("\"bytes\");\nstatic_assert(", code, StringComparison.Ordinal); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// An assertion survives a round trip through YAML, and a clone carries it. |
| 108 | + /// </summary> |
| 109 | + [TestMethod] |
| 110 | + public void Yaml_RoundTripsTheAssertion() |
| 111 | + { |
| 112 | + CompileTimeAssertion original = new("sizeof(Handle) == 8", "a handle is eight bytes"); |
| 113 | + |
| 114 | + string yaml = new YamlSerializer().Serialize(original); |
| 115 | + CompileTimeAssertion restored = (CompileTimeAssertion)new YamlDeserializer().Deserialize(yaml)!; |
| 116 | + |
| 117 | + Assert.AreEqual("sizeof(Handle) == 8", restored.Condition); |
| 118 | + Assert.AreEqual("a handle is eight bytes", restored.Message); |
| 119 | + |
| 120 | + CompileTimeAssertion clone = (CompileTimeAssertion)original.Clone(); |
| 121 | + |
| 122 | + Assert.AreEqual(original.Condition, clone.Condition); |
| 123 | + Assert.AreEqual(original.Message, clone.Message); |
| 124 | + Assert.AreNotSame(original, clone); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// A document says nothing for an assertion that asserts nothing. |
| 129 | + /// </summary> |
| 130 | + [TestMethod] |
| 131 | + public void Yaml_WritesNothingForAnEmptyAssertion() |
| 132 | + { |
| 133 | + string yaml = new YamlSerializer().Serialize(new CompileTimeAssertion()); |
| 134 | + |
| 135 | + Assert.DoesNotContain("condition", yaml, StringComparison.Ordinal); |
| 136 | + Assert.DoesNotContain("message", yaml, StringComparison.Ordinal); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// The editor can place one beside a declaration and edit both of its halves. |
| 141 | + /// </summary> |
| 142 | + [TestMethod] |
| 143 | + public void Graph_AcceptsAndEditsAnAssertion() |
| 144 | + { |
| 145 | + NamespaceDeclaration components = new("holo::components"); |
| 146 | + CompileTimeAssertion assertion = new("sizeof(Handle) == 8"); |
| 147 | + |
| 148 | + Assert.IsTrue(AstSchema.TryAttach(components, AstSchema.SlotsOf(components)[0], assertion)); |
| 149 | + Assert.IsTrue(AstFields.TryWrite(assertion, "Message", "a handle is eight bytes")); |
| 150 | + |
| 151 | + Assert.AreEqual("a handle is eight bytes", assertion.Message); |
| 152 | + } |
| 153 | +} |
0 commit comments