-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrappersDetectorSearchingItemTests.swift
179 lines (132 loc) · 6.42 KB
/
WrappersDetectorSearchingItemTests.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
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
//
// WrappersDetectorSearchingItemTests.swift
// FeltPen
//
// Created by Aleksei Gordeev on 08/07/2017.
// Copyright © 2017 CocoaPods. All rights reserved.
//
import XCTest
@testable import FeltPen
fileprivate extension NSRange {
static func makeRange(_ location: Int, _ length: Int) -> NSRange {
return NSRange(location: location, length: length)
}
var descr: String {
return NSStringFromRange(self)
}
}
fileprivate extension Array where Element == NSRange {
var rangeDescriptions: [String] {
return self.map({$0.descr})
}
var rangesDescription: String {
return "\(rangeDescriptions)"
}
}
class WrappersDetectorSearchingItemTests: XCTestCase {
struct TestTask {
var string: String
/// Sorted by locations
var expectedRanges: [NSRange]
var wholeRange: NSRange {
return .makeRange(0, string.characters.count)
}
init(_ string: String, _ expectedRanges: [NSRange]? = nil) {
self.string = string
self.expectedRanges = expectedRanges ?? [NSRange(location: 0, length: string.characters.count)]
}
}
func testBacktickPattern() {
let pattern = WrappersDetector.createRegex(item: .backtick).pattern
XCTAssertEqual(pattern, "(?:^|[ .,#!$%^&;:{}=_~()\\/-/*])(`[^`]+`)(?:$|[ .,#!$%^&;:{}=_~()\\/-/*])")
}
func testBasicSimpleFalseNegativeCases() {
let testCasesByItems: [WrappersDetector.SearchingItem : [TestTask]] = [
WrappersDetector.SearchingItem.backtick : [
TestTask.init("`test`"),
TestTask.init("`test test`"),
TestTask.init("`test` not-in", [.makeRange(0, 6)]),
TestTask.init(" `test` not from start", [.makeRange(1, 6)]),
TestTask.init(" `test` `test`", [.makeRange(1, 6), .makeRange(8, 6)])
],
WrappersDetector.SearchingItem.asteriks : [
TestTask.init("*test*"),
TestTask.init("*test test*"),
TestTask.init("*test* partial", [.makeRange(0, 6)]),
TestTask.init(" *test* not from start", [.makeRange(1, 6)])
],
WrappersDetector.SearchingItem.underscore : [
TestTask.init("_test_"),
TestTask.init("_test test_"),
TestTask.init("_test_ partial", [.makeRange(0, 6)]),
TestTask.init(" _test_ not from start", [.makeRange(1, 6)])
],
WrappersDetector.SearchingItem.tilde : [
TestTask.init("~test~"),
TestTask.init("~test test~"),
TestTask.init("~test~ partial", [.makeRange(0, 6)]),
TestTask.init(" ~test~ not from start", [.makeRange(1, 6)])
]
]
testCasesByItems.forEach({ (item, tasks) in
let detector = WrappersDetector.init(searchingItems: [item])
for task in tasks {
let text = NSMutableAttributedString.init(string: task.string)
try! detector.process(text: text)
let attr = item.detectorAttributeName.rawValue
var expectedRanges = task.expectedRanges
text.enumerateAttribute(NSAttributedStringKey(rawValue: attr), in: task.wholeRange, options: [], using: { value, range, stop in
guard let nonnullValue = value else {
return
}
guard let value = nonnullValue as? WrappersDetector.SpotAttribute else {
XCTFail("Unexpected attribute type")
return
}
XCTAssertEqual(value.item, item)
guard let idx = expectedRanges.index(where: { NSEqualRanges($0, range)}) else {
let foundSubstring = (text.string as NSString).substring(with: range)
XCTFail("Could not found \(range.descr) (\(foundSubstring)) in \(expectedRanges.map({$0.descr}))")
return
}
expectedRanges.remove(at: idx)
})
}
})
}
func testWrappedInOtherItemsFalseNegativeCases() {
let testCasesByItems: [WrappersDetector.SearchingItem : [TestTask]] = [
WrappersDetector.SearchingItem.backtick : [
TestTask.init("_`test`_", [.makeRange(1, 6)]),
TestTask.init("_`test test`_", [.makeRange(1, 11)]),
TestTask.init("_`test`_ not-in", [.makeRange(1, 6)]),
TestTask.init(" _`test`_ not from start", [.makeRange(2, 6)]),
TestTask.init(" _`test`_ _`test`_", [.makeRange(2, 6), .makeRange(11, 6)])
]
]
testCasesByItems.forEach({ (item, tasks) in
let detector = WrappersDetector.init(searchingItems: [item])
for task in tasks {
let text = NSMutableAttributedString.init(string: task.string)
try! detector.process(text: text)
let attr = item.detectorAttributeName.rawValue
var expectedRanges = task.expectedRanges
text.enumerateAttribute(NSAttributedStringKey(rawValue: attr), in: task.wholeRange, options: [], using: { value, range, stop in
guard let nonnullValue = value else {
return
}
guard let value = nonnullValue as? WrappersDetector.SpotAttribute else {
XCTFail("Unexpected attribute type")
return
}
XCTAssertEqual(value.item, item)
guard let idx = expectedRanges.index(where: { NSEqualRanges($0, range)}) else {
XCTFail("Could not found \(range.descr) in \(expectedRanges.map({$0.descr}))")
return
}
expectedRanges.remove(at: idx)
})
}
})
}
}