-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
StringUtilsTest.kt
132 lines (104 loc) · 4 KB
/
StringUtilsTest.kt
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
package io.sentry.util
import org.mockito.kotlin.mock
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class StringUtilsTest {
@Test
fun `when str has full package, return last string after dot`() {
assertEquals("TEST", StringUtils.getStringAfterDot("io.sentry.TEST"))
}
@Test
fun `when str is null, return null`() {
assertNull(StringUtils.getStringAfterDot(null))
}
@Test
fun `when str is empty, return the original str`() {
assertEquals("", StringUtils.getStringAfterDot(""))
}
@Test
fun `when str ends with a dot, return the original str`() {
assertEquals("io.sentry.", StringUtils.getStringAfterDot("io.sentry."))
}
@Test
fun `when str has no dots, return the original str`() {
assertEquals("iosentry", StringUtils.getStringAfterDot("iosentry"))
}
@Test
fun `capitalize string`() {
assertEquals("Test", StringUtils.capitalize("test"))
}
@Test
fun `capitalize string even if its uppercase`() {
assertEquals("Test", StringUtils.capitalize("TEST"))
}
@Test
fun `capitalize do not throw if only 1 char`() {
assertEquals("T", StringUtils.capitalize("t"))
}
@Test
fun `capitalize returns itself if null`() {
assertNull(StringUtils.capitalize(null))
}
@Test
fun `capitalize returns itself if empty`() {
assertEquals("", StringUtils.capitalize(""))
}
@Test
fun `removeSurrounding returns null if argument is null`() {
assertNull(StringUtils.removeSurrounding(null, "\""))
}
@Test
fun `removeSurrounding returns itself if delimiter is null`() {
assertEquals("test", StringUtils.removeSurrounding("test", null))
}
@Test
fun `removeSurrounding returns itself if first char is different from the last char`() {
assertEquals("'test$", StringUtils.removeSurrounding("'test$", "'"))
}
@Test
fun `removeSurrounding returns trimmed string if first char is the same as the last char and equal to delimiter`() {
assertEquals("test", StringUtils.removeSurrounding("\"test\"", "\""))
}
@Test
fun `byteCountToString appends B when byte count is in +-1000 range`() {
assertEquals("500 B", StringUtils.byteCountToString(500))
assertEquals("-500 B", StringUtils.byteCountToString(-500))
}
@Test
fun `byteCountToString appends kB when byte count is in kilobyte range`() {
assertEquals("100.5 kB", StringUtils.byteCountToString(100_500))
assertEquals("-100.5 kB", StringUtils.byteCountToString(-100_500))
assertEquals("999.0 kB", StringUtils.byteCountToString(999_000))
}
@Test
fun `byteCountToString appends MB when byte count is in megabyte range`() {
assertEquals("100.1 MB", StringUtils.byteCountToString(100_124_500))
assertEquals("-100.1 MB", StringUtils.byteCountToString(-100_124_500))
assertEquals("999.9 MB", StringUtils.byteCountToString(999_945_018))
}
@Test
fun `calculates SHA1 hash of the given string`() {
val hash = StringUtils.calculateStringHash("http://key@localhost/proj", mock())
assertEquals("22be31f64088988d5caeb9d19470bc38a0aa0a78", hash)
}
@Test
fun `returns null if the given string is null or empty when calculating hash`() {
val hashNull = StringUtils.calculateStringHash(null, mock())
assertNull(hashNull)
val hashEmpty = StringUtils.calculateStringHash("", mock())
assertNull(hashEmpty)
}
@Test
fun `returns proper nil UUID if the given string is corrupted`() {
val normalized = StringUtils.normalizeUUID("0000-0000")
assertEquals("00000000-0000-0000-0000-000000000000", normalized)
}
@Test
fun `returns the unchanged UUID if it was not corrupted`() {
val original = UUID.randomUUID().toString()
val normalized = StringUtils.normalizeUUID(original)
assertEquals(original, normalized)
}
}