forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
118 lines (110 loc) · 3.18 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewDefaultVersion(t *testing.T) {
v := NewDefaultVersion("avalanche", 1, 2, 3)
assert.NotNil(t, v)
assert.Equal(t, "avalanche/1.2.3", v.String())
assert.Equal(t, "avalanche", v.App())
assert.Equal(t, 1, v.Major())
assert.Equal(t, 2, v.Minor())
assert.Equal(t, 3, v.Patch())
assert.NoError(t, v.Compatible(v))
assert.False(t, v.Before(v))
}
func TestNewVersion(t *testing.T) {
v := NewVersion("avalanche", ":", ",", 1, 2, 3)
assert.NotNil(t, v)
assert.Equal(t, "avalanche:1,2,3", v.String())
assert.Equal(t, "avalanche", v.App())
assert.Equal(t, 1, v.Major())
assert.Equal(t, 2, v.Minor())
assert.Equal(t, 3, v.Patch())
assert.NoError(t, v.Compatible(v))
assert.False(t, v.Before(v))
}
func TestComparingVersions(t *testing.T) {
tests := []struct {
myVersion Version
peerVersion Version
compatible bool
before bool
}{
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 3),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 3),
compatible: true,
before: false,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 4),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 3),
compatible: true,
before: false,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 3),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 4),
compatible: true,
before: true,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 3, 3),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 3),
compatible: true,
before: false,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 3),
peerVersion: NewDefaultVersion("avalanche", 1, 3, 3),
compatible: true,
before: true,
},
{
myVersion: NewDefaultVersion("avalanche", 2, 2, 3),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 3),
compatible: false,
before: false,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 3),
peerVersion: NewDefaultVersion("avalanche", 2, 2, 3),
compatible: true,
before: true,
},
{
myVersion: NewDefaultVersion("avax", 1, 2, 4),
peerVersion: NewDefaultVersion("avalanche", 1, 2, 3),
compatible: false,
before: false,
},
{
myVersion: NewDefaultVersion("avalanche", 1, 2, 3),
peerVersion: NewDefaultVersion("avax", 1, 2, 3),
compatible: false,
before: false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s %s", test.myVersion, test.peerVersion), func(t *testing.T) {
err := test.myVersion.Compatible(test.peerVersion)
if test.compatible && err != nil {
t.Fatalf("Expected version to be compatible but returned: %s",
err)
} else if !test.compatible && err == nil {
t.Fatalf("Expected version to be incompatible but returned no error")
}
before := test.myVersion.Before(test.peerVersion)
if test.before && !before {
t.Fatalf("Expected version to be before the peer version but wasn't")
} else if !test.before && before {
t.Fatalf("Expected version not to be before the peer version but was")
}
})
}
}