Skip to content

Commit

Permalink
Fixed another bug in comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 22, 2023
1 parent 89efb15 commit fee3392
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
28 changes: 19 additions & 9 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand Down Expand Up @@ -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(""),
Expand Down

0 comments on commit fee3392

Please sign in to comment.