-
Notifications
You must be signed in to change notification settings - Fork 55
/
versions_test.go
162 lines (151 loc) · 4.28 KB
/
versions_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package transformer
import (
"testing"
rpb "github.com/ampproject/amppackager/transformer/request"
)
func TestValidateRequest(t *testing.T) {
tcs := []struct {
desc string
rs []*rpb.VersionRange
expectedError bool
}{
{
desc: "nil",
rs: nil,
expectedError: false,
},
{
desc: "empty",
rs: []*rpb.VersionRange{},
expectedError: false,
},
{
desc: "malformed",
rs: []*rpb.VersionRange{{Max: 1, Min: 2}},
expectedError: true,
},
{
desc: "negative",
rs: []*rpb.VersionRange{{Max: 1, Min: -1}},
expectedError: true,
},
{
desc: "same",
rs: []*rpb.VersionRange{{Max: 1, Min: 1}},
expectedError: false,
},
{
desc: "overlapping",
rs: []*rpb.VersionRange{{Max: 2, Min: 1}, {Max: 1, Min: 1}},
expectedError: true,
},
{
desc: "valid",
rs: []*rpb.VersionRange{{Max: 3, Min: 2}, {Max: 1, Min: 1}},
expectedError: false,
},
{
desc: "out of order",
rs: []*rpb.VersionRange{{Max: 1, Min: 1}, {Max: 3, Min: 2}},
expectedError: true,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
err := validateRequest(tc.rs)
if tc.expectedError != (err != nil) {
t.Errorf("validateRequest(%+v) unexpected err = %v", tc.rs, err)
}
})
}
}
func TestSelectVersion(t *testing.T) {
tcs := []struct {
desc string
requested, supported []*rpb.VersionRange
expectedVersion int64
expectedError bool
}{
{
desc: "not supported",
requested: []*rpb.VersionRange{{Max: 4, Min: 1}},
supported: []*rpb.VersionRange{},
expectedError: true,
},
{
desc: "No request, highest version supported.",
requested: []*rpb.VersionRange{},
supported: []*rpb.VersionRange{{Max: 4, Min: 1}},
expectedVersion: 4,
},
{
desc: "No request (nil), highest version supported.",
requested: nil,
supported: []*rpb.VersionRange{{Max: 4, Min: 1}},
expectedVersion: 4,
},
{
desc: "One version, match.",
requested: []*rpb.VersionRange{{Max: 1, Min: 1}},
supported: []*rpb.VersionRange{{Max: 1, Min: 1}},
expectedVersion: 1,
},
{
desc: "One version, mismatch.",
requested: []*rpb.VersionRange{{Max: 1, Min: 1}},
supported: []*rpb.VersionRange{{Max: 2, Min: 2}},
expectedError: true,
},
{
desc: "Supported is a subset of requested.",
requested: []*rpb.VersionRange{{Max: 4, Min: 1}},
supported: []*rpb.VersionRange{{Max: 2, Min: 2}},
expectedVersion: 2,
},
{
desc: "Requested is a subset of supported.",
requested: []*rpb.VersionRange{{Max: 2, Min: 2}},
supported: []*rpb.VersionRange{{Max: 4, Min: 1}},
expectedVersion: 2,
},
{
desc: "Requested and supported abut.",
requested: []*rpb.VersionRange{{Max: 4, Min: 2}},
supported: []*rpb.VersionRange{{Max: 2, Min: 1}},
expectedVersion: 2,
},
{
desc: "Requested includes a hole in supported.",
requested: []*rpb.VersionRange{{Max: 2, Min: 1}},
supported: []*rpb.VersionRange{{Max: 4, Min: 3}, {Max: 1, Min: 1}},
expectedVersion: 1,
},
{
desc: "Requested requires a hole in supported.",
requested: []*rpb.VersionRange{{Max: 2, Min: 2}},
supported: []*rpb.VersionRange{{Max: 4, Min: 3}, {Max: 1, Min: 1}},
expectedError: true,
},
{
desc: "Overlap is between the second elements of both requested and supported.",
requested: []*rpb.VersionRange{{Max: 6, Min: 5}, {Max: 2, Min: 1}},
supported: []*rpb.VersionRange{{Max: 4, Min: 3}, {Max: 1, Min: 1}},
expectedVersion: 1,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
v, err := selectVersion(tc.requested, tc.supported)
t.Logf("selectVersion(%+v, %+v) = %d", tc.requested, tc.supported, v)
if tc.expectedError {
if err == nil {
t.Error("wanted, but didn't get err")
}
} else if err != nil {
t.Errorf("got unexpected err = %v", err)
} else if v != tc.expectedVersion {
t.Errorf("retval is unexpectedly %v", v)
}
})
}
}