Skip to content

Commit 00573e2

Browse files
committed
TimeFormat unit test and fix bugs founds
1 parent 85eb7c3 commit 00573e2

File tree

3 files changed

+98
-33
lines changed

3 files changed

+98
-33
lines changed

core/src/main/kotlin/br/com/devsrsouza/kotlinbukkitapi/utils/TimeFormatUtils.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ inline class TimeFormat(private val time: Int) {
3838
val months = time / 2419200 % 12
3939
val years = time / 29030400
4040

41-
var formated = formatStyle.replace(if (seconds > 0) "$seconds ${if (seconds > 1) lang.seconds else lang.second}" else "", "%SEC", true)
41+
var formated = formatStyle.replace("%SEC", if (seconds > 0) "$seconds ${if (seconds > 1) lang.seconds else lang.second}" else "", true)
4242

43-
formated = formated.replace(if (minutes > 0) "$minutes ${if (minutes > 1) lang.minutes else lang.minute}" else "", "%MIN", true)
43+
formated = formated.replace("%MIN", if (minutes > 0) "$minutes ${if (minutes > 1) lang.minutes else lang.minute}" else "", true)
4444

45-
formated = formated.replace(if (hours > 0) "$hours ${if (hours > 1) lang.hours else lang.hour}" else "", "%HOUR", true)
45+
formated = formated.replace("%HOUR", if (hours > 0) "$hours ${if (hours > 1) lang.hours else lang.hour}" else "", true)
4646

47-
formated = formated.replace(if (days > 0) "$days ${if (days > 1) lang.days else lang.day}" else "", "%DAY", true)
47+
formated = formated.replace("%DAY", if (days > 0) "$days ${if (days > 1) lang.days else lang.day}" else "", true)
4848

49-
formated = formated.replace(if (weeks > 0) "$weeks ${if (weeks > 1) lang.weeks else lang.week}" else "", "%WEEK", true)
49+
formated = formated.replace("%WEEK", if (weeks > 0) "$weeks ${if (weeks > 1) lang.weeks else lang.week}" else "", true)
5050

51-
formated = formated.replace(if (months > 0) "$months ${if (months > 1) lang.months else lang.month}" else "", "%MONTH", true)
51+
formated = formated.replace("%MONTH", if (months > 0) "$months ${if (months > 1) lang.months else lang.month}" else "", true)
5252

53-
formated = formated.replace(if (years > 0) "$years ${if (years > 1) lang.years else lang.year}" else "", "%YEAR", true)
53+
formated = formated.replace("%YEAR", if (years > 0) "$years ${if (years > 1) lang.years else lang.year}" else "", true)
54+
55+
formated = formated.replace("$formatSpacer$formatSpacer", "$formatSpacer")
5456

5557
return formated.trim { it == formatSpacer }
5658
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package br.com.devsrsouza.kotlinbukkitapi.utils
2+
3+
import org.junit.Assert.*
4+
import org.junit.Test
5+
6+
class TimeFormatUtilsTest {
7+
8+
@Test
9+
fun `should format time to human readable (EN - SINGLE)`() {
10+
val time = 61
11+
12+
assertEquals("1 minute 1 second", time.formater.EN)
13+
}
14+
15+
@Test
16+
fun `should format time to human readable (EN - PLURAL)`() {
17+
val time = 330
18+
19+
assertEquals("5 minutes 30 seconds", time.formater.EN)
20+
}
21+
22+
@Test
23+
fun `should format time to human readable (PT-BR - SINGLE)`() {
24+
val time = 61
25+
26+
assertEquals("1 minuto 1 segundo", time.formater.PTBR)
27+
}
28+
29+
@Test
30+
fun `should format time to human readable (PT-BR - PLURAL)`() {
31+
val time = 330
32+
33+
assertEquals("5 minutos 30 segundos", time.formater.PTBR)
34+
}
35+
36+
@Test
37+
fun `should format time to human readable with new formatter (EN)`() {
38+
val time = 3601
39+
40+
val formater = "%HOUR %MIN %SEC"
41+
42+
assertEquals("1 hour 1 second", time.formater.format(ENFormat, formater))
43+
}
44+
45+
@Test
46+
fun `should format time to human readable with new formatter and spacer (EN)`() {
47+
val time = 3601
48+
49+
val formater = "%HOUR;%MIN;%SEC"
50+
val spacer = ';'
51+
52+
assertEquals("1 hour;1 second", time.formater.format(ENFormat, formater, spacer))
53+
}
54+
55+
@Test
56+
fun `should format time to human readable with new formatter without hour (EN)`() {
57+
val time = 3661
58+
59+
val formater = "%MIN %SEC"
60+
61+
assertEquals("1 minute 1 second", time.formater.format(ENFormat, formater))
62+
}
63+
}
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package br.com.devsrsouza.kotlinbukkitapi.utils
22

33
import br.com.devsrsouza.kotlinbukkitapi.utils.time.*
4-
import org.junit.Assert
4+
import org.junit.Assert.*
55
import org.junit.Test
66

77
class TimeUtilsTest {
@@ -11,198 +11,198 @@ class TimeUtilsTest {
1111
val time = 300L
1212
val millisecond = 300L
1313

14-
Assert.assertEquals(millisecond, time.millisecond.toMillisecond())
14+
assertEquals(millisecond, time.millisecond.toMillisecond())
1515
}
1616

1717
@Test
1818
fun `should convert correctly milliseconds to ticks`() {
1919
val time = 250L
2020
val tick = 5L
2121

22-
Assert.assertEquals(tick, time.millisecond.toTick())
22+
assertEquals(tick, time.millisecond.toTick())
2323
}
2424

2525
@Test
2626
fun `should convert correctly milliseconds to seconds`() {
2727
val time = 5000L
2828
val second = 5
2929

30-
Assert.assertEquals(second, time.millisecond.toSecond())
30+
assertEquals(second, time.millisecond.toSecond())
3131
}
3232

3333
@Test
3434
fun `should convert correctly milliseconds to minutes`() {
3535
val time = 60_000L
3636
val minute = 1
3737

38-
Assert.assertEquals(minute, time.millisecond.toMinute())
38+
assertEquals(minute, time.millisecond.toMinute())
3939
}
4040

4141
@Test
4242
fun `should convert correctly milliseconds to hours`() {
4343
val time = 7_200_000L
4444
val hour = 2
4545

46-
Assert.assertEquals(hour, time.millisecond.toHour())
46+
assertEquals(hour, time.millisecond.toHour())
4747
}
4848

4949
@Test
5050
fun `should convert correctly ticks to milliseconds`() {
5151
val time = 20L
5252
val millisecond = 1000L
5353

54-
Assert.assertEquals(millisecond, time.tick.toMillisecond())
54+
assertEquals(millisecond, time.tick.toMillisecond())
5555
}
5656

5757
@Test
5858
fun `should convert correctly ticks to ticks`() {
5959
val time = 20L
6060
val tick = 20L
6161

62-
Assert.assertEquals(tick, time.tick.toTick())
62+
assertEquals(tick, time.tick.toTick())
6363
}
6464

6565
@Test
6666
fun `should convert correctly ticks to seconds`() {
6767
val time = 20L
6868
val second = 1
6969

70-
Assert.assertEquals(second, time.tick.toSecond())
70+
assertEquals(second, time.tick.toSecond())
7171
}
7272

7373
@Test
7474
fun `should convert correctly ticks to minutes`() {
7575
val time = 2_400L
7676
val minute = 2
7777

78-
Assert.assertEquals(minute, time.tick.toMinute())
78+
assertEquals(minute, time.tick.toMinute())
7979
}
8080

8181
@Test
8282
fun `should convert correctly ticks to hours`() {
8383
val time = 144_000L
8484
val hour = 2
8585

86-
Assert.assertEquals(hour, time.tick.toHour())
86+
assertEquals(hour, time.tick.toHour())
8787
}
8888

8989
@Test
9090
fun `should convert correctly seconds to milliseconds`() {
9191
val time = 8
9292
val millisecond = 8000L
9393

94-
Assert.assertEquals(millisecond, time.second.toMillisecond())
94+
assertEquals(millisecond, time.second.toMillisecond())
9595
}
9696

9797
@Test
9898
fun `should convert correctly seconds to ticks`() {
9999
val time = 2
100100
val tick = 40L
101101

102-
Assert.assertEquals(tick, time.second.toTick())
102+
assertEquals(tick, time.second.toTick())
103103
}
104104

105105
@Test
106106
fun `should convert correctly seconds to seconds`() {
107107
val time = 20
108108
val second = 20
109109

110-
Assert.assertEquals(second, time.second.toSecond())
110+
assertEquals(second, time.second.toSecond())
111111
}
112112

113113
@Test
114114
fun `should convert correctly seconds to minutes`() {
115115
val time = 120
116116
val minute = 2
117117

118-
Assert.assertEquals(minute, time.second.toMinute())
118+
assertEquals(minute, time.second.toMinute())
119119
}
120120

121121
@Test
122122
fun `should convert correctly seconds to hours`() {
123123
val time = 7_200
124124
val hour = 2
125125

126-
Assert.assertEquals(hour, time.second.toHour())
126+
assertEquals(hour, time.second.toHour())
127127
}
128128

129129
@Test
130130
fun `should convert correctly minutes to milliseconds`() {
131131
val time = 1
132132
val millisecond = 60_000L
133133

134-
Assert.assertEquals(millisecond, time.minute.toMillisecond())
134+
assertEquals(millisecond, time.minute.toMillisecond())
135135
}
136136

137137
@Test
138138
fun `should convert correctly minutes to ticks`() {
139139
val time = 1
140140
val tick = 1_200L
141141

142-
Assert.assertEquals(tick, time.minute.toTick())
142+
assertEquals(tick, time.minute.toTick())
143143
}
144144

145145
@Test
146146
fun `should convert correctly minutes to seconds`() {
147147
val time = 1
148148
val second = 60
149149

150-
Assert.assertEquals(second, time.minute.toSecond())
150+
assertEquals(second, time.minute.toSecond())
151151
}
152152

153153
@Test
154154
fun `should convert correctly minutes to minutes`() {
155155
val time = 25
156156
val minute = 25
157157

158-
Assert.assertEquals(minute, time.minute.toMinute())
158+
assertEquals(minute, time.minute.toMinute())
159159
}
160160

161161
@Test
162162
fun `should convert correctly minutes to hours`() {
163163
val time = 120
164164
val hour = 2
165165

166-
Assert.assertEquals(hour, time.minute.toHour())
166+
assertEquals(hour, time.minute.toHour())
167167
}
168168

169169
@Test
170170
fun `should convert correctly hours to milliseconds`() {
171171
val time = 1
172172
val millisecond = 3_600_000L
173173

174-
Assert.assertEquals(millisecond, time.hour.toMillisecond())
174+
assertEquals(millisecond, time.hour.toMillisecond())
175175
}
176176

177177
@Test
178178
fun `should convert correctly hours to ticks`() {
179179
val time = 2
180180
val tick = 144_000L
181181

182-
Assert.assertEquals(tick, time.hour.toTick())
182+
assertEquals(tick, time.hour.toTick())
183183
}
184184

185185
@Test
186186
fun `should convert correctly hours to seconds`() {
187187
val time = 3
188188
val second = 10_800
189189

190-
Assert.assertEquals(second, time.hour.toSecond())
190+
assertEquals(second, time.hour.toSecond())
191191
}
192192

193193
@Test
194194
fun `should convert correctly hours to minutes`() {
195195
val time = 3
196196
val minute = 180
197197

198-
Assert.assertEquals(minute, time.hour.toMinute())
198+
assertEquals(minute, time.hour.toMinute())
199199
}
200200

201201
@Test
202202
fun `should convert correctly hours to hours`() {
203203
val time = 3
204204
val hour = 3
205205

206-
Assert.assertEquals(hour, time.hour.toHour())
206+
assertEquals(hour, time.hour.toHour())
207207
}
208208
}

0 commit comments

Comments
 (0)