607ffc2e15
The semantic validator treated each comma-separated argument after the
script name as a positional parameter and required the count to equal the
declared ParameterDefinitions. The runtime has no positional form: both
calls take ONE parameters object that ScriptArgs.Normalize turns into a
name -> value dictionary, so every Scripts.CallShared("X", new { ... })
was rejected unless exactly one parameter was declared, blocking deploy.
Call sites are now found with a Roslyn script-mode syntax parse (named
arguments and a trailing CancellationToken handled; calls in comments or
strings ignored). The parameters argument is classified as:
- anonymous object: validated by name (ordinal) - missing required
parameter = error, undeclared member = warning, clear literal type
mismatch per member = error (same conservative inference as before);
- any other expression (variable, dictionary, call, null): not checked;
- absent: error only if the target declares required parameters.
Required-ness comes from the shared InboundApiSchema parser (JSON Schema
'required' list; legacy array entries required unless required:false).
Positional-argument tests replaced by by-name CallShared/CallScript tests;
Component-TemplateEngine.md updated.
1396 lines
54 KiB
C#
1396 lines
54 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Validation;
|
|
|
|
public class SemanticValidatorTests
|
|
{
|
|
private readonly SemanticValidator _sut = new();
|
|
|
|
/// <summary>Verifies that a native alarm source referencing an unknown connection returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_NativeAlarmSource_UnknownConnection_ReturnsError()
|
|
{
|
|
var cfg = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
NativeAlarmSources = [new ResolvedNativeAlarmSource { CanonicalName = "P", ConnectionName = "Ghost", SourceReference = "x" }]
|
|
};
|
|
var result = _sut.Validate(cfg, alarmCapableConnectionNames: new HashSet<string> { "RealConn" });
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.NativeAlarmSourceInvalid);
|
|
}
|
|
|
|
/// <summary>Verifies that a native alarm source with an empty source reference returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_NativeAlarmSource_EmptySourceRef_ReturnsError()
|
|
{
|
|
var cfg = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
NativeAlarmSources = [new ResolvedNativeAlarmSource { CanonicalName = "P", ConnectionName = "RealConn", SourceReference = "" }]
|
|
};
|
|
var result = _sut.Validate(cfg, alarmCapableConnectionNames: new HashSet<string> { "RealConn" });
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.NativeAlarmSourceInvalid);
|
|
}
|
|
|
|
/// <summary>Verifies that a native alarm source with a valid binding produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_NativeAlarmSource_ValidBinding_NoError()
|
|
{
|
|
var cfg = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
NativeAlarmSources = [new ResolvedNativeAlarmSource { CanonicalName = "P", ConnectionName = "RealConn", SourceReference = "ns=2;s=T1" }]
|
|
};
|
|
var result = _sut.Validate(cfg, alarmCapableConnectionNames: new HashSet<string> { "RealConn" });
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.NativeAlarmSourceInvalid);
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript target that cannot be found returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptTargetNotFound_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "CallScript(\"NonExistent\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.CallTargetNotFound &&
|
|
e.Message.Contains("NonExistent"));
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript target that exists produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptTargetExists_NoError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Target", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "Caller", Code = "CallScript(\"Target\");" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.CallTargetNotFound);
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript to a composition script referenced by its leaf name produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptCompositionDelegatedByLeafName_NoError()
|
|
{
|
|
// A machine script delegating to a composed child's script via
|
|
// Children["X"].CallScript("MoveIn") captures only the leaf "MoveIn";
|
|
// the flattened script is the composed canonical "LeftMESReceiver.MoveIn".
|
|
// The existence check must accept the leaf-name match (the child segment
|
|
// is typically dynamic and cannot be statically resolved).
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "LeftMESReceiver.MoveIn", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "RightMESReceiver.MoveIn", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "MesMoveIn", Code = "await Children[side + \"MESReceiver\"].CallScript(\"MoveIn\", Parameters);" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.CallTargetNotFound);
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript to a composition script referenced by its leaf name emits an advisory warning.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptCompositionDelegatedByLeafName_EmitsAdvisoryWarning()
|
|
{
|
|
// A call target accepted ONLY by the composed leaf-name fallback
|
|
// (dynamic child path, not statically verifiable) must surface an advisory
|
|
// warning rather than being silently accepted.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "LeftMESReceiver.MoveIn", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "MesMoveIn", Code = "await Children[side + \"MESReceiver\"].CallScript(\"MoveIn\", Parameters);" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Warnings, w =>
|
|
w.Category == ValidationCategory.CallTargetNotFound
|
|
&& w.Message.Contains("leaf name only", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript to a sibling in the same scope emits no leaf-name warning.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptSameScopeSibling_EmitsNoLeafWarning()
|
|
{
|
|
// A same-scope sibling resolves directly (not the leaf fallback), so no
|
|
// advisory warning is emitted.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Target", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "Caller", Code = "CallScript(\"Target\");" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.DoesNotContain(result.Warnings, w =>
|
|
w.Message.Contains("leaf name only", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>Verifies that a CallScript to an unknown leaf name still returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_CallScriptUnknownLeafName_StillReturnsError()
|
|
{
|
|
// Relaxation is leaf-name scoped: a target that matches neither a root
|
|
// canonical name nor any composed leaf name is still flagged.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "LeftMESReceiver.MoveIn", Code = "var x = 1;" },
|
|
new ResolvedScript { CanonicalName = "Caller", Code = "CallScript(\"Typo\");" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.CallTargetNotFound && e.Message.Contains("Typo"));
|
|
}
|
|
|
|
/// <summary>Verifies that a CallShared target that cannot be found returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_CallSharedTargetNotFound_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "CallShared(\"MissingShared\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config, sharedScripts: []);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.CallTargetNotFound &&
|
|
e.Message.Contains("MissingShared"));
|
|
}
|
|
|
|
/// <summary>Verifies that a CallShared target that exists produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_CallSharedTargetExists_NoError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Caller", Code = "CallShared(\"Utility\");" }
|
|
]
|
|
};
|
|
|
|
var shared = new List<ResolvedScript>
|
|
{
|
|
new() { CanonicalName = "Utility", Code = "// shared" }
|
|
};
|
|
|
|
var result = _sut.Validate(config, shared);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.CallTargetNotFound);
|
|
}
|
|
|
|
// ── Parameters-object validation (ScadaBridge#150) ──────────────────
|
|
//
|
|
// CallScript/CallShared take ONE parameters object (anonymous object or
|
|
// dictionary) that the runtime normalizes into a name → value dictionary.
|
|
// An anonymous object is validated by NAME; any other expression is opaque.
|
|
|
|
private const string ThreeParamsLegacy =
|
|
"[{\"name\":\"NotificationList\",\"type\":\"String\",\"required\":true}," +
|
|
"{\"name\":\"Subject\",\"type\":\"String\",\"required\":true}," +
|
|
"{\"name\":\"Body\",\"type\":\"String\",\"required\":true}]";
|
|
|
|
private ValidationResult ValidateCallShared(string callerCode, string? parameterDefinitions)
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts = [new ResolvedScript { CanonicalName = "Caller", Code = callerCode }]
|
|
};
|
|
var shared = new List<ResolvedScript>
|
|
{
|
|
new() { CanonicalName = "SendNotificationExample", Code = "return null;", ParameterDefinitions = parameterDefinitions }
|
|
};
|
|
return _sut.Validate(config, shared);
|
|
}
|
|
|
|
private ValidationResult ValidateCallScript(string callerCode, string? parameterDefinitions)
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Target", Code = "var x = 1;", ParameterDefinitions = parameterDefinitions },
|
|
new ResolvedScript { CanonicalName = "Caller", Code = callerCode }
|
|
]
|
|
};
|
|
return _sut.Validate(config);
|
|
}
|
|
|
|
private static void AssertNoParameterErrors(ValidationResult result) =>
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
|
|
/// <summary>Issue #150 repro: CallShared with an anonymous object supplying every declared parameter is valid.</summary>
|
|
[Fact]
|
|
public void CallShared_AnonymousObjectWithAllParams_NoError()
|
|
{
|
|
var result = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { NotificationList = \"Ops\", Subject = \"Hi\", Body = body });",
|
|
ThreeParamsLegacy);
|
|
|
|
AssertNoParameterErrors(result);
|
|
Assert.DoesNotContain(result.Warnings, w => w.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>CallShared with an anonymous object missing a required parameter is an error naming it.</summary>
|
|
[Fact]
|
|
public void CallShared_AnonymousObjectMissingRequired_ReturnsError()
|
|
{
|
|
var result = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { NotificationList = \"Ops\", Subject = \"Hi\" });",
|
|
ThreeParamsLegacy);
|
|
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.ParameterMismatch && e.Message.Contains("'Body'", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>A missing parameter declared optional (required:false) is not an error.</summary>
|
|
[Fact]
|
|
public void CallShared_AnonymousObjectMissingOptional_NoError()
|
|
{
|
|
var result = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { Subject = \"Hi\" });",
|
|
"[{\"name\":\"Subject\",\"type\":\"String\"},{\"name\":\"Body\",\"type\":\"String\",\"required\":false}]");
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>An extra member the target doesn't declare is a warning, not an error.</summary>
|
|
[Fact]
|
|
public void CallShared_AnonymousObjectUnknownName_WarnsButNoError()
|
|
{
|
|
var result = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { NotificationList = \"Ops\", Subject = \"Hi\", Body = \"b\", Bdy = \"typo\" });",
|
|
ThreeParamsLegacy);
|
|
|
|
Assert.True(result.IsValid);
|
|
Assert.Contains(result.Warnings, w =>
|
|
w.Category == ValidationCategory.ParameterMismatch && w.Message.Contains("'Bdy'", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>A dictionary variable's keys can't be known statically — never flagged.</summary>
|
|
[Fact]
|
|
public void CallShared_DictionaryVariable_NoError()
|
|
{
|
|
var result = ValidateCallShared(
|
|
"var args = new Dictionary<string, object?> { [\"Subject\"] = \"Hi\" }; await Scripts.CallShared(\"SendNotificationExample\", args);",
|
|
ThreeParamsLegacy);
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>No parameters argument against a target with required parameters is an error.</summary>
|
|
[Fact]
|
|
public void CallShared_NoArgumentsAgainstRequiredParams_ReturnsError()
|
|
{
|
|
var result = ValidateCallShared("await Scripts.CallShared(\"SendNotificationExample\");", ThreeParamsLegacy);
|
|
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>No parameters argument against a target that declares none is valid.</summary>
|
|
[Fact]
|
|
public void CallShared_NoArgumentsAgainstNoParams_NoError()
|
|
{
|
|
var result = ValidateCallShared("await Scripts.CallShared(\"SendNotificationExample\");", null);
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>A trailing CancellationToken argument doesn't disturb parsing of the parameters object.</summary>
|
|
[Fact]
|
|
public void CallShared_WithCancellationTokenArgument_ValidatesParametersObject()
|
|
{
|
|
var ok = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { NotificationList = \"a, b\", Subject = Fmt(\"x\", 1), Body = \"(b)\" }, CancellationToken.None);",
|
|
ThreeParamsLegacy);
|
|
AssertNoParameterErrors(ok);
|
|
|
|
var missing = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", new { Subject = \"x\" }, ct);",
|
|
ThreeParamsLegacy);
|
|
Assert.Contains(missing.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
|
|
var namedCt = ValidateCallShared(
|
|
"await Scripts.CallShared(\"SendNotificationExample\", cancellationToken: ct);",
|
|
null);
|
|
AssertNoParameterErrors(namedCt);
|
|
}
|
|
|
|
/// <summary>JSON Schema form: only the names listed in "required" are required.</summary>
|
|
[Fact]
|
|
public void CallShared_JsonSchemaRequiredList_Honored()
|
|
{
|
|
const string schema =
|
|
"{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"integer\"},\"b\":{\"type\":\"string\"}},\"required\":[\"a\"]}";
|
|
|
|
AssertNoParameterErrors(ValidateCallShared("Scripts.CallShared(\"SendNotificationExample\", new { a = 1 });", schema));
|
|
Assert.Contains(
|
|
ValidateCallShared("Scripts.CallShared(\"SendNotificationExample\", new { b = \"x\" });", schema).Errors,
|
|
e => e.Category == ValidationCategory.ParameterMismatch && e.Message.Contains("'a'", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>CallScript with an anonymous object supplying every declared parameter is valid.</summary>
|
|
[Fact]
|
|
public void CallScript_AnonymousObjectWithAllParams_NoError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"await Instance.CallScript(\"Target\", new { a = 42, b = \"hi\", c = true });",
|
|
"[{\"name\":\"a\",\"type\":\"Integer\"},{\"name\":\"b\",\"type\":\"String\"},{\"name\":\"c\",\"type\":\"Boolean\"}]");
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>CallScript with an anonymous object missing a required parameter is an error.</summary>
|
|
[Fact]
|
|
public void CallScript_AnonymousObjectMissingRequired_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\", new { a = 42 });",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"},{\"name\":\"b\",\"type\":\"String\"}]");
|
|
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.ParameterMismatch && e.Message.Contains("'b'", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>CallScript: an unknown member name is a warning only.</summary>
|
|
[Fact]
|
|
public void CallScript_AnonymousObjectUnknownName_WarnsButNoError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\", new { a = 1, extra = 2 });",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"}]");
|
|
|
|
Assert.True(result.IsValid);
|
|
Assert.Contains(result.Warnings, w =>
|
|
w.Category == ValidationCategory.ParameterMismatch && w.Message.Contains("'extra'", StringComparison.Ordinal));
|
|
}
|
|
|
|
/// <summary>CallScript: name matching is ordinal, as the runtime dictionary is.</summary>
|
|
[Fact]
|
|
public void CallScript_AnonymousObjectWrongCase_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\", new { A = 1 });",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"}]");
|
|
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>CallScript: a projection initializer (<c>new { a }</c>) supplies the parameter named after the variable.</summary>
|
|
[Fact]
|
|
public void CallScript_ProjectionInitializer_SuppliesName()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"var a = 1; var o = new { b = 2 }; CallScript(\"Target\", new { a, o.b });",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"},{\"name\":\"b\",\"type\":\"Int32\"}]");
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>CallScript with a dictionary variable is never flagged.</summary>
|
|
[Fact]
|
|
public void CallScript_DictionaryVariable_NoError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\", Parameters);",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"}]");
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>CallScript with a null parameters argument is opaque — not checked.</summary>
|
|
[Fact]
|
|
public void CallScript_NullParameters_NoError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\", null);",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"}]");
|
|
|
|
AssertNoParameterErrors(result);
|
|
}
|
|
|
|
/// <summary>CallScript with no arguments against required parameters is an error.</summary>
|
|
[Fact]
|
|
public void CallScript_NoArgumentsAgainstRequiredParams_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript(
|
|
"CallScript(\"Target\");",
|
|
"[{\"name\":\"a\",\"type\":\"Int32\"},{\"name\":\"b\",\"type\":\"String\"}]");
|
|
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>CallScript with no arguments against a target with no parameters is valid.</summary>
|
|
[Fact]
|
|
public void CallScript_NoArgumentsAgainstNoParams_NoError()
|
|
{
|
|
AssertNoParameterErrors(ValidateCallScript("CallScript(\"Target\");", null));
|
|
AssertNoParameterErrors(ValidateCallScript("CallScript(\"Target\");", "[]"));
|
|
}
|
|
|
|
// ── Member-type validation (anonymous-object members) ───────────────
|
|
|
|
/// <summary>A string literal for an integer parameter is a type error.</summary>
|
|
[Fact]
|
|
public void Validate_MemberTypeMismatch_StringForInteger_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript("CallScript(\"Target\", new { a = \"hello\" });", "[{\"name\":\"a\",\"type\":\"Int32\"}]");
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.ParameterMismatch &&
|
|
e.Message.Contains("type", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>A number literal for a string parameter is a type error.</summary>
|
|
[Fact]
|
|
public void Validate_MemberTypeMismatch_NumberForString_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript("CallScript(\"Target\", new { a = 42 });", "[{\"name\":\"a\",\"type\":\"String\"}]");
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.ParameterMismatch &&
|
|
e.Message.Contains("type", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>A boolean literal for an integer parameter is a type error.</summary>
|
|
[Fact]
|
|
public void Validate_MemberTypeMismatch_BooleanForInteger_ReturnsError()
|
|
{
|
|
var result = ValidateCallScript("CallScript(\"Target\", new { a = true });", "[{\"name\":\"a\",\"type\":\"Integer\"}]");
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>An integer literal for a float parameter is accepted (numeric widening).</summary>
|
|
[Fact]
|
|
public void Validate_MemberType_IntegerLiteralForFloat_NoError()
|
|
{
|
|
AssertNoParameterErrors(ValidateCallScript("CallScript(\"Target\", new { a = 5 });", "[{\"name\":\"a\",\"type\":\"Float\"}]"));
|
|
}
|
|
|
|
/// <summary>Non-literal member expressions are never type-flagged.</summary>
|
|
[Theory]
|
|
[InlineData("v")]
|
|
[InlineData("42 + offset")]
|
|
[InlineData("\"a\" + x")]
|
|
[InlineData("null")]
|
|
[InlineData("Attributes[\"Temp\"].Value")]
|
|
public void Validate_MemberType_UnknownExpression_NoFalsePositive(string expression)
|
|
{
|
|
AssertNoParameterErrors(ValidateCallScript(
|
|
$"CallScript(\"Target\", new {{ a = {expression} }});",
|
|
"[{\"name\":\"a\",\"type\":\"Integer\"}]"));
|
|
}
|
|
|
|
/// <summary>An Object-typed parameter accepts any literal.</summary>
|
|
[Fact]
|
|
public void Validate_MemberType_UntypedParameter_NoFalsePositive()
|
|
{
|
|
AssertNoParameterErrors(ValidateCallScript("CallScript(\"Target\", new { a = \"anything\" });", "[{\"name\":\"a\",\"type\":\"Object\"}]"));
|
|
}
|
|
|
|
// ── Return-type validation ──────────────────────────────────────────
|
|
|
|
/// <summary>Verifies that assigning a boolean result to an integer returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnTypeMismatch_BooleanResultIntoInt_ReturnsError()
|
|
{
|
|
// Target returns Boolean; caller assigns it into a typed `int` local.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return true;",
|
|
ReturnDefinition = "{\"type\":\"boolean\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "int x = CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that assigning a string result to a boolean returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnTypeMismatch_StringResultIntoBool_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return \"x\";",
|
|
ReturnDefinition = "{\"type\":\"string\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "bool b = await CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a compatible assignment of a return value produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnTypeMatch_CompatibleAssignment_NoError()
|
|
{
|
|
// Target returns Integer; caller assigns into an `int` local — compatible.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return 1;",
|
|
ReturnDefinition = "{\"type\":\"integer\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "int x = CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that assigning a return value to a var local does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnType_VarAssignment_NoFalsePositive()
|
|
{
|
|
// `var` LHS — caller's expected type can't be inferred, so no flag.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return \"x\";",
|
|
ReturnDefinition = "{\"type\":\"string\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "var x = CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that an unused return value does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnType_UnusedResult_NoFalsePositive()
|
|
{
|
|
// Result isn't assigned anywhere — nothing to check.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return \"x\";",
|
|
ReturnDefinition = "{\"type\":\"string\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a call to a script with no declared return type does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnType_UndeclaredReturn_NoFalsePositive()
|
|
{
|
|
// Target has no ReturnDefinition — can't compare, so no flag.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Target", Code = "return 1;" },
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "string s = CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a return type mismatch on a qualified instance call returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnTypeMismatch_QualifiedInstanceCall_ReturnsError()
|
|
{
|
|
// Real-code form: `Instance.CallScript(...)`. The receiver prefix must
|
|
// be skipped so return-type validation still sees the typed-local assignment.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return \"x\";",
|
|
ReturnDefinition = "{\"type\":\"string\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "bool b = await Instance.CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a return type mismatch on a qualified shared call returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnTypeMismatch_QualifiedSharedCall_ReturnsError()
|
|
{
|
|
// Real-code form: `Scripts.CallShared(...)`.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "int n = Scripts.CallShared(\"Util\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var shared = new List<ResolvedScript>
|
|
{
|
|
new()
|
|
{
|
|
CanonicalName = "Util",
|
|
Code = "return true;",
|
|
ReturnDefinition = "{\"type\":\"boolean\"}"
|
|
}
|
|
};
|
|
|
|
var result = _sut.Validate(config, shared);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a cast expression on a return value does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ReturnType_CastExpression_NoFalsePositive()
|
|
{
|
|
// The result feeds a cast expression — not a clean typed-local
|
|
// assignment, so the assigned type can't be inferred. No flag.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "return \"x\";",
|
|
ReturnDefinition = "{\"type\":\"string\"}"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "int x = (int)CallScript(\"Target\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ReturnTypeMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a range violation on a non-numeric attribute returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_RangeViolationOnNonNumeric_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Status", DataType = "String" }
|
|
],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "BadAlarm",
|
|
TriggerType = "RangeViolation",
|
|
TriggerConfiguration = "{\"attributeName\":\"Status\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
/// <summary>Verifies that a range violation on a numeric attribute produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_RangeViolationOnNumeric_NoError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" }
|
|
],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "HighTemp",
|
|
TriggerType = "RangeViolation",
|
|
TriggerConfiguration = "{\"attributeName\":\"Temp\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
/// <summary>Verifies that an on-trigger script that cannot be found returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_OnTriggerScriptNotFound_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts = [new ResolvedScript { CanonicalName = "OtherScript", Code = "var x = 1;" }],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "Alarm1",
|
|
TriggerType = "ValueMatch",
|
|
OnTriggerScriptCanonicalName = "MissingScript"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.OnTriggerScriptNotFound);
|
|
}
|
|
|
|
/// <summary>Verifies that an instance script that calls an alarm on-trigger script returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_InstanceScriptCallsAlarmOnTrigger_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "AlarmHandler", Code = "// alarm handler" },
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "RegularScript",
|
|
Code = "CallScript(\"AlarmHandler\");"
|
|
}
|
|
],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "Alarm1",
|
|
TriggerType = "ValueMatch",
|
|
OnTriggerScriptCanonicalName = "AlarmHandler"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.CrossCallViolation);
|
|
}
|
|
|
|
/// <summary>Verifies that call target extraction handles multiple call types.</summary>
|
|
[Fact]
|
|
public void ExtractCallTargets_MultipleCallTypes()
|
|
{
|
|
var code = @"
|
|
var x = CallScript(""Script1"", new { a = 1, b = ""x, y"" });
|
|
CallShared(""Shared1"");
|
|
CallScript(""Script2"", args);
|
|
// CallScript(""InComment"");
|
|
var name = ""CallShared(\""InString\"")"";
|
|
";
|
|
|
|
var targets = SemanticValidator.ExtractCallTargets(code);
|
|
|
|
Assert.Equal(3, targets.Count);
|
|
Assert.Contains(targets, t => t.TargetName == "Script1" && !t.IsShared
|
|
&& t.ParametersKind == SemanticValidator.CallParametersKind.AnonymousObject
|
|
&& t.ParameterMembers.Select(m => m.Name).SequenceEqual(new[] { "a", "b" }));
|
|
Assert.Contains(targets, t => t.TargetName == "Shared1" && t.IsShared
|
|
&& t.ParametersKind == SemanticValidator.CallParametersKind.None);
|
|
Assert.Contains(targets, t => t.TargetName == "Script2" && !t.IsShared
|
|
&& t.ParametersKind == SemanticValidator.CallParametersKind.Opaque);
|
|
}
|
|
|
|
// ── HiLo validation ─────────────────────────────────────────────────────
|
|
|
|
private static FlattenedConfiguration HiLoConfig(string attrName, string dataType, string triggerJson) =>
|
|
new()
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = attrName, DataType = dataType }],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "Hi/Lo Alarm",
|
|
TriggerType = "HiLo",
|
|
TriggerConfiguration = triggerJson
|
|
}
|
|
]
|
|
};
|
|
|
|
/// <summary>Verifies that a hi/lo alarm on a non-numeric attribute returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoOnNonNumericAttribute_ReturnsError()
|
|
{
|
|
var config = HiLoConfig("Status", "String",
|
|
"{\"attributeName\":\"Status\",\"hi\":80}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType
|
|
&& e.Message.Contains("HiLo")
|
|
&& e.Message.Contains("non-numeric"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm on a numeric attribute produces no operand type error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoOnNumericAttribute_NoOperandTypeError()
|
|
{
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"hi\":80,\"hiHi\":100}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm with no setpoints returns a warning.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoNoSetpoints_ReturnsWarning()
|
|
{
|
|
// No setpoints means the alarm can never fire — design-time warning.
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\"}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Warnings,
|
|
w => w.Category == ValidationCategory.TriggerOperandType
|
|
&& w.Message.Contains("no setpoints"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm whose LoLo setpoint exceeds its Lo setpoint returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoLoLoGreaterThanLo_ReturnsError()
|
|
{
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"loLo\":20,\"lo\":10}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType
|
|
&& e.Message.Contains("LoLo")
|
|
&& e.Message.Contains("Lo"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm whose Hi setpoint exceeds its HiHi setpoint returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoHiGreaterThanHiHi_ReturnsError()
|
|
{
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"hi\":120,\"hiHi\":100}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType
|
|
&& e.Message.Contains("Hi")
|
|
&& e.Message.Contains("HiHi"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm whose low side overlaps its high side returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoLowSideOverlapsHighSide_ReturnsError()
|
|
{
|
|
// Lo (50) >= Hi (40) — bands overlap.
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"lo\":50,\"hi\":40}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType
|
|
&& e.Message.Contains("overlap"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm with only the high side configured produces no ordering error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoOnlyHighSideConfigured_NoOrderingError()
|
|
{
|
|
// Only Hi/HiHi configured — no low-side comparison needed.
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"hi\":80,\"hiHi\":100}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.DoesNotContain(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm with a negative deadband returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoNegativeDeadband_ReturnsError()
|
|
{
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"hi\":80,\"hiHi\":100,\"hiDeadband\":-1}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.Contains(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType
|
|
&& e.Message.Contains("Hi deadband")
|
|
&& e.Message.Contains("non-negative"));
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm with a zero deadband produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoZeroDeadband_NoError()
|
|
{
|
|
// Zero deadband is the default (no hysteresis) and must be accepted.
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"hi\":80,\"hiHi\":100,\"hiDeadband\":0,\"hiHiDeadband\":0}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.DoesNotContain(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
/// <summary>Verifies that a hi/lo alarm with valid setpoint ordering produces no errors.</summary>
|
|
[Fact]
|
|
public void Validate_HiLoValidOrdering_NoErrors()
|
|
{
|
|
// LoLo (-10) < Lo (0) < Hi (90) < HiHi (100) — fully valid.
|
|
var config = HiLoConfig("Temp", "Double",
|
|
"{\"attributeName\":\"Temp\",\"loLo\":-10,\"lo\":0,\"hi\":90,\"hiHi\":100}");
|
|
|
|
var result = _sut.Validate(config);
|
|
|
|
Assert.DoesNotContain(result.Errors,
|
|
e => e.Category == ValidationCategory.TriggerOperandType);
|
|
Assert.DoesNotContain(result.Warnings,
|
|
w => w.Category == ValidationCategory.TriggerOperandType);
|
|
}
|
|
|
|
// ── Review nits — comment-aware arg tokenizer ─────────────────────
|
|
|
|
/// <summary>Verifies that a line comment containing a comma inside the arguments does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ArgSplit_LineCommentWithCommaInsideArgs_NoFalsePositive()
|
|
{
|
|
// A `//` line comment containing a comma must NOT be counted as an arg separator.
|
|
// "Target" expects (a: Integer) — one real arg; the comment comma is noise.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "var x = 1;",
|
|
ParameterDefinitions = "[{\"name\":\"a\",\"type\":\"Integer\"}]"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "CallScript(\"Target\", 42 /* , extra */);"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
/// <summary>Verifies that a block comment containing a comma inside the arguments does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ArgSplit_BlockCommentWithCommaInsideArgs_NoFalsePositive()
|
|
{
|
|
// A `/* */` block comment containing a comma must NOT be counted as an arg separator.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "var x = 1;",
|
|
ParameterDefinitions = "[{\"name\":\"a\",\"type\":\"Integer\"},{\"name\":\"b\",\"type\":\"String\"}]"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
// Two real args, but the block comment adds a spurious comma if tokenizer is not comment-aware.
|
|
Code = "CallScript(\"Target\", 42 /* ,bogus */, \"hi\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
// ── Review nits — stricter numeric-literal inference ───────────────
|
|
|
|
/// <summary>Verifies that an identifier with a leading underscore does not produce a false positive.</summary>
|
|
[Fact]
|
|
public void Validate_ArgumentType_UnderscoreLeadingIdentifier_NoFalsePositive()
|
|
{
|
|
// `_2` starts with an underscore — it is a C# identifier, not a numeric literal.
|
|
// IsNumericLiteral must return false → type inferred as Unknown → no mismatch.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Target",
|
|
Code = "var x = 1;",
|
|
ParameterDefinitions = "[{\"name\":\"a\",\"type\":\"Integer\"}]"
|
|
},
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "Caller",
|
|
Code = "CallScript(\"Target\", _2);"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ParameterMismatch);
|
|
}
|
|
|
|
// ── List attribute semantics ────────────────────────────────────────
|
|
|
|
/// <summary>Verifies that a list attribute with a valid element type and default produces no error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_ValidElementTypeAndDefault_NoError()
|
|
{
|
|
// List + String element + a well-formed JSON-array default — fully valid.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute
|
|
{
|
|
CanonicalName = "Tags",
|
|
DataType = "List",
|
|
ElementDataType = "String",
|
|
Value = "[\"a\",\"b\"]"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.MissingMetadata);
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute with a null element type returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_NullElementType_ReturnsError()
|
|
{
|
|
// List with no element type — rule 1 violation (cardinality).
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Tags", DataType = "List", ElementDataType = null }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.MissingMetadata &&
|
|
e.EntityName == "Tags" &&
|
|
e.Message.Contains("element type", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>Verifies that a scalar attribute that has an element type returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ScalarAttribute_WithElementType_ReturnsError()
|
|
{
|
|
// A non-List attribute must not carry an element type — rule 1 violation.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Status", DataType = "String", ElementDataType = "String" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.MissingMetadata &&
|
|
e.EntityName == "Status" &&
|
|
e.Message.Contains("element type", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute with a non-scalar element type returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_NonScalarElementType_ReturnsError()
|
|
{
|
|
// Binary and List are not valid List element scalars — rule 1 violation.
|
|
foreach (var bad in new[] { "Binary", "List" })
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Tags", DataType = "List", ElementDataType = bad }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.MissingMetadata &&
|
|
e.EntityName == "Tags");
|
|
}
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute with a malformed JSON default returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_MalformedJsonDefault_ReturnsError()
|
|
{
|
|
// Unterminated JSON array default — rule 2 (default parseability) violation.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute
|
|
{
|
|
CanonicalName = "Tags",
|
|
DataType = "List",
|
|
ElementDataType = "String",
|
|
Value = "[\"a\""
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.MissingMetadata &&
|
|
e.EntityName == "Tags" &&
|
|
e.Message.Contains("default", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute whose default contains an unparseable element returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_DefaultElementNotParseable_ReturnsError()
|
|
{
|
|
// Element type Int32 but the default carries a non-integer element — rule 2 violation.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute
|
|
{
|
|
CanonicalName = "Counts",
|
|
DataType = "List",
|
|
ElementDataType = "Int32",
|
|
Value = "[\"x\"]"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.MissingMetadata &&
|
|
e.EntityName == "Counts" &&
|
|
e.Message.Contains("Int32"));
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute with an empty default produces no default error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_EmptyDefault_NoDefaultError()
|
|
{
|
|
// No authored default → rule 2 is inert; only the (satisfied) cardinality rule applies.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Tags", DataType = "List", ElementDataType = "String", Value = "" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.MissingMetadata);
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute used as a range violation operand returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_AsRangeViolationOperand_ReturnsError()
|
|
{
|
|
// Rule 3 confirmation: a List attribute is non-numeric, so a RangeViolation
|
|
// trigger over it is rejected by the existing NumericDataTypes guard.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Tags", DataType = "List", ElementDataType = "Int32" }
|
|
],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "BadAlarm",
|
|
TriggerType = "RangeViolation",
|
|
TriggerConfiguration = "{\"attributeName\":\"Tags\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.TriggerOperandType &&
|
|
e.Message.Contains("non-numeric"));
|
|
}
|
|
|
|
/// <summary>Verifies that a list attribute used as a hi/lo operand returns an error.</summary>
|
|
[Fact]
|
|
public void Validate_ListAttribute_AsHiLoOperand_ReturnsError()
|
|
{
|
|
// Rule 3 confirmation for HiLo.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Tags", DataType = "List", ElementDataType = "Double" }
|
|
],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "BadHiLo",
|
|
TriggerType = "HiLo",
|
|
TriggerConfiguration = "{\"attributeName\":\"Tags\",\"hi\":80}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e =>
|
|
e.Category == ValidationCategory.TriggerOperandType &&
|
|
e.Message.Contains("non-numeric"));
|
|
}
|
|
}
|