using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Cli.Commands;
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Cli.Tests;
///
/// Covers for the IEC 61131-3 atomic types
/// TwinCAT exposes. Wider matrix than AB CIP because IEC adds WSTRING + the four
/// TIME/DATE variants that all marshal as UDINT on the wire.
///
[Trait("Category", "Unit")]
public sealed class WriteCommandParseValueTests
{
[Theory]
[InlineData("true", true)]
[InlineData("0", false)]
[InlineData("on", true)]
[InlineData("NO", false)]
public void ParseValue_Bool_accepts_common_aliases(string raw, bool expected)
{
WriteCommand.ParseValue(raw, TwinCATDataType.Bool).ShouldBe(expected);
}
[Fact]
public void ParseValue_Bool_rejects_garbage()
{
Should.Throw(
() => WriteCommand.ParseValue("maybe", TwinCATDataType.Bool));
}
[Fact]
public void ParseValue_SInt_signed_byte()
{
WriteCommand.ParseValue("-128", TwinCATDataType.SInt).ShouldBe((sbyte)-128);
}
[Fact]
public void ParseValue_USInt_unsigned_byte()
{
WriteCommand.ParseValue("255", TwinCATDataType.USInt).ShouldBe((byte)255);
}
[Fact]
public void ParseValue_Int_signed_16bit()
{
WriteCommand.ParseValue("-32768", TwinCATDataType.Int).ShouldBe((short)-32768);
}
[Fact]
public void ParseValue_UInt_unsigned_16bit()
{
WriteCommand.ParseValue("65535", TwinCATDataType.UInt).ShouldBe((ushort)65535);
}
[Fact]
public void ParseValue_DInt_int32_bounds()
{
WriteCommand.ParseValue("-2147483648", TwinCATDataType.DInt).ShouldBe(int.MinValue);
}
[Fact]
public void ParseValue_UDInt_uint32_max()
{
WriteCommand.ParseValue("4294967295", TwinCATDataType.UDInt).ShouldBe(uint.MaxValue);
}
[Fact]
public void ParseValue_LInt_int64_min()
{
WriteCommand.ParseValue("-9223372036854775808", TwinCATDataType.LInt).ShouldBe(long.MinValue);
}
[Fact]
public void ParseValue_ULInt_uint64_max()
{
WriteCommand.ParseValue("18446744073709551615", TwinCATDataType.ULInt).ShouldBe(ulong.MaxValue);
}
[Fact]
public void ParseValue_Real_invariant_culture()
{
WriteCommand.ParseValue("3.14", TwinCATDataType.Real).ShouldBe(3.14f);
}
[Fact]
public void ParseValue_LReal_higher_precision()
{
WriteCommand.ParseValue("2.718281828", TwinCATDataType.LReal).ShouldBeOfType();
}
[Fact]
public void ParseValue_String_passthrough()
{
WriteCommand.ParseValue("hallo beckhoff", TwinCATDataType.String).ShouldBe("hallo beckhoff");
}
[Fact]
public void ParseValue_WString_passthrough()
{
// CLI layer doesn't distinguish UTF-8 input; the driver handles the WSTRING
// encoding on the wire.
WriteCommand.ParseValue("überstall", TwinCATDataType.WString).ShouldBe("überstall");
}
[Theory]
[InlineData(TwinCATDataType.Time)]
[InlineData(TwinCATDataType.Date)]
[InlineData(TwinCATDataType.DateTime)]
[InlineData(TwinCATDataType.TimeOfDay)]
public void ParseValue_IEC_date_time_variants_land_on_uint32(TwinCATDataType type)
{
// IEC 61131-3 TIME / DATE / DT / TOD all marshal as UDINT on the wire; the CLI
// accepts a numeric raw value and lets the caller handle the encoding.
WriteCommand.ParseValue("1234567", type).ShouldBeOfType();
}
[Fact]
public void ParseValue_Structure_refused()
{
Should.Throw(
() => WriteCommand.ParseValue("42", TwinCATDataType.Structure));
}
[Fact]
public void ParseValue_non_numeric_for_numeric_types_throws()
{
Should.Throw(
() => WriteCommand.ParseValue("xyz", TwinCATDataType.DInt));
}
[Theory]
[InlineData("MAIN.bStart", TwinCATDataType.Bool, "MAIN.bStart:Bool")]
[InlineData("GVL.Counter", TwinCATDataType.DInt, "GVL.Counter:DInt")]
[InlineData("Motor1.Status.Running", TwinCATDataType.Bool, "Motor1.Status.Running:Bool")]
[InlineData("Recipe[3]", TwinCATDataType.Real, "Recipe[3]:Real")]
public void SynthesiseTagName_preserves_symbolic_path_verbatim(
string symbol, TwinCATDataType type, string expected)
{
ReadCommand.SynthesiseTagName(symbol, type).ShouldBe(expected);
}
}