-
Notifications
You must be signed in to change notification settings - Fork 677
/
version_test.go
106 lines (102 loc) · 3.33 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
package service
import (
"reflect"
"testing"
)
func Test_versionCompare(t *testing.T) {
type args struct {
v1 []int
v2 []int
}
tests := []struct {
name string
args args
want int
wantErr bool
}{
{"segment-mismatch-1", args{[]int{0, 0, 0, 0}, []int{0, 0, 0}}, 0, true},
{"segment-mismatch-2", args{[]int{0, 0, 0}, []int{0, 0, 0, 0}}, 0, true},
{"equal-to-1", args{[]int{0, 0, 0}, []int{0, 0, 0}}, 0, false},
{"equal-to-2", args{[]int{0, 0, 1}, []int{0, 0, 1}}, 0, false},
{"equal-to-3", args{[]int{0, 1, 0}, []int{0, 1, 0}}, 0, false},
{"equal-to-4", args{[]int{1, 0, 0}, []int{1, 0, 0}}, 0, false},
{"equal-to-5", args{[]int{2, 2, 2}, []int{2, 2, 2}}, 0, false},
{"less-than-1", args{[]int{0, 0, 0}, []int{0, 0, 1}}, -1, false},
{"less-than-2", args{[]int{0, 0, 1}, []int{0, 1, 0}}, -1, false},
{"less-than-3", args{[]int{0, 1, 0}, []int{1, 0, 0}}, -1, false},
{"less-than-4", args{[]int{0, 1, 0}, []int{1, 0, 0}}, -1, false},
{"less-than-5", args{[]int{0, 8, 0}, []int{1, 5, 0}}, -1, false},
{"greater-than-1", args{[]int{0, 0, 1}, []int{0, 0, 0}}, 1, false},
{"greater-than-2", args{[]int{0, 1, 0}, []int{0, 0, 1}}, 1, false},
{"greater-than-3", args{[]int{1, 0, 0}, []int{0, 1, 0}}, 1, false},
{"greater-than-4", args{[]int{1, 0, 0}, []int{0, 1, 0}}, 1, false},
{"greater-than-5", args{[]int{1, 5, 0}, []int{0, 8, 0}}, 1, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := versionCompare(tt.args.v1, tt.args.v2)
if (err != nil) != tt.wantErr {
t.Errorf("versionCompare() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("versionCompare() = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseVersion(t *testing.T) {
type args struct {
v string
}
tests := []struct {
name string
args args
want []int
}{
{"sanity-check", args{"0.0.0"}, []int{0, 0, 0}},
{"should-fail", args{"0.zero.0"}, nil},
{"should-fail-no-semver", args{"0.0.0-test+1"}, nil},
{"double-digits", args{"5.200.1"}, []int{5, 200, 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseVersion(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseVersion() = %v, want %v", got, tt.want)
}
})
}
}
func Test_versionAtMost(t *testing.T) {
type args struct {
version []int
max []int
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{"segment-mismatch-1", args{[]int{0, 0, 0, 0}, []int{0, 0, 0}}, false, true},
{"segment-mismatch-2", args{[]int{0, 0, 0}, []int{0, 0, 0, 0}}, false, true},
{"test-1", args{[]int{0, 0, 0}, []int{0, 0, 1}}, true, false},
{"test-2", args{[]int{0, 1, 0}, []int{0, 1, 0}}, true, false},
{"test-3", args{[]int{1, 0, 0}, []int{1, 0, 0}}, true, false},
{"negative-test-1", args{[]int{0, 0, 1}, []int{0, 0, 0}}, false, false},
{"negative-test-2", args{[]int{0, 1, 0}, []int{0, 0, 1}}, false, false},
{"negative-test-3", args{[]int{1, 0, 0}, []int{0, 1, 0}}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := versionAtMost(tt.args.version, tt.args.max)
if (err != nil) != tt.wantErr {
t.Errorf("versionAtMost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("versionAtMost() = %v, want %v", got, tt.want)
}
})
}
}