From fee3392a904660d1cbc14b7409460cd14a68a429 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 22 Nov 2023 13:01:44 +0100 Subject: [PATCH] Fixed another bug in comparator --- version.go | 28 +++++++++++++++++++--------- version_test.go | 14 ++++++++++++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/version.go b/version.go index 18191c1..b4ee5c6 100644 --- a/version.go +++ b/version.go @@ -288,7 +288,14 @@ func (v *Version) CompareTo(u *Version) int { // Numeric identifiers always have lower precedence than non-numeric identifiers. if vIsAlpha && uIsAlpha { if cmp != 0 { + // alphanumeric vs alphanumeric, sorting has priority return cmp + } else if vIsLonger { + // alphanumeric vs alphanumeric, v is longer, return > + return 1 + } else if uIsLonger { + // alphanumeric vs alphanumeric, u is longer, return < + return -1 } // Both alphanumeric, if comparison is equal, move on the next field } else if vIsAlpha && !uIsAlpha { @@ -297,15 +304,18 @@ func (v *Version) CompareTo(u *Version) int { } else if !vIsAlpha && uIsAlpha { // numeric vs alphanumeric, return < return -1 - } else if vIsLonger { - // numeric vs numeric, v is longer, return > - return 1 - } else if uIsLonger { - // numeric vs numeric, u is longer, return < - return -1 - } else if cmp != 0 { - // numeric vs numeric, return cmp if not equal - return cmp + } else { + if vIsLonger { + // numeric vs numeric, v is longer, return > + return 1 + } else if uIsLonger { + // numeric vs numeric, u is longer, return < + return -1 + } else if cmp != 0 { + // numeric vs numeric, return cmp if not equal + return cmp + } + // Both numeric, if comparison is equal, move on the next field } // A larger set of pre-release fields has a higher precedence than a smaller set, diff --git a/version_test.go b/version_test.go index 6a57d3b..6c54e3a 100644 --- a/version_test.go +++ b/version_test.go @@ -31,7 +31,7 @@ func ascending(t *testing.T, allowEqual bool, list ...string) { require.False(t, a.GreaterThan(b)) } else { fmt.Printf("%s %s %s\n", list[i], sign[comp], list[i+1]) - require.Equal(t, comp, -1) + require.Equal(t, comp, -1, "cmp(%s, %s) must return '<', but returned '%s'", list[i], list[i+1], sign[comp]) require.True(t, a.LessThan(b)) require.True(t, a.LessThanOrEqual(b)) require.False(t, a.Equal(b)) @@ -42,7 +42,7 @@ func ascending(t *testing.T, allowEqual bool, list ...string) { comp = b.CompareTo(a) fmt.Printf("%s %s %s\n", b, sign[comp], a) if allowEqual { - require.GreaterOrEqual(t, comp, 0) + require.GreaterOrEqual(t, comp, 0, "cmp(%s, %s) must return '>=', but returned '%s'", b, a, sign[comp]) require.False(t, b.LessThan(a)) require.True(t, b.GreaterThanOrEqual(a)) } else { @@ -102,6 +102,16 @@ func TestVersionComparator(t *testing.T) { "1.20.0", "2.1.1", "10.0.0", + "17.3.0-atmel3.6.1-arduino7", + "17.3.0-atmel3.6.1-arduino7not", + "17.3.0-atmel3.6.1-beduino8", + "17.3.0-atmel3.6.1-beduino8not", + "17.3.0-atmel3a.6.1-arduino7", + "17.3.0-atmel3a.16.2.arduino7", + "17.3.0-atmel3a.16.12.arduino7", + "17.3.0-atmel3a.16.1-arduino7", + "17.3.0-atmel3a.16.12-arduino7", + "17.3.0-atmel3a.16.2-arduino7", ) equal( MustParse(""),