From e2b282d452e0f4fbe93895ccc86be494d30c5343 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Tue, 5 Sep 2023 11:37:28 +0300 Subject: [PATCH] Implement Stringer in constraint (#6) Signed-off-by: Kimmo Lehto --- constraint.go | 9 +++++++++ constraint_test.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/constraint.go b/constraint.go index 81317b2..3821540 100644 --- a/constraint.go +++ b/constraint.go @@ -47,6 +47,15 @@ func MustConstraint(cs string) Constraints { return c } +// String returns the constraint as a string. +func (cs Constraints) String() string { + s := make([]string, len(cs)) + for i, c := range cs { + s[i] = c.String() + } + return strings.Join(s, ", ") +} + // Check returns true if the given version satisfies all of the constraints. func (cs Constraints) Check(v *Version) bool { for _, c := range cs { diff --git a/constraint_test.go b/constraint_test.go index e4f1b77..55a0528 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -151,3 +151,11 @@ func TestCheckString(t *testing.T) { assert.False(t, c.CheckString("0.9.9")) assert.False(t, c.CheckString("x")) } + +func TestString(t *testing.T) { + c, err := NewConstraint(">= 1.0.0, < 2.0.0") + assert.NoError(t, err) + + assert.Equal(t, ">= 1.0.0, < 2.0.0", c.String()) +} +