From 5b8a2a8af1a07c26e73c64e9f57a1a56bf242590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Wed, 5 Feb 2025 20:46:13 +0100 Subject: [PATCH] Changed UnitTests for further improvements on testing --- .../Common/Contracts/ModbusClientBaseTest.cs | 21 +- .../Extensions/ModbusDecimalExtensionsTest.cs | 56 ++-- .../Common/Extensions/ModbusExtensionsTest.cs | 42 +-- .../Extensions/ModbusSignedExtensionsTest.cs | 96 ++----- .../ModbusUnsignedExtensionsTest.cs | 86 ++----- .../Common/Protocols/AsciiProtocolTest.cs | 239 ++++++------------ .../Protocols/RtuOverTcpProtocolTest.cs | 206 +++++---------- .../Common/Protocols/RtuProtocolTest.cs | 222 +++++----------- .../Common/Protocols/TcpProtocolTest.cs | 201 +++++---------- .../Serial/ModbusRtuProxyTest.cs | 21 +- .../Serial/ModbusSerialConnectionTest.cs | 212 ++++++++++++---- .../Tcp/ModbusTcpClientTest.cs | 13 + .../Tcp/ModbusTcpConnectionTest.cs | 81 +++--- .../Tcp/ModbusTcpProxyTest.cs | 15 +- .../ShouldPrintCleanString.snap.bin | 2 + .../ShouldReadDeviceIdentification.snap.bin | 9 + CHANGELOG.md | 4 +- README.md | 7 +- 18 files changed, 582 insertions(+), 951 deletions(-) create mode 100644 AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpClientTest/ShouldPrintCleanString.snap.bin create mode 100644 AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpProxyTest/ShouldReadDeviceIdentification.snap.bin diff --git a/AMWD.Protocols.Modbus.Tests/Common/Contracts/ModbusClientBaseTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Contracts/ModbusClientBaseTest.cs index 0fa7999..75396fe 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Contracts/ModbusClientBaseTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Contracts/ModbusClientBaseTest.cs @@ -105,16 +105,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Contracts } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowExceptionOnNullConnection() { // Arrange IModbusConnection connection = null; - // Act - new ModbusClientBaseWrapper(connection); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => new ModbusClientBaseWrapper(connection)); } [DataTestMethod] @@ -155,31 +152,25 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Contracts } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public async Task ShouldAssertDisposed() { // Arrange var client = GetClient(); client.Dispose(); - // Act - await client.ReadCoilsAsync(UNIT_ID, START_ADDRESS, READ_COUNT); - - // Assert - ObjectDisposedException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => client.ReadCoilsAsync(UNIT_ID, START_ADDRESS, READ_COUNT)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldAssertProtocolSet() { // Arrange var client = GetClient(); client.Protocol = null; - // Act - await client.ReadCoilsAsync(UNIT_ID, START_ADDRESS, READ_COUNT); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => client.ReadCoilsAsync(UNIT_ID, START_ADDRESS, READ_COUNT)); } #endregion Common/Connection/Assertions diff --git a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusDecimalExtensionsTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusDecimalExtensionsTest.cs index 0598e82..1968069 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusDecimalExtensionsTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusDecimalExtensionsTest.cs @@ -41,20 +41,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetSingle() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetSingle(0); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => registers.GetSingle(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetSingleForLength() { // Arrange @@ -63,16 +59,13 @@ new() { Address = 101, HighByte = 0x01, LowByte = 0x02 } }; - // Act - registers.GetSingle(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetSingle(0)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetSingle(int startIndex) { // Arrange @@ -82,14 +75,11 @@ new() { Address = 100, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetSingle(startIndex); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => registers.GetSingle(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetSingleForType() { // Arrange @@ -99,10 +89,8 @@ new InputRegister { Address = 101, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetSingle(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetSingle(0)); } [TestMethod] @@ -145,20 +133,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetDouble() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetDouble(0); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => registers.GetDouble(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetDoubleForLength() { // Arrange @@ -169,16 +153,13 @@ new() { Address = 102, HighByte = 0x7A, LowByte = 0xE1 } }; - // Act - registers.GetDouble(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetDouble(0)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetDouble(int startIndex) { // Arrange @@ -190,14 +171,11 @@ new() { Address = 103, HighByte = 0x47, LowByte = 0xAE } }; - // Act - registers.GetDouble(startIndex); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => registers.GetDouble(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetDoubleForType() { // Arrange @@ -209,10 +187,8 @@ new InputRegister { Address = 103, HighByte = 0x47, LowByte = 0xAE } }; - // Act - registers.GetDouble(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetDouble(0)); } #endregion Modbus to value diff --git a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusExtensionsTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusExtensionsTest.cs index cc24e7a..210b44a 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusExtensionsTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusExtensionsTest.cs @@ -30,16 +30,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Extensions } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetBoolean() { // Arrange Coil coil = null; - // Act - coil.GetBoolean(); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => coil.GetBoolean()); } [TestMethod] @@ -95,35 +92,28 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Extensions } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnString() { // Arrange HoldingRegister[] list = null; - // Act - list.GetString(2); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => list.GetString(2)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnStringForEmptyList() { // Arrange var registers = Array.Empty(); - // Act - registers.GetString(2); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetString(2)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnString(int startIndex) { // Arrange @@ -133,14 +123,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Extensions new() { Address = 2, HighByte = 67, LowByte = 0 } }; - // Act - registers.GetString(2, startIndex); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => registers.GetString(2, startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnStringForMixedTypes() { // Arrange @@ -150,10 +137,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Extensions new InputRegister { Address = 2, HighByte = 67, LowByte = 0 } }; - // Act - registers.GetString(2); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetString(2)); } #endregion Modbus to value @@ -272,16 +257,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Extensions } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetString() { // Arrange string str = null; - // Act - _ = str.ToRegisters(100).ToArray(); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => str.ToRegisters(100).ToArray()); } #endregion Value to Modbus diff --git a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusSignedExtensionsTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusSignedExtensionsTest.cs index b1c4ec8..f360228 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusSignedExtensionsTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusSignedExtensionsTest.cs @@ -32,31 +32,23 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullForGetSByte() { // Arrange HoldingRegister register = null; - // Act - register.GetSByte(); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => register.GetSByte()); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentForGetSByte() { // Arrange var obj = new Coil(); - // Act - obj.GetSByte(); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => obj.GetSByte()); } [TestMethod] @@ -86,31 +78,23 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullForGetInt16() { // Arrange HoldingRegister register = null; - // Act - register.GetInt16(); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => register.GetInt16()); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentForGetInt16() { // Arrange var obj = new Coil(); - // Act - obj.GetInt16(); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => obj.GetInt16()); } [TestMethod] @@ -149,21 +133,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetInt32() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetInt32(0); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt32(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetInt32ForLength() { // Arrange @@ -172,17 +151,13 @@ new HoldingRegister { Address = 101, HighByte = 0x01, LowByte = 0x02 } }; - // Act - registers.GetInt32(0); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt32(0)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetInt32(int startIndex) { // Arrange @@ -192,15 +167,11 @@ new HoldingRegister { Address = 100, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetInt32(startIndex); - - // Assert - ArgumentOutOfRangeException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt32(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetInt32ForType() { // Arrange @@ -210,11 +181,8 @@ new InputRegister { Address = 101, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetInt32(0); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt32(0)); } [TestMethod] @@ -257,21 +225,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetInt64() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetInt64(0); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt64(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetInt64ForLength() { // Arrange @@ -282,17 +245,13 @@ new HoldingRegister { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetInt64(0); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt64(0)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetInt64(int startIndex) { // Arrange @@ -304,15 +263,11 @@ new HoldingRegister { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetInt64(startIndex); - - // Assert - ArgumentOutOfRangeException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt64(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetInt64ForType() { // Arrange @@ -324,11 +279,8 @@ new InputRegister { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetInt64(0); - - // Assert - ArgumentException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetInt64(0)); } #endregion Modbus to value diff --git a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusUnsignedExtensionsTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusUnsignedExtensionsTest.cs index d521ba1..f068de2 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusUnsignedExtensionsTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Extensions/ModbusUnsignedExtensionsTest.cs @@ -32,29 +32,23 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullForGetByte() { // Arrange HoldingRegister register = null; - // Act - register.GetByte(); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => register.GetByte()); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentForGetByte() { // Arrange var obj = new Coil(); - // Act - obj.GetByte(); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => obj.GetByte()); } [TestMethod] @@ -84,29 +78,23 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullForGetUInt16() { // Arrange HoldingRegister register = null; - // Act - register.GetUInt16(); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => register.GetUInt16()); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentForGetUInt16() { // Arrange var obj = new Coil(); - // Act - obj.GetUInt16(); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => obj.GetUInt16()); } [TestMethod] @@ -145,21 +133,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetUInt32() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetUInt32(0); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt32(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetUInt32ForLength() { // Arrange @@ -168,16 +151,13 @@ new() { Address = 101, HighByte = 0x01, LowByte = 0x02 } }; - // Act - registers.GetUInt32(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt32(1)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetUInt32(int startIndex) { // Arrange @@ -187,14 +167,11 @@ new() { Address = 100, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetUInt32(startIndex); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt32(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetUInt32ForType() { // Arrange @@ -204,10 +181,8 @@ new InputRegister { Address = 101, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetUInt32(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt32(0)); } [TestMethod] @@ -250,21 +225,16 @@ } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowNullOnGetUInt64() { // Arrange HoldingRegister[] registers = null; - // Act - registers.GetUInt64(0); - - // Assert - ArgumentNullException - Assert.Fail(); + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt64(0)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetUInt64ForLength() { // Arrange @@ -275,16 +245,13 @@ new() { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetUInt64(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt64(0)); } [DataTestMethod] [DataRow(1)] [DataRow(-1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeOnGetUInt64(int startIndex) { // Arrange @@ -296,14 +263,11 @@ new() { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetUInt64(startIndex); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt64(startIndex)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentOnGetUInt64ForType() { // Arrange @@ -315,10 +279,8 @@ new InputRegister { Address = 103, HighByte = 0x03, LowByte = 0x04 } }; - // Act - registers.GetUInt64(0); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => registers.GetUInt64(0)); } #endregion Modbus to value diff --git a/AMWD.Protocols.Modbus.Tests/Common/Protocols/AsciiProtocolTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Protocols/AsciiProtocolTest.cs index 5a6f852..87d8096 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Protocols/AsciiProtocolTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Protocols/AsciiProtocolTest.cs @@ -32,29 +32,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 AsciiProtocol(); - // Act - protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadCoils() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -88,7 +82,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadCoils() { // Arrange @@ -98,10 +91,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - _ = protocol.DeserializeReadCoils(responseBytes); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadCoils(responseBytes)); } #endregion Read Coils @@ -129,29 +120,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 AsciiProtocol(); - // Act - protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadDiscreteInputs() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -185,7 +170,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDiscreteInputs() { // Arrange @@ -195,10 +179,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.DeserializeReadDiscreteInputs(responseBytes); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDiscreteInputs(responseBytes)); } #endregion Read Discrete Inputs @@ -226,29 +208,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols [DataTestMethod] [DataRow(0)] [DataRow(2001)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeReadHoldingRegisters(int count) { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadHoldingRegisters() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -276,7 +252,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadHoldingRegisters() { // Arrange @@ -286,10 +261,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.DeserializeReadHoldingRegisters(responseBytes); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadHoldingRegisters(responseBytes)); } #endregion Read Holding Registers @@ -317,29 +290,23 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols [DataTestMethod] [DataRow(0)] [DataRow(2001)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeReadInputRegisters(int count) { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadInputRegisters() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -367,7 +334,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadInputRegisters() { // Arrange @@ -377,10 +343,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.DeserializeReadInputRegisters(responseBytes); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadInputRegisters(responseBytes)); } #endregion Read Input Registers @@ -410,16 +374,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeExceptionForCategoryOnSerializeReadDeviceIdentification() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode)); } [DataTestMethod] @@ -449,7 +410,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForMeiType() { // Arrange @@ -459,12 +419,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.DeserializeReadDeviceIdentification(responseBytes); + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDeviceIdentification(responseBytes)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForCategory() { // Arrange @@ -474,8 +433,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.DeserializeReadDeviceIdentification(responseBytes); + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDeviceIdentification(responseBytes)); } #endregion Read Device Identification @@ -502,16 +461,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteSingleCoil() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteSingleCoil(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteSingleCoil(UNIT_ID, null)); } [TestMethod] @@ -557,16 +513,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteSingleHoldingRegister() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null)); } [TestMethod] @@ -619,22 +572,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteMultipleCoils() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, null)); } [DataTestMethod] [DataRow(0)] [DataRow(1969)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleCoils(int count) { // Arrange @@ -644,14 +593,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleCoils() { // Arrange @@ -662,14 +608,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleCoils() { // Arrange @@ -680,10 +623,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] @@ -732,22 +673,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteMultipleHoldingRegisters() { // Arrange var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null)); } [DataTestMethod] [DataRow(0)] [DataRow(124)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleHoldingRegisters(int count) { // Arrange @@ -757,14 +694,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleHoldingRegisters() { // Arrange @@ -775,14 +709,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleHoldingRegisters() { // Arrange @@ -793,10 +724,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new AsciiProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] @@ -898,7 +827,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForMissingHeaderOnValidateResponse() { // Arrange @@ -907,12 +835,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForMissingTrailerOnValidateResponse() { // Arrange @@ -920,12 +847,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols string response = $":{UNIT_ID:X2}010100"; var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForUnitIdOnValidateResponse() { // Arrange @@ -934,12 +860,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForLrcOnValidateResponse() { // Arrange @@ -947,12 +872,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols string response = $":{UNIT_ID:X2}010001FF00XX\r\n"; var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForFunctionCodeOnValidateResponse() { // Arrange @@ -961,12 +885,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForErrorOnValidateResponse() { // Arrange @@ -975,8 +898,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [DataTestMethod] @@ -984,7 +907,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols [DataRow(0x02)] [DataRow(0x03)] [DataRow(0x04)] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForReadLengthOnValidateResponse(int fn) { // Arrange @@ -993,8 +915,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [DataTestMethod] @@ -1002,7 +924,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols [DataRow(0x06)] [DataRow(0x0F)] [DataRow(0x10)] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForWriteLengthOnValidateResponse(int fn) { // Arrange @@ -1011,8 +932,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols AddTrailer(ref response); var protocol = new AsciiProtocol(); - // Act - protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response)); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(Encoding.ASCII.GetBytes(request), Encoding.ASCII.GetBytes(response))); } [TestMethod] @@ -1033,58 +954,46 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols [DataRow("")] [DataRow(" ")] [DataRow("\t")] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionForMessageOnLrc(string msg) { // Arrange - // Act - AsciiProtocol.LRC(msg); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => AsciiProtocol.LRC(msg)); } [DataTestMethod] [DataRow(-1)] [DataRow(4)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeExceptionForStartOnLrc(int start) { // Arrange string msg = "0207"; - // Act - AsciiProtocol.LRC(msg, start); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => AsciiProtocol.LRC(msg, start)); } [DataTestMethod] [DataRow(0)] [DataRow(5)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeExceptionForLengthOnLrc(int length) { // Arrange string msg = "0207"; - // Act - AsciiProtocol.LRC(msg, 0, length); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => AsciiProtocol.LRC(msg, 0, length)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForMessageLengthOnLrc() { // Arrange string msg = "0207"; - // Act - AsciiProtocol.LRC(msg); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => AsciiProtocol.LRC(msg)); } #endregion Validation diff --git a/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuOverTcpProtocolTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuOverTcpProtocolTest.cs index debda2f..1c163ac 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuOverTcpProtocolTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuOverTcpProtocolTest.cs @@ -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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => protocol.ValidateResponse(request, response)); } #endregion Validation diff --git a/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuProtocolTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuProtocolTest.cs index aae7ded..ca376b9 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuProtocolTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Protocols/RtuProtocolTest.cs @@ -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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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(() => RtuProtocol.CRC16(bytes, 0, length)); } #endregion Validation diff --git a/AMWD.Protocols.Modbus.Tests/Common/Protocols/TcpProtocolTest.cs b/AMWD.Protocols.Modbus.Tests/Common/Protocols/TcpProtocolTest.cs index b2bade7..780caa1 100644 --- a/AMWD.Protocols.Modbus.Tests/Common/Protocols/TcpProtocolTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Common/Protocols/TcpProtocolTest.cs @@ -53,29 +53,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 TcpProtocol(); - // Act - protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadCoils(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadCoils() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadCoils(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -104,16 +98,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadCoils() { // Arrange var protocol = new TcpProtocol(); - // Act - var coils = protocol.DeserializeReadCoils([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x01, 0x02, 0xCD, 0x6B, 0x05]); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadCoils([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x01, 0x02, 0xCD, 0x6B, 0x05])); } #endregion Read Coils @@ -162,29 +153,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 TcpProtocol(); - // Act - protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadDiscreteInputs() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDiscreteInputs(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -213,16 +198,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDiscreteInputs() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.DeserializeReadDiscreteInputs([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x02, 0x03, 0xCD, 0x6B]); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDiscreteInputs([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x02, 0x03, 0xCD, 0x6B])); } #endregion Read Discrete Inputs @@ -271,29 +253,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 TcpProtocol(); - // Act - protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadHoldingRegisters() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadHoldingRegisters(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -317,16 +293,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadHoldingRegisters() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.DeserializeReadHoldingRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x03, 0x04, 0x02, 0x2B]); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadHoldingRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x03, 0x04, 0x02, 0x2B])); } #endregion Read Holding Registers @@ -375,29 +348,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 TcpProtocol(); - // Act - protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadInputRegisters(UNIT_ID, 19, (ushort)count)); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForStartingAddressOnSerializeReadInputRegisters() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadInputRegisters(UNIT_ID, ushort.MaxValue, 2)); } [TestMethod] @@ -421,16 +388,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadInputRegisters() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.DeserializeReadInputRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x04, 0x04, 0x02, 0x2B]); - - // Assert - ModbusException + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadInputRegisters([0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x2A, 0x04, 0x04, 0x02, 0x2B])); } #endregion Read Input Registers @@ -483,16 +447,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeExceptionOnSerializeReadDeviceIdentification() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeReadDeviceIdentification(UNIT_ID, (ModbusDeviceIdentificationCategory)10, ModbusDeviceIdentificationObject.ProductCode)); } [DataTestMethod] @@ -519,27 +480,25 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForMeiType() { // Arrange byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x0D, 0x2A, 0x2B, 0x0D]; var protocol = new TcpProtocol(); - // Act - protocol.DeserializeReadDeviceIdentification(response); + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDeviceIdentification(response)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowExceptionOnDeserializeReadDeviceIdentificationForCategory() { // Arrange byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x0D, 0x2A, 0x2B, 0x0E, 0x08]; var protocol = new TcpProtocol(); - // Act - protocol.DeserializeReadDeviceIdentification(response); + // Act + Assert + Assert.ThrowsException(() => protocol.DeserializeReadDeviceIdentification(response)); } #endregion Read Device Identification @@ -588,16 +547,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteSingleCoil() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteSingleCoil(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteSingleCoil(UNIT_ID, null)); } [TestMethod] @@ -662,16 +618,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteSingleHoldingRegister() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteSingleHoldingRegister(UNIT_ID, null)); } [TestMethod] @@ -749,22 +702,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteMultipleCoils() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, null)); } [DataTestMethod] [DataRow(0)] [DataRow(1969)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleCoils(int count) { // Arrange @@ -774,14 +723,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleCoils() { // Arrange @@ -792,14 +738,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleCoils() { // Arrange @@ -810,10 +753,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleCoils(UNIT_ID, coils); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleCoils(UNIT_ID, coils)); } [TestMethod] @@ -890,22 +831,18 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullOnSerializeWriteMultipleHoldingRegisters() { // Arrange var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, null)); } [DataTestMethod] [DataRow(0)] [DataRow(124)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowOutOfRangeForCountOnSerializeWriteMultipleHoldingRegisters(int count) { // Arrange @@ -915,14 +852,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForDuplicateEntryOnSerializeMultipleHoldingRegisters() { // Arrange @@ -933,14 +867,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void ShouldThrowArgumentExceptionForGapInAddressOnSerializeMultipleHoldingRegisters() { // Arrange @@ -951,10 +882,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols }; var protocol = new TcpProtocol(); - // Act - protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers); - - // Assert - ArgumentException + // Act + Assert + Assert.ThrowsException(() => protocol.SerializeWriteMultipleHoldingRegisters(UNIT_ID, registers)); } [TestMethod] @@ -1045,7 +974,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 @@ -1053,14 +981,13 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x2A, 0x01, 0x01, 0x00]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } [DataTestMethod] [DataRow(0x00, 0x01)] [DataRow(0x01, 0x00)] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForProtocolIdOnValidateResponse(int hi, int lo) { // Arrange @@ -1068,12 +995,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x2A, 0x01, 0x01, 0x00]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForFollowingBytesOnValidateResponse() { // Arrange @@ -1081,12 +1007,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2A, 0x01, 0x01, 0x00]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForUnitIdOnValidateResponse() { // Arrange @@ -1094,12 +1019,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x2B, 0x01, 0x01, 0x00]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForFunctionCodeOnValidateResponse() { // Arrange @@ -1107,12 +1031,11 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x2A, 0x02, 0x01, 0x00]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } [TestMethod] - [ExpectedException(typeof(ModbusException))] public void ShouldThrowForModbusErrorOnValidateResponse() { // Arrange @@ -1120,8 +1043,8 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols byte[] response = [0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x2A, 0x81, 0x01]; var protocol = new TcpProtocol(); - // Act - protocol.ValidateResponse(request, response); + // Act + Assert + Assert.ThrowsException(() => protocol.ValidateResponse(request, response)); } #endregion Validation diff --git a/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs b/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs index 99373f2..6cacf83 100644 --- a/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs @@ -144,49 +144,40 @@ namespace AMWD.Protocols.Modbus.Tests.Serial } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionOnCreateInstanceForClient() { // Arrange - // Act - new ModbusRtuProxy(null, "some-port"); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => new ModbusRtuProxy(null, "some-port")); } [DataTestMethod] [DataRow(null)] [DataRow("")] [DataRow(" ")] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionOnCreateInstanceForPortName(string portName) { // Arrange var connection = new Mock(); var clientMock = new Mock(connection.Object); - // Act - new ModbusRtuProxy(clientMock.Object, portName); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => new ModbusRtuProxy(clientMock.Object, portName)); } [DataTestMethod] [DataRow(null)] [DataRow("")] [DataRow(" ")] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldThrowArgumentNullExceptionOnMissingPortName(string portName) { // Arrange using var proxy = GetProxy(); _serialPortMock.Setup(m => m.PortName).Returns(portName); - // Act - await proxy.StartAsync(); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => proxy.StartAsync()); } [TestMethod] diff --git a/AMWD.Protocols.Modbus.Tests/Serial/ModbusSerialConnectionTest.cs b/AMWD.Protocols.Modbus.Tests/Serial/ModbusSerialConnectionTest.cs index d173eb7..38ab412 100644 --- a/AMWD.Protocols.Modbus.Tests/Serial/ModbusSerialConnectionTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Serial/ModbusSerialConnectionTest.cs @@ -94,58 +94,46 @@ namespace AMWD.Protocols.Modbus.Tests.Serial [DataRow(null)] [DataRow("")] [DataRow(" ")] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionOnCreate(string portName) { // Arrange - // Act - using var test = new ModbusSerialClient(portName); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => new ModbusSerialClient(portName)); } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public async Task ShouldThrowDisposedExceptionOnInvokeAsync() { // Arrange var connection = GetConnection(); connection.Dispose(); - // Act - await connection.InvokeAsync(null, null); - - // Assert - OjbectDisposedException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(null, null)); } [DataTestMethod] [DataRow(null)] [DataRow(new byte[0])] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldThrowArgumentNullExceptionForMissingRequestOnInvokeAsync(byte[] request) { // Arrange var connection = GetConnection(); - // Act - await connection.InvokeAsync(request, null); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, null)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldThrowArgumentNullExceptionForMissingValidationOnInvokeAsync() { // Arrange byte[] request = new byte[1]; var connection = GetConnection(); - // Act - await connection.InvokeAsync(request, null); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, null)); } [TestMethod] @@ -176,10 +164,8 @@ namespace AMWD.Protocols.Modbus.Tests.Serial _serialPortMock.VerifyNoOtherCalls(); } - [DataTestMethod] - [DataRow(false)] - [DataRow(true)] - public async Task ShouldOpenAndCloseOnInvokeAsync(bool modifyDriver) + [TestMethod] + public async Task ShouldOpenAndCloseOnInvokeAsyncOnLinuxNotModifyingDriver() { // Arrange _alwaysOpen = false; @@ -193,8 +179,9 @@ namespace AMWD.Protocols.Modbus.Tests.Serial _serialLineResponseQueue.Enqueue(expectedResponse); var connection = GetSerialConnection(); + connection.GetType().GetField("_isLinux", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(connection, true); connection.IdleTimeout = TimeSpan.FromMilliseconds(200); - connection.DriverEnabledRS485 = modifyDriver; + connection.DriverEnabledRS485 = false; // Act var response = await connection.InvokeAsync(request, validation); @@ -213,11 +200,134 @@ namespace AMWD.Protocols.Modbus.Tests.Serial _serialPortMock.Verify(c => c.ResetRS485DriverStateFlags(), Times.Exactly(2)); _serialPortMock.Verify(c => c.Open(), Times.Once); - if (modifyDriver) - { - _serialPortMock.Verify(c => c.GetRS485DriverStateFlags(), Times.Once); - _serialPortMock.Verify(c => c.ChangeRS485DriverStateFlags(It.IsAny()), Times.Once); - } + _serialPortMock.Verify(ns => ns.WriteAsync(It.IsAny(), It.IsAny()), Times.Once); + _serialPortMock.Verify(ns => ns.ReadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + _serialPortMock.VerifyNoOtherCalls(); + } + + [TestMethod] + public async Task ShouldOpenAndCloseOnInvokeAsyncOnLinuxModifyingDriver() + { + // Arrange + _alwaysOpen = false; + _isOpenQueue.Enqueue(false); + _isOpenQueue.Enqueue(true); + _isOpenQueue.Enqueue(true); + + byte[] request = [1, 2, 3]; + byte[] expectedResponse = [9, 8, 7]; + var validation = new Func, bool>(_ => true); + _serialLineResponseQueue.Enqueue(expectedResponse); + + var connection = GetSerialConnection(); + connection.GetType().GetField("_isLinux", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(connection, true); + connection.IdleTimeout = TimeSpan.FromMilliseconds(200); + connection.DriverEnabledRS485 = true; + + // Act + var response = await connection.InvokeAsync(request, validation); + await Task.Delay(500); + + // Assert + Assert.IsNotNull(response); + + CollectionAssert.AreEqual(expectedResponse, response.ToArray()); + CollectionAssert.AreEqual(request, _serialLineRequestCallbacks.First()); + + _serialPortMock.VerifyGet(c => c.ReadTimeout, Times.Once); + + _serialPortMock.Verify(c => c.IsOpen, Times.Exactly(3)); + _serialPortMock.Verify(c => c.Close(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.ResetRS485DriverStateFlags(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.Open(), Times.Once); + + _serialPortMock.Verify(c => c.GetRS485DriverStateFlags(), Times.Once); + _serialPortMock.Verify(c => c.ChangeRS485DriverStateFlags(It.IsAny()), Times.Once); + + _serialPortMock.Verify(ns => ns.WriteAsync(It.IsAny(), It.IsAny()), Times.Once); + _serialPortMock.Verify(ns => ns.ReadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + _serialPortMock.VerifyNoOtherCalls(); + } + + [TestMethod] + public async Task ShouldOpenAndCloseOnInvokeAsyncOnOtherOsNotModifyingDriver() + { + // Arrange + _alwaysOpen = false; + _isOpenQueue.Enqueue(false); + _isOpenQueue.Enqueue(true); + _isOpenQueue.Enqueue(true); + + byte[] request = [1, 2, 3]; + byte[] expectedResponse = [9, 8, 7]; + var validation = new Func, bool>(_ => true); + _serialLineResponseQueue.Enqueue(expectedResponse); + + var connection = GetSerialConnection(); + connection.GetType().GetField("_isLinux", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(connection, false); + connection.IdleTimeout = TimeSpan.FromMilliseconds(200); + connection.DriverEnabledRS485 = false; + + // Act + var response = await connection.InvokeAsync(request, validation); + await Task.Delay(500); + + // Assert + Assert.IsNotNull(response); + + CollectionAssert.AreEqual(expectedResponse, response.ToArray()); + CollectionAssert.AreEqual(request, _serialLineRequestCallbacks.First()); + + _serialPortMock.VerifyGet(c => c.ReadTimeout, Times.Once); + + _serialPortMock.Verify(c => c.IsOpen, Times.Exactly(3)); + _serialPortMock.Verify(c => c.Close(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.ResetRS485DriverStateFlags(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.Open(), Times.Once); + + _serialPortMock.Verify(ns => ns.WriteAsync(It.IsAny(), It.IsAny()), Times.Once); + _serialPortMock.Verify(ns => ns.ReadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + _serialPortMock.VerifyNoOtherCalls(); + } + + [TestMethod] + public async Task ShouldOpenAndCloseOnInvokeAsyncOnOtherOsModifyingDriver() + { + // Arrange + _alwaysOpen = false; + _isOpenQueue.Enqueue(false); + _isOpenQueue.Enqueue(true); + _isOpenQueue.Enqueue(true); + + byte[] request = [1, 2, 3]; + byte[] expectedResponse = [9, 8, 7]; + var validation = new Func, bool>(_ => true); + _serialLineResponseQueue.Enqueue(expectedResponse); + + var connection = GetSerialConnection(); + connection.GetType().GetField("_isLinux", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(connection, false); + connection.IdleTimeout = TimeSpan.FromMilliseconds(200); + connection.DriverEnabledRS485 = true; + + // Act + var response = await connection.InvokeAsync(request, validation); + await Task.Delay(500); + + // Assert + Assert.IsNotNull(response); + + CollectionAssert.AreEqual(expectedResponse, response.ToArray()); + CollectionAssert.AreEqual(request, _serialLineRequestCallbacks.First()); + + _serialPortMock.VerifyGet(c => c.ReadTimeout, Times.Once); + + _serialPortMock.Verify(c => c.IsOpen, Times.Exactly(3)); + _serialPortMock.Verify(c => c.Close(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.ResetRS485DriverStateFlags(), Times.Exactly(2)); + _serialPortMock.Verify(c => c.Open(), Times.Once); _serialPortMock.Verify(ns => ns.WriteAsync(It.IsAny(), It.IsAny()), Times.Once); _serialPortMock.Verify(ns => ns.ReadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); @@ -226,7 +336,6 @@ namespace AMWD.Protocols.Modbus.Tests.Serial } [TestMethod] - [ExpectedException(typeof(EndOfStreamException))] public async Task ShouldThrowEndOfStreamExceptionOnInvokeAsync() { // Arrange @@ -235,10 +344,8 @@ namespace AMWD.Protocols.Modbus.Tests.Serial var connection = GetConnection(); - // Act - var response = await connection.InvokeAsync(request, validation); - - // Assert - EndOfStreamException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, validation)); } [TestMethod] @@ -320,7 +427,6 @@ namespace AMWD.Protocols.Modbus.Tests.Serial } [TestMethod] - [ExpectedException(typeof(TaskCanceledException))] public async Task ShouldThrowTaskCancelledExceptionForDisposeOnInvokeAsync() { // Arrange @@ -332,16 +438,16 @@ namespace AMWD.Protocols.Modbus.Tests.Serial .Setup(ns => ns.WriteAsync(It.IsAny(), It.IsAny())) .Returns(Task.Delay(100)); - // Act - var task = connection.InvokeAsync(request, validation); - connection.Dispose(); - await task; - - // Assert - TaskCancelledException + // Act + Assert + await Assert.ThrowsExceptionAsync(async () => + { + var task = connection.InvokeAsync(request, validation); + connection.Dispose(); + await task; + }); } [TestMethod] - [ExpectedException(typeof(TaskCanceledException))] public async Task ShouldThrowTaskCancelledExceptionForCancelOnInvokeAsync() { // Arrange @@ -354,12 +460,13 @@ namespace AMWD.Protocols.Modbus.Tests.Serial .Setup(ns => ns.WriteAsync(It.IsAny(), It.IsAny())) .Returns(Task.Delay(100)); - // Act - var task = connection.InvokeAsync(request, validation, cts.Token); - cts.Cancel(); - await task; - - // Assert - TaskCancelledException + // Act + Assert + await Assert.ThrowsExceptionAsync(async () => + { + var task = connection.InvokeAsync(request, validation, cts.Token); + cts.Cancel(); + await task; + }); } [TestMethod] @@ -375,7 +482,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial var connection = GetConnection(); _serialPortMock .Setup(ns => ns.WriteAsync(It.IsAny(), It.IsAny())) - .Callback((req, _) => _serialLineRequestCallbacks.Add(req.ToArray())) + .Callback((req, _) => _serialLineRequestCallbacks.Add([.. req])) .Returns(Task.Delay(100)); // Act @@ -418,7 +525,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial var connection = GetConnection(); _serialPortMock .Setup(ns => ns.WriteAsync(It.IsAny(), It.IsAny())) - .Callback((req, _) => _serialLineRequestCallbacks.Add(req.ToArray())) + .Callback((req, _) => _serialLineRequestCallbacks.Add([.. req])) .Returns(Task.Delay(100)); // Act @@ -489,9 +596,6 @@ namespace AMWD.Protocols.Modbus.Tests.Serial (connectionField.GetValue(connection) as SerialPortWrapper)?.Dispose(); connectionField.SetValue(connection, _serialPortMock.Object); - // Set unit test mode - connection.GetType().GetField("_isUnitTest", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(connection, true); - return connection; } } diff --git a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpClientTest.cs b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpClientTest.cs index 1c3e2c9..f16be46 100644 --- a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpClientTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpClientTest.cs @@ -162,5 +162,18 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp _tcpConnectionMock.VerifyNoOtherCalls(); } + + [TestMethod] + public void ShouldPrintCleanString() + { + // Arrange + using var client = new ModbusTcpClient(_tcpConnectionMock.Object); + + // Act + string str = client.ToString(); + + // Assert + SnapshotAssert.AreEqual(str); + } } } diff --git a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpConnectionTest.cs b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpConnectionTest.cs index 5dcf5f1..bde2e0c 100644 --- a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpConnectionTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpConnectionTest.cs @@ -80,31 +80,25 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp [DataRow(null)] [DataRow("")] [DataRow(" ")] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionForInvalidHostname(string hostname) { // Arrange var connection = GetTcpConnection(); - // Act - connection.Hostname = hostname; - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => connection.Hostname = hostname); } [DataTestMethod] [DataRow(0)] [DataRow(65536)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeExceptionForInvalidPort(int port) { // Arrange var connection = GetTcpConnection(); - // Act - connection.Port = port; - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => connection.Port = port); } [TestMethod] @@ -119,46 +113,37 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public async Task ShouldThrowDisposedExceptionOnInvokeAsync() { // Arrange var connection = GetConnection(); connection.Dispose(); - // Act - await connection.InvokeAsync(null, null); - - // Assert - OjbectDisposedException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(null, null)); } [DataTestMethod] [DataRow(null)] [DataRow(new byte[0])] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldThrowArgumentNullExceptionForMissingRequestOnInvokeAsync(byte[] request) { // Arrange var connection = GetConnection(); - // Act - await connection.InvokeAsync(request, null); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, null)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public async Task ShouldThrowArgumentNullExceptionForMissingValidationOnInvokeAsync() { // Arrange byte[] request = new byte[1]; var connection = GetConnection(); - // Act - await connection.InvokeAsync(request, null); - - // Assert - ArgumentNullException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, null)); } [TestMethod] @@ -235,7 +220,6 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp } [TestMethod] - [ExpectedException(typeof(EndOfStreamException))] public async Task ShouldThrowEndOfStreamExceptionOnInvokeAsync() { // Arrange @@ -244,14 +228,11 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp var connection = GetConnection(); - // Act - var response = await connection.InvokeAsync(request, validation); - - // Assert - EndOfStreamException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, validation)); } [TestMethod] - [ExpectedException(typeof(ApplicationException))] public async Task ShouldThrowApplicationExceptionWhenHostNotResolvableOnInvokeAsync() { // Arrange @@ -264,10 +245,8 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp var connection = GetConnection(); connection.GetType().GetField("_hostname", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(connection, ""); - // Act - var response = await connection.InvokeAsync(request, validation); - - // Assert - ApplicationException + // Act + Assert + await Assert.ThrowsExceptionAsync(() => connection.InvokeAsync(request, validation)); } [TestMethod] @@ -351,8 +330,7 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp } [TestMethod] - [ExpectedException(typeof(TaskCanceledException))] - public async Task ShouldThrowTaskCancelledExceptionForDisposeOnInvokeAsync() + public async Task ShouldThrowTaskCanceledExceptionForDisposeOnInvokeAsync() { // Arrange byte[] request = [1, 2, 3]; @@ -363,17 +341,17 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp .Setup(ns => ns.WriteAsync(It.IsAny>(), It.IsAny())) .Returns(new ValueTask(Task.Delay(100))); - // Act - var task = connection.InvokeAsync(request, validation); - connection.Dispose(); - await task; - - // Assert - TaskCancelledException + // Act + Assert + await Assert.ThrowsExceptionAsync(async () => + { + var task = connection.InvokeAsync(request, validation); + connection.Dispose(); + await task; + }); } [TestMethod] - [ExpectedException(typeof(TaskCanceledException))] - public async Task ShouldThrowTaskCancelledExceptionForCancelOnInvokeAsync() + public async Task ShouldThrowTaskCanceledExceptionForCancelOnInvokeAsync() { // Arrange byte[] request = [1, 2, 3]; @@ -385,12 +363,13 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp .Setup(ns => ns.WriteAsync(It.IsAny>(), It.IsAny())) .Returns(new ValueTask(Task.Delay(100))); - // Act - var task = connection.InvokeAsync(request, validation, cts.Token); - cts.Cancel(); - await task; - - // Assert - TaskCancelledException + // Act + Assert + await Assert.ThrowsExceptionAsync(async () => + { + var task = connection.InvokeAsync(request, validation, cts.Token); + cts.Cancel(); + await task; + }); } [TestMethod] diff --git a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpProxyTest.cs b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpProxyTest.cs index 07ef5ac..9d2660a 100644 --- a/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpProxyTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Tcp/ModbusTcpProxyTest.cs @@ -157,15 +157,12 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionOnCreateInstanceForClient() { // Arrange - // Act - new ModbusTcpProxy(null, IPAddress.Loopback); - - // Assert - ArgumentNullException + // Act + Assert + Assert.ThrowsException(() => new ModbusTcpProxy(null, IPAddress.Loopback)); } [TestMethod] @@ -212,17 +209,14 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void ShouldThrowArgumentOutOfRangeExceptionForInvalidTimeout() { // Arrange _connectClient = false; using var proxy = GetProxy(); - // Act - proxy.ReadWriteTimeout = TimeSpan.FromSeconds(-3); - - // Assert - ArgumentOutOfRangeException + // Act + Assert + Assert.ThrowsException(() => proxy.ReadWriteTimeout = TimeSpan.FromSeconds(-3)); } [TestMethod] @@ -886,6 +880,7 @@ namespace AMWD.Protocols.Modbus.Tests.Tcp Assert.AreEqual(ModbusDeviceIdentificationObject.VendorName, objectId); CollectionAssert.AreEqual(expectedResponse, _responseBytesCallbacks.First()); + SnapshotAssert.AreEqual(_clientDeviceIdentificationResponse.ToString()); } [TestMethod] diff --git a/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpClientTest/ShouldPrintCleanString.snap.bin b/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpClientTest/ShouldPrintCleanString.snap.bin new file mode 100644 index 0000000..85ab124 --- /dev/null +++ b/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpClientTest/ShouldPrintCleanString.snap.bin @@ -0,0 +1,2 @@ +TCP Client 127.0.0.1 + Port: 502 diff --git a/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpProxyTest/ShouldReadDeviceIdentification.snap.bin b/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpProxyTest/ShouldReadDeviceIdentification.snap.bin new file mode 100644 index 0000000..76fddc0 --- /dev/null +++ b/AMWD.Protocols.Modbus.Tests/Tcp/Snapshots/ModbusTcpProxyTest/ShouldReadDeviceIdentification.snap.bin @@ -0,0 +1,9 @@ +DeviceIdentification + VendorName: VendorName + ProductCode: ProductCode + MajorMinorRevision: MajorMinorRevision + VendorUrl: + ProductName: + ModelName: + UserApplicationName: + IsIndividualAccessAllowed: False diff --git a/CHANGELOG.md b/CHANGELOG.md index f21c1ff..ec0fcc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -_nothing changed yet_ +### Changed + +- Async methods do not return on captured context anymore (`Task.ConfigureAwait(false)`). ## [v0.4.0] (2025-01-29) diff --git a/README.md b/README.md index 73eb4b2..236f5c8 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,12 @@ Here you can find a basic implementation of the Modbus protocol. +![NuGet Version](https://shields.io/nuget/v/AMWD.Protocols.Modbus.Common?style=flat&logo=nuget) +![Test Coverage](https://git.am-wd.de/am-wd/amwd.protocols.modbus/badges/main/coverage.svg?style=flat) + ## Overview -The project is divided into four parts. +The project is divided into multiple parts. To be mentioned at the beginning: Only the clients are build very modular to fit any requirement reached on the first implementation back in 2018 ([see here]). @@ -35,7 +38,7 @@ It uses a specific TCP connection implementation and plugs all things from the C --- Published under [MIT License] (see [choose a license]) -[![Buy me a Coffee](https://shields.am-wd.de/badge/PayPal-Buy_me_a_Coffee-yellow?style=flat&logo=paypal)](https://link.am-wd.de/donate) +[![Buy me a Coffee](https://shields.io/badge/PayPal-Buy_me_a_Coffee-yellow?style=flat&logo=paypal)](https://link.am-wd.de/donate) [![built with Codeium](https://codeium.com/badges/main)](https://link.am-wd.de/codeium)