-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
229 lines (212 loc) · 10.6 KB
/
parser_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
218
219
220
221
222
223
224
225
226
227
228
229
//
// Copyright 2018-2023 Cristian Maglie. 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
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestParser(t *testing.T) {
MustParse("").CompareTo(MustParse("0+aaa"))
valid := func(in, normalized, expectedDump string) {
v, err := Parse(in)
require.NoError(t, err, "parsing '%s'", in)
require.Equal(t, in, v.String(), "printing of '%s'", in)
require.Equal(t, normalized, string(v.NormalizedString()), "normalized printing of '%s'", in)
dump := fmt.Sprintf("%v,%v,%v,%v,%v,%v", v.raw, v.majorString(), v.minorString(), v.patchString(), v.prereleaseString(), v.buildString())
require.Equal(t, expectedDump, dump, "fields of parsed '%s'", in)
fmt.Printf("%s -> %s\n", in, v.String())
v.Normalize()
require.Equal(t, normalized, v.String(), "normalization of '%s'", in)
vn, err := Parse(normalized)
require.NoError(t, err)
dump = fmt.Sprintf("%v,%v,%v,%v,%v,%v", v.raw, v.majorString(), v.minorString(), v.patchString(), v.prereleaseString(), v.buildString())
dumpNormalized := fmt.Sprintf("%v,%v,%v,%v,%v,%v", vn.raw, vn.majorString(), vn.minorString(), vn.patchString(), vn.prereleaseString(), vn.buildString())
require.Equal(t, dumpNormalized, dump)
}
invalid := func(in string) {
v, err := Parse(in)
require.Error(t, err, "parsing '%s'", in)
require.Nil(t, v, "parsed '%s'", in)
fmt.Printf("%s -> %s\n", in, err)
}
t.Run("NoMajorOrSingleMajorVariants", func(t *testing.T) {
valid("", "0.0.0", ",,,,,")
invalid("0.0.0.0")
invalid("a")
invalid(".")
invalid("-ab")
invalid("+ab")
valid("0", "0.0.0", "0,0,,,,")
valid("0.0.0", "0.0.0", "0.0.0,0,0,0,,")
valid("1", "1.0.0", "1,1,,,,")
valid("1.0.0", "1.0.0", "1.0.0,1,0,0,,")
valid("14", "14.0.0", "14,14,,,,")
valid("123456789123456789123456789", "123456789123456789123456789.0.0", "123456789123456789123456789,123456789123456789123456789,,,,")
invalid("12ab")
invalid("01")
invalid("0ab")
invalid(".1.1")
invalid("1-")
valid("1-0", "1.0.0-0", "1-0,1,,,0,")
valid("1-pre", "1.0.0-pre", "1-pre,1,,,pre,")
valid("1-pre.a", "1.0.0-pre.a", "1-pre.a,1,,,pre.a,")
valid("1-pre.a.0", "1.0.0-pre.a.0", "1-pre.a.0,1,,,pre.a.0,")
valid("1-pre.0.a", "1.0.0-pre.0.a", "1-pre.0.a,1,,,pre.0.a,")
valid("1-pre.a.10", "1.0.0-pre.a.10", "1-pre.a.10,1,,,pre.a.10,")
invalid("1-pre.a.01")
invalid("1-pre.a..1")
invalid("1-pre.a.01.1")
invalid("1-pre.a.01*.1")
valid("1+build3", "1.0.0+build3", "1+build3,1,,,,build3")
invalid("1+build3+build2")
valid("1+build3.123.001", "1.0.0+build3.123.001", "1+build3.123.001,1,,,,build3.123.001")
invalid("1+build3.123..001")
invalid("1+build3.123*.001")
valid("1-0+build3", "1.0.0-0+build3", "1-0+build3,1,,,0,build3")
valid("1-pre+build3", "1.0.0-pre+build3", "1-pre+build3,1,,,pre,build3")
valid("1-pre.a+build3", "1.0.0-pre.a+build3", "1-pre.a+build3,1,,,pre.a,build3")
valid("1-pre.a.10+build3", "1.0.0-pre.a.10+build3", "1-pre.a.10+build3,1,,,pre.a.10,build3")
invalid("1-pre.a.01+build3")
invalid("1-pre.a..1+build3")
invalid("1-pre.a.01.1+build3")
invalid("1-pre.a.01*.1+build3")
valid("1-0+build3.123.001", "1.0.0-0+build3.123.001", "1-0+build3.123.001,1,,,0,build3.123.001")
valid("1-pre+build3.123.001", "1.0.0-pre+build3.123.001", "1-pre+build3.123.001,1,,,pre,build3.123.001")
valid("1-pre.a+build3.123.001", "1.0.0-pre.a+build3.123.001", "1-pre.a+build3.123.001,1,,,pre.a,build3.123.001")
valid("1-pre.a.0+build3.123.001", "1.0.0-pre.a.0+build3.123.001", "1-pre.a.0+build3.123.001,1,,,pre.a.0,build3.123.001")
valid("1-pre.0.a+build3.123.001", "1.0.0-pre.0.a+build3.123.001", "1-pre.0.a+build3.123.001,1,,,pre.0.a,build3.123.001")
valid("1-pre.a.10+build3.123.001", "1.0.0-pre.a.10+build3.123.001", "1-pre.a.10+build3.123.001,1,,,pre.a.10,build3.123.001")
invalid("1-pre.a.+build3.123.001")
invalid("1-pre.a.01+build3.123.001")
invalid("1-pre.a.01*+build3.123.001")
})
t.Run("NoMinorOrSingleMinorVariants", func(t *testing.T) {
invalid("1.")
invalid("1.a")
invalid("1..2")
valid("1.2", "1.2.0", "1.2,1,2,,,")
valid("1.0", "1.0.0", "1.0,1,0,,,")
invalid("1.02")
invalid("1.0ab")
invalid("1.12ab")
valid("1.123456789123456789123456789", "1.123456789123456789123456789.0", "1.123456789123456789123456789,1,123456789123456789123456789,,,")
invalid("1.2-")
valid("1.2-0", "1.2.0-0", "1.2-0,1,2,,0,")
valid("1.2-pre", "1.2.0-pre", "1.2-pre,1,2,,pre,")
valid("1.2-pre.a", "1.2.0-pre.a", "1.2-pre.a,1,2,,pre.a,")
valid("1.2-pre.a.0", "1.2.0-pre.a.0", "1.2-pre.a.0,1,2,,pre.a.0,")
valid("1.2-pre.0.a", "1.2.0-pre.0.a", "1.2-pre.0.a,1,2,,pre.0.a,")
valid("1.2-pre.a.10", "1.2.0-pre.a.10", "1.2-pre.a.10,1,2,,pre.a.10,")
invalid("1.2-pre.a.01")
invalid("1.2-pre.a..1")
invalid("1.2-pre.a.01.1")
invalid("1.2-pre.a.01*.1")
valid("1.2+build3", "1.2.0+build3", "1.2+build3,1,2,,,build3")
valid("1.2-0+build3", "1.2.0-0+build3", "1.2-0+build3,1,2,,0,build3")
invalid("1.2+build3+build2")
valid("1.2+build3.123.001", "1.2.0+build3.123.001", "1.2+build3.123.001,1,2,,,build3.123.001")
invalid("1.2+build3.123..001")
invalid("1.2+build3.123*.001")
valid("1.2-pre+build3", "1.2.0-pre+build3", "1.2-pre+build3,1,2,,pre,build3")
valid("1.2-pre.a.0+build3", "1.2.0-pre.a.0+build3", "1.2-pre.a.0+build3,1,2,,pre.a.0,build3")
valid("1.2-pre.0.a+build3", "1.2.0-pre.0.a+build3", "1.2-pre.0.a+build3,1,2,,pre.0.a,build3")
valid("1.2-pre.a.10+build3", "1.2.0-pre.a.10+build3", "1.2-pre.a.10+build3,1,2,,pre.a.10,build3")
valid("1.2-pre.a+build3", "1.2.0-pre.a+build3", "1.2-pre.a+build3,1,2,,pre.a,build3")
valid("1.2-pre.a.10+build3", "1.2.0-pre.a.10+build3", "1.2-pre.a.10+build3,1,2,,pre.a.10,build3")
invalid("1.2-pre.a.01+build3")
invalid("1.2-pre.a..1+build3")
invalid("1.2-pre.a.01.1+build3")
invalid("1.2-pre.a.01*.1+build3")
valid("1.2-0+build3.123.001", "1.2.0-0+build3.123.001", "1.2-0+build3.123.001,1,2,,0,build3.123.001")
valid("1.2-pre+build3.123.001", "1.2.0-pre+build3.123.001", "1.2-pre+build3.123.001,1,2,,pre,build3.123.001")
valid("1.2-pre.a+build3.123.001", "1.2.0-pre.a+build3.123.001", "1.2-pre.a+build3.123.001,1,2,,pre.a,build3.123.001")
valid("1.2-pre.a.0+build3.123.001", "1.2.0-pre.a.0+build3.123.001", "1.2-pre.a.0+build3.123.001,1,2,,pre.a.0,build3.123.001")
valid("1.2-pre.0.a+build3.123.001", "1.2.0-pre.0.a+build3.123.001", "1.2-pre.0.a+build3.123.001,1,2,,pre.0.a,build3.123.001")
valid("1.2-pre.a.10+build3.123.001", "1.2.0-pre.a.10+build3.123.001", "1.2-pre.a.10+build3.123.001,1,2,,pre.a.10,build3.123.001")
valid("1.2-pre.a.10+build3.123.001", "1.2.0-pre.a.10+build3.123.001", "1.2-pre.a.10+build3.123.001,1,2,,pre.a.10,build3.123.001")
invalid("1.2-pre.a.+build3.123.001")
invalid("1.2-pre.a.01+build3.123.001")
invalid("1.2-pre.a.01*+build3.123.001")
})
t.Run("FullVariants", func(t *testing.T) {
invalid("1.2.a")
invalid("1.2.")
valid("1.2.3", "1.2.3", "1.2.3,1,2,3,,")
valid("1.2.0", "1.2.0", "1.2.0,1,2,0,,")
invalid("1.2.03")
invalid("1.2.0ab")
invalid("1.2.34ab")
valid("1.2.123456789123456789123456789", "1.2.123456789123456789123456789", "1.2.123456789123456789123456789,1,2,123456789123456789123456789,,")
invalid("1.2.3-")
valid("1.2.3-0", "1.2.3-0", "1.2.3-0,1,2,3,0,")
valid("1.2.3-pre", "1.2.3-pre", "1.2.3-pre,1,2,3,pre,")
valid("1.2.3-pre.a", "1.2.3-pre.a", "1.2.3-pre.a,1,2,3,pre.a,")
valid("1.2.3-pre.a.0", "1.2.3-pre.a.0", "1.2.3-pre.a.0,1,2,3,pre.a.0,")
valid("1.2.3-pre.0.a", "1.2.3-pre.0.a", "1.2.3-pre.0.a,1,2,3,pre.0.a,")
valid("1.2.3-pre.a.10", "1.2.3-pre.a.10", "1.2.3-pre.a.10,1,2,3,pre.a.10,")
valid("1.2.3-pre.a.10", "1.2.3-pre.a.10", "1.2.3-pre.a.10,1,2,3,pre.a.10,")
invalid("1.2.3-pre.a.01")
invalid("1.2.3-pre.a..1")
invalid("1.2.3-pre.a.01.1")
invalid("1.2.3-pre.a.01*.1")
valid("1.2.3+build3", "1.2.3+build3", "1.2.3+build3,1,2,3,,build3")
invalid("1.2.3+build3+build2")
valid("1.2.3+build3.123.001", "1.2.3+build3.123.001", "1.2.3+build3.123.001,1,2,3,,build3.123.001")
invalid("1.2.3+build3.123..001")
invalid("1.2.3+build3.123*.001")
valid("1.2.3-0+build3", "1.2.3-0+build3", "1.2.3-0+build3,1,2,3,0,build3")
valid("1.2.3-pre+build3", "1.2.3-pre+build3", "1.2.3-pre+build3,1,2,3,pre,build3")
valid("1.2.3-pre.a+build3", "1.2.3-pre.a+build3", "1.2.3-pre.a+build3,1,2,3,pre.a,build3")
valid("1.2.3-pre.a.0+build3", "1.2.3-pre.a.0+build3", "1.2.3-pre.a.0+build3,1,2,3,pre.a.0,build3")
valid("1.2.3-pre.0.a+build3", "1.2.3-pre.0.a+build3", "1.2.3-pre.0.a+build3,1,2,3,pre.0.a,build3")
valid("1.2.3-pre.a.10+build3", "1.2.3-pre.a.10+build3", "1.2.3-pre.a.10+build3,1,2,3,pre.a.10,build3")
valid("1.2.3-pre.a.10+build3", "1.2.3-pre.a.10+build3", "1.2.3-pre.a.10+build3,1,2,3,pre.a.10,build3")
invalid("1.2.3-pre.a.01+build3")
invalid("1.2.3-pre.a..1+build3")
invalid("1.2.3-pre.a.01.1+build3")
invalid("1.2.3-pre.a.01*.1+build3")
valid("1.2.3-0+build3.123.001", "1.2.3-0+build3.123.001", "1.2.3-0+build3.123.001,1,2,3,0,build3.123.001")
valid("1.2.3-pre+build3.123.001", "1.2.3-pre+build3.123.001", "1.2.3-pre+build3.123.001,1,2,3,pre,build3.123.001")
valid("1.2.3-pre.a+build3.123.001", "1.2.3-pre.a+build3.123.001", "1.2.3-pre.a+build3.123.001,1,2,3,pre.a,build3.123.001")
valid("1.2.3-pre.a.10+build3.123.001", "1.2.3-pre.a.10+build3.123.001", "1.2.3-pre.a.10+build3.123.001,1,2,3,pre.a.10,build3.123.001")
invalid("1.2.3-pre.a.+build3.123.001")
invalid("1.2.3-pre.a.01+build3.123.001")
invalid("1.2.3-pre.a.01*+build3.123.001")
invalid("1.2.3.4")
invalid("1.2.3.")
})
t.Run("AbsurdlyWeirdVersions", func(t *testing.T) {
valid("1.2.3-pre.a-10.20.c-30", "1.2.3-pre.a-10.20.c-30", "1.2.3-pre.a-10.20.c-30,1,2,3,pre.a-10.20.c-30,")
valid("1.2.3--1-.23.1", "1.2.3--1-.23.1", "1.2.3--1-.23.1,1,2,3,-1-.23.1,")
})
}
func TestNilVersionStringOutput(t *testing.T) {
var nilVersion *Version
require.Equal(t, "", nilVersion.String())
require.Equal(t, "", string(nilVersion.NormalizedString()))
}
func TestParseRelaxed(t *testing.T) {
bad := ParseRelaxed("bad")
require.Nil(t, bad.version)
require.Equal(t, []byte("bad"), bad.customversion)
require.Equal(t, "bad", bad.String())
good := ParseRelaxed("1.2.3-pre.a.10+build3.123.001")
require.Nil(t, good.customversion)
require.Equal(t, "1.2.3-pre.a.10+build3.123.001", good.version.String())
require.Equal(t, "1.2.3-pre.a.10+build3.123.001", good.String())
}
func ExampleParseRelaxed() {
WarnInvalidVersionWhenParsingRelaxed = true
ParseRelaxed("bad")
WarnInvalidVersionWhenParsingRelaxed = false
// Output:
// WARNING invalid semver version bad: no major version found
}
func TestMustParse(t *testing.T) {
require.NotPanics(t, func() { MustParse("1.2.3") })
require.Panics(t, func() { MustParse("bad") })
}