This repository was archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathStringExtensionSpec.swift
More file actions
168 lines (124 loc) · 5.71 KB
/
StringExtensionSpec.swift
File metadata and controls
168 lines (124 loc) · 5.71 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
//
// StringExtensionSpec.swift
// Rocket.Chat
//
// Created by Rafael K. Streit on 7/14/16.
// Copyright © 2016 Rocket.Chat. All rights reserved.
//
import XCTest
@testable import Rocket_Chat
class StringExtensionSpec: XCTestCase {
// MARK: Random string
func testRandomStringIsAlwaysDifferent() {
let string1 = String.random()
let string2 = String.random()
XCTAssert(string1 != string2, "Random strings are always different")
}
func testRandomStringCanHaveDifferentSizes() {
XCTAssert(String.random(10).count == 10, "Random string have 10 characters")
XCTAssert(String.random(20).count == 20, "Random string have 20 characters")
XCTAssert(String.random(100).count == 100, "Random string have 100 characters")
}
// MARK: SHA-256
func testSHA256ReturnsString() {
let string = "foobar"
let hash = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"
XCTAssert(string.sha256() == hash, "String SHA-256 cryptographic is correct")
}
// MARK: Removing Last Slash (when needed)
func testRemovingLastSlashURLPresent() {
XCTAssertEqual("http://foo.bar/".removingLastSlashIfNeeded(), "http://foo.bar")
XCTAssertEqual("http://foo.bar/foo/bar/".removingLastSlashIfNeeded(), "http://foo.bar/foo/bar")
XCTAssertEqual("http://foo.bar/foo/bar/?12345".removingLastSlashIfNeeded(), "http://foo.bar/foo/bar/?12345")
}
func testRemovingLastSlashURLNotPresent() {
XCTAssertEqual("foo".removingLastSlashIfNeeded(), "foo")
XCTAssertEqual("http://foo.bar".removingLastSlashIfNeeded(), "http://foo.bar")
}
// MARK: Range
func testRangesOf() {
let string = "this word is in position 5 and this word is in position 36"
let ranges = string.ranges(of: "word")
XCTAssert(ranges.count == 2, "will find correct number of words")
XCTAssert(string.distance(from: string.startIndex, to: ranges[0].lowerBound) == 5, "will find word 1 in correct place")
XCTAssert(string.distance(from: string.startIndex, to: ranges[1].lowerBound) == 36, "will find word 2 in correct place")
XCTAssert(string.distance(from: ranges[0].lowerBound, to: ranges[0].upperBound) == 4, "will word 1 have correct size")
XCTAssert(string.distance(from: ranges[1].lowerBound, to: ranges[1].upperBound) == 4, "will word 2 have correct size")
}
func testRemovingWhitespaces() {
// arrange
let withWhitespaces1 = "a b c d "
let expected1 = "abcd"
let withWhitespaces2 = " a b c d "
let expected2 = "abcd"
let withWhitespaces3 = " a b c d "
let expected3 = "abcd"
let withWhitespaces4 = " "
let expected4 = ""
let withWhitespaces5 = " a "
let expected5 = "a"
// act
let result1 = withWhitespaces1.removingWhitespaces()
let result2 = withWhitespaces2.removingWhitespaces()
let result3 = withWhitespaces3.removingWhitespaces()
let result4 = withWhitespaces4.removingWhitespaces()
let result5 = withWhitespaces5.removingWhitespaces()
// assert
XCTAssertEqual(result1, expected1, "string has no whitespaces")
XCTAssertEqual(result2, expected2, "string has no whitespaces")
XCTAssertEqual(result3, expected3, "string has no whitespaces")
XCTAssertEqual(result4, expected4, "string has no whitespaces")
XCTAssertEqual(result5, expected5, "string has no whitespaces")
}
func testRemovingWhitespacesAndNewlines() {
let withWhitespaces1 = "a b\n\n c\n d \n"
let expected1 = "abcd"
let withWhitespaces2 = "\n\n\n\n\n\nabcd"
let expected2 = "abcd"
let result1 = withWhitespaces1.removingWhitespaces()
let result2 = withWhitespaces2.removingWhitespaces()
XCTAssertEqual(result1, expected1, "string has no whitespaces and no new lines")
XCTAssertEqual(result2, expected2, "string has no whitespaces and no new lines")
}
func testNewlines() {
let withWhitespaces1 = "a b\n\n c\n d \n"
let expected1 = "a b c d "
let withWhitespaces2 = "\n\n\n\n\n\nabcd"
let expected2 = "abcd"
let result1 = withWhitespaces1.removingNewLines()
let result2 = withWhitespaces2.removingNewLines()
XCTAssertEqual(result1, expected1, "string has no new lines")
XCTAssertEqual(result2, expected2, "string has no new lines")
}
// MARK: Base64
func testBase64() {
XCTAssertEqual("test".base64Encoded(), "dGVzdA==", "base64Encoded encodes correctly")
XCTAssertEqual("dGVzdA==".base64Decoded(), "test", "base64Decoded decodes correctly")
let randomString = String.random(10)
XCTAssertEqual(randomString.base64Encoded()?.base64Decoded(), randomString, "base64Encoded <-> base64Decoded pass random test")
}
// MARK: Others
func testCommandAndParams() {
let string = "/gimme hello world"
guard let (command, params) = string.commandAndParams() else {
return XCTFail("string is valid command")
}
XCTAssertEqual(command, "gimme")
XCTAssertEqual(params, "hello world")
let string2 = "gimme"
XCTAssertNil(string2.commandAndParams())
}
func testReaction() {
let string = "+:upside_down:"
guard let emoji = string.reaction() else {
return XCTFail("string is valid reaction")
}
XCTAssertEqual(emoji, ":upside_down:")
let string2 = ":upside_down:"
XCTAssertNil(string2.reaction())
let string3 = "+upside_down:"
XCTAssertNil(string3.reaction())
let string4 = "+:upside_down"
XCTAssertNil(string4.reaction())
}
}