Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Sep 26, 2016
1 parent b93f40c commit b5a25d3
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ A Reactive Serial Port Library
private static void Main(string[] args)
{
// configure the data to write, this can be a string, a byte array, or a char array
var DataR60 = "DataToWrite";
var dataToWrite = "DataToWrite";

using (var port = new SerialPortRx("COM1", 9600))
{
var dis = new CompositeDisposable();
// Subscribe to com ports available
port.PortNames.ForEach().Subscribe(name =>
SerialPortRx.PortNames.ForEach().Subscribe(name =>
{
Console.WriteLine(name);
}).AddTo(dis);
Expand All @@ -41,7 +41,7 @@ A Reactive Serial Port Library
// Subscribe to the Is Open @500ms intervals and write to com port
port.WhileIsOpen(TimeSpan.FromMilliseconds(500)).Subscribe(x =>
{
port.Write(DataR60);
port.Write(dataToWrite);
}).AddTo(dis);
Console.ReadLine();
// Cleanup port
Expand Down
15 changes: 15 additions & 0 deletions SerialPortRx.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("SerialPortRx.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ChrisPulman")]
[assembly: AssemblyProduct("SerialPortRx.Tests")]
[assembly: AssemblyCopyright("Copyright © https://github.com/ChrisPulman 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("155faa12-bec6-45f1-905f-22523e412f47")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
70 changes: 70 additions & 0 deletions SerialPortRx.Tests/SerialPortRx.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{155FAA12-BEC6-45F1-905F-22523E412F47}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SerialPortRx.Tests</RootNamespace>
<AssemblyName>SerialPortRx.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SerialPortRxTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SerialPortRx\SerialPortRx.csproj">
<Project>{cda3585b-d3d6-4be4-9a6b-582ab5de0b8f}</Project>
<Name>SerialPortRx</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
53 changes: 53 additions & 0 deletions SerialPortRx.Tests/SerialPortRxTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace SerialPortRx.Tests
{
using System;
using CP.IO.Ports;
using NUnit.Framework;

[TestFixture]
[Timeout(10000)]
public class SerialPortRxTest
{
[Test]
[Category("SerialPortRx")]
public void SimpleConstructor()
{
SerialPortRx src = new SerialPortRx();
src.Dispose();
Assert.That(src.IsDisposed, Is.True);
}

[Test]
[Category("SerialPortRx")]
public void SimpleConstructorWithPort()
{
SerialPortRx src = new SerialPortRx("COM1");
Assert.That(src.PortName, Is.EqualTo("COM1"));
src.Dispose();
Assert.That(src.IsDisposed, Is.True);
}

[Test]
[Category("SerialPortRx")]
public void SimpleConstructorWithPortandBaud()
{
SerialPortRx src = new SerialPortRx("COM1", 9600);
Assert.That(src.PortName, Is.EqualTo("COM1"));
Assert.That(src.BaudRate, Is.EqualTo(9600));
src.Dispose();
Assert.That(src.IsDisposed, Is.True);
}

[Test]
[Category("SerialPortRx")]
public void SimpleConstructorWithPortandBaudAndDatabits()
{
SerialPortRx src = new SerialPortRx("COM1", 9600, 8);
Assert.That(src.PortName, Is.EqualTo("COM1"));
Assert.That(src.BaudRate, Is.EqualTo(9600));
Assert.That(src.DataBits, Is.EqualTo(8));
src.Dispose();
Assert.That(src.IsDisposed, Is.True);
}
}
}
4 changes: 4 additions & 0 deletions SerialPortRx.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.4.1" targetFramework="net461" />
</packages>
6 changes: 6 additions & 0 deletions SerialPortRx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConfigFiles", "ConfigFiles"
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialPortRx.Tests", "SerialPortRx.Tests\SerialPortRx.Tests.csproj", "{155FAA12-BEC6-45F1-905F-22523E412F47}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{CDA3585B-D3D6-4BE4-9A6B-582AB5DE0B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDA3585B-D3D6-4BE4-9A6B-582AB5DE0B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDA3585B-D3D6-4BE4-9A6B-582AB5DE0B8F}.Release|Any CPU.Build.0 = Release|Any CPU
{155FAA12-BEC6-45F1-905F-22523E412F47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{155FAA12-BEC6-45F1-905F-22523E412F47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{155FAA12-BEC6-45F1-905F-22523E412F47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{155FAA12-BEC6-45F1-905F-22523E412F47}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 7 additions & 12 deletions SerialPortRx/ISerialPortRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// <summary>
/// Serial Port Rx interface
/// </summary>
public interface ISerialPortRx
public interface ISerialPortRx : IDisposable
{
/// <summary>
/// Gets or sets the baud rate.
Expand Down Expand Up @@ -39,6 +39,12 @@ public interface ISerialPortRx
/// <value>The handshake.</value>
Handshake Handshake { get; set; }

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value><c>true</c> if this instance is disposed; otherwise, <c>false</c>.</value>
bool IsDisposed { get; }

/// <summary>
/// Gets the is open.
/// </summary>
Expand All @@ -57,12 +63,6 @@ public interface ISerialPortRx
/// <value>The port.</value>
string PortName { get; set; }

/// <summary>
/// Gets the port names.
/// </summary>
/// <value>The port names.</value>
IObservable<string[]> PortNames { get; }

/// <summary>
/// Gets or sets the read timeout.
/// </summary>
Expand All @@ -86,11 +86,6 @@ public interface ISerialPortRx
/// </summary>
void Close();

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
void Dispose();

/// <summary>
/// Opens this instance.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions SerialPortRx/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("cda3585b-d3d6-4be4-9a6b-582ab5de0b8f")]
[assembly: AssemblyVersion("0.0.0.1")]
[assembly: AssemblyFileVersion("0.0.0.1")]
[assembly: AssemblyVersion("0.0.0.2")]
[assembly: AssemblyFileVersion("0.0.0.2")]
39 changes: 35 additions & 4 deletions SerialPortRx/SerialPortRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
/// <summary>
/// Serial Port Rx
/// </summary>
/// <seealso cref="System.IDisposable"/>
public class SerialPortRx : IDisposable, ISerialPortRx
/// <seealso cref="CP.IO.Ports.ISerialPortRx"/>
public class SerialPortRx : ISerialPortRx
{
private ISubject<char> dataReceived = new Subject<char>();
private IDisposable disposablePort;
Expand Down Expand Up @@ -158,6 +158,14 @@ public SerialPortRx()
[MonitoringDescription("Handshake")]
public Handshake Handshake { get; set; } = Handshake.None;

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value><c>true</c> if this instance is disposed; otherwise, <c>false</c>.</value>
[Browsable(true)]
[MonitoringDescription("IsDisposed")]
public bool IsDisposed { get; private set; } = false;

/// <summary>
/// Gets the is open.
/// </summary>
Expand Down Expand Up @@ -188,7 +196,7 @@ public SerialPortRx()
/// Gets the port names.
/// </summary>
/// <value>The port names.</value>
public IObservable<string[]> PortNames => Observable.Create<string[]>(obs =>
public static IObservable<string[]> PortNames => Observable.Create<string[]>(obs =>
{
string[] compare = null;
return Observable.Interval(TimeSpan.FromMilliseconds(500)).Subscribe(_ =>
Expand Down Expand Up @@ -222,6 +230,9 @@ public SerialPortRx()
/// Gets or sets the stop bits.
/// </summary>
/// <value>The stop bits.</value>
[Browsable(true)]
[DefaultValue(StopBits.One)]
[MonitoringDescription("StopBits")]
public StopBits StopBits { get; set; } = StopBits.One;

/// <summary>
Expand Down Expand Up @@ -325,7 +336,7 @@ public void Close()
/// </summary>
public void Dispose()
{
this.disposablePort.Dispose();
Dispose(true);
}

/// <summary>
Expand Down Expand Up @@ -393,5 +404,25 @@ public void WriteLine(string text)
{
this.writeStringLine?.OnNext(text);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only
/// unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
this.disposablePort?.Dispose();
}

IsDisposed = true;
}
}
}
}
2 changes: 1 addition & 1 deletion SerialPortRx/SerialPortRx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ProjectGuid>{CDA3585B-D3D6-4BE4-9A6B-582AB5DE0B8F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SerialPortRx</RootNamespace>
<RootNamespace>CP.IO.Ports</RootNamespace>
<AssemblyName>SerialPortRx</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
Expand Down

0 comments on commit b5a25d3

Please sign in to comment.