modbus icon indicating copy to clipboard operation
modbus copied to clipboard

Add unit tests for modbus core

Open Copilot opened this issue 4 months ago • 0 comments

Adds comprehensive test coverage for the modbus core library to ensure correctness and stability.

Changes

  • Migrated Core to .NET Standard 2.0 - Converted from .NET Framework 4.5 to enable modern tooling and cross-platform compatibility
  • Added 59 xUnit tests covering:
    • CRC16 checksum computation (edge cases, offsets, empty ranges)
    • Big-endian byte conversions for Int16/UInt16 types
    • Struct serialization with endianness attributes and arrays
    • RTU request builder (CRC validation, data appending, error cases)
    • TCP request builder (MBAP header, auto-length computation)
    • Constants validation
  • Exposed internal types via InternalsVisibleTo for test access

Example Usage

[Fact]
public void Build_CrcValidation_ProducesCorrectCrc()
{
    var builder = new RtuRequest.Builder()
        .SetSlaveAddress(0x01)
        .SetFunctionCode(0x03)
        .SetInt16(0x0000)
        .AddInt16(0x0002);
    
    var request = builder.Build();
    
    // For message: 01 03 00 00 00 02, CRC should be C4 0B
    Assert.Equal(0xC4, request.RequestBytes[6]);
    Assert.Equal(0x0B, request.RequestBytes[7]);
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Add unit test for modbus core</issue_title> <issue_description>Ensure the core modbus work correctly and stable with bestpractice</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes sontx/modbus#6

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Oct 27 '25 13:10 Copilot