fix(driver-ablegacy): resolve Medium code-review finding (Driver.AbLegacy-010)

MapLibplctagStatus now casts the int to libplctag.Status and switches on
named enum members (mirroring AbCipStatusMapper) instead of unverified
magic integers. A strongly-typed Status overload is the canonical path;
the int overload delegates to it. MapPcccStatus is retained with a comment
marking it as the reference mapping for future PCCC-STS inspection.
Tests updated to use Status enum members rather than raw integers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-05-22 09:28:27 -04:00
parent 54d51a1d20
commit 228ad42ad7
3 changed files with 64 additions and 25 deletions

View File

@@ -1,3 +1,4 @@
using libplctag;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;
@@ -53,16 +54,38 @@ public sealed class AbLegacyHostAndStatusTests
AbLegacyStatusMapper.MapPcccStatus(sts).ShouldBe(expected);
}
// Driver.AbLegacy-010 — tests use the libplctag.NET Status enum members (what
// (int)Tag.GetStatus() actually returns) rather than the unverified magic integers
// that predated this fix (-5/-7/-14/-16/-17 matched neither native PLCTAG_ERR_*
// constants nor the .NET wrapper enum ordinals reliably).
[Theory]
[InlineData(0, AbLegacyStatusMapper.Good)]
[InlineData(1, AbLegacyStatusMapper.GoodMoreData)]
[InlineData(-5, AbLegacyStatusMapper.BadTimeout)]
[InlineData(-7, AbLegacyStatusMapper.BadCommunicationError)]
[InlineData(-14, AbLegacyStatusMapper.BadNodeIdUnknown)]
[InlineData(-16, AbLegacyStatusMapper.BadNotWritable)]
[InlineData(-17, AbLegacyStatusMapper.BadOutOfRange)]
public void LibplctagStatus_maps_known_codes(int status, uint expected)
[InlineData(Status.Ok, AbLegacyStatusMapper.Good)]
[InlineData(Status.Pending, AbLegacyStatusMapper.GoodMoreData)]
[InlineData(Status.ErrorTimeout, AbLegacyStatusMapper.BadTimeout)]
[InlineData(Status.ErrorNotFound, AbLegacyStatusMapper.BadNodeIdUnknown)]
[InlineData(Status.ErrorNoMatch, AbLegacyStatusMapper.BadNodeIdUnknown)]
[InlineData(Status.ErrorNotAllowed, AbLegacyStatusMapper.BadNotWritable)]
[InlineData(Status.ErrorOutOfBounds, AbLegacyStatusMapper.BadOutOfRange)]
[InlineData(Status.ErrorTooLarge, AbLegacyStatusMapper.BadOutOfRange)]
[InlineData(Status.ErrorBadConnection, AbLegacyStatusMapper.BadCommunicationError)]
[InlineData(Status.ErrorBadGateway, AbLegacyStatusMapper.BadCommunicationError)]
[InlineData(Status.ErrorUnsupported, AbLegacyStatusMapper.BadNotSupported)]
[InlineData(Status.ErrorNoMem, AbLegacyStatusMapper.BadCommunicationError)] // unmapped → generic comms
public void LibplctagStatus_maps_real_enum_members(Status status, uint expected)
{
AbLegacyStatusMapper.MapLibplctagStatus(status).ShouldBe(expected);
// The int overload must agree — it is the seam IAbLegacyTagRuntime.GetStatus() drives.
AbLegacyStatusMapper.MapLibplctagStatus((int)status).ShouldBe(expected);
}
[Fact]
public void MapLibplctagStatus_distinguishes_timeout_from_generic_comms_error()
{
// Regression for Driver.AbLegacy-010: timeout must not fall through to
// BadCommunicationError the way the old magic-integer switch did.
AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorTimeout)
.ShouldBe(AbLegacyStatusMapper.BadTimeout);
AbLegacyStatusMapper.MapLibplctagStatus((int)Status.ErrorNotFound)
.ShouldBe(AbLegacyStatusMapper.BadNodeIdUnknown);
}
}