-
Notifications
You must be signed in to change notification settings - Fork 17
/
size_bytes_test.go
109 lines (102 loc) · 1.98 KB
/
size_bytes_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
package baker
import (
"fmt"
"testing"
"github.com/rasky/toml"
)
func TestSizeBytes(t *testing.T) {
tests := []struct {
name string
tomlString string
want SizeBytes // want value in bytes
wantErr bool
}{
{
name: "positive int",
tomlString: `123456`,
want: 123456,
},
{
name: "positive float",
tomlString: `123.456`,
want: 123,
},
{
name: "string no unit",
tomlString: `"123456"`,
want: 123456,
},
{
name: "string IEC unit",
tomlString: `"123GiB"`,
want: 123 * 1024 * 1024 * 1024,
},
{
name: "string SI unit",
tomlString: `"123GB"`,
want: 123 * 1000 * 1000 * 1000,
},
// default value
{
name: "zero int",
tomlString: `0`,
want: 0,
},
{
name: "zero float",
tomlString: `0.0`,
want: 0,
},
{
name: "zero string",
tomlString: `"0"`,
want: 0,
},
{
name: "empty string",
tomlString: `""`,
want: 0,
},
// errors
{
name: "negative int",
tomlString: `-123456`,
wantErr: true,
},
{
name: "negative float",
tomlString: `-123.456`,
wantErr: true,
},
{
name: "overflow float",
tomlString: `100000000000000000000`, // 100000000000000000000
wantErr: true,
},
{
name: "unparsable string",
tomlString: `"some string"`,
wantErr: true,
},
{
name: "partially parsable string",
tomlString: `"123 some string"`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
val := struct{ Field SizeBytes }{}
_, err := toml.Decode(fmt.Sprintf("\nfield = %s", tt.tomlString), &val)
if (err != nil) != tt.wantErr {
t.Fatalf("got: %v, wantErr: %t", err, tt.wantErr)
}
if tt.wantErr {
return
}
if got := val.Field; got != SizeBytes(tt.want) {
t.Errorf("Field = SizeBytes(%v), want SizeBytes(%v)", got, tt.want)
}
})
}
}