forked from snyk/vervet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
108 lines (101 loc) · 2.55 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
package vervet_test
import (
"testing"
qt "github.com/frankban/quicktest"
. "github.com/snyk/vervet"
)
func TestParseVersion(t *testing.T) {
c := qt.New(t)
tests := []struct {
vs string
d string
stab Stability
err string
}{{
vs: "2021-01-01",
d: "2021-01-01",
stab: StabilityGA,
}, {
vs: "2021-02-02~beta",
d: "2021-02-02",
stab: StabilityBeta,
}, {
vs: "2021-03-03~experimental",
d: "2021-03-03",
stab: StabilityExperimental,
}, {
vs: "unknown",
err: `invalid version "unknown"`,
}}
for i := range tests {
c.Logf("parse version %q", tests[i].vs)
v, err := ParseVersion(tests[i].vs)
if tests[i].err != "" {
c.Assert(err, qt.ErrorMatches, tests[i].err)
} else {
c.Assert(v.Date.Format("2006-01-02"), qt.Equals, tests[i].d)
c.Assert(v.Stability, qt.Equals, tests[i].stab)
}
}
}
func mustParseVersion(s string) *Version {
v, err := ParseVersion(s)
if err != nil {
panic(err)
}
return v
}
func TestVersionOrder(t *testing.T) {
c := qt.New(t)
tests := []struct {
l, r string
cmp int
}{{
// Compare some date versions
l: "2020-12-31", r: "2021-01-01", cmp: -1,
}, {
l: "2021-01-01", r: "2020-12-31", cmp: 1,
}, {
l: "2021-01-01", r: "2021-01-01", cmp: 0,
}, {
l: "9999-12-31", r: "9999-12-31~beta", cmp: -1,
}, {
// Compare date versions and special tags
l: "9999-12-31~beta", r: "9999-12-31", cmp: 1,
}, {
l: "9999-12-31", r: "9999-12-31~experimental", cmp: -1,
}, {
l: "9999-12-31~experimental", r: "9999-12-31", cmp: 1,
// Compare special tags
}, {
l: "2021-08-01~beta", r: "2021-08-01~experimental", cmp: -1,
}, {
l: "2021-08-01~experimental", r: "2021-08-01~beta", cmp: 1,
}, {
l: "2021-08-01~beta", r: "2021-08-01~beta", cmp: 0,
}, {
l: "2021-08-01~experimental", r: "2021-08-01~experimental", cmp: 0,
}, {
l: "2021-08-01~wip", r: "2021-08-01~experimental", cmp: 1,
}}
for i := range tests {
c.Logf("test %d %#v", i, tests[i])
lv, err := ParseVersion(tests[i].l)
c.Assert(err, qt.IsNil)
rv, err := ParseVersion(tests[i].r)
c.Assert(err, qt.IsNil)
c.Assert(lv.Compare(rv), qt.Equals, tests[i].cmp)
}
}
func TestVersionDateStrings(t *testing.T) {
c := qt.New(t)
c.Assert(VersionDateStrings([]*Version{
mustParseVersion("2021-06-01~wip"),
mustParseVersion("2021-06-01~beta"),
mustParseVersion("2021-06-10~beta"),
mustParseVersion("2021-06-10"),
mustParseVersion("2021-07-12~wip"),
mustParseVersion("2021-07-12~experimental"),
mustParseVersion("2021-07-12~beta"),
}), qt.ContentEquals, []string{"2021-06-01", "2021-06-10", "2021-07-12"})
}