using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
///
/// Lane B review N4 — decides whether a value deferred during a plan window may
/// be replayed onto a node an in-place edit re-shaped: never an old-typed value the new DataType contradicts.
///
public sealed class VariableShapeTests
{
[Fact]
public void A_value_of_the_new_type_fits_unchanged()
{
new VariableShape("Int32", false, null).TryFit(7, out var fitted).ShouldBeTrue();
fitted.ShouldBe(7);
}
[Fact]
public void A_convertible_old_typed_value_is_converted_to_the_new_type()
{
new VariableShape("Int32", false, null).TryFit(5.0f, out var fitted).ShouldBeTrue();
fitted.ShouldBeOfType().ShouldBe(5);
new VariableShape("Double", false, null).TryFit((short)3, out var d).ShouldBeTrue();
d.ShouldBeOfType().ShouldBe(3d);
}
[Theory]
[InlineData("Int32", "fast")] // not a number
[InlineData("Byte", 300)] // overflow
[InlineData("DateTime", 12)] // no conversion
public void A_value_the_new_type_cannot_hold_does_not_fit(string dataType, object value)
{
new VariableShape(dataType, false, null).TryFit(value, out var fitted).ShouldBeFalse();
fitted.ShouldBeNull();
}
[Fact]
public void Scalar_and_array_never_cross()
{
new VariableShape("Int32", false, null).TryFit(new[] { 1, 2 }, out _).ShouldBeFalse();
new VariableShape("Int32", true, 2).TryFit(1, out _).ShouldBeFalse();
new VariableShape("String", true, null).TryFit("ab", out _).ShouldBeFalse("a string is not an array of strings");
}
[Fact]
public void An_array_is_converted_element_wise_and_its_fixed_length_enforced()
{
new VariableShape("Int16", true, 3).TryFit(new[] { 1, 2, 3 }, out var fitted).ShouldBeTrue();
fitted.ShouldBeOfType().ShouldBe(new short[] { 1, 2, 3 });
new VariableShape("Int16", true, 4).TryFit(new[] { 1, 2, 3 }, out _).ShouldBeFalse("wrong length");
new VariableShape("Int16", true, null).TryFit(new[] { 1, 2, 3 }, out _).ShouldBeTrue("no fixed length");
new VariableShape("Byte", true, null).TryFit(new[] { 1, 999 }, out _).ShouldBeFalse("an element overflows");
}
[Fact]
public void Null_and_unknown_types_always_fit()
{
new VariableShape("Int32", false, null).TryFit(null, out var n).ShouldBeTrue();
n.ShouldBeNull();
new VariableShape("Structure", false, null).TryFit("anything", out var any).ShouldBeTrue();
any.ShouldBe("anything");
}
}