Skip to content

Commit

Permalink
Merge pull request #238 from grosser/grosser/lte
Browse files Browse the repository at this point in the history
add >= and <=
  • Loading branch information
mattfarina committed Jun 4, 2024
2 parents 347f541 + be924f5 commit 9ec6f29
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,21 @@ func (v *Version) LessThan(o *Version) bool {
return v.Compare(o) < 0
}

// LessThanEqual tests if one version is less or equal than another one.
func (v *Version) LessThanEqual(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
}

// GreaterThanEqual tests if one version is greater or equal than another one.
func (v *Version) GreaterThanEqual(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
66 changes: 66 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,39 @@ func TestLessThan(t *testing.T) {
}
}

func TestLessThanEqual(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},
{"1.5.1", "1.5.1", 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.LessThanEqual(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 @@ -341,6 +374,39 @@ func TestGreaterThan(t *testing.T) {
}
}

func TestGreaterThanEqual(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},
{"1.5.1", "1.5.1", 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.GreaterThanEqual(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 *Version
Expand Down

0 comments on commit 9ec6f29

Please sign in to comment.