cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.15.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/output/humanize_test.go

119lines · modecode

1package output_test
2
3import (
4 "strconv"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9
10 "github.com/cloudflare/pint/internal/output"
11)
12
13func TestHumanizeDuration(t *testing.T) {
14 type testCaseT struct {
15 input time.Duration
16 output string
17 }
18
19 testCases := []testCaseT{
20 {
21 input: 0,
22 output: "0",
23 },
24 {
25 input: time.Microsecond * 3,
26 output: "0",
27 },
28 {
29 input: time.Millisecond * 542,
30 output: "542ms",
31 },
32 {
33 input: time.Second * 9,
34 output: "9s",
35 },
36 {
37 input: time.Minute * 59,
38 output: "59m",
39 },
40 {
41 input: time.Hour * 23,
42 output: "23h",
43 },
44 {
45 input: time.Hour * 24 * 6,
46 output: "6d",
47 },
48 {
49 input: time.Hour * 24 * 7 * 14,
50 output: "14w",
51 },
52 {
53 input: (time.Hour * (24*7*14 + 24*6 + 3)),
54 output: "14w6d3h",
55 },
56 {
57 input: (time.Hour * (24*7*14 + 24*6 + 3)) + time.Minute*33 + time.Second*3 + time.Millisecond + 999 + time.Microsecond*5,
58 output: "14w6d3h33m3s1ms",
59 },
60 }
61
62 for _, tc := range testCases {
63 t.Run(tc.input.String(), func(t *testing.T) {
64 assert := assert.New(t)
65 output := output.HumanizeDuration(tc.input)
66 assert.Equal(tc.output, output)
67 })
68 }
69}
70
71func TestHumanizeBytes(t *testing.T) {
72 type testCaseT struct {
73 input int
74 output string
75 }
76
77 testCases := []testCaseT{
78 {
79 input: 0,
80 output: "0B",
81 },
82 {
83 input: 10,
84 output: "10B",
85 },
86 {
87 input: 1024,
88 output: "1.0KiB",
89 },
90 {
91 input: 100000,
92 output: "97.7KiB",
93 },
94 {
95 input: 1024 * 4096,
96 output: "4.0MiB",
97 },
98 {
99 input: 1024*1024*1024 + 5,
100 output: "1.0GiB",
101 },
102 {
103 input: 1024*1024*1024 + 500000000,
104 output: "1.5GiB",
105 },
106 {
107 input: 1024 * 1024 * 1024 * 1024,
108 output: "1.0TiB",
109 },
110 }
111
112 for _, tc := range testCases {
113 t.Run(strconv.Itoa(tc.input), func(t *testing.T) {
114 assert := assert.New(t)
115 output := output.HumanizeBytes(tc.input)
116 assert.Equal(tc.output, output)
117 })
118 }
119}
120