cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.83.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/output/humanize_test.go

230lines · 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 input: time.Minute*3 + time.Second*45,
62 output: "3m45s",
63 },
64 {
65 input: time.Second*30 + time.Millisecond*250,
66 output: "30s250ms",
67 },
68 {
69 input: time.Hour*24*7 + time.Millisecond*1,
70 output: "1w1ms",
71 },
72 {
73 input: time.Millisecond,
74 output: "1ms",
75 },
76 {
77 input: time.Hour,
78 output: "1h",
79 },
80 {
81 input: time.Minute,
82 output: "1m",
83 },
84 {
85 input: time.Second,
86 output: "1s",
87 },
88 }
89
90 for _, tc := range testCases {
91 t.Run(tc.input.String(), func(t *testing.T) {
92 output := output.HumanizeDuration(tc.input)
93 require.Equal(t, tc.output, output)
94 })
95 }
96}
97
98func TestHumanizeBytes(t *testing.T) {
99 type testCaseT struct {
100 output string
101 input int
102 }
103
104 testCases := []testCaseT{
105 {
106 input: 0,
107 output: "0B",
108 },
109 {
110 input: 10,
111 output: "10B",
112 },
113 {
114 input: 1024,
115 output: "1.0KiB",
116 },
117 {
118 input: 100000,
119 output: "97.7KiB",
120 },
121 {
122 input: 1024 * 4096,
123 output: "4.0MiB",
124 },
125 {
126 input: 1024*1024*1024 + 5,
127 output: "1.0GiB",
128 },
129 {
130 input: 1024*1024*1024 + 500000000,
131 output: "1.5GiB",
132 },
133 {
134 input: 1024 * 1024 * 1024 * 1024,
135 output: "1.0TiB",
136 },
137 {
138 input: 1024 * 1024 * 1024 * 1024 * 1024,
139 output: "1.0PiB",
140 },
141 {
142 input: 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
143 output: "1.0EiB",
144 },
145 {
146 input: 512,
147 output: "512B",
148 },
149 {
150 input: 1536,
151 output: "1.5KiB",
152 },
153 }
154
155 for _, tc := range testCases {
156 t.Run(strconv.Itoa(tc.input), func(t *testing.T) {
157 output := output.HumanizeBytes(tc.input)
158 require.Equal(t, tc.output, output)
159 })
160 }
161}
162
163func TestMaybeColor(t *testing.T) {
164 type testCaseT struct {
165 name string
166 input string
167 output string
168 color output.Color
169 disabled bool
170 }
171
172 testCases := []testCaseT{
173 {
174 name: "color disabled",
175 color: output.Red,
176 disabled: true,
177 input: "test string",
178 output: "test string",
179 },
180 {
181 name: "color enabled with red",
182 color: output.Red,
183 disabled: false,
184 input: "error",
185 output: "\033[91merror\033[0m",
186 },
187 {
188 name: "color enabled with yellow",
189 color: output.Yellow,
190 disabled: false,
191 input: "warning",
192 output: "\033[93mwarning\033[0m",
193 },
194 {
195 name: "color enabled with blue",
196 color: output.Blue,
197 disabled: false,
198 input: "info",
199 output: "\033[94minfo\033[0m",
200 },
201 {
202 name: "color enabled with bold",
203 color: output.Bold,
204 disabled: false,
205 input: "bold text",
206 output: "\033[1mbold text\033[0m",
207 },
208 {
209 name: "color enabled with none",
210 color: output.None,
211 disabled: false,
212 input: "no color",
213 output: "\033[0mno color\033[0m",
214 },
215 {
216 name: "empty string with color",
217 color: output.Cyan,
218 disabled: false,
219 input: "",
220 output: "\033[96m\033[0m",
221 },
222 }
223
224 for _, tc := range testCases {
225 t.Run(tc.name, func(t *testing.T) {
226 result := output.MaybeColor(tc.color, tc.disabled, tc.input)
227 require.Equal(t, tc.output, result)
228 })
229 }
230}
231