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

@@ -55,29 +55,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 RtuOverTcpProtocol();
// 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 RtuOverTcpProtocol();
// Act
protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -106,16 +100,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadCoils()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadCoils([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x01, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadCoils([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x01, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]));
}
#endregion Read Coils
@@ -166,29 +157,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 RtuOverTcpProtocol();
// 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 RtuOverTcpProtocol();
// Act
protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -217,16 +202,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDiscreteInputs()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadDiscreteInputs([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x02, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDiscreteInputs([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x02, 0x02, 0xCD, 0x6B, 0x05, 0x00, 0x00]));
}
#endregion Read Discrete Inputs
@@ -277,29 +259,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 RtuOverTcpProtocol();
// 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 RtuOverTcpProtocol();
// Act
protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -323,16 +299,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadHoldingRegisters()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadHoldingRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x07, UNIT_ID, 0x03, 0x04, 0x02, 0x2B, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadHoldingRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x03, 0x04, 0x02, 0x2B, 0x00, 0x00]));
}
#endregion Read Holding Registers
@@ -383,29 +356,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 RtuOverTcpProtocol();
// 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 RtuOverTcpProtocol();
// Act
protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2);
// Assert - ArgumentOutOfRangeException
// Act + Assert
Assert.ThrowsException<ArgumentOutOfRangeException>(() => protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2));
}
[TestMethod]
@@ -429,16 +396,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadInputRegisters()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadInputRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x07, UNIT_ID, 0x04, 0x04, 0x02, 0x2B, 0x00, 0x00]);
// Assert - ModbusException
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadInputRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x08, UNIT_ID, 0x04, 0x04, 0x02, 0x2B, 0x00, 0x00]));
}
#endregion Read Input Registers
@@ -493,16 +457,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeExceptionForCategoryOnSerializeReadDeviceIdentification()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// 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]
@@ -529,27 +490,25 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForMeiType()
{
// Arrange
byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x05, UNIT_ID, 0x2B, 0x0D, 0x00, 0x00];
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadDeviceIdentification(response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDeviceIdentification(response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForCategory()
{
// Arrange
byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x06, UNIT_ID, 0x2B, 0x0E, 0x08, 0x00, 0x00];
var protocol = new RtuOverTcpProtocol();
// Act
protocol.DeserializeReadDeviceIdentification(response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.DeserializeReadDeviceIdentification(response));
}
#endregion Read Device Identification
@@ -600,16 +559,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleCoil()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.SerializeWriteSingleCoil(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteSingleCoil(UNIT_ID, null));
}
[TestMethod]
@@ -676,16 +632,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleHoldingRegister()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// Act
protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null);
// Assert - ArgumentNullException
// Act + Assert
Assert.ThrowsException<ArgumentNullException>(() => protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null));
}
[TestMethod]
@@ -765,22 +718,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleCoils()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// 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
@@ -790,14 +739,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
var protocol = new RtuOverTcpProtocol();
// 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
@@ -808,14 +754,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuOverTcpProtocol();
// 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
@@ -826,10 +769,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuOverTcpProtocol();
// Act
protocol.SerializeWriteMultipleCoils(UNIT_ID, coils);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils));
}
[TestMethod]
@@ -908,22 +849,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleHoldingRegisters()
{
// Arrange
var protocol = new RtuOverTcpProtocol();
// 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
@@ -933,14 +870,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
var protocol = new RtuOverTcpProtocol();
// 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
@@ -951,14 +885,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuOverTcpProtocol();
// 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
@@ -969,10 +900,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
};
var protocol = new RtuOverTcpProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers);
// Assert - ArgumentException
// Act + Assert
Assert.ThrowsException<ArgumentException>(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers));
}
[TestMethod]
@@ -1065,7 +994,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
[DataTestMethod]
[DataRow(0x00, 0x00)]
[DataRow(0x01, 0x01)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForTransactionIdOnValidateResponse(int hi, int lo)
{
// Arrange
@@ -1074,14 +1002,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[DataTestMethod]
[DataRow(0x00, 0x01)]
[DataRow(0x01, 0x00)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForProtocolIdOnValidateResponse(int hi, int lo)
{
// Arrange
@@ -1090,12 +1017,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForFollowingBytesOnValidateResponse()
{
// Arrange
@@ -1104,12 +1030,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForUnitIdOnValidateResponse()
{
// Arrange
@@ -1118,12 +1043,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForFunctionCodeOnValidateResponse()
{
// Arrange
@@ -1132,12 +1056,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[TestMethod]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForModbusErrorOnValidateResponse()
{
// Arrange
@@ -1146,14 +1069,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
SetCrc(response);
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
[DataTestMethod]
[DataRow(0x59, 0x6C)]
[DataRow(0x58, 0x6B)]
[ExpectedException(typeof(ModbusException))]
public void ShouldThrowForCrcOnValidateResponse(int hi, int lo)
{
// Arrange
@@ -1161,8 +1083,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x06, UNIT_ID, 0x01, 0x01, 0x00, (byte)hi, (byte)lo];
var protocol = new RtuOverTcpProtocol();
// Act
protocol.ValidateResponse(request, response);
// Act + Assert
Assert.ThrowsException<ModbusException>(() => protocol.ValidateResponse(request, response));
}
#endregion Validation