From 4d9ce169270990f9b434f8672d8247003afc4a5e Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Tue, 7 Feb 2023 20:04:39 +0000 Subject: [PATCH] Add GreaterThanOrEqual and LessThanOrEqual methods --- version.go | 10 +++++++ version_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/version.go b/version.go index 7c4bed3..d1b9c22 100644 --- a/version.go +++ b/version.go @@ -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. diff --git a/version_test.go b/version_test.go index ed82a53..687c8a9 100644 --- a/version_test.go +++ b/version_test.go @@ -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 @@ -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