Browse Source

Add additional compare helpers

Benedikt Lang 11 years ago
parent
commit
61fed1c667
2 changed files with 40 additions and 0 deletions
  1. +25
    -0
      semver.go
  2. +15
    -0
      semver_test.go

+ 25
- 0
semver.go View File

@ -55,6 +55,21 @@ func (v Version) String() string {
return strings.Join(versionArray, "")
}
// Checks if v is equal to o.
func (v Version) Equals(o Version) bool {
return (v.Compare(o) == 0)
}
// Checks if v is equal to o.
func (v Version) EQ(o Version) bool {
return (v.Compare(o) == 0)
}
// Checks if v is not equal to o.
func (v Version) NE(o Version) bool {
return (v.Compare(o) != 0)
}
// Checks if v is greater than o.
func (v Version) GT(o Version) bool {
return (v.Compare(o) == 1)
@ -65,6 +80,11 @@ func (v Version) GTE(o Version) bool {
return (v.Compare(o) >= 0)
}
// Checks if v is greater than or equal to o.
func (v Version) GE(o Version) bool {
return (v.Compare(o) >= 0)
}
// Checks if v is less than o.
func (v Version) LT(o Version) bool {
return (v.Compare(o) == -1)
@ -75,6 +95,11 @@ func (v Version) LTE(o Version) bool {
return (v.Compare(o) <= 0)
}
// Checks if v is less than or equal to o.
func (v Version) LE(o Version) bool {
return (v.Compare(o) <= 0)
}
// Compares Versions v to o:
// -1 == v is less than o
// 0 == v is equal to o


+ 15
- 0
semver_test.go View File

@ -169,6 +169,15 @@ func TestWrongFormat(t *testing.T) {
func TestCompareHelper(t *testing.T) {
v := Version{1, 0, 0, []PRVersion{prstr("alpha")}, nil}
v1 := Version{1, 0, 0, nil, nil}
if !v.EQ(v) {
t.Errorf("%q should be equal to %q", v, v)
}
if !v.Equals(v) {
t.Errorf("%q should be equal to %q", v, v)
}
if !v1.NE(v) {
t.Errorf("%q should not be equal to %q", v, v)
}
if !v.GTE(v) {
t.Errorf("%q should be greater than or equal to %q", v, v)
}
@ -181,12 +190,18 @@ func TestCompareHelper(t *testing.T) {
if !v.LTE(v1) {
t.Errorf("%q should be less than or equal %q", v, v1)
}
if !v.LE(v1) {
t.Errorf("%q should be less than or equal %q", v, v1)
}
if !v1.GT(v) {
t.Errorf("%q should be less than %q", v1, v)
}
if !v1.GTE(v) {
t.Errorf("%q should be less than or equal %q", v1, v)
}
if !v1.GE(v) {
t.Errorf("%q should be less than or equal %q", v1, v)
}
}
func TestPreReleaseVersions(t *testing.T) {


Loading…
Cancel
Save