using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
///
/// lmxopcua#1100: the script-result rule (, applied by both
/// script runners) must agree with what the publish path really takes — , which
/// every node value write goes through. A value the rule passes but FromClr refuses is the defect #1100 found (the
/// node write threw, the node silently kept its previous value and status); a value FromClr takes but the rule refuses
/// would take a working result away from clients.
///
[Trait("Category", "Unit")]
public sealed class SdkValueBoundaryScriptResultTests
{
public static TheoryData Values() => new()
{
// publishable
{ "bool", true }, { "sbyte", (sbyte)1 }, { "byte", (byte)1 }, { "short", (short)1 }, { "ushort", (ushort)1 },
{ "int", 1 }, { "uint", 1u }, { "long", 1L }, { "ulong", 1UL }, { "float", 1f }, { "double", 1.0 },
{ "string", "s" }, { "DateTime", DateTime.UtcNow }, { "Guid", Guid.NewGuid() },
{ "int[]", new[] { 1 } }, { "byte[]", new byte[] { 1 } }, { "string[]", new[] { "a", null! } },
{ "DateTime[]", new[] { DateTime.UtcNow } }, { "Guid[]", new[] { Guid.NewGuid() } }, { "int[][]", new[] { new[] { 1 } } },
// not publishable
{ "decimal", 1.5m }, { "decimal[]", new[] { 1.5m } }, { "enum", DayOfWeek.Monday }, { "enum[]", new[] { DayOfWeek.Monday } },
{ "TimeSpan", TimeSpan.FromSeconds(1) }, { "char", 'a' }, { "char[]", new[] { 'a' } },
{ "DateTimeOffset", DateTimeOffset.UnixEpoch }, { "KeyValuePair", new KeyValuePair(1, 2) },
{ "KeyValuePair[]", new[] { new KeyValuePair(1, 2) } }, { "tuple[]", new[] { (1, 2) } },
{ "double[,]", new double[1, 1] }, { "Guid?[]", new Guid?[] { Guid.Empty } },
};
[Theory]
[MemberData(nameof(Values))]
public void The_script_result_rule_is_what_the_publish_path_takes(string name, object value)
{
var publishable = ScriptResultMaterializer.IsPublishable(value.GetType());
bool taken;
try
{
SdkValueBoundary.FromClr(value);
taken = true;
}
catch (Exception)
{
taken = false;
}
publishable.ShouldBe(taken, name);
}
[Fact]
public void Every_publishable_scalar_type_and_its_array_reach_the_publish_path()
{
foreach (var type in ScriptResultMaterializer.PublishableScalarTypes)
{
var scalar = type == typeof(string) ? "s" : Activator.CreateInstance(type)!;
Should.NotThrow(() => SdkValueBoundary.FromClr(scalar), type.Name);
Should.NotThrow(() => SdkValueBoundary.FromClr(Array.CreateInstance(type, 1)), type.Name + "[]");
}
}
}