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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
package layout
import "testing"
func TestValue_Constructors(t *testing.T) {
type tc struct {
value Value
isAuto bool
unit Unit
amount float64
}
tests := map[string]tc{
"Auto": {
value: Auto(),
isAuto: true,
unit: UnitAuto,
amount: 0,
},
"Fixed": {
value: Fixed(100),
isAuto: false,
unit: UnitFixed,
amount: 100,
},
"Percent": {
value: Percent(50),
isAuto: false,
unit: UnitPercent,
amount: 50,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := tt.value.IsAuto(); got != tt.isAuto {
t.Errorf("IsAuto() = %v, want %v", got, tt.isAuto)
}
if tt.value.Unit != tt.unit {
t.Errorf("Unit = %v, want %v", tt.value.Unit, tt.unit)
}
if tt.value.Amount != tt.amount {
t.Errorf("Amount = %v, want %v", tt.value.Amount, tt.amount)
}
})
}
}
func TestValue_Resolve_Fixed(t *testing.T) {
type tc struct {
value Value
available int
fallback int
expected int
}
tests := map[string]tc{
"fixed ignores available": {
value: Fixed(50),
available: 100,
fallback: 0,
expected: 50,
},
"fixed ignores fallback": {
value: Fixed(50),
available: 100,
fallback: 999,
expected: 50,
},
"fixed zero": {
value: Fixed(0),
available: 100,
fallback: 50,
expected: 0,
},
"fixed negative": {
value: Fixed(-10),
available: 100,
fallback: 50,
expected: -10,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.value.Resolve(tt.available, tt.fallback)
if got != tt.expected {
t.Errorf("Resolve(%d, %d) = %d, want %d",
tt.available, tt.fallback, got, tt.expected)
}
})
}
}
func TestValue_Resolve_Percent(t *testing.T) {
type tc struct {
value Value
available int
fallback int
expected int
}
tests := map[string]tc{
"50 percent of 100": {
value: Percent(50),
available: 100,
fallback: 0,
expected: 50,
},
"100 percent of 80": {
value: Percent(100),
available: 80,
fallback: 0,
expected: 80,
},
"25 percent of 200": {
value: Percent(25),
available: 200,
fallback: 0,
expected: 50,
},
"0 percent": {
value: Percent(0),
available: 100,
fallback: 50,
expected: 0,
},
"percent of zero available": {
value: Percent(50),
available: 0,
fallback: 50,
expected: 0,
},
"fractional percent rounds down": {
value: Percent(33.33),
available: 100,
fallback: 0,
expected: 33,
},
"percent over 100": {
value: Percent(150),
available: 100,
fallback: 0,
expected: 150,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.value.Resolve(tt.available, tt.fallback)
if got != tt.expected {
t.Errorf("Resolve(%d, %d) = %d, want %d",
tt.available, tt.fallback, got, tt.expected)
}
})
}
}
func TestValue_Resolve_Auto(t *testing.T) {
type tc struct {
value Value
available int
fallback int
expected int
}
tests := map[string]tc{
"auto returns fallback": {
value: Auto(),
available: 100,
fallback: 50,
expected: 50,
},
"auto with zero fallback": {
value: Auto(),
available: 100,
fallback: 0,
expected: 0,
},
"auto ignores available": {
value: Auto(),
available: 999,
fallback: 42,
expected: 42,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.value.Resolve(tt.available, tt.fallback)
if got != tt.expected {
t.Errorf("Resolve(%d, %d) = %d, want %d",
tt.available, tt.fallback, got, tt.expected)
}
})
}
}
func TestValue_IsAuto(t *testing.T) {
type tc struct {
value Value
expected bool
}
tests := map[string]tc{
"Auto is auto": {
value: Auto(),
expected: true,
},
"Fixed is not auto": {
value: Fixed(10),
expected: false,
},
"Percent is not auto": {
value: Percent(50),
expected: false,
},
"zero value is auto": {
value: Value{},
expected: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := tt.value.IsAuto(); got != tt.expected {
t.Errorf("IsAuto() = %v, want %v", got, tt.expected)
}
})
}
}
|