Skip to content

Commit fee3392

Browse files
committed
Fixed another bug in comparator
1 parent 89efb15 commit fee3392

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

Diff for: version.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,14 @@ func (v *Version) CompareTo(u *Version) int {
288288
// Numeric identifiers always have lower precedence than non-numeric identifiers.
289289
if vIsAlpha && uIsAlpha {
290290
if cmp != 0 {
291+
// alphanumeric vs alphanumeric, sorting has priority
291292
return cmp
293+
} else if vIsLonger {
294+
// alphanumeric vs alphanumeric, v is longer, return >
295+
return 1
296+
} else if uIsLonger {
297+
// alphanumeric vs alphanumeric, u is longer, return <
298+
return -1
292299
}
293300
// Both alphanumeric, if comparison is equal, move on the next field
294301
} else if vIsAlpha && !uIsAlpha {
@@ -297,15 +304,18 @@ func (v *Version) CompareTo(u *Version) int {
297304
} else if !vIsAlpha && uIsAlpha {
298305
// numeric vs alphanumeric, return <
299306
return -1
300-
} else if vIsLonger {
301-
// numeric vs numeric, v is longer, return >
302-
return 1
303-
} else if uIsLonger {
304-
// numeric vs numeric, u is longer, return <
305-
return -1
306-
} else if cmp != 0 {
307-
// numeric vs numeric, return cmp if not equal
308-
return cmp
307+
} else {
308+
if vIsLonger {
309+
// numeric vs numeric, v is longer, return >
310+
return 1
311+
} else if uIsLonger {
312+
// numeric vs numeric, u is longer, return <
313+
return -1
314+
} else if cmp != 0 {
315+
// numeric vs numeric, return cmp if not equal
316+
return cmp
317+
}
318+
// Both numeric, if comparison is equal, move on the next field
309319
}
310320

311321
// A larger set of pre-release fields has a higher precedence than a smaller set,

Diff for: version_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ascending(t *testing.T, allowEqual bool, list ...string) {
3131
require.False(t, a.GreaterThan(b))
3232
} else {
3333
fmt.Printf("%s %s %s\n", list[i], sign[comp], list[i+1])
34-
require.Equal(t, comp, -1)
34+
require.Equal(t, comp, -1, "cmp(%s, %s) must return '<', but returned '%s'", list[i], list[i+1], sign[comp])
3535
require.True(t, a.LessThan(b))
3636
require.True(t, a.LessThanOrEqual(b))
3737
require.False(t, a.Equal(b))
@@ -42,7 +42,7 @@ func ascending(t *testing.T, allowEqual bool, list ...string) {
4242
comp = b.CompareTo(a)
4343
fmt.Printf("%s %s %s\n", b, sign[comp], a)
4444
if allowEqual {
45-
require.GreaterOrEqual(t, comp, 0)
45+
require.GreaterOrEqual(t, comp, 0, "cmp(%s, %s) must return '>=', but returned '%s'", b, a, sign[comp])
4646
require.False(t, b.LessThan(a))
4747
require.True(t, b.GreaterThanOrEqual(a))
4848
} else {
@@ -102,6 +102,16 @@ func TestVersionComparator(t *testing.T) {
102102
"1.20.0",
103103
"2.1.1",
104104
"10.0.0",
105+
"17.3.0-atmel3.6.1-arduino7",
106+
"17.3.0-atmel3.6.1-arduino7not",
107+
"17.3.0-atmel3.6.1-beduino8",
108+
"17.3.0-atmel3.6.1-beduino8not",
109+
"17.3.0-atmel3a.6.1-arduino7",
110+
"17.3.0-atmel3a.16.2.arduino7",
111+
"17.3.0-atmel3a.16.12.arduino7",
112+
"17.3.0-atmel3a.16.1-arduino7",
113+
"17.3.0-atmel3a.16.12-arduino7",
114+
"17.3.0-atmel3a.16.2-arduino7",
105115
)
106116
equal(
107117
MustParse(""),

0 commit comments

Comments
 (0)