Skip to content

Commit 53dbf70

Browse files
committed
Add Prerelease function onto Constraint
1 parent b30d5d1 commit 53dbf70

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

constraint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ func (c *Constraint) Check(v *Version) bool {
163163
return c.f(v, c.check)
164164
}
165165

166+
// Prerelease returns true if the version underlying this constraint
167+
// contains a prerelease field.
168+
func (c *Constraint) Prerelease() bool {
169+
return len(c.check.Prerelease()) > 0
170+
}
171+
166172
func (c *Constraint) String() string {
167173
return c.original
168174
}

constraint_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,34 @@ func TestConstraintCheck(t *testing.T) {
100100
}
101101
}
102102

103+
func TestConstraintPrerelease(t *testing.T) {
104+
cases := []struct {
105+
constraint string
106+
prerelease bool
107+
}{
108+
{"= 1.0", false},
109+
{"= 1.0-beta", true},
110+
{"~> 2.1.0", false},
111+
{"~> 2.1.0-dev", true},
112+
{"> 2.0", false},
113+
{">= 2.1.0-a", true},
114+
}
115+
116+
for _, tc := range cases {
117+
c, err := parseSingle(tc.constraint)
118+
if err != nil {
119+
t.Fatalf("err: %s", err)
120+
}
121+
122+
actual := c.Prerelease()
123+
expected := tc.prerelease
124+
if actual != expected {
125+
t.Fatalf("Constraint: %s\nExpected: %#v",
126+
tc.constraint, expected)
127+
}
128+
}
129+
}
130+
103131
func TestConstraintEqual(t *testing.T) {
104132
cases := []struct {
105133
leftConstraint string

0 commit comments

Comments
 (0)