/* ========================================================================
* Copyright (c) 2005-2026 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 System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Opc.Ua.Tests;
namespace Opc.Ua.SourceGeneration.Generator.Tests
{
///
/// Retains explicitly declared nested placeholders without instantiating template nodes by default.
///
[TestFixture]
[Category("Generator")]
public sealed class NestedPlaceholderDeclarationTests
{
///
/// A program placeholder inside a job placeholder keeps its explicit numeric identity and required properties.
///
[TestCase("i=11510")]
[TestCase("i=11508")]
public void NestedPlaceholdersPreserveDeclaredIdsAndChildren(string modellingRule)
{
using var files = new VirtualFileSystem();
string path = Path.Combine(Directory.GetCurrentDirectory(), "NestedPlaceholders.NodeSet2.xml");
files.Add(path, Encoding.UTF8.GetBytes(
kNodeSet.Replace("PLACEHOLDER_RULE", modellingRule, StringComparison.Ordinal)));
ITelemetryContext telemetry = NUnitTelemetryContext.Create(logLevel: LogLevel.Error);
var nodesets = new NodesetFileCollection(
[(path, new NodesetFileOptions())], [], files, telemetry);
nodesets.GenerateCode(
files, string.Empty, telemetry, new GeneratorOptions { OmitFluentApi = true });
string code = Encoding.UTF8.GetString(files.Get(files.CreatedFiles.Single(
file => file.EndsWith(".NodeStates.ex.g.cs", StringComparison.Ordinal))));
MethodDeclarationSyntax[] methods = CSharpSyntaxTree.ParseText(code).GetRoot()
.DescendantNodes().OfType().ToArray();
MethodDeclarationSyntax container = FindDeclarationFactory(methods, 76);
MethodDeclarationSyntax placeholder = FindDeclarationFactory(methods, 93);
string placeholderName = placeholder.Identifier.ValueText;
Assert.That(container.ToString(), Does.Contain(placeholderName + "(context, state,"),
"A locally declared template must survive even when nested beneath another placeholder.");
MethodDeclarationSyntax name = FindDeclarationFactory(methods, 214);
MethodDeclarationSyntax number = FindDeclarationFactory(methods, 255);
Assert.Multiple(() =>
{
Assert.That(placeholder.ToString(), Does.Contain(name.Identifier.ValueText + "(context, state,"));
Assert.That(placeholder.ToString(), Does.Contain(number.Identifier.ValueText + "(context, state,"));
Assert.That(methods.Single(
method => method.Identifier.ValueText == "CreateInstanceOfJobListType").ToString(),
Does.Not.Contain(FindDeclarationFactory(methods, 74).Identifier.ValueText + "(context,"));
});
}
private static MethodDeclarationSyntax FindDeclarationFactory(
MethodDeclarationSyntax[] methods,
uint identifier)
{
string assignment = $"global::Opc.Ua.NodeId.Create({identifier}u,";
MethodDeclarationSyntax? factory = methods.SingleOrDefault(method =>
method.DescendantNodes().OfType().Any(expression =>
expression.Left.ToString() == "nodeState.NodeId" &&
expression.Right.ToString().StartsWith(assignment, StringComparison.Ordinal)));
Assert.That(factory, Is.Not.Null, $"No generated factory retains declared numeric NodeId {identifier}.");
return factory!;
}
private const string kNodeSet =
"""
http://test.org/UA/NestedPlaceholders/
ProgramType
i=58
JobListType
i=23518
ns=1;i=74
<OrderedObject>
ns=1;i=30
i=58
i=11510
ns=1;i=76
ProductionPrograms
ns=1;i=74
i=23518
i=78
ns=1;i=93
<OrderedObject>
ns=1;i=76
ns=1;i=59
PLACEHOLDER_RULE
ns=1;i=214
ns=1;i=255
Name
ns=1;i=93
i=68
i=78
NumberInList
ns=1;i=93
i=68
i=78
""";
}
}