Skip to content

Commit

Permalink
Rename to LexicographicSortedVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
tleed5 committed Jan 12, 2024
1 parent 475333b commit 48638fd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using NUnit.Framework;
using Octopus.Versioning.Unsortable;
using Octopus.Versioning.Lexicographic;

namespace Octopus.Versioning.Tests.Unsortable;
namespace Octopus.Versioning.Tests.LexicographicSortedVersion;

public class UnsortableVersionCompareTests
public class LexicographicSortedVersionCompareTests
{
static readonly UnsortableVersionParser UnsortableVersionParser = new();
static readonly LexicographicSortedVersionParser LexicographicSortedVersionParser = new();

[Test]
[TestCase("release", "release", 0)]
Expand Down Expand Up @@ -36,8 +37,8 @@ public class UnsortableVersionCompareTests
[TestCase("release-1+123", "release-2+321", -1)]
public void TestComparisons(string version1, string version2, int result)
{
var parsedVersion1 = UnsortableVersionParser.Parse(version1);
var parsedVersion2 = UnsortableVersionParser.Parse(version2);
var parsedVersion1 = LexicographicSortedVersionParser.Parse(version1);
var parsedVersion2 = LexicographicSortedVersionParser.Parse(version2);
Assert.AreEqual(result, parsedVersion1.CompareTo(parsedVersion2));
}

Expand All @@ -46,16 +47,16 @@ public void TestComparisons(string version1, string version2, int result)
[TestCase("release-1", "release-2", false)]
public void TestEquality(string version1, string version2, bool result)
{
var parsedVersion1 = UnsortableVersionParser.Parse(version1);
var parsedVersion2 = UnsortableVersionParser.Parse(version2);
var parsedVersion1 = LexicographicSortedVersionParser.Parse(version1);
var parsedVersion2 = LexicographicSortedVersionParser.Parse(version2);
Assert.AreEqual(result, Equals(parsedVersion1, parsedVersion2));
}

[Test]
public void TestGetHashCode()
{
var versionString = "release-1";
var parsedVersion = UnsortableVersionParser.Parse(versionString);
var parsedVersion = LexicographicSortedVersionParser.Parse(versionString);

Assert.AreEqual(versionString.GetHashCode(), parsedVersion.GetHashCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using NUnit.Framework;
using Octopus.Versioning.Unsortable;
using Octopus.Versioning.Lexicographic;

namespace Octopus.Versioning.Tests.Unsortable;
namespace Octopus.Versioning.Tests.LexicographicSortedVersion;

[TestFixture]
public class UnsortableVersionParserTests
public class LexicographicSortedVersionParserTests
{
[Test]
// Release
Expand Down Expand Up @@ -38,7 +38,7 @@ public class UnsortableVersionParserTests
[TestCase("foobar!", "", "")]
public void ShouldParseSuccessfully(string input, string expectedRelease, string expectedMetadata)
{
_ = new UnsortableVersionParser().TryParse(input, out var parsedVersion);
_ = new LexicographicSortedVersionParser().TryParse(input, out var parsedVersion);
AssertVersionNumbersAreZero(parsedVersion);
Assert.AreEqual(expectedRelease, parsedVersion.Release);
Assert.AreEqual(expectedMetadata, parsedVersion.Metadata);
Expand All @@ -48,30 +48,30 @@ public void ShouldParseSuccessfully(string input, string expectedRelease, string
public void ShouldThrowExceptionOnEmptyInput()
{
var input = "";
Assert.Catch<ArgumentException>(() => new UnsortableVersionParser().Parse(input));
Assert.Catch<ArgumentException>(() => new LexicographicSortedVersionParser().Parse(input));
}

[Test]
public void ShouldThrowExceptionOnWhiteSpaceInput()
{
var input = " ";
Assert.Catch<ArgumentException>(() => new UnsortableVersionParser().Parse(input));
Assert.Catch<ArgumentException>(() => new LexicographicSortedVersionParser().Parse(input));
}

[Test]
public void ShouldThrowExceptionOnNullInput()
{
Assert.Catch<ArgumentException>(() => new UnsortableVersionParser().Parse(null));
Assert.Catch<ArgumentException>(() => new LexicographicSortedVersionParser().Parse(null));
}

[Test]
public void ShouldThrowExceptionOnFailureToParse()
{
var input = "bad versions string";
Assert.Catch<ArgumentException>(() => new UnsortableVersionParser().Parse(input));
Assert.Catch<ArgumentException>(() => new LexicographicSortedVersionParser().Parse(input));
}

void AssertVersionNumbersAreZero(UnsortableVersion version)
void AssertVersionNumbersAreZero(Lexicographic.LexicographicSortedVersion version)
{
Assert.AreEqual(0, version.Major);
Assert.AreEqual(0, version.Minor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using System.Collections.Generic;
using System.Linq;

namespace Octopus.Versioning.Unsortable
namespace Octopus.Versioning.Lexicographic
{
public class UnsortableVersion: IVersion
public class LexicographicSortedVersion: IVersion
{
public UnsortableVersion(string release, string? metadata, string? originalString)
public LexicographicSortedVersion(string release, string? metadata, string? originalString)
{
Metadata = metadata;
Release = release;
Expand All @@ -24,7 +24,7 @@ public UnsortableVersion(string release, string? metadata, string? originalStrin
public string Release { get; }
public string OriginalString { get; }

public VersionFormat Format => VersionFormat.Unsortable;
public VersionFormat Format => VersionFormat.Lexicographic;

public int CompareTo(object obj)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Text.RegularExpressions;

namespace Octopus.Versioning.Unsortable
namespace Octopus.Versioning.Lexicographic
{
public class UnsortableVersionParser
public class LexicographicSortedVersionParser
{
const string Release = "release";
const string Meta = "buildmetadata";
Expand All @@ -12,7 +12,7 @@ public class UnsortableVersionParser
$@"(?:\+(?<{Meta}>[A-Za-z0-9_\-.\\+]*?))?\s*$"
);

public UnsortableVersion Parse(string? version)
public LexicographicSortedVersion Parse(string? version)
{
if (string.IsNullOrWhiteSpace(version))
throw new ArgumentException("The version can not be an empty string");
Expand All @@ -28,14 +28,14 @@ public UnsortableVersion Parse(string? version)
if (!result.Success)
throw new ArgumentException("The supplied version was not valid");

return new UnsortableVersion(
return new LexicographicSortedVersion(
result.Groups[Release].Success ? result.Groups[Release].Value : string.Empty,
result.Groups[Meta].Success ? result.Groups[Meta].Value : string.Empty,
noSpaces
);
}

public bool TryParse(string version, out UnsortableVersion parsedVersion)
public bool TryParse(string version, out LexicographicSortedVersion parsedVersion)
{
try
{
Expand All @@ -44,7 +44,7 @@ public bool TryParse(string version, out UnsortableVersion parsedVersion)
}
catch
{
parsedVersion = new UnsortableVersion(
parsedVersion = new LexicographicSortedVersion(
string.Empty,
string.Empty,
null
Expand Down
18 changes: 9 additions & 9 deletions source/Octopus.Versioning/VersionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using Octopus.Versioning.Docker;
using Octopus.Versioning.Lexicographic;
using Octopus.Versioning.Maven;
using Octopus.Versioning.Octopus;
using Octopus.Versioning.Semver;
using Octopus.Versioning.Unsortable;

namespace Octopus.Versioning
{
Expand All @@ -20,8 +20,8 @@ public static IVersion CreateVersion(string input, VersionFormat format)
return CreateDockerTag(input);
case VersionFormat.Octopus:
return CreateOctopusVersion(input);
case VersionFormat.Unsortable:
return CreateUnsortableVersion(input);
case VersionFormat.Lexicographic:
return CreateLexicographicSortedVersion(input);
default:
return CreateSemanticVersion(input);
}
Expand All @@ -37,8 +37,8 @@ public static IVersion CreateVersion(string input, VersionFormat format)
return TryCreateDockerTag(input);
case VersionFormat.Octopus:
return TryCreateOctopusVersion(input);
case VersionFormat.Unsortable:
return TryCreateUnsortableVersion(input);
case VersionFormat.Lexicographic:
return TryCreateLexicographicSortedVersion(input);
default:
return TryCreateSemanticVersion(input);
}
Expand Down Expand Up @@ -156,16 +156,16 @@ public static IVersion CreateOctopusVersion(string input)
}
}

public static IVersion CreateUnsortableVersion(string input)
public static IVersion CreateLexicographicSortedVersion(string input)
{
return new UnsortableVersionParser().Parse(input);
return new LexicographicSortedVersionParser().Parse(input);
}

public static IVersion? TryCreateUnsortableVersion(string input)
public static IVersion? TryCreateLexicographicSortedVersion(string input)
{
try
{
return CreateUnsortableVersion(input);
return CreateLexicographicSortedVersion(input);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion source/Octopus.Versioning/VersionFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public enum VersionFormat
Maven,
Docker,
Octopus,
Unsortable
Lexicographic
}
}

0 comments on commit 48638fd

Please sign in to comment.