Changed UnitTests for further improvements on testing

This commit is contained in:
2025-02-05 20:46:13 +01:00
parent 980dab22f3
commit 5b8a2a8af1
18 changed files with 582 additions and 951 deletions

View File

@@ -43,29 +43,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(0)]
[DataRow(2001)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeReadCoils(int count)
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadCoils()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -94,16 +88,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadCoils()
{
// Arrange
var protocol = new RtuProtocol();
// Act
_ = protocol.DeserializeReadCoils([UNIT_ID, 0x01, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadCoils([UNIT_ID, 0x01, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]));
}
#endregion Read Coils
@@ -142,29 +133,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(0)]
[DataRow(2001)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeReadDiscreteInputs(int count)
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadDiscreteInputs()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -193,16 +178,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDiscreteInputs()
{
// Arrange
var protocol = new RtuProtocol();
// Act
_ = protocol.DeserializeReadDiscreteInputs([UNIT_ID, 0x02, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDiscreteInputs([UNIT_ID, 0x02, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]));
}
#endregion Read Discrete Inputs
@@ -241,29 +223,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(0)]
[DataRow(126)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeReadHoldingRegisters(int count)
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadHoldingRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -287,16 +263,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadHoldingRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.DeserializeReadHoldingRegisters([UNIT_ID, 0x03, 0x04, 0x02, 0x2B, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadHoldingRegisters([UNIT_ID, 0x03, 0x04, 0x02, 0x2B, 0x00, 0x00]));
}
#endregion Read Holding Registers
@@ -335,29 +308,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(0)]
[DataRow(126)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeReadInputRegisters(int count)
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadInputRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -381,16 +348,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadInputRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.DeserializeReadInputRegisters([UNIT_ID, 0x04, 0x04, 0x02, 0x2B, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadInputRegisters([UNIT_ID, 0x04, 0x04, 0x02, 0x2B, 0x00, 0x00]));
}
#endregion Read Input Registers
@@ -433,16 +397,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeExceptionForCategoryOnSerializeReadDeviceIdentification()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode));
}
[DataTestMethod]
@@ -469,27 +430,25 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForMeiType()
{
// Arrange
byte[] response = [UNIT_ID, 0x2B, 0x0D, 0x00, 0x00];
var protocol = new RtuProtocol();
// Act
protocol.DeserializeReadDeviceIdentification(response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDeviceIdentification(response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForCategory()
{
// Arrange
byte[] response = [UNIT_ID, 0x2B, 0x0E, 0x08, 0x00, 0x00];
var protocol = new RtuProtocol();
// Act
protocol.DeserializeReadDeviceIdentification(response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDeviceIdentification(response));
}
#endregion Read Device Identification
@@ -528,16 +487,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleCoil()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteSingleCoil(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteSingleCoil(UNIT_ID, null));
}
[TestMethod]
@@ -592,16 +548,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleHoldingRegister()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null));
}
[TestMethod]
@@ -669,22 +622,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleCoils()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleCoils(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, null));
}
[DataTestMethod]
[DataRow(0)]
[DataRow(1969)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleCoils(int count)
{
// Arrange
@@ -694,14 +643,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleCoils(UNIT_ID, coils);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleCoils()
{
// Arrange
@@ -712,14 +658,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleCoils(UNIT_ID, coils);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleCoils()
{
// Arrange
@@ -730,10 +673,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleCoils(UNIT_ID, coils);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils));
}
[TestMethod]
@@ -800,22 +741,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleHoldingRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null));
}
[DataTestMethod]
[DataRow(0)]
[DataRow(124)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleHoldingRegisters(int count)
{
// Arrange
@@ -825,14 +762,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleHoldingRegisters()
{
// Arrange
@@ -843,14 +777,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleHoldingRegisters()
{
// Arrange
@@ -861,10 +792,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers));
}
[TestMethod]
@@ -1105,7 +1034,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForUnitIdOnValidateResponse()
{
// Arrange
@@ -1114,14 +1042,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[DataTestMethod]
[DataRow(0x57, 0x6C)]
[DataRow(0x58, 0x6B)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForCrcOnValidateResponse(int hi, int lo)
{
// Arrange
@@ -1129,12 +1056,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
byte[] response = [UNIT_ID, 0x01, 0x01, 0x00, (byte)hi, (byte)lo];
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForFunctionCodeOnValidateResponse()
{
// Arrange
@@ -1143,12 +1069,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForErrorOnValidateResponse()
{
// Arrange
@@ -1157,8 +1082,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[DataTestMethod]
@@ -1166,7 +1091,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataRow(0x02)]
[DataRow(0x03)]
[DataRow(0x04)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForReadLengthOnValidateResponse(int fn)
{
// Arrange
@@ -1175,8 +1099,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[DataTestMethod]
@@ -1184,7 +1108,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataRow(0x06)]
[DataRow(0x0F)]
[DataRow(0x10)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForWriteLengthOnValidateResponse(int fn)
{
// Arrange
@@ -1193,8 +1116,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
@@ -1217,43 +1140,36 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(null)]
[DataRow(new byte[0])]
[ExpectedException(typeof(ArgumentNullException))]
public void ShuldThrowArgumentNullExceptionForBytesOnCrc16(byte[] bytes)
{
// Act
_ = RtuProtocol.CRC16(bytes);
// Arrange
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => RtuProtocol.CRC16(bytes));
}
[DataTestMethod]
[DataRow(-1)]
[DataRow(10)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForStartOnCrc16(int start)
{
// Arrange
byte[] bytes = Encoding.UTF8.GetBytes("0123456789");
// Act
_ = RtuProtocol.CRC16(bytes, start);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => RtuProtocol.CRC16(bytes, start));
}
[DataTestMethod]
[DataRow(0)]
[DataRow(11)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForLengthOnCrc16(int length)
{
// Arrange
byte[] bytes = Encoding.UTF8.GetBytes("0123456789");
// Act
_ = RtuProtocol.CRC16(bytes, 0, length);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => RtuProtocol.CRC16(bytes, 0, length));
}
#endregion Validation