-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
217 lines (175 loc) · 5.39 KB
/
version_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2021-2023 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package semver_test
import (
"fmt"
"testing"
"github.com/jbowes/semver"
)
func ExampleParse() {
v, err := semver.Parse("1.2.0-rc.0+happysunshine")
if err != nil {
fmt.Println("invalid semver!")
}
fmt.Println(v)
// Output:
// 1.2.0-rc.0+happysunshine
}
func ExampleVersion_Compare() {
v1, err := semver.Parse("1.2.0-rc.0+happysunshine")
if err != nil {
fmt.Println("invalid semver!")
}
v2, err := semver.Parse("1.4.0")
if err != nil {
fmt.Println("invalid semver!")
}
fmt.Println(v1.Compare(v2))
// Output:
// -1
}
// In accordance with the semver spec, build fields are ignored for determining
// version precedence.
func ExampleVersion_Compare_buildField() {
v1, err := semver.Parse("1.0.0+somebuild")
if err != nil {
fmt.Println("invalid semver!")
}
v2, err := semver.Parse("1.0.0+builds.are.ignored.for.comparison")
if err != nil {
fmt.Println("invalid semver!")
}
fmt.Println(v1.Compare(v2))
// Output:
// 0
}
func TestParse(t *testing.T) {
tcs := map[string]struct {
in string
valid bool
}{
"simple": {"1.0.0", true},
"multi digit": {"10.22.345", true},
"pre": {"1.0.0-alpha", true},
"pre (multi-part)": {"1.0.0-alpha.0.2", true},
"build": {"1.0.0+build", true},
"pre+build": {"1.0.0-alpha.1+build.data", true},
"pre+build (hyphens)": {"1.0.0-alp-ha.1+me-ta.data", true},
"invalid (leading zero)": {"1.0.02", false},
"invalid (many leading zeros)": {"01.02.03", false},
"invalid (double semver)": {"1.0.01.0.1", false},
"invalid (slash)": {"1.0.1-r\\c.0", false},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
out, err := semver.Parse(tc.in)
if (err == nil) != tc.valid {
t.Error("got unexpected error:", err)
}
if tc.valid {
outS := out.String()
if outS != tc.in {
t.Error("got unexpected output. got:", outS, "want:", tc.in)
}
}
})
}
}
func TestCompare(t *testing.T) {
tcs := map[string]struct {
v1 string
v2 string
out int
}{
"basic equality": {"1.2.3", "1.2.3", 0},
"less than (major)": {"1.2.3", "2.2.3", -1},
"less than (minor)": {"1.2.3", "1.3.3", -1},
"less than (patch)": {"1.2.3", "1.2.4", -1},
"build ignored": {"1.2.3+foo", "1.2.3", 0},
"build ignored (both)": {"1.2.3+foo", "1.2.3+bar", 0},
"pre equality": {"1.2.3-rc", "1.2.3-rc", 0},
"pre equality (multi part)": {"1.2.3-rc.0", "1.2.3-rc.0", 0},
"pre build ignored": {"1.2.3-rc.0+foo", "1.2.3-rc.0", 0},
"pre build ignored (both)": {"1.2.3-rc.0+foo", "1.2.3-rc.0+bar", 0},
"less than (same pre)": {"1.2.3-rc", "2.2.3-rc", -1},
"pre less than no pre": {"1.2.3-rc", "1.2.3", -1},
"lexicographic pre comparison": {"1.2.3-beta", "1.2.3-alpha", 1},
"lexicographic pre comparison (multi-part)": {"1.2.3-alpha.beta", "1.2.3-beta.beta", -1},
"lexicographic pre comparison (uneven multi-part)": {"1.2.3-alpha.beta", "1.2.3-beta", -1},
"more pre parts wins": {"1.2.3-alpha.first", "1.2.3-alpha", 1},
"pre digit comparison": {"1.2.3-rc.0", "1.2.3-rc.1", -1},
"pre digit comparison (multi digit)": {"1.2.3-rc.2", "1.2.3-rc.10", -1},
"pre digit comparison (multi part)": {"1.2.3-rc.0.0", "1.2.3-rc.0.1", -1},
"pre leading zeros aren't digits": {"1.2.3-rc.01", "1.2.3-rc.1", -1},
"pre comparison (mixed alpha and digit)": {"1.2.3-rc", "1.2.3-rc.2", -1},
// According to semver, `-` is a non-digit and so eg
// 3 is less than -2 for pre. This is not what masterminds/semver does.
"pre comparison (negatives aren't numeric)": {"1.2.3-rc.3", "1.2.3-rc.-2", 1},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
v1, err := semver.Parse(tc.v1)
if err != nil {
t.Error("couldn't parse v1:", err)
}
v2, err := semver.Parse(tc.v2)
if err != nil {
t.Error("couldn't parse v2:", err)
}
cmp := v1.Compare(v2)
if cmp != tc.out {
t.Errorf("comparison failed. expected: %d, got: %d", tc.out, cmp)
}
cmp = v2.Compare(v1)
if cmp != -1*tc.out {
t.Errorf("inverse comparison failed. expected: %d, got: %d", -1*tc.out, cmp)
}
})
}
}
func TestVersionString_nil(t *testing.T) {
var v *semver.Version
if v.String() != "" {
t.Error("nil version string was not empty")
}
}
func TestCompare_nil(t *testing.T) {
var nv *semver.Version
v, err := semver.Parse("0.0.0")
if err != nil {
t.Fatal("got error parsing test version")
}
if nv.Compare(v) != -1 {
t.Errorf("nil version not less than other version")
}
if v.Compare(nv) != 1 {
t.Errorf("other version not greater than nil version")
}
if nv.Compare(nv) != 0 {
t.Errorf("two nil versions are not equal")
}
}
func TestVersion_Prerelease(t *testing.T) {
mustParse := func(s string) *semver.Version {
v, _ := semver.Parse(s)
return v
}
tcs := map[string]struct {
in *semver.Version
out string
}{
"no prerelease": {mustParse("1.0.0"), ""},
"prerelease": {mustParse("1.0.0-alpha"), "alpha"},
"multi-part prerelease": {mustParse("1.0.0-rc.0"), "rc.0"},
"nil version": {nil, ""},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
out := tc.in.Prerelease()
if out != tc.out {
t.Error("bad prerelease. got:", out, "wanted:", tc.out)
}
})
}
}