@@ -251,12 +251,85 @@ class TestNSMutableAttributedString : XCTestCase {
251
251
static var allTests : [ ( String , ( TestNSMutableAttributedString ) -> ( ) throws -> Void ) ] {
252
252
return [
253
253
( " test_initWithString " , test_initWithString) ,
254
+ ( " test_addAttribute " , test_addAttribute) ,
255
+ ( " test_addAttributes " , test_addAttributes) ,
256
+ ( " test_setAttributes " , test_setAttributes) ,
254
257
]
255
258
}
256
259
257
260
func test_initWithString( ) {
258
261
let string = " Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ "
259
262
let mutableAttrString = NSMutableAttributedString ( string: string)
260
263
XCTAssertEqual ( mutableAttrString. mutableString, NSMutableString ( string: string) )
261
- }
264
+ }
265
+
266
+ func test_addAttribute( ) {
267
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
268
+ let mutableAttrString = NSMutableAttributedString ( string: string)
269
+
270
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
271
+ let attrValue1 = " attribute.placeholder.value1 "
272
+ let attrRange1 = NSRange ( location: 0 , length: 20 )
273
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
274
+
275
+ let attrValue = mutableAttrString. attribute ( attrKey1, at: 10 , effectiveRange: nil )
276
+ guard let validAttribute = attrValue as? NSString else {
277
+ XCTAssert ( false , " attribute not found " )
278
+ return
279
+ }
280
+ XCTAssertEqual ( validAttribute, " attribute.placeholder.value1 " )
281
+ }
282
+
283
+ func test_addAttributes( ) {
284
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
285
+ let mutableAttrString = NSMutableAttributedString ( string: string)
286
+
287
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
288
+ let attrValue1 = " attribute.placeholder.value1 "
289
+ let attrRange1 = NSRange ( location: 0 , length: 20 )
290
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
291
+
292
+ let attrs2 = [
293
+ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ,
294
+ NSAttributedStringKey ( " attribute.placeholder.key3 " ) : " attribute.placeholder.value3 " ,
295
+ ]
296
+ let attrRange2 = NSRange ( location: 0 , length: 20 )
297
+ mutableAttrString. addAttributes ( attrs2, range: attrRange2)
298
+
299
+ let result = mutableAttrString. attributes ( at: 10 , effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
300
+ let expectedResult : [ NSAttributedStringKey : String ] = [
301
+ NSAttributedStringKey ( " attribute.placeholder.key1 " ) : " attribute.placeholder.value1 " ,
302
+ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ,
303
+ NSAttributedStringKey ( " attribute.placeholder.key3 " ) : " attribute.placeholder.value3 " ,
304
+ ]
305
+ XCTAssertEqual ( result, expectedResult)
306
+ }
307
+
308
+ func test_setAttributes( ) {
309
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
310
+ let mutableAttrString = NSMutableAttributedString ( string: string)
311
+
312
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
313
+ let attrValue1 = " attribute.placeholder.value1 "
314
+ let attrRange1 = NSRange ( location: 0 , length: 20 )
315
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
316
+
317
+ let attrs2 = [
318
+ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ,
319
+ NSAttributedStringKey ( " attribute.placeholder.key3 " ) : " attribute.placeholder.value3 " ,
320
+ ]
321
+ let attrRange2 = NSRange ( location: 0 , length: 20 )
322
+ mutableAttrString. setAttributes ( attrs2, range: attrRange2)
323
+
324
+ let result = mutableAttrString. attributes ( at: 10 , effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
325
+ let expectedResult : [ NSAttributedStringKey : String ] = [
326
+ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ,
327
+ NSAttributedStringKey ( " attribute.placeholder.key3 " ) : " attribute.placeholder.value3 " ,
328
+ ]
329
+ XCTAssertEqual ( result, expectedResult)
330
+
331
+ mutableAttrString. setAttributes ( nil , range: attrRange2)
332
+ let emptyResult = mutableAttrString. attributes ( at: 10 , effectiveRange: nil )
333
+ XCTAssertTrue ( emptyResult. isEmpty)
334
+ }
262
335
}
0 commit comments