Nefarius.Peripherals.Serial.../PInvokeSerialPort.Test/PInvokeSerialPortTest.cs

87 lines
2.4 KiB
C#
Executable File

using System;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nefarius.Peripherals.SerialPort;
namespace PInvokeSerialPort.Test
{
/// <summary>
/// Test class.
/// Attention: Run it just in test debug.
/// </summary>
[TestClass]
public class PInvokeSerialPortTest
{
private dynamic _reciever;
private dynamic _sender;
private StringBuilder _stringBuilder;
public void OpenWriteDoWaitClose(Action action)
{
const string testSting = "test";
_stringBuilder = new StringBuilder();
_sender.Open();
_reciever.Open();
action();
_sender.Write(testSting);
Thread.Sleep(100);
Assert.AreEqual(testSting, _stringBuilder.ToString());
_sender.Close();
_reciever.Close();
}
[TestMethod]
public void OverallTest1()
{
_sender = new SerialPort("com1");
_reciever = new SerialPort("com2");
OpenWriteDoWaitClose(() =>
{
((SerialPort)_reciever).DataReceived += x => _stringBuilder.Append((char)x);
});
}
[TestMethod]
public void OverallTest2()
{
_sender = new System.IO.Ports.SerialPort("com1");
_reciever = new SerialPort("com2");
OpenWriteDoWaitClose(() =>
{
((SerialPort)(object)_reciever).DataReceived += x => _stringBuilder.Append((char)x);
});
}
[TestMethod]
public void OverallTest3()
{
_sender = new SerialPort("com1");
_reciever = new System.IO.Ports.SerialPort("com2");
OpenWriteDoWaitClose(() =>
{
((System.IO.Ports.SerialPort)_reciever).DataReceived +=
(x, y) => _stringBuilder.Append(_reciever.ReadExisting());
});
}
[TestMethod]
public void OverallTest4() // this is not really a PInvokeSerialTest :D
{
_sender = new System.IO.Ports.SerialPort("com1");
_reciever = new System.IO.Ports.SerialPort("com2");
OpenWriteDoWaitClose(() =>
{
((System.IO.Ports.SerialPort)_reciever).DataReceived +=
(x, y) => _stringBuilder.Append(_reciever.ReadExisting());
});
}
}
}