Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Bonsai.Harp.Tests/TestFirmwareMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,25 @@ public class TestFirmwareMetadata
[TestMethod]
public void ParseAndToString_AreReversible()
{
var x = new FirmwareMetadata("Behavior", new HarpVersion(2, 5), new HarpVersion(1, 6), new HarpVersion(1, 2), 0);
var y = FirmwareMetadata.Parse(x.ToString());
var x = new FirmwareMetadata("Behavior",
firmwareVersion: new HarpVersion(2, 5, 2),
protocolVersion: new HarpVersion(1, 9, 0),
hardwareVersion: new HarpVersion(1, 2),
assemblyVersion: 0);
var text = x.ToString();
var y = FirmwareMetadata.Parse(text);
Assert.IsTrue(x.Equals(y));
}

[TestMethod]
public void ParseAndToStringPatchFloating_AreReversible()
{
var x = new FirmwareMetadata("Behavior",
firmwareVersion: new HarpVersion(2, 5),
protocolVersion: new HarpVersion(1, 6),
hardwareVersion: new HarpVersion(1, 2));
var text = x.ToString();
var y = FirmwareMetadata.Parse(text);
Assert.IsTrue(x.Equals(y));
}
}
Expand Down
57 changes: 52 additions & 5 deletions src/Bonsai.Harp.Tests/TestHarpVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ public void CompareNullWithNonNullVersion()
[TestMethod]
public void CompareSpecificVersions_Equal()
{
HarpVersion a = new HarpVersion(1, 0);
HarpVersion b = new HarpVersion(1, 0);
HarpVersion a = new HarpVersion(1, 0, 0);
HarpVersion b = new HarpVersion(1, 0, 0);
AssertEquals(a, b);
}

[TestMethod]
public void ComparePatchSpecificVersions_NotEqual()
{
HarpVersion a = new HarpVersion(1, 0, 0);
HarpVersion b = new HarpVersion(1, 0, 1);
AssertLessThan(a, b);
AssertGreaterThan(b, a);
}

[TestMethod]
public void CompareMinorSpecificVersions_NotEqual()
{
Expand All @@ -87,17 +96,39 @@ public void CompareMajorSpecificVersions_NotEqual()
AssertGreaterThan(a, b);
}

[TestMethod]
public void ComparePatchFloatingVersion_NotEqual()
{
HarpVersion a = new HarpVersion(2, 1, null);
HarpVersion b = new HarpVersion(2, 1, 0);
AssertLessThan(a, b);
AssertGreaterThan(b, a);
Assert.IsTrue(a.Satisfies(b));
Assert.IsTrue(b.Satisfies(a));
}

[TestMethod]
public void CompareMinorFloatingVersion_NotEqual()
{
HarpVersion a = new HarpVersion(2, null);
HarpVersion b = new HarpVersion(2, 1);
HarpVersion b = new HarpVersion(2, 1, 0);
AssertLessThan(a, b);
AssertGreaterThan(b, a);
Assert.IsTrue(a.Satisfies(b));
Assert.IsTrue(b.Satisfies(a));
}

[TestMethod]
public void ComparePatchFloatingVersion_NotSatisfies()
{
HarpVersion a = new HarpVersion(2, 2, null);
HarpVersion b = new HarpVersion(2, 1);
AssertLessThan(b, a);
AssertGreaterThan(a, b);
Assert.IsFalse(a.Satisfies(b));
Assert.IsFalse(b.Satisfies(a));
}

[TestMethod]
public void CompareMinorFloatingVersion_NotSatisfies()
{
Expand All @@ -121,14 +152,24 @@ public void CompareMajorFloatingVersion_NotEqual()
}

[TestMethod]
public void ParseAndToString_AreReversible()
public void ParseAndToStringSpecificVersion_AreReversible()
{
var x = new HarpVersion(2, null);
var x = new HarpVersion(2, 1, 0);
var text = x.ToString();
var y = HarpVersion.Parse(text);
AssertEquals(x, y);
}

[TestMethod]
public void ParseAndToStringPatchFloating_AreReversible()
{
var input = "1.0.x";
var x = HarpVersion.Parse(input);
Assert.AreEqual("1.0", x.ToString());
var y = HarpVersion.Parse(x.ToString());
Assert.AreEqual(x, y);
}

[TestMethod]
public void InvalidParseRunawayCharacters_ThrowsException()
{
Expand All @@ -146,5 +187,11 @@ public void InvalidParseWithFloatingMajor_ThrowsException()
{
Assert.ThrowsExactly<ArgumentException>(() => HarpVersion.Parse("x.1"));
}

[TestMethod]
public void InvalidParseWithFloatingMinor_ThrowsException()
{
Assert.ThrowsExactly<ArgumentException>(() => HarpVersion.Parse("1.x.0"));
}
}
}
48 changes: 30 additions & 18 deletions src/Bonsai.Harp/FirmwareMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Text.RegularExpressions;

Expand All @@ -10,29 +11,29 @@ namespace Bonsai.Harp
/// </summary>
public sealed class FirmwareMetadata : IEquatable<FirmwareMetadata>
{
static readonly Regex MetadataRegex = new Regex("^(?<device>\\w+)-fw(?<firmware>\\d+\\.\\d+)-harp(?<core>\\d+\\.\\d+)-hw(?<hardware>(?:x|\\d+)\\.(?:x|\\d+))-ass(?<sequence>x|\\d+)(?:-preview(?<prerelease>\\d+))?$");
static readonly Regex MetadataRegex = new("^(?<device>\\w+)-fw(?<firmware>\\d+\\.\\d+(\\.\\d+)?)-harp(?<core>\\d+\\.\\d+(\\.\\d+)?)-hw(?<hardware>\\d+\\.\\d+(\\.\\d+)?)-ass(?<sequence>x|\\d+)(?:-preview(?<prerelease>\\d+))?$");

/// <summary>
/// Initializes a new instance of the <see cref="FirmwareMetadata"/> class with the
/// specified device name, the firmware version and compatible hardware versions.
/// </summary>
/// <param name="deviceName">The unique identifier of the device type on which the firmware should be installed.</param>
/// <param name="firmwareVersion">The version of the firmware contained in the device or hex file.</param>
/// <param name="coreVersion">The version of the Harp core implemented by the firmware.</param>
/// <param name="protocolVersion">The version of the Harp core implemented by the firmware.</param>
/// <param name="hardwareVersion">The hardware version of the device, or range of hardware versions supported by the firmware.</param>
/// <param name="assemblyVersion">The board assembly version of the device, or range of assembly versions supported by the firmware.</param>
/// <param name="prereleaseVersion">The optional prerelease number, for preview versions of the firmware.</param>
public FirmwareMetadata(
string deviceName,
HarpVersion firmwareVersion,
HarpVersion coreVersion,
HarpVersion protocolVersion,
HarpVersion hardwareVersion,
int? assemblyVersion = default,
int? prereleaseVersion = default)
{
DeviceName = deviceName ?? throw new ArgumentNullException(nameof(deviceName));
FirmwareVersion = firmwareVersion ?? throw new ArgumentNullException(nameof(firmwareVersion));
CoreVersion = coreVersion ?? throw new ArgumentNullException(nameof(coreVersion));
ProtocolVersion = protocolVersion ?? throw new ArgumentNullException(nameof(protocolVersion));
HardwareVersion = hardwareVersion ?? throw new ArgumentNullException(nameof(hardwareVersion));
AssemblyVersion = assemblyVersion;
PrereleaseVersion = prereleaseVersion;
Expand All @@ -49,9 +50,20 @@ public FirmwareMetadata(
public HarpVersion FirmwareVersion { get; }

/// <summary>
/// Gets the version of the Harp core implemented by the firmware.
/// Gets the version of the Harp protocol implemented by the firmware.
/// </summary>
public HarpVersion CoreVersion { get; }
public HarpVersion ProtocolVersion { get; }

/// <summary>
/// Gets the version of the Harp protocol implemented by the firmware.
/// </summary>
/// <remarks>
/// This property is obsolete. Use <see cref="ProtocolVersion"/> instead.
/// </remarks>
[Obsolete]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public HarpVersion CoreVersion => ProtocolVersion;

/// <summary>
/// Gets the hardware version of the device, or range of hardware versions supported by the firmware.
Expand Down Expand Up @@ -113,7 +125,7 @@ public bool Equals(FirmwareMetadata other)
if (other is null) return false;
return DeviceName == other.DeviceName &&
FirmwareVersion.Equals(other.FirmwareVersion) &&
CoreVersion.Equals(other.CoreVersion) &&
ProtocolVersion.Equals(other.ProtocolVersion) &&
HardwareVersion.Equals(other.HardwareVersion) &&
AssemblyVersion == other.AssemblyVersion &&
PrereleaseVersion == other.PrereleaseVersion;
Expand All @@ -130,7 +142,7 @@ public override int GetHashCode()
{
return 17 * DeviceName.GetHashCode() +
8971 * FirmwareVersion.GetHashCode() +
2803 * CoreVersion.GetHashCode() +
2803 * ProtocolVersion.GetHashCode() +
691 * HardwareVersion.GetHashCode() +
1409 * AssemblyVersion.GetHashCode() +
2333 * PrereleaseVersion.GetHashCode();
Expand Down Expand Up @@ -164,7 +176,7 @@ public override int GetHashCode()
/// </returns>
public static bool operator !=(FirmwareMetadata lhs, FirmwareMetadata rhs)
{
if (lhs is null) return !(rhs is null);
if (lhs is null) return rhs is not null;
else return !lhs.Equals(rhs);
}

Expand Down Expand Up @@ -200,15 +212,15 @@ public static bool TryParse(string input, out FirmwareMetadata metadata)
{
if (input == null) throw new ArgumentNullException(nameof(input));
var match = MetadataRegex.Match(input);
if (match.Success && match.Groups.Count == 7)
if (match.Success && match.Groups.Count == 10)
{
var deviceName = match.Groups[1].Value;
var firmwareVersion = HarpVersion.Parse(match.Groups[2].Value);
var coreVersion = HarpVersion.Parse(match.Groups[3].Value);
var hardwareVersion = HarpVersion.Parse(match.Groups[4].Value);
var assemblyVersion = match.Groups[5].Value == HarpVersion.FloatingWildcard ? (int?)null : int.Parse(match.Groups[5].Value);
var prereleaseVersion = string.IsNullOrEmpty(match.Groups[6].Value) ? (int?)null : int.Parse(match.Groups[6].Value);
metadata = new FirmwareMetadata(deviceName, firmwareVersion, coreVersion, hardwareVersion, assemblyVersion, prereleaseVersion);
var deviceName = match.Groups[4].Value;
var firmwareVersion = HarpVersion.Parse(match.Groups[5].Value);
var protocolVersion = HarpVersion.Parse(match.Groups[6].Value);
var hardwareVersion = HarpVersion.Parse(match.Groups[7].Value);
var assemblyVersion = match.Groups[8].Value == HarpVersion.FloatingWildcard ? (int?)null : int.Parse(match.Groups[8].Value);
var prereleaseVersion = string.IsNullOrEmpty(match.Groups[9].Value) ? (int?)null : int.Parse(match.Groups[9].Value);
metadata = new FirmwareMetadata(deviceName, firmwareVersion, protocolVersion, hardwareVersion, assemblyVersion, prereleaseVersion);
return true;
}
else
Expand All @@ -228,7 +240,7 @@ public override string ToString()
{
var prerelease = PrereleaseVersion.HasValue ? $"-preview{PrereleaseVersion.Value}" : string.Empty;
var assemblyNumber = AssemblyVersion.HasValue ? AssemblyVersion.Value.ToString(CultureInfo.InvariantCulture) : HarpVersion.FloatingWildcard;
return $"{DeviceName}-fw{FirmwareVersion}-harp{CoreVersion}-hw{HardwareVersion}-ass{assemblyNumber}{prerelease}";
return $"{DeviceName}-fw{FirmwareVersion}-harp{ProtocolVersion}-hw{HardwareVersion}-ass{assemblyNumber}{prerelease}";
}
}
}
Loading