forked from amayne/SwiftString
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSwiftStringTests.swift
More file actions
326 lines (310 loc) · 10.5 KB
/
Copy pathSwiftStringTests.swift
File metadata and controls
326 lines (310 loc) · 10.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import XCTest
#if os(Linux)
import SwiftGlibc
#else
import Darwin
#endif
@testable import SwiftString
class SwiftStringTests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testBetween() {
let s = "The stupid brown fox"
XCTAssert(((s.between("stupid", "fox")?.count) != nil), "Between is invalid")
}
func testCamelize() {
let s = "The stupid brown fox"
XCTAssert((s.camelize() != "The Stupid Brown Fox"), "Camelize is invalid")
}
func testCapitalize() {
let s = "the Fox"
XCTAssert((s.capitalize() != "THE FOX"), "Capitalize is invalid")
}
func testChompLeft() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.chompLeft("The "), "stupid brown Fox", "ChompLeft is invalid")
}
func testChompRight() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.chompLeft(" Fox"), "The stupid brown", "ChompRight is invalid")
}
//collapseWhitespace
func testcollapseWhitespace() {
let s = "The stupid brown fox"
XCTAssertEqual(s.collapseWhitespace(), "The stupid brown fox", "collapseWhitespace is invalid")
}
//clean
func testclean() {
let s = "The stupid brøwn føx"
XCTAssertEqual(s.clean(with: "o",allOf: "ø"), "The stupid brown fox", "clean is invalid")
}
//count
func testcount() {
let s = "The stupid brown fox"
XCTAssertEqual(s.count("o"), 2, "count is invalid")
}
//endsWith
func testendsWith() {
let s = "The stupid brown fox"
XCTAssertEqual(s.endsWith("foc"), false, "endsWith is invalid")
XCTAssertEqual(s.endsWith("fox"), true, "endsWith is invalid")
}
//ensureLeft
func testensureLeft() {
let s = "The stupid brown fox"
XCTAssertNotEqual(s.ensureLeft("Tha"), s, "ensureLeft is invalid")
XCTAssertEqual(s.ensureLeft("The"), "The stupid brown fox", "ensureLeft is invalid")
XCTAssertEqual(s.ensureLeft("And "), "And The stupid brown fox", "ensureLeft is invalid")
}
//ensureRight
func testensureRight() {
let s = "The stupid brown fox"
XCTAssertNotEqual(s.ensureRight("fax"), s, "ensureRight is invalid")
XCTAssertEqual(s.ensureRight("fox"), "The stupid brown fox", "ensureRight is invalid")
XCTAssertEqual(s.ensureRight(" died."), "The stupid brown fox died.", "ensureRight is invalid")
}
//indexOf
func testindexOf() {
let s = "The stupid brøwn føx"
XCTAssertEqual(s.index(of:"s"), 4, "indexOf is invalid")
XCTAssertNotEqual(s.index(of:"s"), 7, "indexOf is invalid")
}
//initials
func testinitials() {
let s = "brown Fox"
XCTAssertEqual(s.initials(), "bF", "initials is invalid")
XCTAssertNotEqual(s.initials(), "BS", "initials is invalid")
}
//initialsFirstAndLast
func testinitialsFirstAndLast() {
let s = "stupid brown Fox"
XCTAssertEqual(s.initialsFirstAndLast(), "sF", "initialsFirstAndLast is invalid")
XCTAssertNotEqual(s.initialsFirstAndLast(), "bF", "initialsFirstAndLast is invalid")
}
//isAlpha
func testisAlpha() {
let s = "stupid brown Fox"
XCTAssertEqual(s.isAlpha(), false, "isAlpha is invalid")
let sn = "stupid2Fox"
XCTAssertEqual(sn.isAlpha(), false, "isAlpha is invalid")
let ss = "stupidbrownFox"
XCTAssertEqual(ss.isAlpha(), true, "isAlpha is invalid")
}
//isAlphaNumeric
func testisAlphaNumeric() {
let s = "stupid brown Fox!"
XCTAssertEqual(s.isAlphaNumeric(), false, "isAlphaNumeric is invalid")
let snx = "stupid2Fox!"
XCTAssertEqual(snx.isAlphaNumeric(), false, "isAlphaNumeric is invalid")
let sn = "stupid2Fox"
XCTAssertEqual(sn.isAlphaNumeric(), true, "isAlphaNumeric is invalid")
let ss = "stupidbrownFox"
XCTAssertEqual(ss.isAlphaNumeric(), true, "isAlphaNumeric is invalid")
}
//isEmpty
func testisEmpty() {
let s = "stupid brown Fox!"
XCTAssertEqual(s.isEmpty(), false, "isEmpty is invalid")
let snx = ""
XCTAssertEqual(snx.isEmpty(), true, "isEmpty is invalid")
}
//isNumeric
func testisNumeric() {
let s = "stupid brown Fox!"
XCTAssertEqual(s.isNumeric(), false, "isNumeric is invalid")
let snx = "12"
XCTAssertEqual(snx.isNumeric(), true, "isNumeric is invalid")
}
//latinize
func testlatinize() {
let s = "The stüpid brown Fox"
XCTAssertEqual(s.latinize(), "The stupid brown Fox", "latinize is invalid")
}
//lines
func testlines() {
let s = "The stupid brown Fox\nis dead."
XCTAssertEqual(s.lines(), ["The stupid brown Fox","is dead."], "lines is invalid")
}
//length
func testlength() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.length, 20, "length is invalid")
}
//pad
func testpad() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.pad(3,"x"), "xxxThe stupid brown Foxxxx", "pad is invalid")
}
//padLeft
func testpadLeft() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.padLeft(3,"x"), "xxxThe stupid brown Fox", "padLeft is invalid")
}
//padRight
func testpadRight() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.padRight(3,"x"), "The stupid brown Foxxxx", "padRight is invalid")
}
//slugify
func testslugify() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.slugify(), "the-stupid-brown-fox", "slugify is invalid")
}
//split
func testsplit() {
let s = "The stupid brown Fox - is dead"
XCTAssertEqual(s.split(), ["The","stupid","brown","Fox","-","is","dead"], "split is invalid")
XCTAssertEqual(s.split("-"), ["The stupid brown Fox "," is dead"], "split is invalid")
}
//startsWith
func teststartsWith() {
let s = "The stupid brown Fox"
XCTAssertEqual(s.startsWith("The stupid"), true, "startsWith is invalid")
XCTAssertEqual(s.startsWith("The nice"), false, "startsWith is invalid")
}
//stripPunctuation
func teststripPunctuation() {
let s = "The stupid! brown Fox"
XCTAssertEqual(s.stripPunctuation(), "The stupid brown Fox", "stripPunctuation is invalid")
}
//toFloat
func testtoFloat() {
let s = "2.0"
XCTAssertEqual(s.toFloat(), 2.0, "toFloat is invalid")
}
//toInt
func testtoInt() {
let s = "2"
XCTAssertEqual(s.toInt(), 2, "toInt is invalid")
}
//toBool
func testtoBool() {
let s = "0"
XCTAssertEqual(s.toBool(), false, "toBool is invalid")
let s1 = "-1"
XCTAssertEqual(s1.toBool(), true, "toBool is invalid")
let s2 = "1"
XCTAssertEqual(s2.toBool(), true, "toBool is invalid")
let s3 = "2"
XCTAssertEqual(s3.toBool(), true, "toBool is invalid")
}
#if !os(Linux)
//toDate
func testtoDate() {
let s = "2016-03-01"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
let someDateTime = formatter.date(from: "2016-03-01")
XCTAssertEqual(s.toDate(),someDateTime, "toDate is invalid")
}
//toDateTime
func testtoDateTime() {
let s = "2016-03-01 18:31:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm:SS"
let someDateTime = formatter.date(from: "2016-03-01 18:31:00")
XCTAssertEqual(s.toDateTime(),someDateTime, "toDateTime is invalid")
}
#endif
//trimmedLeft
func testtrimmedLeft() {
let s = " The stupid brown fox "
XCTAssertEqual(s.trimmedLeft(), "The stupid brown fox ", "trimmedLeft is invalid")
}
//trimmedRight
func testtrimmedRight() {
let s = " The stupid brown fox "
XCTAssertEqual(s.trimmedRight(), " The stupid brown fox", "trimmedRight is invalid")
}
//trimmed
func testtrimmed() {
let s = " The stupid brown fox "
XCTAssertEqual(s.trimmed(), "The stupid brown fox", "trimmed is invalid")
}
//substring
func testsubstring() {
let s = "The stupid brown fox"
XCTAssertEqual(s.substring(4,length: 9), "stupid br", "trimmed is invalid")
}
//subscript
func testsubscript() {
let s = "0123456789"
XCTAssertEqual(s[4..<9], "45678", "subscript is invalid")
XCTAssertEqual(s[4...8], "45678", "subscript is invalid")
}
//safesubscript
func testsafesubscript() {
let s = "0123456789"
XCTAssertEqual(s[safe: 1 ..< 6], "12345", "safesubscript is invalid")
XCTAssertEqual(s[safe: 1 ... 6], "123456", "safesubscript is invalid")
XCTAssertEqual(s[safe: 7 ..< 100], "789", "safesubscript is invalid")
XCTAssertEqual(s[safe: 7 ... 100], "789", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ..< 2], "01", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ... 2], "012", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ..< 100], "0123456789", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ... 100], "0123456789", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ..< -20], "", "safesubscript is invalid")
XCTAssertEqual(s[safe: -100 ... -20], "0", "safesubscript is invalid")
XCTAssertEqual(s[safe: 20 ..< 100], "", "safesubscript is invalid")
XCTAssertEqual(s[safe: 20 ... 100], "9", "safesubscript is invalid")
XCTAssertEqual(""[safe: 20 ..< 100], "", "safesubscript is invalid")
XCTAssertEqual(""[safe: 20 ... 100], "", "safesubscript is invalid")
}
func testBase64() {
let str = "hello:world"
let encoded = str.toBase64()
let decoded = encoded.fromBase64()
XCTAssertEqual(str, decoded, "Base64 does not match")
}
func testtimes() {
let fivetimesc = "ccccc"
let fivetimescComputed = "c".times(5)
XCTAssertEqual(fivetimesc, fivetimescComputed, "times doesnt work properly")
}
static var allTests : [(String, (SwiftStringTests) -> () throws -> Void)] {
return [
("testBetween", testBetween),
("testCamelize", testCamelize),
("testCapitalize", testCapitalize),
("testChompLeft", testChompLeft),
("testChompRight", testChompRight),
("testcollapseWhitespace", testcollapseWhitespace),
("testclean", testclean),
("testcount", testcount),
("testendsWith", testendsWith),
("testensureLeft", testensureLeft),
("testensureRight", testensureRight),
("testindexOf", testindexOf),
("testinitials", testinitials),
("testinitialsFirstAndLast", testinitialsFirstAndLast),
("testisAlpha", testisAlpha),
("testisAlphaNumeric", testisAlphaNumeric),
("testisEmpty", testisEmpty),
("testisNumeric", testisNumeric),
("testlatinize", testlatinize),
("testlines", testlines),
("testlength", testlength),
("testpad", testpad),
("testpadLeft", testpadLeft),
("testpadRight", testpadRight),
("testslugify", testslugify),
("testsplit", testsplit),
("teststartsWith", teststartsWith),
("teststripPunctuation", teststripPunctuation),
("testtoFloat", testtoFloat),
("testtoInt", testtoInt),
("testtoBool", testtoBool),
("testtrimmedLeft", testtrimmedLeft),
("testtrimmedRight", testtrimmedRight),
("testtrimmed", testtrimmed),
("testsubstring", testsubstring),
("testsubscript", testsubscript),
("testsafesubscript", testsafesubscript),
("testtimes", testtimes)
]
}
}