feat(driver-modbus): shared TagConfigJson readers, writable key, Inspect warnings (R2-11)

This commit is contained in:
Joseph Doherty
2026-07-13 10:55:41 -04:00
parent a9b7a5002a
commit 64b9bba9e9
4 changed files with 121 additions and 8 deletions
@@ -0,0 +1,71 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.Modbus;
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
/// <summary>
/// R2-11 (05/CONV-2) strictness surface for the Modbus equipment-tag parser: the runtime stays lenient
/// (freeze), the new <c>Inspect</c> reports the silently-defaulted typo, and the <c>writable</c> key is
/// now honoured (default true).
/// </summary>
public sealed class ModbusEquipmentTagParserStrictnessTests
{
// ---- Phase-A behaviour freeze: a typo'd enum still silently defaults ----
[Fact]
public void Freeze_typo_dataType_still_defaults_to_Int16()
{
ModbusEquipmentTagParser.TryParse(
"{\"region\":\"HoldingRegisters\",\"address\":1,\"dataType\":\"Intt16\"}", out var def)
.ShouldBeTrue();
def.DataType.ShouldBe(ModbusDataType.Int16);
}
// ---- Inspect reports the typo ----
[Fact]
public void Inspect_reports_typo_dataType_with_field_and_valid_values()
{
var warnings = ModbusEquipmentTagParser.Inspect(
"{\"region\":\"HoldingRegisters\",\"address\":1,\"dataType\":\"Intt16\"}");
warnings.ShouldHaveSingleItem();
warnings[0].ShouldContain("Intt16");
warnings[0].ShouldContain("dataType");
warnings[0].ShouldContain("Int16");
}
[Fact]
public void Inspect_clean_config_has_no_warnings()
{
ModbusEquipmentTagParser.Inspect(
"{\"region\":\"HoldingRegisters\",\"address\":1,\"dataType\":\"Int16\"}")
.ShouldBeEmpty();
}
[Fact]
public void Inspect_malformed_json_warns_unresolvable()
{
ModbusEquipmentTagParser.Inspect("{not json").ShouldHaveSingleItem();
}
// ---- writable now honoured ----
[Fact]
public void Writable_false_is_honoured()
{
ModbusEquipmentTagParser.TryParse(
"{\"region\":\"HoldingRegisters\",\"address\":1,\"dataType\":\"Int16\",\"writable\":false}", out var def)
.ShouldBeTrue();
def.Writable.ShouldBeFalse();
}
[Fact]
public void Writable_absent_defaults_to_true()
{
ModbusEquipmentTagParser.TryParse(
"{\"region\":\"HoldingRegisters\",\"address\":1,\"dataType\":\"Int16\"}", out var def)
.ShouldBeTrue();
def.Writable.ShouldBeTrue();
}
}