mirror of
https://github.com/restic/restic.git
synced 2025-08-21 00:23:11 +00:00
cmd, ui: Move size parsing code and make it more robust
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package ui
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestFormatBytes(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
@@ -36,3 +40,47 @@ func TestFormatPercent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBytes(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
in string
|
||||
expected int64
|
||||
}{
|
||||
{"1024", 1024},
|
||||
{"1024b", 1024},
|
||||
{"1024B", 1024},
|
||||
{"1k", 1024},
|
||||
{"100k", 102400},
|
||||
{"100K", 102400},
|
||||
{"10M", 10485760},
|
||||
{"100m", 104857600},
|
||||
{"20G", 21474836480},
|
||||
{"10g", 10737418240},
|
||||
{"2T", 2199023255552},
|
||||
{"2t", 2199023255552},
|
||||
{"9223372036854775807", 1<<63 - 1},
|
||||
} {
|
||||
actual, err := ParseBytes(tt.in)
|
||||
test.OK(t, err)
|
||||
test.Equals(t, tt.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBytesInvalid(t *testing.T) {
|
||||
for _, s := range []string{
|
||||
"",
|
||||
" ",
|
||||
"foobar",
|
||||
"zzz",
|
||||
"18446744073709551615", // 1<<64-1.
|
||||
"9223372036854775807k", // 1<<63-1 kiB.
|
||||
"9999999999999M",
|
||||
"99999999999999999999",
|
||||
} {
|
||||
v, err := ParseBytes(s)
|
||||
if err == nil {
|
||||
t.Errorf("wanted error for invalid value %q, got nil", s)
|
||||
}
|
||||
test.Equals(t, int64(0), v)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user