@@ -129,51 +129,52 @@ class TestNSRange : XCTestCase {
129
129
}
130
130
}
131
131
132
- private func _assert< S: StringProtocol , R: RangeExpression > (
132
+ /// Specialized for the below tests.
133
+ private func _assertNSRangeInit< S: StringProtocol , R: RangeExpression > (
133
134
_ region: R , in target: S , is rangeString: String
134
135
) where R. Bound == S . Index {
135
- XCTAssertEqual ( NSStringFromRange ( NSRange ( region, in: target) ) , rangeString )
136
+ XCTAssert ( NSEqualRanges ( NSRangeFromString ( rangeString ) , NSRange ( region, in: target) ) )
136
137
}
137
138
138
139
func test_init_region_in_ascii_string( ) {
139
140
// all count = 18
140
141
let normalString = " 1;DROP TABLE users "
141
142
142
- _assert ( normalString. index ( normalString. startIndex, offsetBy: 2 ) ..< normalString. index ( normalString. endIndex, offsetBy: - 6 ) , in: normalString, is: " {2, 10} " )
143
- _assert ( normalString. index ( after: normalString. startIndex) ... normalString. index ( before: normalString. endIndex) , in: normalString, is: " {1, 17} " )
144
- _assert ( normalString. startIndex... , in: normalString, is: " {0, 18} " )
145
- _assert ( ... normalString. firstIndex ( of: " " ) !, in: normalString, is: " {0, 7} " )
146
- _assert ( ..< normalString. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
143
+ _assertNSRangeInit ( normalString. index ( normalString. startIndex, offsetBy: 2 ) ..< normalString. index ( normalString. endIndex, offsetBy: - 6 ) , in: normalString, is: " {2, 10} " )
144
+ _assertNSRangeInit ( normalString. index ( after: normalString. startIndex) ... normalString. index ( before: normalString. endIndex) , in: normalString, is: " {1, 17} " )
145
+ _assertNSRangeInit ( normalString. startIndex... , in: normalString, is: " {0, 18} " )
146
+ _assertNSRangeInit ( ... normalString. firstIndex ( of: " " ) !, in: normalString, is: " {0, 7} " )
147
+ _assertNSRangeInit ( ..< normalString. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
147
148
148
149
let normalSubstring : Substring = normalString. split ( separator: " ; " ) [ 1 ]
149
150
150
- _assert ( normalSubstring. range ( of: " TABLE " ) !, in: normalSubstring, is: " {5, 5} " )
151
- _assert ( normalSubstring. index ( after: normalSubstring. firstIndex ( of: " " ) !) ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {7, 5} " )
152
- _assert ( normalSubstring. firstIndex ( of: " u " ) !... normalSubstring. lastIndex ( of: " u " ) !, in: normalSubstring, is: " {11, 1} " )
153
- _assert ( normalSubstring. startIndex... , in: normalSubstring, is: " {0, 16} " )
154
- _assert ( normalSubstring. startIndex... , in: normalString, is: " {2, 16} " )
155
- _assert ( ... normalSubstring. lastIndex ( of: " " ) !, in: normalSubstring, is: " {0, 11} " )
156
- _assert ( ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
151
+ _assertNSRangeInit ( normalSubstring. range ( of: " TABLE " ) !, in: normalSubstring, is: " {5, 5} " )
152
+ _assertNSRangeInit ( normalSubstring. index ( after: normalSubstring. firstIndex ( of: " " ) !) ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {7, 5} " )
153
+ _assertNSRangeInit ( normalSubstring. firstIndex ( of: " u " ) !... normalSubstring. lastIndex ( of: " u " ) !, in: normalSubstring, is: " {11, 1} " )
154
+ _assertNSRangeInit ( normalSubstring. startIndex... , in: normalSubstring, is: " {0, 16} " )
155
+ _assertNSRangeInit ( normalSubstring. startIndex... , in: normalString, is: " {2, 16} " )
156
+ _assertNSRangeInit ( ... normalSubstring. lastIndex ( of: " " ) !, in: normalSubstring, is: " {0, 11} " )
157
+ _assertNSRangeInit ( ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
157
158
}
158
159
159
160
func test_init_region_in_unicode_string( ) {
160
161
// count: 46, utf8: 90, utf16: 54
161
162
let unicodeString = " This is a #naughty👻 string (╯°□°)╯︵ ┻━┻👨👩👧👦) "
162
163
163
- _assert ( unicodeString. index ( unicodeString. startIndex, offsetBy: 10 ) ..< unicodeString. index ( unicodeString. startIndex, offsetBy: 28 ) , in: unicodeString, is: " {10, 19} " )
164
- _assert ( unicodeString. index ( after: unicodeString. startIndex) ... unicodeString. index ( before: unicodeString. endIndex) , in: unicodeString, is: " {1, 53} " )
165
- _assert ( unicodeString. startIndex... , in: unicodeString, is: " {0, 54} " )
166
- _assert ( ... unicodeString. firstIndex ( of: " 👻 " ) !, in: unicodeString, is: " {0, 22} " )
167
- _assert ( ..< unicodeString. range ( of: " 👨👩👧👦 " ) !. lowerBound, in: unicodeString, is: " {0, 42} " )
164
+ _assertNSRangeInit ( unicodeString. index ( unicodeString. startIndex, offsetBy: 10 ) ..< unicodeString. index ( unicodeString. startIndex, offsetBy: 28 ) , in: unicodeString, is: " {10, 19} " )
165
+ _assertNSRangeInit ( unicodeString. index ( after: unicodeString. startIndex) ... unicodeString. index ( before: unicodeString. endIndex) , in: unicodeString, is: " {1, 53} " )
166
+ _assertNSRangeInit ( unicodeString. startIndex... , in: unicodeString, is: " {0, 54} " )
167
+ _assertNSRangeInit ( ... unicodeString. firstIndex ( of: " 👻 " ) !, in: unicodeString, is: " {0, 22} " )
168
+ _assertNSRangeInit ( ..< unicodeString. range ( of: " 👨👩👧👦 " ) !. lowerBound, in: unicodeString, is: " {0, 42} " )
168
169
169
170
let unicodeSubstring : Substring = unicodeString [ unicodeString. firstIndex ( of: " 👻 " ) !... ]
170
171
171
- _assert ( unicodeSubstring. range ( of: " 👨👩👧👦 " ) !, in: unicodeSubstring, is: " {22, 11} " )
172
- _assert ( unicodeSubstring. range ( of: " 👨 " ) !. lowerBound..< unicodeSubstring. range ( of: " 👦 " ) !. upperBound, in: unicodeString, is: " {42, 11} " )
173
- _assert ( unicodeSubstring. index ( after: unicodeSubstring. startIndex) ... unicodeSubstring. index ( before: unicodeSubstring. endIndex) , in: unicodeSubstring, is: " {2, 32} " )
174
- _assert ( unicodeSubstring. startIndex... , in: unicodeSubstring, is: " {0, 34} " )
175
- _assert ( unicodeSubstring. startIndex... , in: unicodeString, is: " {20, 34} " )
176
- _assert ( ... unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeSubstring, is: " {0, 12} " )
177
- _assert ( ..< unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeString, is: " {0, 31} " )
172
+ _assertNSRangeInit ( unicodeSubstring. range ( of: " 👨👩👧👦 " ) !, in: unicodeSubstring, is: " {22, 11} " )
173
+ _assertNSRangeInit ( unicodeSubstring. range ( of: " 👨 " ) !. lowerBound..< unicodeSubstring. range ( of: " 👦 " ) !. upperBound, in: unicodeString, is: " {42, 11} " )
174
+ _assertNSRangeInit ( unicodeSubstring. index ( after: unicodeSubstring. startIndex) ... unicodeSubstring. index ( before: unicodeSubstring. endIndex) , in: unicodeSubstring, is: " {2, 32} " )
175
+ _assertNSRangeInit ( unicodeSubstring. startIndex... , in: unicodeSubstring, is: " {0, 34} " )
176
+ _assertNSRangeInit ( unicodeSubstring. startIndex... , in: unicodeString, is: " {20, 34} " )
177
+ _assertNSRangeInit ( ... unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeSubstring, is: " {0, 12} " )
178
+ _assertNSRangeInit ( ..< unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeString, is: " {0, 31} " )
178
179
}
179
180
}
0 commit comments