Updated serial implementations
This commit is contained in:
@@ -13,13 +13,15 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
string portName = "COM-42";
|
||||
|
||||
_genericConnectionMock = new Mock<IModbusConnection>();
|
||||
_genericConnectionMock.Setup(c => c.IdleTimeout).Returns(TimeSpan.FromSeconds(40));
|
||||
_genericConnectionMock.Setup(c => c.ConnectTimeout).Returns(TimeSpan.FromSeconds(30));
|
||||
_genericConnectionMock.Setup(c => c.ReadTimeout).Returns(TimeSpan.FromSeconds(20));
|
||||
_genericConnectionMock.Setup(c => c.WriteTimeout).Returns(TimeSpan.FromSeconds(10));
|
||||
|
||||
_serialConnectionMock = new Mock<ModbusSerialConnection>();
|
||||
_serialConnectionMock = new Mock<ModbusSerialConnection>(portName);
|
||||
|
||||
_serialConnectionMock.Setup(c => c.IdleTimeout).Returns(TimeSpan.FromSeconds(10));
|
||||
_serialConnectionMock.Setup(c => c.ConnectTimeout).Returns(TimeSpan.FromSeconds(20));
|
||||
@@ -28,7 +30,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
|
||||
|
||||
_serialConnectionMock.Setup(c => c.DriverEnabledRS485).Returns(true);
|
||||
_serialConnectionMock.Setup(c => c.InterRequestDelay).Returns(TimeSpan.FromSeconds(50));
|
||||
_serialConnectionMock.Setup(c => c.PortName).Returns("COM-42");
|
||||
_serialConnectionMock.Setup(c => c.PortName).Returns(portName);
|
||||
_serialConnectionMock.Setup(c => c.BaudRate).Returns(BaudRate.Baud2400);
|
||||
_serialConnectionMock.Setup(c => c.DataBits).Returns(7);
|
||||
_serialConnectionMock.Setup(c => c.Handshake).Returns(Handshake.XOnXOff);
|
||||
@@ -231,5 +233,18 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
|
||||
|
||||
_serialConnectionMock.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldPrintCleanString()
|
||||
{
|
||||
// Arrange
|
||||
using var client = new ModbusSerialClient(_serialConnectionMock.Object);
|
||||
|
||||
// Act
|
||||
string str = client.ToString();
|
||||
|
||||
// Assert
|
||||
SnapshotAssert.AreEqual(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,21 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
|
||||
connection.Dispose();
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(null)]
|
||||
[DataRow("")]
|
||||
[DataRow(" ")]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void ShouldThrowArgumentNullExceptionOnCreate(string portName)
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
using var test = new ModbusSerialClient(portName);
|
||||
|
||||
// Assert - ArgumentNullException
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ObjectDisposedException))]
|
||||
public async Task ShouldThrowDisposedExceptionOnInvokeAsync()
|
||||
@@ -467,7 +482,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
|
||||
var connection = new ModbusSerialConnection();
|
||||
var connection = new ModbusSerialConnection("some-port");
|
||||
|
||||
// Replace real connection with mock
|
||||
var connectionField = connection.GetType().GetField("_serialPort", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
Serial Client COM-42
|
||||
BaudRate: 2400
|
||||
DataBits: 7
|
||||
StopBits: 1.5
|
||||
Parity: space
|
||||
Handshake: xonxoff
|
||||
RtsEnable: true
|
||||
DriverEnabledRS485: true
|
||||
83
AMWD.Protocols.Modbus.Tests/SnapshotAssert.cs
Normal file
83
AMWD.Protocols.Modbus.Tests/SnapshotAssert.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace AMWD.Protocols.Modbus.Tests
|
||||
{
|
||||
// ================================================================================================================================ //
|
||||
// Source: https://git.am-wd.de/am-wd/common/-/blob/fb26e441a48214aaae72003c4a5ac33d5c7b929a/src/AMWD.Common.Test/SnapshotAssert.cs //
|
||||
// ================================================================================================================================ //
|
||||
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
internal sealed class SnapshotAssert
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests whether the specified string is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content string.</param>
|
||||
/// <param name="message">An optional message to display if the assertion fails.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(string actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string cleanLineEnding = actual
|
||||
.Replace("\r\n", "\n") // Windows
|
||||
.Replace("\r", "\n"); // MacOS
|
||||
AreEqual(Encoding.UTF8.GetBytes(cleanLineEnding), message, callerFilePath, callerMemberName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="message">An optional message to display if the assertion fails.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(byte[] actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
=> AreEqual(actual, null, message, callerFilePath, callerMemberName);
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The past has shown, that e.g. wkhtmltopdf prints the current timestamp at the beginning of the PDF file.
|
||||
/// Therefore you can specify which sequences of bytes should be excluded from the comparison.
|
||||
/// </remarks>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="excludedSequences">The excluded sequences.</param>
|
||||
/// <param name="message">An optional message to display if the assertion fails.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(byte[] actual, List<(int Start, int Length)> excludedSequences = null, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string callerDirectory = Path.GetDirectoryName(callerFilePath);
|
||||
string callerFileName = Path.GetFileNameWithoutExtension(callerFilePath);
|
||||
|
||||
string snapshotDirectory = Path.Combine(callerDirectory, "Snapshots", callerFileName);
|
||||
string snapshotFilePath = Path.Combine(snapshotDirectory, $"{callerMemberName}.snap.bin");
|
||||
|
||||
if (File.Exists(snapshotFilePath))
|
||||
{
|
||||
byte[] expected = File.ReadAllBytes(snapshotFilePath);
|
||||
if (actual.Length != expected.Length)
|
||||
Assert.Fail(message);
|
||||
|
||||
for (int i = 0; i < actual.Length; i++)
|
||||
{
|
||||
if (excludedSequences?.Any(s => s.Start <= i && i < s.Start + s.Length) == true)
|
||||
continue;
|
||||
|
||||
if (actual[i] != expected[i])
|
||||
Assert.Fail(message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Directory.Exists(snapshotDirectory))
|
||||
Directory.CreateDirectory(snapshotDirectory);
|
||||
|
||||
File.WriteAllBytes(snapshotFilePath, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user