-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathStringExtensionTests.swift
More file actions
272 lines (224 loc) · 9.64 KB
/
StringExtensionTests.swift
File metadata and controls
272 lines (224 loc) · 9.64 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import XCTest
import Hamcrest
import SwiftString
class SwiftStringTests: XCTestCase {
func testBetween() {
assertThat("<a>foo</a>".between("<a>", "</a>"), presentAnd(equalTo("foo")))
assertThat("<a><a>foo</a></a>".between("<a>", "</a>"), presentAnd(equalTo("<a>foo</a>")))
assertThat("<a>foo".between("<a>", "</a>"), nilValue())
assertThat("Some strings } are very {weird}, dont you think?".between("{", "}"), presentAnd(equalTo("weird")))
assertThat("<a></a>".between("<a>", "</a>"), nilValue())
assertThat("<a>foo</a>".between("<a>", "<a>"), nilValue())
}
func testCamelize() {
assertThat("os version".camelize() == "osVersion")
assertThat("HelloWorld".camelize() == "helloWorld")
assertThat("someword With Characters".camelize() == "somewordWithCharacters")
assertThat("data_rate".camelize() == "dataRate")
assertThat("background-color".camelize() == "backgroundColor")
}
func testCapitalize() {
assertThat("hello world".capitalize() == "Hello World")
}
func testChompLeft() {
assertThat("foobar".chompLeft("foo") == "bar")
assertThat("foobar".chompLeft("bar") == "foo")
}
func testChompRight() {
assertThat("foobar".chompRight("bar") == "foo")
assertThat("foobar".chompRight("foo") == "bar")
}
func testClean() {
assertThat("thisoneistwoathreetest".clean(with: " ", allOf: "one", "two", "three") == "this is a test")
}
func testCollapseWhitespace() {
assertThat(" String \t libraries are \n\n\t fun\n! ".collapseWhitespace() == "String libraries are fun !")
}
func testContains() {
assertThat("foobar".contains("foo") == true)
assertThat("foobar".contains("ba") == true)
assertThat("foobar".contains("something") == false)
}
func testCount() {
assertThat("hi hi ho hey hihey".count("hi") == 3)
}
func testEndsWith() {
assertThat("hello world".endsWith("world") == true)
assertThat("hello world".endsWith("foo") == false)
}
func testEnsureLeft() {
assertThat("/subdir".ensureLeft("/") == "/subdir")
assertThat("subdir".ensureLeft("/") == "/subdir")
}
func testEnsureRight() {
assertThat("subdir/".ensureRight("/") == "subdir/")
assertThat("subdir".ensureRight("/") == "subdir/")
}
func testIndexOf() {
assertThat("hello".indexOf("hell"), presentAnd(equalTo(0)))
assertThat("hello".indexOf("lo"), presentAnd(equalTo(3)))
assertThat("hello".indexOf("world"), nilValue())
}
func testRindexOf() {
assertThat("hello".rindexOf("hell"), presentAnd(equalTo(0)))
assertThat("helloll".rindexOf("ll"), presentAnd(equalTo(5)))
assertThat("hellolloll".rindexOf("ll"), presentAnd(equalTo(8)))
assertThat("hello".rindexOf("world"), nilValue())
}
func testInitials() {
assertThat("First".initials() == "F")
assertThat("First Last".initials() == "FL")
assertThat("First Middle1 Middle2 Middle3 Last".initials() == "FMMML")
}
func testInitialsFirstAndLast() {
assertThat("First Last".initialsFirstAndLast() == "FL")
assertThat("First Middle1 Middle2 Middle3 Last".initialsFirstAndLast() == "FL")
}
func testIsAlpha() {
assertThat("fdafaf3".isAlpha() == false)
assertThat("afaf".isAlpha() == true)
assertThat("dfdf--dfd".isAlpha() == false)
}
func testIsAlphaNumeric() {
assertThat("afaf35353afaf".isAlphaNumeric() == true)
assertThat("FFFF99fff".isAlphaNumeric() == true)
assertThat("99".isAlphaNumeric() == true)
assertThat("afff".isAlphaNumeric() == true)
assertThat("-33".isAlphaNumeric() == false)
assertThat("aaff..".isAlphaNumeric() == false)
}
func testIsEmpty() {
assertThat("".isEmpty() == true)
assertThat(" ".isEmpty() == true)
assertThat("\t\t\t ".isEmpty() == true)
assertThat("\n\n".isEmpty() == true)
assertThat("helo".isEmpty() == false)
}
func testIsNumeric() {
assertThat("abc".isNumeric() == false)
assertThat("123a".isNumeric() == false)
assertThat("1".isNumeric() == true)
assertThat("22".isNumeric() == true)
assertThat("33.0".isNumeric() == true)
assertThat("-63.0".isNumeric() == true)
}
func testJoin() {
assertThat(",".join([1,2,3]) == "1,2,3")
assertThat(",".join([]) == "")
assertThat(",".join(["a","b","c"]) == "a,b,c")
assertThat("! ".join(["hey","who are you?"]) == "hey! who are you?")
}
func testLatinize() {
assertThat("šÜįéïöç".latinize() == "sUieioc")
assertThat("crème brûlée".latinize() == "creme brulee")
}
func testLines() {
assertThat("test".lines() == ["test"])
assertThat("test\nsentence".lines() == ["test", "sentence"])
assertThat("test \nsentence".lines() == ["test ", "sentence"])
//Test Carriage return instead of just newlines
assertThat("test\rsentence".lines() == ["test", "sentence"])
}
func testPad() {
assertThat("hello".pad(2) == " hello ")
assertThat("hello".pad(1, "\t") == "\thello\t")
}
func testPadLeft() {
assertThat("hello".padLeft(10) == " hello")
assertThat("what?".padLeft(2, "!") == "!!what?")
}
func testPadRight() {
assertThat("hello".padRight(10) == "hello ")
assertThat("hello".padRight(2, "!") == "hello!!")
}
func testStartsWith() {
assertThat("hello world".startsWith("hello") == true)
assertThat("hello world".startsWith("foo") == false)
}
func testSplit() {
assertThat("hello world".split(" ")[0] == "hello")
assertThat("hello world".split(" ")[1] == "world")
assertThat("helloworld".split(" ")[0] == "helloworld")
}
func testTimes() {
assertThat("hi".times(3) == "hihihi")
assertThat(" ".times(10) == " ")
}
func testFindSubstring() {
assertThat("hello-world-abc".findSubstring("\\w+\\-\\w+", ignoreCase: false) == "hello-world")
assertThat("hello-world-abc".findSubstring("HELLO-world", ignoreCase: true) == "hello-world")
assertThat("hello-world-abc".findSubstring("\\d+\\-\\d+", ignoreCase: false) == nil)
}
func testTrimmedLeft() {
assertThat(" How are you? ".trimmedLeft() == "How are you? ")
}
func testTrimmedRight() {
assertThat(" How are you? ".trimmedRight() == " How are you?")
}
func testTrimmed() {
assertThat(" How are you? ".trimmed() == "How are you?")
//Added per Issue #9 - https://github.com/amayne/SwiftString/issues/9
assertThat(" ".trimmed().characters.count == 0)
assertThat(" ".trimmed().characters.count == 0)
assertThat(" ".trimmed().characters.count == 0)
}
func testToBool() {
assertThat("asdwads".toBool(), nilValue())
assertThat("true".toBool(), presentAnd(equalTo(true)))
assertThat("false".toBool(), presentAnd(equalTo(false)))
}
func testToFloat() {
assertThat("asdwads".toFloat(), nilValue())
assertThat("2.00".toFloat(), presentAnd(equalTo(2.0)))
assertThat("2".toFloat(), presentAnd(equalTo(2.0)))
}
func testToInt() {
assertThat("asdwads".toInt(), nilValue())
assertThat("2.00".toInt(), presentAnd(equalTo(2)))
assertThat("2".toInt(), presentAnd(equalTo(2)))
}
func testToDate() {
assertThat("asdwads".toDate(), nilValue())
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day , .Month , .Year], fromDate: "2014-06-03".toDate()!)
assertThat(components.year == 2014)
assertThat(components.month == 6)
assertThat(components.day == 3)
}
func testToDateTime() {
assertThat("asdwads".toDateTime(), nilValue())
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day , .Month , .Year, .Hour, .Minute, .Second], fromDate: "2014-06-03 13:15:01".toDateTime()!)
assertThat(components.year == 2014)
assertThat(components.month == 6)
assertThat(components.day == 3)
assertThat(components.hour == 13)
assertThat(components.minute == 15)
assertThat(components.second == 1)
}
func testToDouble() {
assertThat("asdwads".toDouble(), nilValue())
assertThat("2.00".toDouble(), presentAnd(equalTo(2.0)))
assertThat("2".toDouble(), presentAnd(equalTo(2.0)))
}
func testSlugify() {
assertThat("Global Thermonuclear Warfare".slugify() == "global-thermonuclear-warfare")
assertThat("Global Thermonuclear Warfare".slugify(withSeparator: "_") == "global_thermonuclear_warfare")
assertThat("Crème brûlée".slugify() == "creme-brulee")
}
func testStripPunctuation() {
assertThat("My, st[ring] *full* of %punct)".stripPunctuation() == "My string full of punct")
}
func testSubstring() {
let subject = "hello world"
assertThat(subject.substring(0, length: 1) == "h")
assertThat(subject.substring(0, length: 11) == "hello world")
}
func testSubscripts() {
let subject = "hello world"
assertThat(subject[0...1] == "he")
assertThat(subject[0..<1] == "h")
assertThat(subject[0] == "h")
assertThat(subject[0...10] == "hello world")
}
}