Skip to content

Commit a3a14e5

Browse files
committed
Added Not constraint
1 parent 391e101 commit a3a14e5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

constraints.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,21 @@ func (and *And) String() string {
246246
res += ")"
247247
return res
248248
}
249+
250+
// Not match if Operand does not match and viceversa
251+
type Not struct {
252+
Operand Constraint
253+
}
254+
255+
// Match returns ture if v does NOT satisfies the condition
256+
func (not *Not) Match(v *Version) bool {
257+
return !not.Operand.Match(v)
258+
}
259+
260+
func (not *Not) String() string {
261+
op := not.Operand.String()
262+
if op[0] != '(' {
263+
return "!(" + op + ")"
264+
}
265+
return "!" + op
266+
}

constraints_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func TestConstraints(t *testing.T) {
6969
require.False(t, or.Match(v("2.0.0")))
7070
require.True(t, or.Match(v("2.1.0")))
7171
require.Equal(t, "(>2.0.0 || <=1.0.0)", or.String())
72+
73+
notOr := &Not{or}
74+
require.False(t, notOr.Match(v("0.9.0")))
75+
require.False(t, notOr.Match(v("1.0.0")))
76+
require.True(t, notOr.Match(v("1.3.0")))
77+
require.True(t, notOr.Match(v("2.0.0")))
78+
require.False(t, notOr.Match(v("2.1.0")))
79+
require.Equal(t, "!(>2.0.0 || <=1.0.0)", notOr.String())
7280
}
7381

7482
func TestConstraintsParser(t *testing.T) {

0 commit comments

Comments
 (0)