-
Notifications
You must be signed in to change notification settings - Fork 284
/
NSMutableAttributedStringStylingTests.swift
138 lines (113 loc) · 6.07 KB
/
NSMutableAttributedStringStylingTests.swift
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
import XCTest
@testable import Simplenote
// MARK: - NSMutableAttributedString Styling Tests
//
class NSMutableAttributedStringStylingTests: XCTestCase {
/// Verifies that `NSRegularExpression.regexForChecklists` will not match checklists that are in the middle of a string
///
func testRegexForChecklistsWillNotMatchChecklistsLocatedAtTheMiddleOfTheString() {
let string = "This is a badly formed todo - [ ] Buy avocados - []"
let regex = NSRegularExpression.regexForChecklists
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertTrue(matches.isEmpty)
}
/// Verifies that `NSRegularExpression.regexForChecklistsEmbeddedAnywhere` matches multiple checklists in the same line
///
func testRegexForChecklistsEmbeddedAnywhereWillMatchChecklistsLocatedAtTheMiddleOfTheString() {
let string = "The second regex should consider this as a valid checklist - [ ] Buy avocados - []"
let regex = NSRegularExpression.regexForChecklistsEmbeddedAnywhere
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 2)
}
/// Verifies that `NSRegularExpression.regexForChecklists` matches multiple spacing prefixes
///
func testRegexForChecklistsProperlyMatchesMultipleWhitespacePrefixes() {
let string = " - [ ] Buy avocados - [ ]"
let regex = NSRegularExpression.regexForChecklists
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 1)
}
/// Verifies that `NSRegularExpression.regexForChecklistsEmbeddedAnywhere` matches multiple spacing prefixes
///
func testRegexForChecklistsEverywhereProperlyMatchesMultipleWhitespacePrefixes() {
let string = " - [ ] Buy avocados - [ ]"
let regex = NSRegularExpression.regexForChecklistsEmbeddedAnywhere
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 2)
}
/// Verifies that `NSRegularExpression.regexForChecklists` only matches corretly formed strings
///
func testRegexForChecklistsMatchProperlyFormattedChecklists() {
let string = "ToDo\n\n- [ ] Buy avocados\n- [ ] Ship it\n- [x ] Malformed!\n- [x] Correct."
let regex = NSRegularExpression.regexForChecklists
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 3)
}
/// Verifies that `NSRegularExpression.regexForChecklists` will not match malformed strings
///
func testRegexForChecklistsWillNotMatchMalformedChecklists() {
let string = "- [x ] Malformed!"
let regex = NSRegularExpression.regexForChecklists
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertTrue(matches.isEmpty)
}
/// Verifies that `NSRegularExpression.regexForChecklistsEverywhere` will not match malformed strings
///
func testRegexForChecklistsEverywhereWillNotMatchMalformedChecklists() {
let string = "- [x ] Malformed!"
let regex = NSRegularExpression.regexForChecklistsEmbeddedAnywhere
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertTrue(matches.isEmpty)
}
/// Verifies that `NSRegularExpression.regexForChecklists` will match checklists with no spaces between brackets
///
func testRegexForChecklistsWillMatchChecklistsWithNoInternalSpaces() {
let string = "- [] Item"
let regex = NSRegularExpression.regexForChecklists
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 1)
}
/// Verifies that `NSRegularExpression.regexForChecklistsEmbeddedAnywhere` will match checklists with no spaces between brackets
///
func testRegexForChecklistsEverywhereWillMatchChecklistsWithNoInternalSpaces() {
let string = "- [] Item"
let regex = NSRegularExpression.regexForChecklistsEmbeddedAnywhere
let matches = regex.matches(in: string, options: [], range: string.fullRange)
XCTAssertEqual(matches.count, 1)
}
/// Verifies that `NSRegularExpression.regexForChecklists` always produces the expected number of ranges
///
func testRegexForChecklistsAlwaysProduceTwoRanges() {
let samples = [
(text: " - [ ] Buy avocados - [ ]", expected: 1),
(text: "ToDo\n\n- [ ] Buy avocados\n- [ ] Ship it\n- [x ] Malformed!\n- [x] Correct.", expected: 3),
(text: "- [] Item", expected: 1)
]
let regex = NSRegularExpression.regexForChecklists
for (sample, expected) in samples {
let matches = regex.matches(in: sample, options: [], range: sample.fullRange)
XCTAssertEqual(matches.count, expected)
for match in matches where match.numberOfRanges != NSRegularExpression.regexForChecklistsExpectedNumberOfRanges {
XCTFail()
}
}
}
/// Verifies that `NSRegularExpression.regexForChecklistsEmbeddedAnywhere` always produces the expected number of ranges
///
func testRegexForChecklistsEverywhereAlwaysProduceTwoRanges() {
let samples = [
(text: " - [ ] Buy avocados - [ ]", expected: 2),
(text: "ToDo\n\n- [ ] Buy avocados\n- [ ] Ship it\n- [x ] Malformed!\n- [x] Correct.", expected: 3),
(text: "- [] Item", expected: 1),
(text: "The second regex should consider this as a valid checklist - [ ] Buy avocados - []", expected: 2)
]
let regex = NSRegularExpression.regexForChecklistsEmbeddedAnywhere
for (sample, expected) in samples {
let matches = regex.matches(in: sample, options: [], range: sample.fullRange)
XCTAssertEqual(matches.count, expected)
for match in matches where match.numberOfRanges != NSRegularExpression.regexForChecklistsExpectedNumberOfRanges {
XCTFail()
}
}
}
}