|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="ClassHelperTestFixture.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2026 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +namespace SysML2.NET.CodeGenerator.Tests.Generators.UmlHandleBarsGenerators |
| 22 | +{ |
| 23 | + using System; |
| 24 | + using System.Collections.Generic; |
| 25 | + using System.Linq; |
| 26 | + |
| 27 | + using HandlebarsDotNet; |
| 28 | + using HandlebarsDotNet.Helpers; |
| 29 | + |
| 30 | + using NUnit.Framework; |
| 31 | + |
| 32 | + using SysML2.NET.CodeGenerator.Extensions; |
| 33 | + using SysML2.NET.CodeGenerator.HandleBarHelpers; |
| 34 | + |
| 35 | + using uml4net.Extensions; |
| 36 | + using uml4net.SimpleClassifiers; |
| 37 | + using uml4net.StructuredClassifiers; |
| 38 | + |
| 39 | + [TestFixture] |
| 40 | + public class ClassHelperTestFixture |
| 41 | + { |
| 42 | + private IHandlebars handlebars; |
| 43 | + private List<IClass> allClasses; |
| 44 | + |
| 45 | + [OneTimeSetUp] |
| 46 | + public void OneTimeSetUp() |
| 47 | + { |
| 48 | + this.handlebars = Handlebars.CreateSharedEnvironment(); |
| 49 | + HandlebarsHelpers.Register(this.handlebars); |
| 50 | + this.handlebars.RegisterClassHelper(); |
| 51 | + |
| 52 | + this.allClasses = GeneratorSetupFixture.XmiReaderResult.QueryContainedAndImported("SysML") |
| 53 | + .SelectMany(x => x.PackagedElement.OfType<IClass>()) |
| 54 | + .ToList(); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Helper to invoke a template with IClass as context via {{#each}} |
| 59 | + /// </summary> |
| 60 | + private string RenderWithClassContext(string helperExpression, IClass @class) |
| 61 | + { |
| 62 | + var template = this.handlebars.Compile($"{{{{#each items}}}}{helperExpression}{{{{/each}}}}"); |
| 63 | + return template(new { items = new[] { @class } }); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void Verify_that_WriteEnumerationNameSpaces_writes_using_statements_for_enum_properties() |
| 68 | + { |
| 69 | + var classWithEnums = this.allClasses.FirstOrDefault(c => |
| 70 | + c.QueryAllProperties().Any(p => p.QueryIsEnum())); |
| 71 | + |
| 72 | + if (classWithEnums == null) |
| 73 | + { |
| 74 | + Assert.Ignore("No class with enum properties found in the test data"); |
| 75 | + } |
| 76 | + |
| 77 | + var result = this.RenderWithClassContext("{{Class.WriteEnumerationNameSpaces}}", classWithEnums); |
| 78 | + |
| 79 | + Assert.That(result, Does.Contain("using SysML2.NET.Core.")); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void Verify_that_WriteEnumerationNameSpaces_writes_nothing_for_class_without_enums() |
| 84 | + { |
| 85 | + var classWithoutEnums = this.allClasses.FirstOrDefault(c => |
| 86 | + !c.QueryAllProperties().Any(p => p.QueryIsEnum())); |
| 87 | + |
| 88 | + if (classWithoutEnums == null) |
| 89 | + { |
| 90 | + Assert.Ignore("All classes have enum properties"); |
| 91 | + } |
| 92 | + |
| 93 | + var result = this.RenderWithClassContext("{{Class.WriteEnumerationNameSpaces}}", classWithoutEnums); |
| 94 | + |
| 95 | + Assert.That(result, Is.Empty); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void Verify_that_WriteEnumerationNameSpace_writes_using_for_enumeration() |
| 100 | + { |
| 101 | + var enumerations = GeneratorSetupFixture.XmiReaderResult.QueryContainedAndImported("SysML") |
| 102 | + .SelectMany(x => x.PackagedElement.OfType<IEnumeration>()) |
| 103 | + .ToList(); |
| 104 | + |
| 105 | + if (enumerations.Count == 0) |
| 106 | + { |
| 107 | + Assert.Ignore("No enumerations found in the test data"); |
| 108 | + } |
| 109 | + |
| 110 | + var template = this.handlebars.Compile("{{#each items}}{{Class.WriteEnumerationNameSpace}}{{/each}}"); |
| 111 | + var result = template(new { items = new[] { enumerations.First() } }); |
| 112 | + |
| 113 | + Assert.That(result, Does.Contain("using SysML2.NET.Core.")); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void Verify_that_WriteNameSpaces_writes_using_statements_with_DTO_prefix() |
| 118 | + { |
| 119 | + var testClass = this.allClasses.First(c => c.SuperClass.Any()); |
| 120 | + |
| 121 | + var result = this.RenderWithClassContext("{{Class.WriteNameSpaces this \"DTO\"}}", testClass); |
| 122 | + |
| 123 | + Assert.That(result, Does.Contain("using SysML2.NET.Core.DTO.")); |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void Verify_that_WriteNameSpaces_writes_using_statements_with_POCO_prefix() |
| 128 | + { |
| 129 | + var testClass = this.allClasses.First(c => c.SuperClass.Any()); |
| 130 | + |
| 131 | + var result = this.RenderWithClassContext("{{Class.WriteNameSpaces this \"POCO\"}}", testClass); |
| 132 | + |
| 133 | + Assert.That(result, Does.Contain("using SysML2.NET.Core.POCO.")); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void Verify_that_WriteCountAllNonDerivedProperties_writes_numeric_count() |
| 138 | + { |
| 139 | + var testClass = this.allClasses.First(c => !c.IsAbstract); |
| 140 | + |
| 141 | + var result = this.RenderWithClassContext("{{Class.WriteCountAllNonDerivedProperties}}", testClass); |
| 142 | + |
| 143 | + Assert.That(result, Is.Not.Null.And.Not.Empty); |
| 144 | + Assert.That(int.TryParse(result.Trim(), out var count), Is.True, |
| 145 | + $"Expected numeric output but got: '{result}'"); |
| 146 | + Assert.That(count, Is.GreaterThanOrEqualTo(0)); |
| 147 | + } |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void Verify_that_QueryAllNonDerivedNonRedefinedProperties_does_not_throw() |
| 151 | + { |
| 152 | + var testClass = this.allClasses.First(c => !c.IsAbstract && c.QueryAllNonDerivedNonRedefinedProperties().Any()); |
| 153 | + |
| 154 | + Assert.That(() => this.RenderWithClassContext( |
| 155 | + "{{#Class.QueryAllNonDerivedNonRedefinedProperties}}item{{/Class.QueryAllNonDerivedNonRedefinedProperties}}", |
| 156 | + testClass), Throws.Nothing); |
| 157 | + } |
| 158 | + |
| 159 | + [Test] |
| 160 | + public void Verify_that_QueryAllNonDerivedNonRedefinedProperties_returns_expected_count() |
| 161 | + { |
| 162 | + var testClass = this.allClasses.First(c => !c.IsAbstract); |
| 163 | + |
| 164 | + var expectedCount = testClass.QueryAllNonDerivedNonRedefinedProperties().Count; |
| 165 | + var countResult = this.RenderWithClassContext("{{Class.WriteCountAllNonDerivedNonRedefinedProperties}}", testClass); |
| 166 | + var actualCount = int.Parse(countResult.Trim()); |
| 167 | + |
| 168 | + Assert.That(actualCount, Is.EqualTo(expectedCount)); |
| 169 | + } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public void Verify_that_WriteCountAllNonDerivedNonRedefinedProperties_writes_numeric_count() |
| 173 | + { |
| 174 | + var testClass = this.allClasses.First(c => !c.IsAbstract); |
| 175 | + |
| 176 | + var result = this.RenderWithClassContext("{{Class.WriteCountAllNonDerivedNonRedefinedProperties}}", testClass); |
| 177 | + |
| 178 | + Assert.That(result, Is.Not.Null.And.Not.Empty); |
| 179 | + Assert.That(int.TryParse(result.Trim(), out var count), Is.True, |
| 180 | + $"Expected numeric output but got: '{result}'"); |
| 181 | + Assert.That(count, Is.GreaterThanOrEqualTo(0)); |
| 182 | + } |
| 183 | + |
| 184 | + [Test] |
| 185 | + public void Verify_that_WriteInternalInterface_does_not_throw() |
| 186 | + { |
| 187 | + var testClass = this.allClasses.First(c => !c.IsAbstract); |
| 188 | + |
| 189 | + var result = this.RenderWithClassContext("{{Class.WriteInternalInterface}}", testClass); |
| 190 | + |
| 191 | + Assert.That(result, Is.Not.Null); |
| 192 | + } |
| 193 | + |
| 194 | + [Test] |
| 195 | + public void Verify_that_QueryNonDerivedCompositeAggregation_iterates_composite_properties() |
| 196 | + { |
| 197 | + var testClass = this.allClasses.First(c => !c.IsAbstract); |
| 198 | + |
| 199 | + var result = this.RenderWithClassContext( |
| 200 | + "{{#Class.QueryNonDerivedCompositeAggregation}}{{Name}},{{/Class.QueryNonDerivedCompositeAggregation}}", |
| 201 | + testClass); |
| 202 | + |
| 203 | + Assert.That(result, Is.Not.Null); |
| 204 | + } |
| 205 | + |
| 206 | + [Test] |
| 207 | + public void Verify_that_QueryAllPropertiesSorted_does_not_throw() |
| 208 | + { |
| 209 | + var testClass = this.allClasses.First(c => c.QueryAllProperties().Count > 1); |
| 210 | + |
| 211 | + Assert.That(() => this.RenderWithClassContext( |
| 212 | + "{{#Class.QueryAllPropertiesSorted}}item{{/Class.QueryAllPropertiesSorted}}", |
| 213 | + testClass), Throws.Nothing); |
| 214 | + } |
| 215 | + |
| 216 | + [Test] |
| 217 | + public void Verify_that_all_helpers_can_be_invoked_on_every_class_without_throwing() |
| 218 | + { |
| 219 | + foreach (var @class in this.allClasses.Take(10)) |
| 220 | + { |
| 221 | + Assert.That(() => this.RenderWithClassContext( |
| 222 | + "{{Class.WriteCountAllNonDerivedProperties}}|{{Class.WriteCountAllNonDerivedNonRedefinedProperties}}|{{Class.WriteInternalInterface}}", |
| 223 | + @class), Throws.Nothing, |
| 224 | + $"Failed for class {@class.Name}"); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments