-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample_test.go
264 lines (197 loc) · 5.55 KB
/
example_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2014 The Semver Package Authors. 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"
"blitznote.com/src/semver/v3"
)
func Example_version() {
v1 := semver.MustParse("1.2.3-beta")
v2 := semver.MustParse("2.0.0-alpha20140805.456-rc3+build1800")
fmt.Println(v1.Less(&v2))
// Output: true
}
func Example_range() {
v1 := semver.MustParse("1.2.3-beta")
r1, _ := semver.NewRange([]byte("~1.2"))
fmt.Println(r1.Contains(v1))
fmt.Println(r1.IsSatisfiedBy(v1)) // pre-releases don't satisfy
// Output:
// true
// false
}
func ExampleCompare() {
v1 := semver.MustParse("v1")
v2 := semver.MustParse("v2.0")
v3 := semver.MustParse("v3.0.0")
fmt.Println("Compare", v3, v2, "=", semver.Compare(&v3, &v2))
fmt.Println("Compare", v2, v2, "=", semver.Compare(&v2, &v2))
fmt.Println("Compare", v1, v2, "=", semver.Compare(&v1, &v2))
// Output:
// Compare 3.0.0 2.0.0 = 1
// Compare 2.0.0 2.0.0 = 0
// Compare 1.0.0 2.0.0 = -1
}
func ExampleRange_Contains_first() {
v := semver.MustParse("1.4.3")
r, _ := semver.NewRange([]byte("^1.2"))
fmt.Println(r.Contains(v))
// Output: true
}
func ExampleRange_Contains_second() {
v := semver.MustParse("1.4.3")
r, _ := semver.NewRange([]byte("1.2 <2.0.0"))
fmt.Println(r.Contains(v))
// Output: true
}
func ExampleRange_Contains_prerelases() {
v := semver.MustParse("1.4.3-beta")
r, _ := semver.NewRange([]byte("1.2 <2.0.0"))
fmt.Println(r.Contains(v))
// Output: true
}
func ExampleRange_GetLowerBoundary() {
r, _ := semver.NewRange([]byte("^1.2"))
fmt.Println(*r.GetLowerBoundary())
// Output:
// 1.2.0
}
func ExampleRange_GetUpperBoundary_first() {
r, _ := semver.NewRange([]byte("1.2 <2.0.0"))
fmt.Println(*r.GetUpperBoundary())
// Output:
// 2.0.0
}
func ExampleRange_GetUpperBoundary_second() {
r, _ := semver.NewRange([]byte("~1.2.3"))
fmt.Println(*r.GetUpperBoundary())
// Output:
// 1.3.0
}
func ExampleRange_IsSatisfiedBy_full() {
v := semver.MustParse("1.2.3")
r, _ := semver.NewRange([]byte("~1.2"))
fmt.Println(r.IsSatisfiedBy(v))
// Output: true
}
func ExampleRange_IsSatisfiedBy_prerelases() {
// Unlike with Contains, this won't select prereleases.
pre := semver.MustParse("1.2.3-beta")
r, _ := semver.NewRange([]byte("~1.2"))
fmt.Println(r.IsSatisfiedBy(pre))
// Output: false
}
func ExampleMustParse() {
v := semver.MustParse("v1.14")
fmt.Println(v)
// Output:
// 1.14.0
}
func ExampleNewVersion() {
for _, str := range []string{"v1.14", "6.0.2.1", "14b6"} {
v, err := semver.NewVersion([]byte(str))
fmt.Println(v, err)
}
// Output:
// 1.14.0 <nil>
// 6.0.2.1 <nil>
// 14.0.0 Given string does not resemble a Version
}
func ExampleVersion_Bytes_first() {
v := semver.MustParse("1.0")
fmt.Println(v.Bytes())
// Output:
// [49]
}
func ExampleVersion_Bytes_second() {
v := semver.MustParse("4.8")
fmt.Println(v.Bytes())
// Output:
// [52 46 56]
}
func ExampleVersion_Bytes_minimal() {
v := semver.MustParse("1.13beta")
fmt.Println("Bytes() =", string(v.Bytes()))
fmt.Println("String() =", v.String())
// Output:
// Bytes() = 1.13-beta
// String() = 1.13.0-beta
}
func ExampleVersion_IsAPreRelease() {
v, pre := semver.MustParse("1.12"), semver.MustParse("1.13beta")
fmt.Println(v, "is a pre-release:", v.IsAPreRelease())
fmt.Println(pre, "is a pre-release:", pre.IsAPreRelease())
// Output:
// 1.12.0 is a pre-release: false
// 1.13.0-beta is a pre-release: true
}
func ExampleVersion_Less() {
l, r := semver.MustParse("v2"), semver.MustParse("v3")
fmt.Println(l.Less(&r), ",", l, "is 'less' than", r)
l, r = semver.MustParse("v1+build7"), semver.MustParse("v1+build9")
fmt.Println(l.Less(&r), ",", l, "is 'less' than", r)
// Output:
// true , 2.0.0 is 'less' than 3.0.0
// true , 1.0.0+build7 is 'less' than 1.0.0+build9
}
func ExampleVersion_LimitedEqual_first() {
// The version prefix does, but the first pre-release type does not match.
pre := semver.MustParse("1.0.0-pre")
rc := semver.MustParse("1.0.0-rc")
fmt.Println(pre.LimitedEqual(rc))
// Output: false
}
func ExampleVersion_LimitedEqual_second() {
// The difference is beyond LimitedEqual's cutoff, so these "equal".
a := semver.MustParse("1.0.0-beta-pre3")
b := semver.MustParse("1.0.0-beta-pre5")
fmt.Println(a.LimitedEqual(b))
// Output: true
}
func ExampleVersion_LimitedEqual_third() {
regular := semver.MustParse("1.0.0")
patched := semver.MustParse("1.0.0-p1")
// A patched version supposedly does more, so is more than; and its unequal.
fmt.Println(patched.LimitedEqual(regular))
// This will work because the regular version is a subset is in its subset.
fmt.Println(regular.LimitedEqual(patched))
// Output:
// false
// true
}
func ExampleVersion_Major() {
v := semver.MustParse("v1.2.3")
fmt.Println(v.Major())
// Output: 1
}
func ExampleVersion_Minor() {
v := semver.MustParse("v1.2.3")
fmt.Println(v.Minor())
// Output: 2
}
func ExampleVersion_Patch() {
v := semver.MustParse("v1.2.3")
fmt.Println(v.Patch())
// Output: 3
}
func ExampleVersion_Scan() {
a, b, c := new(semver.Version), new(semver.Version), new(semver.Version)
errA := a.Scan("5.5.65")
errB := b.Scan(int64(12))
errC := c.Scan(-1)
fmt.Println(a, errA)
fmt.Println(b, errB)
fmt.Println(c, errC)
// Output:
// 5.5.65 <nil>
// 12.0.0 <nil>
// 0.0.0 Cannot read this type into a Version
}
func ExampleVersion_String() {
str := "v2.1"
v := semver.MustParse(str)
fmt.Println(str, "is", v.String(), "but as Bytes():", string(v.Bytes()))
// Output:
// v2.1 is 2.1.0 but as Bytes(): 2.1
}