forked from cheggaaa/pb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
86 lines (75 loc) · 1.84 KB
/
format_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
package pb
import (
"fmt"
"strconv"
"testing"
"time"
)
func Test_DefaultsToInteger(t *testing.T) {
value := int64(1000)
expected := strconv.Itoa(int(value))
actual := Format(value).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
func Test_CanFormatAsInteger(t *testing.T) {
value := int64(1000)
expected := strconv.Itoa(int(value))
actual := Format(value).To(U_NO).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
func Test_CanFormatAsBytes(t *testing.T) {
inputs := []struct {
v int64
e string
}{
{v: 1000, e: "1000 B"},
{v: 1024, e: "1.00 KiB"},
{v: 3*MiB + 140*KiB, e: "3.14 MiB"},
{v: 2 * GiB, e: "2.00 GiB"},
{v: 2048 * GiB, e: "2.00 TiB"},
}
for _, input := range inputs {
actual := Format(input.v).To(U_BYTES).String()
if actual != input.e {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
}
}
}
func Test_CanFormatAsBytesDec(t *testing.T) {
inputs := []struct {
v int64
e string
}{
{v: 999, e: "999 B"},
{v: 1024, e: "1.02 KB"},
{v: 3*MB + 140*KB, e: "3.14 MB"},
{v: 2 * GB, e: "2.00 GB"},
{v: 2048 * GB, e: "2.05 TB"},
}
for _, input := range inputs {
actual := Format(input.v).To(U_BYTES_DEC).String()
if actual != input.e {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
}
}
}
func Test_CanFormatDuration(t *testing.T) {
value := 10 * time.Minute
expected := "10m0s"
actual := Format(int64(value)).To(U_DURATION).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
func Test_DefaultUnitsWidth(t *testing.T) {
value := 10
expected := " 10"
actual := Format(int64(value)).Width(7).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}