This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatting_test.go
157 lines (132 loc) · 4.12 KB
/
formatting_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"testing"
"time"
)
func TestGetDurationAbbreviationFromUnit(t *testing.T) {
t.Run("Returns the expected abbreviation for hours", func(t *testing.T) {
expected := "h"
actual := getDurationAbbreviationFromUnit("Hour")
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the expected abbreviation for minutes", func(t *testing.T) {
expected := "m"
actual := getDurationAbbreviationFromUnit("Minute")
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}
func TestGetRoundedFloat(t *testing.T) {
t.Run("Returns rounded float to tenths place", func(t *testing.T) {
expected := 2.0
actual := getRoundedFloat(1.984, 1)
if actual != expected {
t.Errorf("received %.2f; want %f", actual, expected)
}
})
t.Run("Returns rounded float to hundredths place", func(t *testing.T) {
expected := 1.98
actual := getRoundedFloat(1.984, 2)
if actual != expected {
t.Errorf("received %.2f; want %f", actual, expected)
}
})
}
func TestGetFormattedNumber(t *testing.T) {
t.Run("Returns a string without trailing zeros", func(t *testing.T) {
expected := "2"
actual := getFormattedNumber(2.000)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns a string with the only required trailing zeros", func(t *testing.T) {
expected := "2.02"
actual := getFormattedNumber(2.0200)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}
func TestGetFormattedDuration(t *testing.T) {
t.Run("Returns the correctly formatted duration when not rounding", func(t *testing.T) {
expected := "2.02"
actual := getFormattedDuration(2.0240, 2)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correctly formatted duration when rounding", func(t *testing.T) {
expected := "2.03"
actual := getFormattedDuration(2.0290, 2)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}
func TestGetFormattedHour(t *testing.T) {
t.Run("Returns the correctly formatted duration when not rounding", func(t *testing.T) {
expected := "2.02"
actual, _ := getFormattedHour(2.0240)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correctly formatted duration when rounding", func(t *testing.T) {
expected := "2.03"
actual, _ := getFormattedHour(2.0250)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correct unit when not rounding", func(t *testing.T) {
expected := "Minute"
_, actual := getFormattedHour(0.490)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correct unit when rounding from minutes to hours", func(t *testing.T) {
expected := "Hour"
_, actual := getFormattedHour(0.9950)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}
func TestGetFormattedPercentageFromFloat(t *testing.T) {
t.Run("Returns the correctly formatted float when not rounding", func(t *testing.T) {
expected := "1.2"
actual := getFormattedPercentageFromFloat(0.012)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correctly formatted float when rounding to the tenths place", func(t *testing.T) {
expected := "1.3"
actual := getFormattedPercentageFromFloat(0.0125)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
t.Run("Returns the correctly formatted float when rounding to whole number", func(t *testing.T) {
expected := "2"
actual := getFormattedPercentageFromFloat(0.01984)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}
func TestGetFormattedTime(t *testing.T) {
t.Run("Returns the correctly formatted time", func(t *testing.T) {
ts := time.Date(1984, 01, 24, 8, 0, 0, 0, time.UTC)
expected := "8:00 AM"
actual := getFormattedTime(ts)
if actual != expected {
t.Errorf("received %s; want %s", actual, expected)
}
})
}