package bytesize_test import ( "encoding/json" "math" "testing" "gitoa.ru/go-4devs/bytesize" ) var marshalTextTests = []struct { in bytesize.Size expect string }{ {bytesize.Size(0), `"0B"`}, {bytesize.Size(100), `"100B"`}, {bytesize.Size(1024), `"1.024kB"`}, {bytesize.Size(1024 * 1024), `"1.048576MB"`}, {bytesize.Size(1024 * 1024 * 1024), `"1.073741824GB"`}, {bytesize.Size(1024 * 1024 * 1024 * 1024), `"1.099511627776TB"`}, {bytesize.Size(math.MaxInt64), `"9223.372036854775807PB"`}, {bytesize.Size(1000), `"1kB"`}, {bytesize.Size(1000 * 1000), `"1MB"`}, {bytesize.Size(1000 * 1000 * 1000), `"1GB"`}, {bytesize.Size(1000 * 1000 * 1000 * 1000), `"1TB"`}, {bytesize.Size(1000 * 1000 * 1000 * 1000 * 1000), `"1PB"`}, } func TestMarshalText(t *testing.T) { t.Parallel() for _, tc := range marshalTextTests { data, err := json.Marshal(tc.in) if err != nil { t.Errorf("json.Marshal(%q) = _, err:%q", tc.in, err) } else if string(data) != tc.expect { t.Errorf("json.Marshal(%q) = %q, data does not equals %q", tc.in, data, tc.expect) } } } func TestUnmarshalText(t *testing.T) { t.Parallel() for _, tc := range marshalTextTests { var data bytesize.Size err := json.Unmarshal([]byte(tc.expect), &data) if err != nil { t.Errorf("json.Unmarshal(%q) = _, err:%q", tc.in, err) } else if data != tc.in { t.Errorf("json.Unmarshal(%q) = %q, data does not equals %q", tc.in, data, tc.expect) } } }