-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples_test.go
91 lines (73 loc) · 2.43 KB
/
examples_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
package semver_test
import (
"fmt"
"sort"
"pkg.package-operator.run/semver"
)
func ExampleMustNewVersion() {
v := semver.MustNewVersion("1.2.4-alpha.0+meta")
fmt.Println(v.Major, v.Minor, v.Patch, v.PreRelease, v.BuildMetadata)
// Output: 1 2 4 alpha.0 [meta]
}
func ExampleNewVersion() {
v, err := semver.NewVersion("1.2.4-alpha.0+meta")
if err != nil {
panic(err)
}
fmt.Println(v.Major, v.Minor, v.Patch, v.PreRelease, v.BuildMetadata)
// Output: 1 2 4 alpha.0 [meta]
}
func ExampleNewConstraint_version() {
constraint := "1.0.0 - 2.0.0"
c, err := semver.NewConstraint(constraint)
if err != nil {
panic(err)
}
notContained := semver.MustNewVersion("3.0.0")
contained := semver.MustNewVersion("1.2.0")
fmt.Printf("%s is contained in range %q: %v\n", contained.String(), constraint, c.Check(contained))
fmt.Printf("%s is contained in range %q: %v\n", notContained.String(), constraint, c.Check(notContained))
// Output:
// 1.2.0 is contained in range "1.0.0 - 2.0.0": true
// 3.0.0 is contained in range "1.0.0 - 2.0.0": false
}
func ExampleNewConstraint_range() {
constraint := "1.0.0 - 2.0.0"
c, err := semver.NewConstraint(constraint)
if err != nil {
panic(err)
}
notContained := semver.MustNewConstraint("2.0.0 - 3.0.0")
contained := semver.MustNewConstraint("1.0.0 - 1.4.0")
fmt.Printf("1.0.0 - 1.4.0 is contained in range %q: %v\n", constraint, c.Contains(contained))
fmt.Printf("2.0.0 - 3.0.0 is contained in range %q: %v\n", constraint, c.Contains(notContained))
// Output:
// 1.0.0 - 1.4.0 is contained in range "1.0.0 - 2.0.0": true
// 2.0.0 - 3.0.0 is contained in range "1.0.0 - 2.0.0": false
}
func ExampleAscending() {
versions := []semver.Version{
semver.MustNewVersion("1.2.4"),
semver.MustNewVersion("1.2.3"),
semver.MustNewVersion("1.0.0"),
semver.MustNewVersion("1.3.0"),
semver.MustNewVersion("2.0.0"),
semver.MustNewVersion("0.4.2"),
}
sort.Sort(semver.Ascending(versions))
fmt.Println(semver.VersionList(versions).String())
// Output: 0.4.2, 1.0.0, 1.2.3, 1.2.4, 1.3.0, 2.0.0
}
func ExampleDescending() {
versions := []semver.Version{
semver.MustNewVersion("1.2.4"),
semver.MustNewVersion("1.2.3"),
semver.MustNewVersion("1.0.0"),
semver.MustNewVersion("1.3.0"),
semver.MustNewVersion("2.0.0"),
semver.MustNewVersion("0.4.2"),
}
sort.Sort(semver.Descending(versions))
fmt.Println(semver.VersionList(versions).String())
// Output: 2.0.0, 1.3.0, 1.2.4, 1.2.3, 1.0.0, 0.4.2
}