Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active April 1, 2022 15:02
Show Gist options
  • Save itn3000/7f684d75561451fc679b78c39a8b7c23 to your computer and use it in GitHub Desktop.
Save itn3000/7f684d75561451fc679b78c39a8b7c23 to your computer and use it in GitHub Desktop.
nuget semver2 parse test
// dotnet add package NuGet.Versioning --version 6.1.0
using System;
using System.Reflection;
using NuGet.Versioning;
namespace nugetclienttest
{
class Program
{
static void OutputVersion(NuGetVersion v)
{
Console.WriteLine($"{v.Major}, {v.Minor}, {v.Patch}, {v.Revision}, {v.Release}, {string.Join('|', v.ReleaseLabels)}");
}
static void Main(string[] args)
{
var v1 = NuGet.Versioning.NuGetVersion.Parse("1.2.3-rc9");
var v2 = NuGet.Versioning.NuGetVersion.Parse("1.2.3-rc10");
var v3 = NuGet.Versioning.NuGetVersion.Parse("1.2.3-rc.9");
var v4 = NuGet.Versioning.NuGetVersion.Parse($"1.2.3-rc.10");
var v5 = NuGet.Versioning.NuGetVersion.Parse("1-rc.2.3-rc.0");
var v6 = NuGet.Versioning.NuGetVersion.Parse($"1.2.3");
var v7 = NuGet.Versioning.NuGetVersion.Parse("1.2.3-1");
// parse error
// var verror = NuGet.Versioning.NuGetVersion.Parse("1.2.3.4.5");
// about A.CompareTo(B) result
// * if < 0, A is older than B
// * if > 0, A is newer than B
// * if == 0, A is equal to B
Console.WriteLine("{0}, {1}: {2}", v1, v2, v1.CompareTo(v2));
Console.WriteLine("{0}, {1}: {2}", v2, v3, v2.CompareTo(v3));
Console.WriteLine("{0}, {1}: {2}", v3, v1, v3.CompareTo(v1));
Console.WriteLine("{0}, {1}: {2}", v3, v4, v3.CompareTo(v4));
Console.WriteLine("{0}, {1}: {2}", v1, v6, v1.CompareTo(v6));
Console.WriteLine("{0}, {1}: {2}", v1, v7, v1.CompareTo(v7));
Console.WriteLine("{0}, {1}: {2}", v6, v7, v6.CompareTo(v7));
OutputVersion(v1);
OutputVersion(v2);
OutputVersion(v3);
OutputVersion(v4);
OutputVersion(v5);
OutputVersion(v6);
OutputVersion(v7);
}
}
}
1.2.3-rc9, 1.2.3-rc10: 8
1.2.3-rc10, 1.2.3-rc.9: 2
1.2.3-rc.9, 1.2.3-rc9: -1
1.2.3-rc.9, 1.2.3-rc.10: -1
1.2.3-rc9, 1.2.3: -1
1.2.3-rc9, 1.2.3-1: 1
1.2.3, 1.2.3-1: 1
1, 2, 3, 0, rc9, rc9
1, 2, 3, 0, rc10, rc10
1, 2, 3, 0, rc.9, rc|9
1, 2, 3, 0, rc.10, rc|10
1, 0, 0, 0, rc.2.3-rc.0, rc|2|3-rc|0
1, 2, 3, 0, ,
1, 2, 3, 0, 1, 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment