Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GreaterThanOrEqual and LessThanOrEqual methods #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions version.go
Expand Up @@ -381,11 +381,21 @@ func (v *Version) LessThan(o *Version) bool {
return v.Compare(o) < 0
}

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

// GreaterThan tests if one version is greater than another one.
func (v *Version) GreaterThan(o *Version) bool {
return v.Compare(o) > 0
}

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

// Equal tests if two versions are equal to each other.
// Note, versions can be equal with different metadata since metadata
// is not considered part of the comparable version.
Expand Down
71 changes: 71 additions & 0 deletions version_test.go
Expand Up @@ -296,6 +296,39 @@ func TestLessThan(t *testing.T) {
}
}

func TestLessThanOrEqual(t *testing.T) {
tests := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.5.1", true},
{"2.2.3", "1.5.1", false},
{"3.2-beta", "3.2-beta", true},
}

for _, tc := range tests {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

a := v1.LessThanOrEqual(v2)
e := tc.expected
if a != e {
t.Errorf(
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
tc.v1, tc.v2, e, a,
)
}
}
}

func TestGreaterThan(t *testing.T) {
tests := []struct {
v1 string
Expand Down Expand Up @@ -334,6 +367,44 @@ func TestGreaterThan(t *testing.T) {
}
}

func TestGreaterThanOrEqual(t *testing.T) {
tests := []struct {
v1 string
v2 string
expected bool
}{
{"1.2.3", "1.5.1", false},
{"2.2.3", "1.5.1", true},
{"3.2-beta", "3.2-beta", true},
{"3.2.0-beta.1", "3.2.0-beta.5", false},
{"3.2-beta.4", "3.2-beta.2", true},
{"7.43.0-SNAPSHOT.99", "7.43.0-SNAPSHOT.103", false},
{"7.43.0-SNAPSHOT.FOO", "7.43.0-SNAPSHOT.103", true},
{"7.43.0-SNAPSHOT.99", "7.43.0-SNAPSHOT.BAR", false},
}

for _, tc := range tests {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

a := v1.GreaterThanOrEqual(v2)
e := tc.expected
if a != e {
t.Errorf(
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
tc.v1, tc.v2, e, a,
)
}
}
}

func TestEqual(t *testing.T) {
tests := []struct {
v1 string
Expand Down