Skip to content

Commit

Permalink
Merge pull request #53 from hashicorp/f-lte-gte
Browse files Browse the repository at this point in the history
Implement GreaterThanOrEqual + LessThanOrEqual
  • Loading branch information
radeksimko authored Apr 22, 2019
2 parents d40cf49 + 67c9bf3 commit ac23dc3
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: go

go:
- 1.0
- 1.1
- 1.2
- 1.3
- 1.4
- 1.9
- "1.10"
- 1.11
- 1.12

script:
- go test
12 changes: 11 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func Must(v *Version, err error) *Version {
// or larger than the other version, respectively.
//
// If you want boolean results, use the LessThan, Equal,
// or GreaterThan methods.
// GreaterThan, GreaterThanOrEqual or LessThanOrEqual methods.
func (v *Version) Compare(other *Version) int {
// A quick, efficient equality check
if v.String() == other.String() {
Expand Down Expand Up @@ -288,11 +288,21 @@ func (v *Version) GreaterThan(o *Version) bool {
return v.Compare(o) > 0
}

// GreaterThanOrEqualTo tests if this version is greater than or equal to another version.
func (v *Version) GreaterThanOrEqual(o *Version) bool {
return v.Compare(o) >= 0
}

// LessThan tests if this version is less than another version.
func (v *Version) LessThan(o *Version) bool {
return v.Compare(o) < 0
}

// LessThanOrEqualTo tests if this version is less than or equal to another version.
func (v *Version) LessThanOrEqual(o *Version) bool {
return v.Compare(o) <= 0
}

// Metadata returns any metadata that was part of the version
// string.
//
Expand Down
235 changes: 235 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,238 @@ func TestVersionString(t *testing.T) {
}
}
}

func TestEqual(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.4.5", false},
{"1.2-beta", "1.2-beta", true},
{"1.2", "1.1.4", false},
{"1.2", "1.2-beta", false},
{"1.2+foo", "1.2+beta", true},
{"v1.2", "v1.2-beta", false},
{"v1.2+foo", "v1.2+beta", true},
{"v1.2.3.4", "v1.2.3.4", true},
{"v1.2.0.0", "v1.2", true},
{"v1.2.0.0.1", "v1.2", false},
{"v1.2", "v1.2.0.0", true},
{"v1.2", "v1.2.0.0.1", false},
{"v1.2.0.0", "v1.2.0.0.1", false},
{"v1.2.3.0", "v1.2.3.4", false},
{"1.7rc2", "1.7rc1", false},
{"1.7rc2", "1.7", false},
{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", false},
}

for _, tc := range cases {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Fatalf("err: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v1.Equal(v2)
expected := tc.expected
if actual != expected {
t.Fatalf(
"%s <=> %s\nexpected: %t\nactual: %t",
tc.v1, tc.v2,
expected, actual)
}
}
}

func TestGreaterThan(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.4.5", false},
{"1.2-beta", "1.2-beta", false},
{"1.2", "1.1.4", true},
{"1.2", "1.2-beta", true},
{"1.2+foo", "1.2+beta", false},
{"v1.2", "v1.2-beta", true},
{"v1.2+foo", "v1.2+beta", false},
{"v1.2.3.4", "v1.2.3.4", false},
{"v1.2.0.0", "v1.2", false},
{"v1.2.0.0.1", "v1.2", true},
{"v1.2", "v1.2.0.0", false},
{"v1.2", "v1.2.0.0.1", false},
{"v1.2.0.0", "v1.2.0.0.1", false},
{"v1.2.3.0", "v1.2.3.4", false},
{"1.7rc2", "1.7rc1", true},
{"1.7rc2", "1.7", false},
{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", true},
}

for _, tc := range cases {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Fatalf("err: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v1.GreaterThan(v2)
expected := tc.expected
if actual != expected {
t.Fatalf(
"%s > %s\nexpected: %t\nactual: %t",
tc.v1, tc.v2,
expected, actual)
}
}
}

func TestLessThan(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.4.5", true},
{"1.2-beta", "1.2-beta", false},
{"1.2", "1.1.4", false},
{"1.2", "1.2-beta", false},
{"1.2+foo", "1.2+beta", false},
{"v1.2", "v1.2-beta", false},
{"v1.2+foo", "v1.2+beta", false},
{"v1.2.3.4", "v1.2.3.4", false},
{"v1.2.0.0", "v1.2", false},
{"v1.2.0.0.1", "v1.2", false},
{"v1.2", "v1.2.0.0", false},
{"v1.2", "v1.2.0.0.1", true},
{"v1.2.0.0", "v1.2.0.0.1", true},
{"v1.2.3.0", "v1.2.3.4", true},
{"1.7rc2", "1.7rc1", false},
{"1.7rc2", "1.7", true},
{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", false},
}

for _, tc := range cases {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Fatalf("err: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v1.LessThan(v2)
expected := tc.expected
if actual != expected {
t.Fatalf(
"%s < %s\nexpected: %t\nactual: %t",
tc.v1, tc.v2,
expected, actual)
}
}
}

func TestGreaterThanOrEqual(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.4.5", false},
{"1.2-beta", "1.2-beta", true},
{"1.2", "1.1.4", true},
{"1.2", "1.2-beta", true},
{"1.2+foo", "1.2+beta", true},
{"v1.2", "v1.2-beta", true},
{"v1.2+foo", "v1.2+beta", true},
{"v1.2.3.4", "v1.2.3.4", true},
{"v1.2.0.0", "v1.2", true},
{"v1.2.0.0.1", "v1.2", true},
{"v1.2", "v1.2.0.0", true},
{"v1.2", "v1.2.0.0.1", false},
{"v1.2.0.0", "v1.2.0.0.1", false},
{"v1.2.3.0", "v1.2.3.4", false},
{"1.7rc2", "1.7rc1", true},
{"1.7rc2", "1.7", false},
{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", true},
}

for _, tc := range cases {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Fatalf("err: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v1.GreaterThanOrEqual(v2)
expected := tc.expected
if actual != expected {
t.Fatalf(
"%s >= %s\nexpected: %t\nactual: %t",
tc.v1, tc.v2,
expected, actual)
}
}
}

func TestLessThanOrEqual(t *testing.T) {
cases := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.4.5", true},
{"1.2-beta", "1.2-beta", true},
{"1.2", "1.1.4", false},
{"1.2", "1.2-beta", false},
{"1.2+foo", "1.2+beta", true},
{"v1.2", "v1.2-beta", false},
{"v1.2+foo", "v1.2+beta", true},
{"v1.2.3.4", "v1.2.3.4", true},
{"v1.2.0.0", "v1.2", true},
{"v1.2.0.0.1", "v1.2", false},
{"v1.2", "v1.2.0.0", true},
{"v1.2", "v1.2.0.0.1", true},
{"v1.2.0.0", "v1.2.0.0.1", true},
{"v1.2.3.0", "v1.2.3.4", true},
{"1.7rc2", "1.7rc1", false},
{"1.7rc2", "1.7", true},
{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", false},
}

for _, tc := range cases {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Fatalf("err: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v1.LessThanOrEqual(v2)
expected := tc.expected
if actual != expected {
t.Fatalf(
"%s <= %s\nexpected: %t\nactual: %t",
tc.v1, tc.v2,
expected, actual)
}
}
}

0 comments on commit ac23dc3

Please sign in to comment.