From de48fd8966b703cb086ee5cbf489167af9b733e6 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 18 Aug 2022 20:36:08 +0100 Subject: [PATCH 1/4] * Fix SerialPort Dispose warning * Update Nerdbank.GitVersioning * Use OutputHelper for unit tests --- System.IO.Ports/SerialPort.cs | 2 +- System.IO.Ports/System.IO.Ports.nfproj | 4 +- System.IO.Ports/packages.config | 2 +- Tests/UnitTestsSerialPort/SerialTests.cs | 67 ++++++++++++++++++------ version.json | 2 +- 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/System.IO.Ports/SerialPort.cs b/System.IO.Ports/SerialPort.cs index 4b43e37..7b93478 100644 --- a/System.IO.Ports/SerialPort.cs +++ b/System.IO.Ports/SerialPort.cs @@ -783,7 +783,7 @@ internal static SerialPort FindDevice(int index) /// The port is in an invalid state. -or- An attempt to set the state of the underlying /// port failed. For example, the parameters passed from this /// object were invalid. - protected void Dispose(bool disposing) + internal void Dispose(bool disposing) { if (!_disposed) { diff --git a/System.IO.Ports/System.IO.Ports.nfproj b/System.IO.Ports/System.IO.Ports.nfproj index 4cca3cf..5eed4cc 100644 --- a/System.IO.Ports/System.IO.Ports.nfproj +++ b/System.IO.Ports/System.IO.Ports.nfproj @@ -80,11 +80,11 @@ - + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. - + \ No newline at end of file diff --git a/System.IO.Ports/packages.config b/System.IO.Ports/packages.config index 2950a9a..750cc21 100644 --- a/System.IO.Ports/packages.config +++ b/System.IO.Ports/packages.config @@ -4,5 +4,5 @@ - + \ No newline at end of file diff --git a/Tests/UnitTestsSerialPort/SerialTests.cs b/Tests/UnitTestsSerialPort/SerialTests.cs index 8adc22c..9736d1c 100644 --- a/Tests/UnitTestsSerialPort/SerialTests.cs +++ b/Tests/UnitTestsSerialPort/SerialTests.cs @@ -3,7 +3,7 @@ // See LICENSE file in the project root for full license information. // -using nanoFramework.Hardware.Esp32; +using nanoFramework.Hardware.Esp32; // TODO: only include if platform needs it?! using nanoFramework.TestFramework; using System; using System.Diagnostics; @@ -20,12 +20,14 @@ public class SerialTests static SerialPort _serOne; static SerialPort _serTwo; + // TODO: only include if platform needs it?! [Setup] - public void SetupComPorts() + public void SetupComPorts_ESP32() { + OutputHelper.WriteLine("Setting up tests for an ESP32..."); try { - Debug.WriteLine("Please adjust for your own usage. If you need another hardware, please add the proper nuget and adjust as well"); + OutputHelper.WriteLine("Please adjust for your own usage. If you need another hardware, please add the proper nuget and adjust as well"); Configuration.SetPinFunction(32, DeviceFunction.COM2_RX); Configuration.SetPinFunction(33, DeviceFunction.COM2_TX); Configuration.SetPinFunction(12, DeviceFunction.COM2_RTS); @@ -36,22 +38,23 @@ public void SetupComPorts() Configuration.SetPinFunction(27, DeviceFunction.COM3_RTS); Configuration.SetPinFunction(14, DeviceFunction.COM3_CTS); - Debug.WriteLine("You will need to connect:"); - Debug.WriteLine(" COM2 RX <-> COM3 TX"); - Debug.WriteLine(" COM2 TX <-> COM3 RX"); - Debug.WriteLine(" COM2 RTS <-> COM3 CTS"); - Debug.WriteLine(" COM2 CTS <-> COM3 RTS"); + OutputHelper.WriteLine("You will need to connect:"); + OutputHelper.WriteLine(" COM2 RX <-> COM3 TX"); + OutputHelper.WriteLine(" COM2 TX <-> COM3 RX"); + OutputHelper.WriteLine(" COM2 RTS <-> COM3 CTS"); + OutputHelper.WriteLine(" COM2 CTS <-> COM3 RTS"); _serOne = new SerialPort("COM2"); _serTwo = new SerialPort("COM3"); - Debug.WriteLine("Devices created, trying to open them"); + OutputHelper.WriteLine("SerialPorts created, trying to open them"); _serOne.Open(); - Debug.WriteLine("Serial One COM2 opened"); + OutputHelper.WriteLine("SerialPort One COM2 opened."); _serTwo.Open(); - Debug.WriteLine("Devices opened, will close them"); - // Wait a bit just to make sure and close them again + OutputHelper.WriteLine("SerialPort Two COM3 opened."); + OutputHelper.WriteLine("All SerialPorts opened, will close them"); + // Wait a bit just to make sure and close them all Thread.Sleep(100); - _serOne.Close(); - _serTwo.Close(); + EnsurePortsClosed(); + OutputHelper.WriteLine("SerialPorts Closed."); } catch { @@ -59,14 +62,46 @@ public void SetupComPorts() } } + //[Setup] + //public void SetupComPorts_ChibOs_STM32F769I_Disco() + //{ + // OutputHelper.WriteLine("Setting up tests for an STM32F769I..."); + // try + // { + // Debug.WriteLine("Please adjust for your own usage. If you need another hardware, please add the proper nuget and adjust as well"); + + // OutputHelper.WriteLine("You will need to connect:"); + // OutputHelper.WriteLine(" COM5 (PD2) RX <-> COM6 (PC6) TX"); + // OutputHelper.WriteLine(" COM5 (PC12) TX <-> COM6 (PC7) RX"); + // // OutputHelper.WriteLine(" COM5 RTS <-> COM6 CTS"); + // // OutputHelper.WriteLine(" COM5 CTS <-> COM6 RTS"); + // _serOne = new SerialPort("COM5"); + // _serTwo = new SerialPort("COM6"); + // OutputHelper.WriteLine("SerialPorts created, trying to open them"); + // _serOne.Open(); + // OutputHelper.WriteLine("SerialPort One COM5 opened"); + // _serTwo.Open(); + // OutputHelper.WriteLine("SerialPort Two COM6 opened"); + // OutputHelper.WriteLine("SarialPorts opened, will close them"); + // // Wait a bit just to make sure and close them all + // Thread.Sleep(100); + // EnsurePortsClosed(); + // OutputHelper.WriteLine("SerialPorts Closed."); + // } + // catch + // { + // Assert.SkipTest("Serial Ports not supported in this platform or not properly configured"); + // } + //} + [TestMethod] public void GetPortNamesTest() { var ports = SerialPort.GetPortNames(); - Debug.WriteLine("Available ports:"); + OutputHelper.WriteLine("Available SerialPorts:"); foreach (string port in ports) { - Debug.WriteLine($" {port}"); + OutputHelper.WriteLine($" {port}"); } } diff --git a/version.json b/version.json index 55dc1fc..4b6034f 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.1", + "version": "2.2", "assemblyVersion": { "precision": "revision" }, From 636541e39d35205d56acc7db7771522cb673ff84 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 18 Aug 2022 22:10:02 +0100 Subject: [PATCH 2/4] Improve test names. --- Tests/UnitTestsSerialPort/SerialTests.cs | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Tests/UnitTestsSerialPort/SerialTests.cs b/Tests/UnitTestsSerialPort/SerialTests.cs index 9736d1c..121b4b2 100644 --- a/Tests/UnitTestsSerialPort/SerialTests.cs +++ b/Tests/UnitTestsSerialPort/SerialTests.cs @@ -41,8 +41,8 @@ public void SetupComPorts_ESP32() OutputHelper.WriteLine("You will need to connect:"); OutputHelper.WriteLine(" COM2 RX <-> COM3 TX"); OutputHelper.WriteLine(" COM2 TX <-> COM3 RX"); - OutputHelper.WriteLine(" COM2 RTS <-> COM3 CTS"); - OutputHelper.WriteLine(" COM2 CTS <-> COM3 RTS"); + OutputHelper.WriteLine(" COM2 RTS <-> COM3 CTS"); // TODO: Optional?! + OutputHelper.WriteLine(" COM2 CTS <-> COM3 RTS"); // TODO: Optional?! _serOne = new SerialPort("COM2"); _serTwo = new SerialPort("COM3"); OutputHelper.WriteLine("SerialPorts created, trying to open them"); @@ -95,7 +95,7 @@ public void SetupComPorts_ESP32() //} [TestMethod] - public void GetPortNamesTest() + public void GetAllAvailableSerialPortNames() { var ports = SerialPort.GetPortNames(); OutputHelper.WriteLine("Available SerialPorts:"); @@ -106,7 +106,7 @@ public void GetPortNamesTest() } [TestMethod] - public void BasicReadWriteTests() + public void WriteAndReadFourBytes() { // Arrange EnsurePortsOpen(); @@ -146,7 +146,7 @@ public void VerifyDefaultReadLineCharacter() } [TestMethod] - public void WriteAndReadStringTests() + public void WriteAndReadSimpleAsciiString_37Chars() { // Arrange EnsurePortsOpen(); @@ -163,7 +163,7 @@ public void WriteAndReadStringTests() } [TestMethod] - public void ReadMultipleLinesTests() + public void WriteAndReadMultipleLineAsciiString() { // Arrange EnsurePortsOpen(); @@ -184,7 +184,7 @@ public void ReadMultipleLinesTests() } [TestMethod] - public void CheckReadByteSize() + public void CheckReadByteSizeUtf8String() { // Arrange EnsurePortsOpen(); @@ -223,7 +223,7 @@ public void CheckReadLineWithoutAnything() } [TestMethod] - public void CheckReadTimeoutTest() + public void CheckReadTimeout() { Assert.Throws(typeof(TimeoutException), () => { @@ -239,7 +239,7 @@ public void CheckReadTimeoutTest() } [TestMethod] - public void ReadByteWriteByteTests() + public void WriteThreeBytesReadSingleByte() { // Arrange EnsurePortsOpen(); @@ -262,7 +262,7 @@ public void ReadByteWriteByteTests() } [TestMethod] - public void CheckBytesAvailableTest() + public void CheckBytesAreAvailable() { // Arrange EnsurePortsOpen(); @@ -301,7 +301,7 @@ public void TryReadWriteWhileClosed() } [TestMethod] - public void AdjustBaudRateTests() + public void AdjustBaudRateTo115200() { // Arrange _serOne.BaudRate = 115200; @@ -311,7 +311,7 @@ public void AdjustBaudRateTests() } [TestMethod] - public void AdjustHandshakeTests() + public void AdjustHandshakeRTS() { // Arrange _serOne.Handshake = Handshake.RequestToSend; @@ -324,7 +324,7 @@ public void AdjustHandshakeTests() } [TestMethod] - public void TestStreams() + public void StreamsCanWriteRead() { // Arrange EnsurePortsOpen(); @@ -350,7 +350,7 @@ public void TestStreams() } [TestMethod] - public void TestEvents() + public void DataReceivedEventsAreGenerated() { // Arrange EnsurePortsOpen(); @@ -378,7 +378,7 @@ private void DataReceivedNormalEventTest(object sender, SerialDataReceivedEventA } [TestMethod] - public void TestWatchCharEvents() + public void WatchCharEventsAreGenerated() { // Arrange EnsurePortsOpen(); @@ -423,7 +423,7 @@ private void SendAndReceiveBasic() } [Cleanup] - public void CleanPorts() + public void DisposeSerialPorts() { EnsurePortsClosed(); _serOne.Dispose(); From ef68d584adc33bfcc9347fded2c4287b3e42ab85 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 19 Aug 2022 22:59:19 +0100 Subject: [PATCH 3/4] Fixes for review comments --- Tests/UnitTestsSerialPort/SerialTests.cs | 15 +++++++++------ version.json | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Tests/UnitTestsSerialPort/SerialTests.cs b/Tests/UnitTestsSerialPort/SerialTests.cs index 121b4b2..66383c5 100644 --- a/Tests/UnitTestsSerialPort/SerialTests.cs +++ b/Tests/UnitTestsSerialPort/SerialTests.cs @@ -1,16 +1,16 @@ -// -// Copyright (c) .NET Foundation and Contributors +// Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. -// -using nanoFramework.Hardware.Esp32; // TODO: only include if platform needs it?! -using nanoFramework.TestFramework; using System; using System.Diagnostics; using System.IO; using System.IO.Ports; using System.Text; using System.Threading; +using nanoFramework.TestFramework; + +// The following dependenies are target or platform dependent (comment out if required and remove nuget). +using nanoFramework.Hardware.Esp32; namespace UnitTestsSerialPort { @@ -20,7 +20,7 @@ public class SerialTests static SerialPort _serOne; static SerialPort _serTwo; - // TODO: only include if platform needs it?! + // The default test setup is for an ESP32 [Setup] public void SetupComPorts_ESP32() { @@ -62,6 +62,9 @@ public void SetupComPorts_ESP32() } } + // This commented out method is provided as an example of how to "setup" a different target board. + // in this case, an STM32F769I. + // When uncommened, other setup methods (such as `SetupComPorts_ESP32()`) should be removed. //[Setup] //public void SetupComPorts_ChibOs_STM32F769I_Disco() //{ diff --git a/version.json b/version.json index 4b6034f..55dc1fc 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.2", + "version": "1.1", "assemblyVersion": { "precision": "revision" }, From ad019808f14ee47b20d637869bbd85c4965ad5fc Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Tue, 23 Aug 2022 18:28:35 +0100 Subject: [PATCH 4/4] Update nano.runsettings Updates the `TargetFrameworkVersion` so it works on later VS versions. --- Tests/UnitTestsSerialPort/nano.runsettings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTestsSerialPort/nano.runsettings b/Tests/UnitTestsSerialPort/nano.runsettings index fa881e3..cb4e1ca 100644 --- a/Tests/UnitTestsSerialPort/nano.runsettings +++ b/Tests/UnitTestsSerialPort/nano.runsettings @@ -5,7 +5,7 @@ 1 .\TestResults 120000 - Framework40 + net48 None