Skip to content

Commit be924f5

Browse files
committed
add >= and <=
1 parent 347f541 commit be924f5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

version.go

+10
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,21 @@ func (v *Version) LessThan(o *Version) bool {
381381
return v.Compare(o) < 0
382382
}
383383

384+
// LessThanEqual tests if one version is less or equal than another one.
385+
func (v *Version) LessThanEqual(o *Version) bool {
386+
return v.Compare(o) <= 0
387+
}
388+
384389
// GreaterThan tests if one version is greater than another one.
385390
func (v *Version) GreaterThan(o *Version) bool {
386391
return v.Compare(o) > 0
387392
}
388393

394+
// GreaterThanEqual tests if one version is greater or equal than another one.
395+
func (v *Version) GreaterThanEqual(o *Version) bool {
396+
return v.Compare(o) >= 0
397+
}
398+
389399
// Equal tests if two versions are equal to each other.
390400
// Note, versions can be equal with different metadata since metadata
391401
// is not considered part of the comparable version.

version_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,39 @@ func TestLessThan(t *testing.T) {
303303
}
304304
}
305305

306+
func TestLessThanEqual(t *testing.T) {
307+
tests := []struct {
308+
v1 string
309+
v2 string
310+
expected bool
311+
}{
312+
{"1.2.3", "1.5.1", true},
313+
{"2.2.3", "1.5.1", false},
314+
{"1.5.1", "1.5.1", true},
315+
}
316+
317+
for _, tc := range tests {
318+
v1, err := NewVersion(tc.v1)
319+
if err != nil {
320+
t.Errorf("Error parsing version: %s", err)
321+
}
322+
323+
v2, err := NewVersion(tc.v2)
324+
if err != nil {
325+
t.Errorf("Error parsing version: %s", err)
326+
}
327+
328+
a := v1.LessThanEqual(v2)
329+
e := tc.expected
330+
if a != e {
331+
t.Errorf(
332+
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
333+
tc.v1, tc.v2, e, a,
334+
)
335+
}
336+
}
337+
}
338+
306339
func TestGreaterThan(t *testing.T) {
307340
tests := []struct {
308341
v1 string
@@ -341,6 +374,39 @@ func TestGreaterThan(t *testing.T) {
341374
}
342375
}
343376

377+
func TestGreaterThanEqual(t *testing.T) {
378+
tests := []struct {
379+
v1 string
380+
v2 string
381+
expected bool
382+
}{
383+
{"1.2.3", "1.5.1", false},
384+
{"2.2.3", "1.5.1", true},
385+
{"1.5.1", "1.5.1", true},
386+
}
387+
388+
for _, tc := range tests {
389+
v1, err := NewVersion(tc.v1)
390+
if err != nil {
391+
t.Errorf("Error parsing version: %s", err)
392+
}
393+
394+
v2, err := NewVersion(tc.v2)
395+
if err != nil {
396+
t.Errorf("Error parsing version: %s", err)
397+
}
398+
399+
a := v1.GreaterThanEqual(v2)
400+
e := tc.expected
401+
if a != e {
402+
t.Errorf(
403+
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
404+
tc.v1, tc.v2, e, a,
405+
)
406+
}
407+
}
408+
}
409+
344410
func TestEqual(t *testing.T) {
345411
tests := []struct {
346412
v1 *Version

0 commit comments

Comments
 (0)