cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.75.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/output/humanize_test.go

117lines · modecode

1package output_test
2
3import (
4 "strconv"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/require"
9
10 "github.com/cloudflare/pint/internal/output"
11)
12
13func TestHumanizeDuration(t *testing.T) {
14 type testCaseT struct {
15 output string
16 input time.Duration
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 output := output.HumanizeDuration(tc.input)
65 require.Equal(t, tc.output, output)
66 })
67 }
68}
69
70func TestHumanizeBytes(t *testing.T) {
71 type testCaseT struct {
72 output string
73 input int
74 }
75
76 testCases := []testCaseT{
77 {
78 input: 0,
79 output: "0B",
80 },
81 {
82 input: 10,
83 output: "10B",
84 },
85 {
86 input: 1024,
87 output: "1.0KiB",
88 },
89 {
90 input: 100000,
91 output: "97.7KiB",
92 },
93 {
94 input: 1024 * 4096,
95 output: "4.0MiB",
96 },
97 {
98 input: 1024*1024*1024 + 5,
99 output: "1.0GiB",
100 },
101 {
102 input: 1024*1024*1024 + 500000000,
103 output: "1.5GiB",
104 },
105 {
106 input: 1024 * 1024 * 1024 * 1024,
107 output: "1.0TiB",
108 },
109 }
110
111 for _, tc := range testCases {
112 t.Run(strconv.Itoa(tc.input), func(t *testing.T) {
113 output := output.HumanizeBytes(tc.input)
114 require.Equal(t, tc.output, output)
115 })
116 }
117}
118