ParseByteSize accepts "-1" or "-1B" as ByteSizeNoLimit, and String gives it back as "-1". A setting for which no limit is unsafe can reject it.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package util
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestByteSizeString(t *testing.T) {
|
|
tests := []struct {
|
|
b ByteSize
|
|
want string
|
|
}{
|
|
{0, "0B"},
|
|
{1500, "1500B"},
|
|
{1024, "1KiB"},
|
|
{1000, "1KB"},
|
|
{32768, "32KiB"},
|
|
{1000000, "1MB"},
|
|
{1024000, "1000KiB"},
|
|
{3 << 30, "3GiB"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := tt.b.String(); got != tt.want {
|
|
t.Errorf("ByteSize(%d).String() = %q; want %q", int64(tt.b), got, tt.want)
|
|
}
|
|
if back, err := ParseByteSize(tt.want); err != nil || back != tt.b {
|
|
t.Errorf("ParseByteSize(%q) = %d, %v; want %d", tt.want, back, err, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// -1 is ByteSizeNoLimit, parsed and formatted as "-1"; no other size may be
|
|
// negative.
|
|
func TestByteSizeNoLimit(t *testing.T) {
|
|
for _, text := range []string{"-1", "-1B", "-1b", " -1 "} {
|
|
size, err := ParseByteSize(text)
|
|
if err != nil || size != ByteSizeNoLimit {
|
|
t.Errorf("ParseByteSize(%q) = %d, %v; want ByteSizeNoLimit", text, size, err)
|
|
}
|
|
}
|
|
if got := ByteSizeNoLimit.String(); got != "-1" {
|
|
t.Errorf("ByteSizeNoLimit.String() = %q; want -1", got)
|
|
}
|
|
for _, text := range []string{"-2", "-1KB", "-0.5", "-1KiB", "-100"} {
|
|
size, err := ParseByteSize(text)
|
|
if err == nil || !strings.Contains(err.Error(), "only -1 (no limit)") {
|
|
t.Errorf("ParseByteSize(%q) = %d, %v; want a negative size error", text, size, err)
|
|
}
|
|
}
|
|
}
|