Repository navigation
Expand file tree
/
Copy pathversion_test.go
More file actions
124 lines (119 loc) · 3.33 KB
/
Copy pathversion_test.go
File metadata and controls
124 lines (119 loc) · 3.33 KB
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
package gatekit
import (
"errors"
"testing"
)
func TestEffectiveVersion(t *testing.T) {
tests := []struct {
name string
specs Registry
window int // -1 means "use the default (omit the option)"
active Activation
wantMin Activation
wantMax Activation
}{
{
name: "default window W=1 floors above firstVersion",
specs: testRegistry(), // firstVersion 4, max 6
window: -1,
active: 6,
wantMin: 5, // max(4, 6-1)
wantMax: 6,
},
{
name: "wide window clamps to firstVersion",
specs: Registry{featureA: {Specs: VersionedSpecs{{ActiveFrom: 0}, {ActiveFrom: 6}}}},
window: 5,
active: 3,
wantMin: 1, // max(0, 6-5)
wantMax: 6,
},
{
name: "W=0 pins min to max",
specs: testRegistry(),
window: 0,
active: 6,
wantMin: 6,
wantMax: 6,
},
{
name: "empty registry is all zeros",
specs: Registry{},
window: -1,
active: 0,
wantMin: 0,
wantMax: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var opts []Option
if tt.window >= 0 {
opts = append(opts, WithSupportWindow(tt.window))
}
g, err := New(tt.specs, StaticVersion(tt.active), opts...)
if err != nil {
t.Fatalf("New() error: %v", err)
}
ev := g.EffectiveVersion()
if ev.Min != tt.wantMin || ev.Max != tt.wantMax || ev.Active != tt.active {
t.Errorf("EffectiveVersion() = %+v, want {Min:%d Max:%d Active:%d}",
ev, tt.wantMin, tt.wantMax, tt.active)
}
})
}
}
func TestCheck(t *testing.T) {
// testRegistry has firstVersion 4, max 6. Try both W=1 (min 5) and W=3
// (min max(4, 6-3)=4), to cover W=1 and W>1.
tests := []struct {
name string
window int
active Activation
wantErr bool
}{
{name: "W=1 active=max ok", window: 1, active: 6, wantErr: false},
{name: "W=1 active=min ok", window: 1, active: 5, wantErr: false},
{name: "W=1 active below min", window: 1, active: 4, wantErr: true},
{name: "W=1 active above max", window: 1, active: 7, wantErr: true},
{name: "W=3 active=min ok", window: 3, active: 4, wantErr: false},
{name: "W=3 active below min", window: 3, active: 3, wantErr: true},
{name: "W=3 active above max", window: 3, active: 7, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g, err := New(testRegistry(), StaticVersion(tt.active), WithSupportWindow(tt.window))
if err != nil {
t.Fatalf("New() error: %v", err)
}
err = g.Check()
if (err != nil) != tt.wantErr {
t.Fatalf("Check() err = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
var vre *VersionRangeError
if !errors.As(err, &vre) {
t.Fatalf("Check() err = %v, want *VersionRangeError", err)
}
if vre.Active != tt.active {
t.Errorf("VersionRangeError.Active = %d, want %d", vre.Active, tt.active)
}
}
})
}
}
func TestCheckDefaultWindow(t *testing.T) {
// No WithSupportWindow option => default W=1 => min 5 for testRegistry.
g, err := New(testRegistry(), StaticVersion(4))
if err != nil {
t.Fatalf("New() error: %v", err)
}
if err := g.Check(); err == nil {
t.Errorf("Check() at active=4 with default W=1 = nil, want error")
}
}
func TestWithSupportWindowRejectsNegative(t *testing.T) {
if _, err := New(testRegistry(), StaticVersion(6), WithSupportWindow(-1)); err == nil {
t.Errorf("New() with negative window = nil error, want error")
}
}