fix nil version comparisons, add equal func
This commit is contained in:
+12
-1
@@ -19,7 +19,11 @@ type Version struct {
|
|||||||
//
|
//
|
||||||
// Extensions such as pre-release version or build metadata are ignored when comparing versions.
|
// Extensions such as pre-release version or build metadata are ignored when comparing versions.
|
||||||
func (a *Version) Compare(b *Version) int {
|
func (a *Version) Compare(b *Version) int {
|
||||||
if a == nil || b == nil {
|
if a == nil && b != nil {
|
||||||
|
return -1
|
||||||
|
} else if a != nil && b == nil {
|
||||||
|
return 1
|
||||||
|
} else if a == nil && b == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +44,13 @@ func (a *Version) Compare(b *Version) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Equal checks for equality between two versions.
|
||||||
|
//
|
||||||
|
// Extensions such as pre-release version or build metadata are ignored when comparing versions.
|
||||||
|
func (a *Version) Equal(b *Version) bool {
|
||||||
|
return a.Compare(b) == 0
|
||||||
|
}
|
||||||
|
|
||||||
// Match tests the version against a constraint.
|
// Match tests the version against a constraint.
|
||||||
// Gt and Lt take precedence over Gte and Lte.
|
// Gt and Lt take precedence over Gte and Lte.
|
||||||
func (v *Version) Match(c *Constraint) bool {
|
func (v *Version) Match(c *Constraint) bool {
|
||||||
|
|||||||
+2
-2
@@ -55,8 +55,8 @@ func TestVersion_Compare(t *testing.T) {
|
|||||||
{A: MustParse("1.20.0"), B: MustParse("1.2.0"), Expected: 1},
|
{A: MustParse("1.20.0"), B: MustParse("1.2.0"), Expected: 1},
|
||||||
{A: MustParse("1.20.0"), B: MustParse("1.2.20"), Expected: 1},
|
{A: MustParse("1.20.0"), B: MustParse("1.2.20"), Expected: 1},
|
||||||
{A: MustParse("1.20.0"), B: MustParse("1.20.1"), Expected: -1},
|
{A: MustParse("1.20.0"), B: MustParse("1.20.1"), Expected: -1},
|
||||||
{A: MustParse("1.0.0"), Expected: 0},
|
{A: MustParse("1.0.0"), Expected: 1},
|
||||||
{B: MustParse("1.0.0"), Expected: 0},
|
{B: MustParse("1.0.0"), Expected: -1},
|
||||||
{Expected: 0},
|
{Expected: 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user