/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using Moq; using NUnit.Framework; namespace Opc.Ua.SourceGeneration.Generator.Tests { /// /// Unit tests for the Generators.GenerateCode method /// [TestFixture] [Category("Api")] [SetCulture("en-us")] [SetUICulture("en-us")] [Parallelizable] public class GeneratorsTests { /// /// Tests that GenerateCode returns early when DesignFiles collection is empty. /// Input: Empty DesignFiles collection /// Expected: Method returns without processing, no file system or telemetry calls /// [Test] public void GenerateCode_EmptyDesignFilesCollection_ReturnsEarlyWithoutProcessing() { // Arrange var designFiles = new DesignFileCollection { Targets = [] }; var mockFileSystem = new Mock(MockBehavior.Strict); var mockTelemetry = new Mock(MockBehavior.Strict); const string outputDir = "output"; // Act designFiles.GenerateCode(mockFileSystem.Object, outputDir, mockTelemetry.Object); // Assert - No calls should be made to mocks (MockBehavior.Strict would throw if they were) mockFileSystem.VerifyNoOtherCalls(); mockTelemetry.VerifyNoOtherCalls(); } /// /// Tests that GenerateCode returns early when the nodesets Files collection is empty. /// Condition: nodesets.Files.Count == 0 /// Expected: Method returns immediately without processing /// [Test] public void GenerateCode_EmptyFilesCollection_ReturnsEarly() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object); // Assert Assert.That(nodesets.Files, Is.Empty); } /// /// Tests that GenerateCode handles null outputDir parameter. /// Condition: outputDir is null /// Expected: Method executes without throwing exception /// [Test] public void GenerateCode_NullOutputDir_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, null, mockTelemetry.Object)); } /// /// Tests that GenerateCode handles empty string outputDir parameter. /// Condition: outputDir is empty string /// Expected: Method executes without throwing exception /// [Test] public void GenerateCode_EmptyOutputDir_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, string.Empty, mockTelemetry.Object)); } /// /// Tests that GenerateCode handles whitespace outputDir parameter. /// Condition: outputDir contains only whitespace /// Expected: Method executes without throwing exception /// [Test] public void GenerateCode_WhitespaceOutputDir_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, " ", mockTelemetry.Object)); } /// /// Tests that GenerateCode handles outputDir with special characters. /// Condition: outputDir contains special characters /// Expected: Method executes without throwing exception /// [Test] public void GenerateCode_OutputDirWithSpecialCharacters_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "C:\\Special@#$%Folder\\Output", mockTelemetry.Object)); } /// /// Tests that GenerateCode handles null options parameter by creating default options. /// Condition: options parameter is null /// Expected: Method creates new GeneratorOptions instance internally /// [Test] public void GenerateCode_NullOptions_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object, options: null)); } /// /// Tests that GenerateCode uses provided options when not null. /// Condition: options parameter is provided /// Expected: Method uses the provided options /// [Test] public void GenerateCode_WithOptions_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); var options = new GeneratorOptions(); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object, options: options)); } /// /// Tests that GenerateCode handles useAllowSubtypes set to false. /// Condition: useAllowSubtypes is false /// Expected: Method executes with useAllowSubtypes false /// [Test] public void GenerateCode_UseAllowSubtypesFalse_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object, useAllowSubtypes: false)); } /// /// Tests that GenerateCode handles useAllowSubtypes set to true. /// Condition: useAllowSubtypes is true /// Expected: Method executes with useAllowSubtypes true /// [Test] public void GenerateCode_UseAllowSubtypesTrue_DoesNotThrow() { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object, useAllowSubtypes: true)); } /// /// Tests that GenerateCode handles all boolean parameter combinations. /// [Theory] public void GenerateCode_BooleanParameterCombinations_DoesNotThrow( bool useAllowSubtypes) { // Arrange var mockFileSystem = new Mock(); var mockTelemetry = new Mock(); var nodesets = new NodesetFileCollection( [], mockFileSystem.Object, mockTelemetry.Object); // Act & Assert Assert.DoesNotThrow(() => nodesets.GenerateCode( mockFileSystem.Object, "output", mockTelemetry.Object, useAllowSubtypes: useAllowSubtypes)); } } }