using Shouldly; using Xunit; namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests; /// /// lmxopcua#1056 — {{equip}}/<RefName> is only substituted inside a REGULAR "…" path literal. In a /// verbatim (@"…"), raw ("""…""") or interpolated ($"…") literal the token used to be /// silently left in the path, so the tag read Bad at runtime while the editor and the deploy gate were green. The /// scanner finds those forms; the editor (OTSCRIPT_EQUIPLITERAL) and the deploy gate both refuse them. /// [Trait("Category", "Unit")] public sealed class EquipTokenLiteralScannerTests { [Theory] [InlineData("return ctx.GetTag(@\"{{equip}}/speed\").Value;")] [InlineData("return ctx.GetTag(\"\"\"{{equip}}/speed\"\"\").Value;")] [InlineData("return ctx.GetTag($\"{{equip}}/speed\").Value;")] [InlineData("return ctx.GetTag($@\"{{equip}}/speed\").Value;")] [InlineData("ctx.SetVirtualTag(@\"{{equip}}/speed\", 1); return 0;")] public void A_token_inside_a_non_regular_path_literal_is_found(string source) { var findings = EquipTokenLiteralScanner.Find(source); findings.ShouldHaveSingleItem(); findings[0].Message.ShouldContain("regular"); source.Substring(findings[0].Span.Start, findings[0].Span.Length).ShouldContain("{{equip}}"); } [Theory] [InlineData("return ctx.GetTag(\"{{equip}}/speed\").Value;")] // the supported form [InlineData("return ctx.GetTag(@\"Plant/Drv/speed\").Value;")] // verbatim without the token [InlineData("return ctx.GetTag(\"A\").Value; // @\"{{equip}}/speed\"")] // a comment [InlineData("var s = @\"{{equip}}/speed\"; return ctx.GetTag(\"A\").Value;")] // not a path argument [InlineData("return other.GetTag(@\"{{equip}}/speed\");")] // not ctx [InlineData("return ctx.GetTag($\"Plant/{1}\").Value;")] // interpolated, no token [InlineData("return 1;")] public void Everything_else_is_not_flagged_positive_controls(string source) => EquipTokenLiteralScanner.Find(source).ShouldBeEmpty(); [Fact] public void Every_offending_call_is_reported_in_source_order() { var findings = EquipTokenLiteralScanner.Find( "var a = ctx.GetTag(@\"{{equip}}/a\"); var b = ctx.GetTag(\"{{equip}}/ok\"); var c = ctx.GetTag($\"{{equip}}/c\"); return 0;"); findings.Count.ShouldBe(2); findings[0].Span.Start.ShouldBeLessThan(findings[1].Span.Start); } [Fact] public void The_regular_literal_form_really_is_the_one_substitution_handles() { // Pins the premise: the same source with a regular literal substitutes, the verbatim one does not. var map = new Dictionary { ["speed"] = "Plant/Drv/dev/speed" }; ZB.MOM.WW.OtOpcUa.Commons.Types.EquipmentScriptPaths .SubstituteEquipmentToken("return ctx.GetTag(\"{{equip}}/speed\").Value;", map) .ShouldContain("Plant/Drv/dev/speed"); ZB.MOM.WW.OtOpcUa.Commons.Types.EquipmentScriptPaths .SubstituteEquipmentToken("return ctx.GetTag(@\"{{equip}}/speed\").Value;", map) .ShouldContain("{{equip}}/speed"); } [Theory] [InlineData(ScriptContextKind.VirtualTag)] [InlineData(ScriptContextKind.AlarmPredicate)] public void The_deploy_gate_refuses_such_a_script(ScriptContextKind kind) { var source = kind == ScriptContextKind.AlarmPredicate ? "return ctx.GetTag(@\"{{equip}}/speed\").Value is not null;" : "return ctx.GetTag(@\"{{equip}}/speed\").Value;"; var result = new RoslynDeployScriptChecker().Check( [new DeployScriptCheckRequest(kind, source)], TestContext.Current.CancellationToken).Single(); result.Passed.ShouldBeFalse(); result.Problems.ShouldContain(p => p.Contains("{{equip}}") && p.Contains("regular")); // positive control: the supported spelling of the same script passes var ok = new RoslynDeployScriptChecker().Check( [new DeployScriptCheckRequest(kind, source.Replace("@\"", "\""))], TestContext.Current.CancellationToken).Single(); ok.Passed.ShouldBeTrue(string.Join("; ", ok.Problems)); } }