/* ======================================================================== * 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.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using NUnit.Framework; namespace Opc.Ua.SourceGeneration { /// /// Verifies placeholder references in generated type and instance factories. /// [TestFixture] [Category("SourceGeneration")] public sealed class PlaceholderReferenceGeneratorTests { /// /// Keeps nested placeholder references on templates without exposing them on runtime instances. /// [TestCase("MandatoryPlaceholder")] [TestCase("OptionalPlaceholder")] public void PlaceholderReferencesAreRetainedOnlyForTheTypeTemplate(string rule) { GeneratorDriver driver = GenerateModel( sModel.Replace("PLACEHOLDER_RULE", rule, StringComparison.Ordinal), out Compilation output); InvocationExpressionSyntax[] references = GetGeneratedReferenceCalls(driver); InvocationExpressionSyntax[] placeholders = [.. references.Where(call => IsForwardReference(call, "47"))]; Assert.That(placeholders, Has.Length.EqualTo(1), "The type template must retain the placeholder's forward HasComponent reference."); foreach (InvocationExpressionSyntax reference in placeholders) { Assert.That(reference.Ancestors().OfType() .Any(statement => statement.Condition.ToString() == "!forInstance"), Is.True, "A runtime instance must not link to an uninstantiated type declaration: " + reference); } InvocationExpressionSyntax[] ordinary = [.. references.Where(call => IsForwardReference(call, "35"))]; Assert.That(ordinary, Has.Length.EqualTo(1)); foreach (InvocationExpressionSyntax reference in ordinary) { Assert.That(reference.Ancestors().OfType() .Any(statement => statement.Condition.ToString() == "!forInstance"), Is.False, "Ordinary external references must remain available on runtime instances."); } output.GetDiagnostics().Check(TestContext.Out, out int errors, out _); Assert.That(errors, Is.Zero); } /// /// Keeps direct and external placeholder references only in guarded, compilable type factories. /// [Test] public void DirectPlaceholderReferencesCompileForTypeAndInstanceFactories( [Values("MandatoryPlaceholder", "OptionalPlaceholder")] string rule, [Values("ObjectType", "VariableType")] string rootType) { string baseType = rootType == "ObjectType" ? "BaseObjectType" : "BaseDataVariableType"; string model = sDirectChildModel .Replace("ROOT_TYPE", rootType, StringComparison.Ordinal) .Replace("BASE_TYPE", baseType, StringComparison.Ordinal) .Replace("PLACEHOLDER_RULE", rule, StringComparison.Ordinal); GeneratorDriver driver = GenerateModel(model, out Compilation output); output.GetDiagnostics().Check(TestContext.Out, out int errors, out _); Assert.That(errors, Is.Zero); InvocationExpressionSyntax[] references = GetGeneratedReferenceCalls(driver); InvocationExpressionSyntax[] placeholders = [.. references.Where(call => IsForwardReference(call, "47"))]; Assert.That(placeholders, Has.Length.EqualTo(2), "The type template must retain both direct-child and external placeholder references."); foreach (InvocationExpressionSyntax reference in placeholders) { MethodDeclarationSyntax factory = reference.Ancestors().OfType().Single(); Assert.That(factory.Identifier.ValueText, Is.EqualTo("CreateDeviceType"), "Instance-only factories must omit every placeholder reference."); Assert.That(factory.ParameterList.Parameters.Any( parameter => parameter.Identifier.ValueText == "forInstance"), Is.True); Assert.That(reference.Ancestors().OfType() .Any(statement => statement.Condition.ToString() == "!forInstance"), Is.True); } InvocationExpressionSyntax[] ordinary = [.. references.Where(call => IsForwardReference(call, "35"))]; Assert.That(ordinary.Select(reference => reference.Ancestors().OfType().Single().Identifier.ValueText), Is.EquivalentTo(sFactoryNames)); foreach (InvocationExpressionSyntax reference in ordinary) { Assert.That(reference.Ancestors().OfType() .Any(statement => statement.Condition.ToString() == "!forInstance"), Is.False, "Ordinary external references must remain unconditional in both factory contexts."); } } private static GeneratorDriver GenerateModel(string model, out Compilation output) { CSharpCompilation compilation = OptimizationLevel.Release.CreateCompilation() .AddCode(new Dictionary().WithOpcUaGeneratedStack(), LanguageVersion.CSharp11); var options = new AnalyzerOptionsProvider(new Dictionary { ["build_property.ModelSourceGeneratorOmitFluentApi"] = "true", ["build_property.ModelSourceGeneratorOmitEventRecords"] = "true" }); GeneratorDriver driver = CSharpGeneratorDriver.Create(new ModelSourceGenerator()) .WithUpdatedParseOptions(new CSharpParseOptions(LanguageVersion.CSharp11)) .AddAdditionalTexts([EmbeddedText.Create("Placeholders.xml", model)]) .WithUpdatedAnalyzerConfigOptions(options); driver = driver.RunGeneratorsAndUpdateCompilation( compilation, out output, out ImmutableArray diagnostics); Assert.That(diagnostics, Is.Empty, string.Join("\n", diagnostics)); return driver; } private static InvocationExpressionSyntax[] GetGeneratedReferenceCalls(GeneratorDriver driver) { return [.. driver.GetRunResult().Results[0].GeneratedSources .SelectMany(source => source.SyntaxTree.GetRoot().DescendantNodes() .OfType()) .Where(call => call.Expression.ToString() == "state.AddReference")]; } private static bool IsForwardReference(InvocationExpressionSyntax call, string referenceType) { return call.ArgumentList.Arguments[1].Expression.IsKind(SyntaxKind.FalseLiteralExpression) && call.ArgumentList.Arguments[0].Expression.DescendantNodes().OfType() .Any(literal => literal.Token.ValueText == referenceType); } private static readonly string[] sFactoryNames = ["CreateDeviceType", "CreateInstanceOfDeviceType"]; private const string sDirectChildModel = """ http://opcfoundation.org/UA/ http://test.org/UA/PlaceholderReferences/ ua:HasComponent DeviceType_Value_Placeholder ua:HasComponent ExternalType_Value_Placeholder ua:Organizes ua:ObjectsFolder """; private const string sModel = """ http://opcfoundation.org/UA/ http://test.org/UA/PlaceholderReferences/ ua:HasComponent DeviceType_Endpoints_Endpoint_Placeholder ua:Organizes ua:ObjectsFolder """; } }