Skip to content

Commit

Permalink
Implement Stringer in constraint
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Sep 5, 2023
1 parent beeed05 commit fa95966
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit fa95966

Please sign in to comment.