Task #250 — AB CIP + AB Legacy test-client CLIs

Second + third of the four driver test clients. Both follow the same shape as
otopcua-modbus-cli (#249) and consume Driver.Cli.Common for DriverCommandBase +
SnapshotFormatter.

New projects:
  - src/ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli/ — otopcua-abcip-cli.
    AbCipCommandBase carries gateway (ab://host[:port]/cip-path) + family
    (ControlLogix/CompactLogix/Micro800/GuardLogix) + timeout.
    Commands: probe, read, write, subscribe.
    Value parser covers every AbCipDataType atomic type (Bool, SInt..LInt,
    USInt..ULInt, Real, LReal, String, Dt); Structure writes refused as
    out-of-scope for the CLI.
  - src/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Cli/ — otopcua-ablegacy-cli.
    AbLegacyCommandBase carries gateway + plc-type (Slc500/MicroLogix/Plc5/
    LogixPccc) + timeout.
    Commands: probe (default address N7:0), read, write, subscribe.
    Value parser covers Bit, Int, Long, Float, AnalogInt, String, and the
    three sub-element types (TimerElement / CounterElement / ControlElement
    all land on int32 at the wire).

Tests (35 new, 73 cumulative across the driver CLI family):
  - AB CIP: 17 tests — ParseValue happy-paths for every Logix atomic type,
    failure cases (non-numeric / bool garbage), tag-name synthesis.
  - AB Legacy: 18 tests — ParseValue coverage (Bit / Int / AnalogInt / Long /
    Float / String / sub-elements), PCCC address round-trip in tag names
    including bit-within-word + sub-element syntax.

Docs:
  - docs/Driver.AbCip.Cli.md — family ↔ CIP-path cheat sheet + examples per
    command + typical workflows.
  - docs/Driver.AbLegacy.Cli.md — PCCC address primer (file letters → CLI
    --type) + known ab_server upstream gap cross-ref to #224 close-out.

Wiring:
  - ZB.MOM.WW.OtOpcUa.slnx grew 4 entries (2 src + 2 tests).

Full-solution build clean. `otopcua-abcip-cli --help` + `otopcua-ablegacy-cli
--help` verified end-to-end.

Next up (#251): S7 + TwinCAT CLIs, same pattern.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-04-21 08:32:43 -04:00
parent 9020b5854c
commit b2065f8730
21 changed files with 1188 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Commands;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Tests;
/// <summary>
/// Covers <see cref="WriteCommand.ParseValue"/>. Every Logix atomic type has at least
/// one happy-path case plus a failure case for unparseable input.
/// </summary>
[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, AbCipDataType.Bool).ShouldBe(expected);
}
[Fact]
public void ParseValue_Bool_rejects_garbage()
{
Should.Throw<CliFx.Exceptions.CommandException>(
() => WriteCommand.ParseValue("maybe", AbCipDataType.Bool));
}
[Fact]
public void ParseValue_SInt_widens_to_sbyte()
{
WriteCommand.ParseValue("-128", AbCipDataType.SInt).ShouldBe((sbyte)-128);
WriteCommand.ParseValue("127", AbCipDataType.SInt).ShouldBe((sbyte)127);
}
[Fact]
public void ParseValue_Int_signed_16bit()
{
WriteCommand.ParseValue("-32768", AbCipDataType.Int).ShouldBe((short)-32768);
}
[Fact]
public void ParseValue_DInt_and_Dt_both_land_on_int()
{
WriteCommand.ParseValue("42", AbCipDataType.DInt).ShouldBeOfType<int>();
WriteCommand.ParseValue("1234567", AbCipDataType.Dt).ShouldBeOfType<int>();
}
[Fact]
public void ParseValue_LInt_64bit()
{
WriteCommand.ParseValue("9223372036854775807", AbCipDataType.LInt).ShouldBe(long.MaxValue);
}
[Fact]
public void ParseValue_unsigned_range_respects_bounds()
{
WriteCommand.ParseValue("255", AbCipDataType.USInt).ShouldBeOfType<byte>();
WriteCommand.ParseValue("65535", AbCipDataType.UInt).ShouldBeOfType<ushort>();
WriteCommand.ParseValue("4294967295", AbCipDataType.UDInt).ShouldBeOfType<uint>();
}
[Fact]
public void ParseValue_Real_invariant_culture_decimal()
{
WriteCommand.ParseValue("3.14", AbCipDataType.Real).ShouldBe(3.14f);
}
[Fact]
public void ParseValue_LReal_handles_double_precision()
{
WriteCommand.ParseValue("2.718281828", AbCipDataType.LReal).ShouldBeOfType<double>();
}
[Fact]
public void ParseValue_String_passthrough()
{
WriteCommand.ParseValue("hello logix", AbCipDataType.String).ShouldBe("hello logix");
}
[Fact]
public void ParseValue_non_numeric_for_numeric_types_throws()
{
Should.Throw<FormatException>(
() => WriteCommand.ParseValue("xyz", AbCipDataType.DInt));
}
[Theory]
[InlineData("Motor01_Speed", AbCipDataType.Real, "Motor01_Speed:Real")]
[InlineData("Program:Main.Counter", AbCipDataType.DInt, "Program:Main.Counter:DInt")]
[InlineData("Recipe[3]", AbCipDataType.Int, "Recipe[3]:Int")]
public void SynthesiseTagName_preserves_path_verbatim(
string path, AbCipDataType type, string expected)
{
ReadCommand.SynthesiseTagName(path, type).ShouldBe(expected);
}
}