Skip to content
Permalink
Browse files

Make HidDevices.EnumerateDevices internal for use with other enumerat…

…ors. Add HidFastReadEnumerator. Convert NUnit tests to XUnit.
  • Loading branch information...
amullins83 authored and mikeobrien committed Dec 30, 2016
1 parent d0a4ad4 commit cc2a5241a82d5ae0588688d63fc77c692bac9244
@@ -41,9 +41,9 @@ public static IEnumerable<HidDevice> Enumerate(int vendorId)
return EnumerateDevices().Select(x => new HidDevice(x.Path, x.Description)).Where(x => x.Attributes.VendorId == vendorId);
}

private class DeviceInfo { public string Path { get; set; } public string Description { get; set; } }
internal class DeviceInfo { public string Path { get; set; } public string Description { get; set; } }

private static IEnumerable<DeviceInfo> EnumerateDevices()
internal static IEnumerable<DeviceInfo> EnumerateDevices()
{
var devices = new List<DeviceInfo>();
var hidClass = HidClassGuid;
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;

namespace HidLibrary
{
public class HidFastReadEnumerator : IHidEnumerator
{
public bool IsConnected(string devicePath)
{
return HidDevices.IsConnected(devicePath);
}

public IHidDevice GetDevice(string devicePath)
{
return Enumerate(devicePath).FirstOrDefault() as IHidDevice;
}

public IEnumerable<IHidDevice> Enumerate()
{
return HidDevices.EnumerateDevices().
Select(d => new HidFastReadDevice(d.Path, d.Description) as IHidDevice);
}

public IEnumerable<IHidDevice> Enumerate(string devicePath)
{
return HidDevices.EnumerateDevices().Where(x => x.Path == devicePath).
Select(d => new HidFastReadDevice(d.Path, d.Description) as IHidDevice);
}

public IEnumerable<IHidDevice> Enumerate(int vendorId, params int[] productIds)
{
return HidDevices.EnumerateDevices().Select(d => new HidFastReadDevice(d.Path, d.Description)).
Where(f => f.Attributes.VendorId == vendorId && productIds.Contains(f.Attributes.ProductId)).
Select(d => d as IHidDevice);
}

public IEnumerable<IHidDevice> Enumerate(int vendorId)
{
return HidDevices.EnumerateDevices().Select(d => new HidFastReadDevice(d.Path, d.Description)).
Where(f => f.Attributes.VendorId == vendorId).
Select(d => d as IHidDevice);
}
}
}
@@ -52,6 +52,7 @@
<Compile Include="HidDeviceData.cs" />
<Compile Include="HidDeviceEventMonitor.cs" />
<Compile Include="HidFastReadDevice.cs" />
<Compile Include="HidFastReadEnumerator.cs" />
<Compile Include="IHidEnumerator.cs" />
<Compile Include="HidDevices.cs" />
<Compile Include="HidReport.cs" />
@@ -1,12 +1,11 @@
using NUnit.Framework;
using Xunit;
using Should;

namespace Tests
namespace HidLibrary.Tests
{
[TestFixture]
public class Hid
{
[Test]
[Fact]
public void Get_Some_Tests_In_Here()
{
true.ShouldBeTrue();
@@ -1,20 +1,17 @@
using NUnit.Framework;
using HidLibrary;
using Xunit;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Should;

namespace Tests
namespace HidLibrary.Tests
{
[TestFixture]
public class HidEnumeratorTests
{
private HidEnumerator enumerator;
private string devicePath;

[SetUp]
public void beforeEach()
public void BeforeEach()
{
enumerator = new HidEnumerator();
var firstDevice = enumerator.Enumerate().FirstOrDefault();
@@ -29,56 +26,62 @@ public void beforeEach()
}
}

[Test]
[Fact]
public void CanConstruct()
{
BeforeEach();
enumerator.ShouldBeType(typeof(HidEnumerator));
}

[Test]
[Fact]
public void WrapsIsConnected()
{
BeforeEach();
bool enumIsConnected = enumerator.IsConnected(devicePath);
bool hidIsConnected = HidDevices.IsConnected(devicePath);
enumIsConnected.ShouldEqual(hidIsConnected);
}

[Test]
[Fact]
public void WrapsGetDevice()
{
BeforeEach();
IHidDevice enumDevice = enumerator.GetDevice(devicePath);
IHidDevice hidDevice = HidDevices.GetDevice(devicePath);
enumDevice.DevicePath.ShouldEqual(hidDevice.DevicePath);
}

[Test]
[Fact]
public void WrapsEnumerateDefault()
{
BeforeEach();
IEnumerable<IHidDevice> enumDevices = enumerator.Enumerate();
IEnumerable<IHidDevice> hidDevices = HidDevices.Enumerate().
Select(d => d as IHidDevice);


allDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
AllDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
}

[Test]
[Fact]
public void WrapsEnumerateDevicePath()
{
BeforeEach();
IEnumerable<IHidDevice> enumDevices =
enumerator.Enumerate(devicePath);
IEnumerable<IHidDevice> hidDevices =
HidDevices.Enumerate(devicePath).
Select(d => d as IHidDevice);


allDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
AllDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
}

[Test]
[Fact]
public void WrapsEnumerateVendorId()
{
int vid = getVid();
BeforeEach();
int vid = GetVid();

IEnumerable<IHidDevice> enumDevices =
enumerator.Enumerate(vid);
@@ -87,14 +90,15 @@ public void WrapsEnumerateVendorId()
Select(d => d as IHidDevice);


allDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
AllDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
}

[Test]
[Fact]
public void WrapsEnumerateVendorIdProductId()
{
int vid = getVid();
int pid = getPid();
BeforeEach();
int vid = GetVid();
int pid = GetPid();

IEnumerable<IHidDevice> enumDevices =
enumerator.Enumerate(vid, pid);
@@ -103,11 +107,10 @@ public void WrapsEnumerateVendorIdProductId()
Select(d => d as IHidDevice);


allDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
AllDevicesTheSame(enumDevices, hidDevices).ShouldBeTrue();
}


private bool allDevicesTheSame(IEnumerable<IHidDevice> a,
private bool AllDevicesTheSame(IEnumerable<IHidDevice> a,
IEnumerable<IHidDevice> b)
{
if(a.Count() != b.Count())
@@ -133,17 +136,17 @@ public void WrapsEnumerateVendorIdProductId()
return allSame;
}

private int getVid()
private int GetVid()
{
return getNumberFromRegex("vid_([0-9a-f]{4})");
return GetNumberFromRegex("vid_([0-9a-f]{4})");
}

private int getPid()
private int GetPid()
{
return getNumberFromRegex("pid_([0-9a-f]{3,4})");
return GetNumberFromRegex("pid_([0-9a-f]{3,4})");
}

private int getNumberFromRegex(string pattern)
private int GetNumberFromRegex(string pattern)
{
var match = Regex.Match(devicePath, pattern,
RegexOptions.IgnoreCase);

0 comments on commit cc2a524

Please sign in to comment.
You can’t perform that action at this time.