Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import XCTest
7
8
class StringExtensionsTests: XCTestCase {
9
10
func testEllipsize() {
11
// Odd maxLength. Note that we ellipsize with a Unicode join character to avoid wrapping.
12
XCTAssertEqual("abcd…\u{2060}fgh", "abcdefgh".ellipsize(maxLength: 7))
13
14
// Even maxLength.
15
XCTAssertEqual("abcd…\u{2060}ijkl", "abcdefghijkl".ellipsize(maxLength: 8))
16
17
// String shorter than maxLength.
18
XCTAssertEqual("abcd", "abcd".ellipsize(maxLength: 7))
19
20
// Empty String.
21
XCTAssertEqual("", "".ellipsize(maxLength: 8))
22
23
// maxLength < 2.
24
XCTAssertEqual("abcdefgh", "abcdefgh".ellipsize(maxLength: 0))
25
}
26
27
func testStringByTrimmingLeadingCharactersInSet() {
28
XCTAssertEqual("foo ", " foo ".stringByTrimmingLeadingCharactersInSet(.whitespaces))
29
XCTAssertEqual("foo456", "123foo456".stringByTrimmingLeadingCharactersInSet(.decimalDigits))
30
XCTAssertEqual("", "123456".stringByTrimmingLeadingCharactersInSet(.decimalDigits))
31
}
32
33
func testStringSplitWithNewline() {
34
XCTAssertEqual("", "".stringSplitWithNewline())
35
XCTAssertEqual("foo", "foo".stringSplitWithNewline())
36
XCTAssertEqual("aaa\n bbb", "aaa bbb".stringSplitWithNewline())
37
XCTAssertEqual("Mark as\n Read", "Mark as Read".stringSplitWithNewline())
38
XCTAssertEqual("aa\n bbbbbb", "aa bbbbbb".stringSplitWithNewline())
39
}
40
41
func testPercentEscaping() {
42
func roundtripTest(_ input: String, _ expected: String, file: StaticString = #file, line: UInt = #line) {
43
let observed = input.escape()!
44
XCTAssertEqual(observed, expected, "input is \(input)", file: file, line: line)
45
let roundtrip = observed.unescape()
46
XCTAssertEqual(roundtrip, input, "encoded is \(observed)", file: file, line: line)
47
}
48
49
roundtripTest("https://mozilla.com", "https://mozilla.com")
52
}
53
54
}