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

87 lines
2.4 KiB
C#
Raw Normal View History

2012-03-03 10:50:54 +01:00
using System;
using System.Text;
using System.Threading;
2022-09-30 16:37:04 +02:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nefarius.Peripherals.SerialPort;
2012-03-03 10:50:54 +01:00
namespace PInvokeSerialPort.Test
{
/// <summary>
2022-09-30 16:37:04 +02:00
/// Test class.
/// Attention: Run it just in test debug.
2012-03-03 10:50:54 +01:00
/// </summary>
[TestClass]
public class PInvokeSerialPortTest
{
2022-09-30 16:37:04 +02:00
private dynamic _reciever;
private dynamic _sender;
private StringBuilder _stringBuilder;
2012-03-03 10:50:54 +01:00
public void OpenWriteDoWaitClose(Action action)
{
const string testSting = "test";
_stringBuilder = new StringBuilder();
_sender.Open();
_reciever.Open();
action();
2022-09-30 16:37:04 +02:00
2012-03-03 10:50:54 +01:00
_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(() =>
2022-09-30 16:37:04 +02:00
{
((SerialPort)_reciever).DataReceived += x => _stringBuilder.Append((char)x);
});
2012-03-03 10:50:54 +01:00
}
[TestMethod]
public void OverallTest2()
{
_sender = new System.IO.Ports.SerialPort("com1");
2022-09-30 16:37:04 +02:00
2012-03-03 10:50:54 +01:00
_reciever = new SerialPort("com2");
2022-09-30 16:37:04 +02:00
2012-03-03 10:50:54 +01:00
OpenWriteDoWaitClose(() =>
2022-09-30 16:37:04 +02:00
{
((SerialPort)(object)_reciever).DataReceived += x => _stringBuilder.Append((char)x);
});
2012-03-03 10:50:54 +01:00
}
[TestMethod]
public void OverallTest3()
{
_sender = new SerialPort("com1");
_reciever = new System.IO.Ports.SerialPort("com2");
OpenWriteDoWaitClose(() =>
2022-09-30 16:37:04 +02:00
{
((System.IO.Ports.SerialPort)_reciever).DataReceived +=
(x, y) => _stringBuilder.Append(_reciever.ReadExisting());
});
2012-03-03 10:50:54 +01:00
}
[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");
2022-09-30 16:37:04 +02:00
2012-03-03 10:50:54 +01:00
OpenWriteDoWaitClose(() =>
2022-09-30 16:37:04 +02:00
{
((System.IO.Ports.SerialPort)_reciever).DataReceived +=
(x, y) => _stringBuilder.Append(_reciever.ReadExisting());
});
2012-03-03 10:50:54 +01:00
}
}
2022-09-30 16:37:04 +02:00
}