-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
size_test.go
128 lines (116 loc) · 3.13 KB
/
size_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
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package quota_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/gitpod-io/gitpod/ws-daemon/pkg/quota"
)
func TestParseSize(t *testing.T) {
tests := []struct {
Input string
Expected quota.Size
Err error
}{
{"42", 42, nil},
{"42k", 42 * quota.Kilobyte, nil},
{"42m", 42 * quota.Megabyte, nil},
{"42g", 42 * quota.Gigabyte, nil},
{"42t", 42 * quota.Terabyte, nil},
// This test should fail but doesn't because match somehow matches the regexp
// {"-42m", 0, quota.ErrInvalidSize},
{"abc", 0, quota.ErrInvalidSize},
{"99999999999999999999999999999999999999999999999999999999999999999999999999999999", 0, quota.ErrInvalidSize},
}
for _, test := range tests {
t.Run(test.Input, func(t *testing.T) {
act, err := quota.ParseSize(test.Input)
if err != test.Err {
t.Errorf("unexepected error %v: expected %v", err, test.Err)
return
}
if act != test.Expected {
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
}
})
}
}
func TestStringSize(t *testing.T) {
tests := []struct {
Input quota.Size
Expected string
}{
{42, "42"},
{42 * quota.Kilobyte, "42k"},
{42 * quota.Megabyte, "42m"},
{42 * quota.Gigabyte, "42g"},
{42 * quota.Terabyte, "42t"},
{0 * quota.Terabyte, "0"},
{0, "0"},
}
for _, test := range tests {
t.Run(test.Expected, func(t *testing.T) {
act := test.Input.String()
if act != test.Expected {
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
}
})
t.Run(fmt.Sprintf("Parse((%s).String())", test.Expected), func(t *testing.T) {
act, err := quota.ParseSize(test.Input.String())
if err != nil {
t.Errorf("unexepected error: %v", err)
return
}
if act != test.Input {
t.Errorf("unexpected result %v: expected %v", act, test.Input)
}
})
}
}
func TestMarshalUnmarshal(t *testing.T) {
tests := []struct {
Input quota.Size `json:"input"`
Expected string `json:"expected"`
}{
{42, "\"42\""},
{42 * quota.Kilobyte, "\"42k\""},
{42 * quota.Megabyte, "\"42m\""},
{42 * quota.Gigabyte, "\"42g\""},
{42 * quota.Terabyte, "\"42t\""},
{0 * quota.Terabyte, "\"0\""},
{0, "\"0\""},
{0, "\"\""},
}
for _, test := range tests {
t.Run(fmt.Sprintf("json.marshal(%v)", test.Input), func(t *testing.T) {
if test.Expected == `""` {
return
}
out, err := json.Marshal(test.Input)
if err != nil {
t.Errorf("unexepected error: %v", err)
return
}
act := string(out)
if act != test.Expected {
t.Errorf("unexpected result %v: expected %v", act, test.Expected)
}
})
t.Run(fmt.Sprintf("json.unmarshal(%s)", test.Expected), func(t *testing.T) {
var out struct {
Q quota.Size `json:"q"`
}
err := json.Unmarshal([]byte(fmt.Sprintf(`{"q":%s}`, test.Expected)), &out)
if err != nil {
t.Errorf("unexepected error: %v", err)
return
}
act := out.Q
if act != test.Input {
t.Errorf("unexpected result %v: expected %v", act, test.Input)
}
})
}
}