-
Notifications
You must be signed in to change notification settings - Fork 1
/
version_test.go
127 lines (112 loc) · 3.46 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
package main
import (
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
var _ = Suite(&VersionSuite{})
type VersionSuite struct{}
var versionParseTests = []struct {
major int
minor int
patch int
dev bool
s string
}{
{-1, -1, -1, false, "v"},
{-1, -1, -1, false, "v-1"},
{-1, -1, -1, false, "v-deb"},
{-1, -1, -1, false, "v01"},
{-1, -1, -1, false, "v1.01"},
{-1, -1, -1, false, "a1"},
{-1, -1, -1, false, "v1a"},
{-1, -1, -1, false, "v1..2"},
{-1, -1, -1, false, "v1.2.3.4"},
{-1, -1, -1, false, "v1."},
{-1, -1, -1, false, "v1.2."},
{-1, -1, -1, false, "v1.2.3."},
{0, -1, -1, false, "v0"},
{0, -1, -1, true, "v0-unstable"},
{1, -1, -1, false, "v1"},
{1, -1, -1, true, "v1-unstable"},
{1, 2, -1, false, "v1.2"},
{1, 2, -1, true, "v1.2-unstable"},
{1, 2, 3, false, "v1.2.3"},
{1, 2, 3, true, "v1.2.3-unstable"},
{12, 34, 56, false, "v12.34.56"},
{12, 34, 56, true, "v12.34.56-unstable"},
}
func (s *VersionSuite) TestParse(c *C) {
for _, t := range versionParseTests {
got, ok := parseVersion(t.s)
if t.major == -1 {
if ok || got != InvalidVersion {
c.Fatalf("version %q is invalid but parsed as %#v", t.s, got)
}
} else {
want := Version{t.major, t.minor, t.patch, t.dev}
if got != want {
c.Fatalf("version %q must parse as %#v, got %#v", t.s, want, got)
}
if got.String() != t.s {
c.Fatalf("version %q got parsed as %#v and stringified as %q", t.s, got, got.String())
}
}
}
}
var versionLessTests = []struct {
oneMajor, oneMinor, onePatch int
oneUnstable bool
twoMajor, twoMinor, twoPatch int
twoUnstable, less bool
}{
{0, 0, 0, false, 0, 0, 0, false, false},
{1, 0, 0, false, 1, 0, 0, false, false},
{1, 0, 0, false, 1, 1, 0, false, true},
{1, 0, 0, false, 2, 0, 0, false, true},
{0, 1, 0, false, 0, 1, 0, false, false},
{0, 1, 0, false, 0, 1, 1, false, true},
{0, 0, 0, false, 0, 2, 0, false, true},
{0, 0, 1, false, 0, 0, 1, false, false},
{0, 0, 1, false, 0, 0, 2, false, true},
{0, 0, 0, false, 0, 0, 0, true, false},
{0, 0, 0, true, 0, 0, 0, false, true},
{0, 0, 1, true, 0, 0, 0, false, false},
}
func (s *VersionSuite) TestLess(c *C) {
for _, t := range versionLessTests {
one := Version{t.oneMajor, t.oneMinor, t.onePatch, t.oneUnstable}
two := Version{t.twoMajor, t.twoMinor, t.twoPatch, t.twoUnstable}
if one.Less(two) != t.less {
c.Fatalf("version %s < %s returned %v", one, two, !t.less)
}
}
}
var versionContainsTests = []struct {
oneMajor, oneMinor, onePatch int
oneUnstable bool
twoMajor, twoMinor, twoPatch int
twoUnstable, contains bool
}{
{12, 34, 56, false, 12, 34, 56, false, true},
{12, 34, 56, false, 12, 34, 78, false, false},
{12, 34, -1, false, 12, 34, 56, false, true},
{12, 34, -1, false, 12, 78, 56, false, false},
{12, -1, -1, false, 12, 34, 56, false, true},
{12, -1, -1, false, 78, 34, 56, false, false},
{12, -1, -1, true, 12, -1, -1, false, false},
{12, -1, -1, false, 12, -1, -1, true, false},
}
func (s *VersionSuite) TestContains(c *C) {
for _, t := range versionContainsTests {
one := Version{t.oneMajor, t.oneMinor, t.onePatch, t.oneUnstable}
two := Version{t.twoMajor, t.twoMinor, t.twoPatch, t.twoUnstable}
if one.Contains(two) != t.contains {
c.Fatalf("version %s.Contains(%s) returned %v", one, two, !t.contains)
}
}
}
func (s *VersionSuite) TestIsValid(c *C) {
c.Assert(InvalidVersion.IsValid(), Equals, false)
c.Assert(Version{0, 0, 0, false}.IsValid(), Equals, true)
}