-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstraints_test.go
153 lines (128 loc) · 2.94 KB
/
constraints_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package semver
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAnd(t *testing.T) {
t.Parallel()
t.Run("all true", func(t *testing.T) {
t.Parallel()
and := and{
&positiveConstraint{}, &positiveConstraint{}, &positiveConstraint{},
}
assert.True(t, and.Check(Version{}))
assert.True(t, and.Contains(&Range{}))
})
t.Run("one false", func(t *testing.T) {
t.Parallel()
and := and{
&positiveConstraint{}, &negativeConstraint{}, &positiveConstraint{},
}
assert.False(t, and.Check(Version{}))
assert.False(t, and.Contains(&Range{}))
})
}
func TestAnd_String(t *testing.T) {
t.Parallel()
and := and{
&Range{
Min: MustNewVersion("1.1.0"),
Max: MustNewVersion("1.2.5"),
},
&Range{
Min: MustNewVersion("2.1.0"),
Max: MustNewVersion("2.2.5"),
},
}
assert.Equal(t, "1.1.0 - 1.2.5 && 2.1.0 - 2.2.5", and.String())
}
func TestOr(t *testing.T) {
t.Parallel()
t.Run("one true", func(t *testing.T) {
t.Parallel()
or := or{
&negativeConstraint{}, &negativeConstraint{}, &positiveConstraint{},
}
assert.True(t, or.Check(Version{}))
assert.True(t, or.Contains(&Range{}))
})
t.Run("all false", func(t *testing.T) {
t.Parallel()
or := or{
&negativeConstraint{}, &negativeConstraint{}, &negativeConstraint{},
}
assert.False(t, or.Check(Version{}))
assert.False(t, or.Contains(&Range{}))
})
}
func TestOr_String(t *testing.T) {
t.Parallel()
or := or{
&Range{
Min: MustNewVersion("1.1.0"),
Max: MustNewVersion("1.2.5"),
},
&Range{
Min: MustNewVersion("2.1.0"),
Max: MustNewVersion("2.2.5"),
},
}
assert.Equal(t, "1.1.0 - 1.2.5 || 2.1.0 - 2.2.5", or.String())
}
func TestNot(t *testing.T) {
t.Parallel()
t.Run("true becomes false", func(t *testing.T) {
t.Parallel()
not := not{
Range{},
}
assert.False(t, not.Check(Version{}))
assert.False(t, not.Contains(&Range{}))
})
t.Run("false becomes true", func(t *testing.T) {
t.Parallel()
not := not{
Range{
Min: MustNewVersion("1.0.0"),
Max: MustNewVersion("2.0.0"),
},
}
assert.True(t, not.Check(MustNewVersion("0.2.0")))
assert.True(t, not.Contains(&Range{
Min: MustNewVersion("3.0.0"),
Max: MustNewVersion("4.0.0"),
}))
})
}
func TestNot_String(t *testing.T) {
t.Parallel()
not := not{
Range{
Min: MustNewVersion("1.1.2"),
Max: MustNewVersion("1.1.2"),
},
}
assert.Equal(t, "!=1.1.2", not.String())
}
// constraint that is always true.
type positiveConstraint struct{}
func (c *positiveConstraint) Check(_ Version) bool {
return true
}
func (c *positiveConstraint) Contains(_ Constraint) bool {
return true
}
func (c *positiveConstraint) String() string {
return "positiveStub"
}
// constraint that is always false.
type negativeConstraint struct{}
func (c *negativeConstraint) Check(_ Version) bool {
return false
}
func (c *negativeConstraint) Contains(_ Constraint) bool {
return false
}
func (c *negativeConstraint) String() string {
return "negativeStub"
}