@@ -23,6 +23,7 @@ class TestNSAttributedString : XCTestCase {
23
23
static var allTests : [ ( String , ( TestNSAttributedString ) -> ( ) throws -> Void ) ] {
24
24
return [
25
25
( " test_initWithString " , test_initWithString) ,
26
+ ( " test_attributedSubstring " , test_attributedSubstring) ,
26
27
( " test_initWithStringAndAttributes " , test_initWithStringAndAttributes) ,
27
28
( " test_longestEffectiveRange " , test_longestEffectiveRange) ,
28
29
( " test_enumerateAttributeWithName " , test_enumerateAttributeWithName) ,
@@ -48,9 +49,29 @@ class TestNSAttributedString : XCTestCase {
48
49
XCTAssertEqual ( range. length, string. utf16. count)
49
50
}
50
51
52
+ func test_attributedSubstring( ) {
53
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
54
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
55
+
56
+ let attrString = NSAttributedString ( string: string, attributes: attributes)
57
+ let subStringRange = NSRange ( location: 0 , length: 26 )
58
+ let substring = attrString. attributedSubstring ( from: subStringRange)
59
+ XCTAssertEqual ( substring. string, " Lorem ipsum dolor sit amet " )
60
+
61
+ var range = NSRange ( )
62
+ let attrs = attrString. attributes ( at: 0 , effectiveRange: & range)
63
+ guard let value = attrs [ NSAttributedStringKey ( " attribute.placeholder.key " ) ] as? String else {
64
+ XCTAssert ( false , " attribute value not found " )
65
+ return
66
+ }
67
+ XCTAssertEqual ( range. location, 0 )
68
+ XCTAssertEqual ( range. length, attrString. length)
69
+ XCTAssertEqual ( value, " attribute.placeholder.value " )
70
+ }
71
+
51
72
func test_initWithStringAndAttributes( ) {
52
73
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
53
- let attributes : [ NSAttributedStringKey : AnyObject ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " as NSString ]
74
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
54
75
55
76
let attrString = NSAttributedString ( string: string, attributes: attributes)
56
77
XCTAssertEqual ( attrString. string, string)
@@ -85,7 +106,7 @@ class TestNSAttributedString : XCTestCase {
85
106
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
86
107
87
108
let attrKey = NSAttributedStringKey ( " attribute.placeholder.key " )
88
- let attrValue = " attribute.placeholder.value " as NSString
109
+ let attrValue = " attribute.placeholder.value "
89
110
90
111
let attrRange1 = NSRange ( location: 0 , length: 20 )
91
112
let attrRange2 = NSRange ( location: 18 , length: 10 )
@@ -254,6 +275,11 @@ class TestNSMutableAttributedString : XCTestCase {
254
275
( " test_addAttribute " , test_addAttribute) ,
255
276
( " test_addAttributes " , test_addAttributes) ,
256
277
( " test_setAttributes " , test_setAttributes) ,
278
+ ( " test_replaceCharactersWithString " , test_replaceCharactersWithString) ,
279
+ ( " test_replaceCharactersWithAttributedString " , test_replaceCharactersWithAttributedString) ,
280
+ ( " test_insert " , test_insert) ,
281
+ ( " test_append " , test_append) ,
282
+ ( " test_deleteCharacters " , test_deleteCharacters) ,
257
283
]
258
284
}
259
285
@@ -332,4 +358,139 @@ class TestNSMutableAttributedString : XCTestCase {
332
358
let emptyResult = mutableAttrString. attributes ( at: 10 , effectiveRange: nil )
333
359
XCTAssertTrue ( emptyResult. isEmpty)
334
360
}
361
+
362
+ func test_replaceCharactersWithString( ) {
363
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
364
+ let mutableAttrString = NSMutableAttributedString ( string: string)
365
+
366
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
367
+ let attrValue1 = " attribute.placeholder.value1 "
368
+ let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
369
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
370
+
371
+ let replacement = " Sample replacement "
372
+ let replacementRange = NSRange ( location: 0 , length: replacement. utf16. count)
373
+
374
+ mutableAttrString. replaceCharacters ( in: replacementRange, with: replacement)
375
+
376
+ let expectedString = string. replacingCharacters ( in: replacement. startIndex..< replacement. endIndex, with: replacement)
377
+ XCTAssertEqual ( mutableAttrString. string, expectedString)
378
+
379
+ let attrValue = mutableAttrString. attribute ( attrKey1, at: 0 , effectiveRange: nil )
380
+ guard let validAttribute = attrValue as? NSString else {
381
+ XCTAssert ( false , " attribute not found " )
382
+ return
383
+ }
384
+ XCTAssertEqual ( validAttribute, " attribute.placeholder.value1 " )
385
+ }
386
+
387
+ func test_replaceCharactersWithAttributedString( ) {
388
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
389
+ let mutableAttrString = NSMutableAttributedString ( string: string)
390
+
391
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
392
+ let attrValue1 = " attribute.placeholder.value1 "
393
+ let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
394
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
395
+
396
+ let replacement = " Sample replacement "
397
+ let replacementAttrKey = NSAttributedStringKey ( " attribute.replacement.key " )
398
+ let replacementAttributes : [ NSAttributedStringKey : Any ] = [ replacementAttrKey : " attribute.replacement.value " ]
399
+
400
+ let replacementAttrString = NSAttributedString ( string: replacement, attributes: replacementAttributes)
401
+ let replacementRange = NSRange ( location: 0 , length: replacementAttrString. length)
402
+
403
+ mutableAttrString. replaceCharacters ( in: replacementRange, with: replacementAttrString)
404
+
405
+ let expectedString = string. replacingCharacters ( in: replacement. startIndex..< replacement. endIndex, with: replacement)
406
+ XCTAssertEqual ( mutableAttrString. string, expectedString)
407
+
408
+ // the original attribute should be replaced
409
+ XCTAssertNil ( mutableAttrString. attribute ( attrKey1, at: 0 , effectiveRange: nil ) )
410
+
411
+ guard let attrValue = mutableAttrString. attribute ( replacementAttrKey, at: 0 , effectiveRange: nil ) as? NSString else {
412
+ XCTAssert ( false , " attribute not found " )
413
+ return
414
+ }
415
+ XCTAssertEqual ( attrValue, " attribute.replacement.value " )
416
+ }
417
+
418
+ func test_insert( ) {
419
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
420
+ let mutableAttrString = NSMutableAttributedString ( string: string)
421
+
422
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
423
+ let attrValue1 = " attribute.placeholder.value1 "
424
+ let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
425
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
426
+
427
+ let insertString = " Sample insertion. "
428
+ let insertAttrKey = NSAttributedStringKey ( " attribute.insertion.key " )
429
+ let insertAttributes : [ NSAttributedStringKey : Any ] = [ insertAttrKey : " attribute.insertion.value " ]
430
+ let insertAttrString = NSAttributedString ( string: insertString, attributes: insertAttributes)
431
+
432
+ mutableAttrString. insert ( insertAttrString, at: 0 )
433
+
434
+ let expectedString = insertString + string
435
+ XCTAssertEqual ( mutableAttrString. string, expectedString)
436
+
437
+ let insertedAttributes = mutableAttrString. attributes ( at: 0 , effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
438
+ let expectedInserted = [ insertAttrKey : " attribute.insertion.value " ]
439
+ XCTAssertEqual ( insertedAttributes, expectedInserted)
440
+
441
+ let originalAttributes = mutableAttrString. attributes ( at: insertAttrString. length, effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
442
+ let expectedOriginal = [ attrKey1 : attrValue1]
443
+ XCTAssertEqual ( originalAttributes, expectedOriginal)
444
+ }
445
+
446
+ func test_append( ) {
447
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
448
+ let mutableAttrString = NSMutableAttributedString ( string: string)
449
+
450
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
451
+ let attrValue1 = " attribute.placeholder.value1 "
452
+ let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
453
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
454
+
455
+ let appendString = " Sample appending. "
456
+ let appendAttrKey = NSAttributedStringKey ( " attribute.appending.key " )
457
+ let appendAttributes : [ NSAttributedStringKey : Any ] = [ appendAttrKey : " attribute.appending.value " ]
458
+ let appendAttrString = NSAttributedString ( string: appendString, attributes: appendAttributes)
459
+
460
+ mutableAttrString. append ( appendAttrString)
461
+
462
+ let expectedString = string + appendString
463
+ XCTAssertEqual ( mutableAttrString. string, expectedString)
464
+
465
+ let originalAttributes = mutableAttrString. attributes ( at: 0 , effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
466
+ let expectedOriginal = [ attrKey1 : attrValue1]
467
+ XCTAssertEqual ( originalAttributes, expectedOriginal)
468
+
469
+ let appendedAttributes = mutableAttrString. attributes ( at: string. utf16. count, effectiveRange: nil ) as? [ NSAttributedStringKey : String ]
470
+ let expectedAppended = [ appendAttrKey : " attribute.appending.value " ]
471
+ XCTAssertEqual ( appendedAttributes, expectedAppended)
472
+ }
473
+
474
+ func test_deleteCharacters( ) {
475
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
476
+ let mutableAttrString = NSMutableAttributedString ( string: string)
477
+
478
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
479
+ let attrValue1 = " attribute.placeholder.value1 "
480
+ let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
481
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
482
+
483
+ let deleteRange = NSRange ( location: 0 , length: 10 )
484
+ mutableAttrString. deleteCharacters ( in: deleteRange)
485
+
486
+ let expectedString = String ( string [ string. startIndex. advanced ( by: 10 ) ... ] )
487
+ XCTAssertEqual ( mutableAttrString. string, expectedString)
488
+
489
+ let expectedLongestEffectiveRange = NSRange ( location: 0 , length: expectedString. utf16. count)
490
+ var longestEffectiveRange = NSRange ( )
491
+ let searchRange = NSRange ( location: 0 , length: mutableAttrString. length)
492
+ _ = mutableAttrString. attribute ( attrKey1, at: 0 , longestEffectiveRange: & longestEffectiveRange, in: searchRange)
493
+ XCTAssertEqual ( longestEffectiveRange. location, expectedLongestEffectiveRange. location)
494
+ XCTAssertEqual ( longestEffectiveRange. length, expectedLongestEffectiveRange. length)
495
+ }
335
496
}
0 commit comments