-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathInteractiveTextFormatterTests.swift
More file actions
100 lines (84 loc) · 4.18 KB
/
InteractiveTextFormatterTests.swift
File metadata and controls
100 lines (84 loc) · 4.18 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
//
// InteractiveTextFormatterTests.swift
// MessageViewControllerTests
//
// Created by Viktoras Laukevičius on 31/03/2018.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import XCTest
@testable import MessageViewController
private func generateList(withDelimiter d: String) -> String {
return "\(d) Item A\n\(d) Item B"
}
class InteractiveTextFormatterTests: XCTestCase {
var formatter: InteractiveTextFormatter!
override func setUp() {
super.setUp()
formatter = InteractiveTextFormatter()
}
override func tearDown() {
formatter = nil
super.tearDown()
}
func test_continuesNumberedList_whenAtTheEnd() {
let str = "1. Item A\n2. Item B"
let attrStr = NSAttributedString(string: str)
let text = formatter.applying(change: "\n", in: attrStr, in: NSRange(location: str.count, length: 0))?.0
XCTAssertEqual(text?.string, "\(str)\n3. ")
}
func test_ignoresIncrement_whenReallyLongList() {
let maxInt = Int.max
let str = "\(maxInt - 1). Item A \n\(maxInt). Item B"
let attrStr = NSAttributedString(string: str)
let text = formatter.applying(change: "\n", in: attrStr, in: NSRange(location: str.count, length: 0))?.0
XCTAssertNil(text)
}
func test_continuesNumberedList_whenInTheMiddle() {
let str1 = "1. Item A \n2. Item B"
let str2 = "\nContinues..."
let attrStr = NSAttributedString(string: "\(str1)\(str2)")
let text = formatter.applying(change: "\n", in: attrStr, in: NSRange(location: str1.count, length: 0))?.0
XCTAssertEqual(text?.string, "\(str1)\n3. \(str2)")
}
func test_continuesList_whenUsedAsterisks() {
let str = generateList(withDelimiter: "*")
let text = formatter.applying(change: "\n", in: NSAttributedString(string: str), in: NSRange(location: str.count, length: 0))?.0
XCTAssertEqual(text?.string, "\(str)\n* ")
}
func test_continuesList_whenUsedMinuses() {
let str = generateList(withDelimiter: "-")
let text = formatter.applying(change: "\n", in: NSAttributedString(string: str), in: NSRange(location: str.count, length: 0))?.0
XCTAssertEqual(text?.string, "\(str)\n- ")
}
func test_continuesList_whenUsedPuses() {
let str = generateList(withDelimiter: "+")
let text = formatter.applying(change: "\n", in: NSAttributedString(string: str), in: NSRange(location: str.count, length: 0))?.0
XCTAssertEqual(text?.string, "\(str)\n+ ")
}
func test_preservesAttributes_whenTextPrefilled() {
let str = generateList(withDelimiter: "-")
let attrs: [NSAttributedStringKey : AnyHashable] = [NSAttributedStringKey.baselineOffset : 3]
let attrStr = NSAttributedString(string: str, attributes: attrs)
let text = formatter.applying(change: "\n", in: attrStr, in: NSRange(location: str.count, length: 0))?.0
let resultAttrs = text?.attributes(at: str.count + 2, effectiveRange: nil) as? [NSAttributedStringKey : AnyHashable]
XCTAssertEqual(resultAttrs!, attrs)
}
func test_adjustsSelectionPosition_whenTextAppended() {
let str = generateList(withDelimiter: "-")
let selectionBeginning = formatter.applying(change: "\n", in: NSAttributedString(string: str), in: NSRange(location: str.count, length: 0))?.1
let appended = "\n- "
XCTAssertEqual(selectionBeginning, str.count + appended.count)
}
func test_adjustsSelectionPosition_whenTextInserted() {
let str1 = generateList(withDelimiter: "-")
let str2 = "\nContinues..."
let selectionBeginning = formatter.applying(change: "\n", in: NSAttributedString(string: "\(str1)\(str2)"), in: NSRange(location: str1.count, length: 0))?.1
let inserted = "\n- "
XCTAssertEqual(selectionBeginning, str1.count + inserted.count)
}
func test_notContinuesList_whenLineNotListItem() {
let str = generateList(withDelimiter: "-") + "\nContinues..."
let text = formatter.applying(change: "\n", in: NSAttributedString(string: str), in: NSRange(location: str.count, length: 0))?.0
XCTAssertNil(text)
}
}