@@ -87,20 +87,16 @@ class TestNSAttributedString : XCTestCase {
87
87
}
88
88
89
89
func test_initWithAttributedString( ) {
90
- let string1 = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
91
- let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
92
- let attrValue1 = " attribute.placeholder.value1 "
93
- let attrRange1 = NSRange ( location: 0 , length: string1. utf8. count)
94
-
95
- let mutableAttrString = NSMutableAttributedString ( string: string1)
96
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
90
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
91
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
92
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
97
93
98
94
let initializedAttrString = NSAttributedString ( attributedString: mutableAttrString)
99
95
XCTAssertTrue ( initializedAttrString. isEqual ( to: mutableAttrString) )
100
96
101
97
// changing the mutable attr string should not affect the initialized attr string
102
98
mutableAttrString. append ( mutableAttrString)
103
- XCTAssertEqual ( initializedAttrString. string, string1 )
99
+ XCTAssertEqual ( initializedAttrString. string, string )
104
100
}
105
101
106
102
func test_attributedSubstring( ) {
@@ -263,18 +259,15 @@ class TestNSAttributedString : XCTestCase {
263
259
264
260
func test_copy( ) {
265
261
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
266
- let attrKey = NSAttributedStringKey ( " attribute.placeholder.key " )
267
- let attrValue = " attribute.placeholder.value "
268
- let attrRange = NSRange ( location: 0 , length: string. utf8. count)
269
-
270
- let originalAttrString = NSAttributedString ( string: string, attributes: [ attrKey : attrValue] )
262
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
263
+
264
+ let originalAttrString = NSAttributedString ( string: string, attributes: attributes)
271
265
let attrStringCopy = originalAttrString. copy ( )
272
266
XCTAssertTrue ( attrStringCopy is NSAttributedString )
273
267
XCTAssertFalse ( attrStringCopy is NSMutableAttributedString )
274
268
XCTAssertTrue ( ( attrStringCopy as! NSAttributedString ) . isEqual ( to: originalAttrString) )
275
269
276
- let originalMutableAttrString = NSMutableAttributedString ( string: string)
277
- originalMutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
270
+ let originalMutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
278
271
let mutableAttrStringCopy = originalMutableAttrString. copy ( )
279
272
XCTAssertTrue ( mutableAttrStringCopy is NSAttributedString )
280
273
XCTAssertFalse ( mutableAttrStringCopy is NSMutableAttributedString )
@@ -283,42 +276,35 @@ class TestNSAttributedString : XCTestCase {
283
276
284
277
func test_mutableCopy( ) {
285
278
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
286
- let attrKey = NSAttributedStringKey ( " attribute.placeholder.key " )
287
- let attrValue = " attribute.placeholder.value "
288
- let attrRange = NSRange ( location: 0 , length: string. utf8. count)
279
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
289
280
290
- let originalAttrString = NSAttributedString ( string: string, attributes: [ attrKey : attrValue ] )
281
+ let originalAttrString = NSAttributedString ( string: string, attributes: attributes )
291
282
let attrStringMutableCopy = originalAttrString. mutableCopy ( )
292
283
XCTAssertTrue ( attrStringMutableCopy is NSMutableAttributedString )
293
284
XCTAssertTrue ( ( attrStringMutableCopy as! NSMutableAttributedString ) . isEqual ( to: originalAttrString) )
294
285
295
- let originalMutableAttrString = NSMutableAttributedString ( string: string)
296
- originalMutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
286
+ let originalMutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
297
287
let mutableAttrStringMutableCopy = originalMutableAttrString. mutableCopy ( )
298
288
XCTAssertTrue ( mutableAttrStringMutableCopy is NSMutableAttributedString )
299
289
XCTAssertTrue ( ( mutableAttrStringMutableCopy as! NSMutableAttributedString ) . isEqual ( to: originalMutableAttrString) )
300
290
}
301
291
302
292
func test_isEqual( ) {
303
293
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
304
- let attrKey = NSAttributedStringKey ( " attribute.placeholder.key " )
305
- let attrValue = " attribute.placeholder.value "
306
- let attrRange = NSRange ( location: 0 , length: string. utf8. count)
294
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
307
295
308
- let attrString = NSAttributedString ( string: string, attributes: [ attrKey : attrValue ] )
296
+ let attrString = NSAttributedString ( string: string, attributes: attributes )
309
297
310
- let attrString2 = NSAttributedString ( string: string, attributes: [ attrKey : attrValue ] )
298
+ let attrString2 = NSAttributedString ( string: string, attributes: attributes )
311
299
XCTAssertTrue ( attrString. isEqual ( to: attrString2) )
312
300
313
- let mutableAttrString = NSMutableAttributedString ( string: string)
314
- mutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
301
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
315
302
XCTAssertTrue ( attrString. isEqual ( to: mutableAttrString) )
316
303
XCTAssertTrue ( mutableAttrString. isEqual ( to: attrString) )
317
304
318
- let newAttrs : [ NSAttributedStringKey : Any ] = [
319
- NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 "
320
- ]
321
- mutableAttrString. addAttributes ( newAttrs, range: attrRange)
305
+ let newAttrs : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ]
306
+ let newAttrsRange = NSRange ( string. startIndex... , in: string)
307
+ mutableAttrString. addAttributes ( newAttrs, range: newAttrsRange)
322
308
XCTAssertFalse ( attrString. isEqual ( to: mutableAttrString) )
323
309
XCTAssertFalse ( mutableAttrString. isEqual ( to: attrString) )
324
310
}
@@ -409,20 +395,17 @@ class TestNSMutableAttributedString : XCTestCase {
409
395
}
410
396
411
397
func test_initWithAttributedString( ) {
412
- let string1 = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
413
- let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
414
- let attrValue1 = " attribute.placeholder.value1 "
415
- let attrRange1 = NSRange ( location: 0 , length: string1. utf8. count)
416
-
417
- let mutableAttrString = NSMutableAttributedString ( string: string1)
418
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
398
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
399
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
400
+
401
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
419
402
420
403
let initializedMutableAttrString = NSMutableAttributedString ( attributedString: mutableAttrString)
421
404
XCTAssertTrue ( initializedMutableAttrString. isEqual ( to: mutableAttrString) )
422
405
423
406
// changing the mutable attr string should not affect the initialized attr string
424
407
mutableAttrString. append ( mutableAttrString)
425
- XCTAssertEqual ( initializedMutableAttrString. mutableString, NSMutableString ( string: string1 ) )
408
+ XCTAssertEqual ( initializedMutableAttrString. mutableString, NSMutableString ( string: string ) )
426
409
}
427
410
428
411
func test_addAttribute( ) {
@@ -497,15 +480,12 @@ class TestNSMutableAttributedString : XCTestCase {
497
480
498
481
func test_replaceCharactersWithString( ) {
499
482
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
500
- let mutableAttrString = NSMutableAttributedString ( string: string)
501
-
502
483
let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
503
484
let attrValue1 = " attribute.placeholder.value1 "
504
- let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
505
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
506
-
485
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: [ attrKey1 : attrValue1] )
486
+
507
487
let replacement = " Sample replacement "
508
- let replacementRange = NSRange ( location : 0 , length : replacement. utf16 . count )
488
+ let replacementRange = NSRange ( replacement . startIndex ... , in : replacement)
509
489
510
490
mutableAttrString. replaceCharacters ( in: replacementRange, with: replacement)
511
491
@@ -522,13 +502,10 @@ class TestNSMutableAttributedString : XCTestCase {
522
502
523
503
func test_replaceCharactersWithAttributedString( ) {
524
504
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
525
- let mutableAttrString = NSMutableAttributedString ( string: string)
526
-
527
505
let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
528
506
let attrValue1 = " attribute.placeholder.value1 "
529
- let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
530
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
531
-
507
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: [ attrKey1 : attrValue1] )
508
+
532
509
let replacement = " Sample replacement "
533
510
let replacementAttrKey = NSAttributedStringKey ( " attribute.replacement.key " )
534
511
let replacementAttributes : [ NSAttributedStringKey : Any ] = [ replacementAttrKey : " attribute.replacement.value " ]
@@ -553,13 +530,10 @@ class TestNSMutableAttributedString : XCTestCase {
553
530
554
531
func test_insert( ) {
555
532
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
556
- let mutableAttrString = NSMutableAttributedString ( string: string)
557
-
558
533
let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
559
534
let attrValue1 = " attribute.placeholder.value1 "
560
- let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
561
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
562
-
535
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: [ attrKey1 : attrValue1] )
536
+
563
537
let insertString = " Sample insertion. "
564
538
let insertAttrKey = NSAttributedStringKey ( " attribute.insertion.key " )
565
539
let insertAttributes : [ NSAttributedStringKey : Any ] = [ insertAttrKey : " attribute.insertion.value " ]
@@ -581,12 +555,9 @@ class TestNSMutableAttributedString : XCTestCase {
581
555
582
556
func test_append( ) {
583
557
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
584
- let mutableAttrString = NSMutableAttributedString ( string: string)
585
-
586
558
let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
587
559
let attrValue1 = " attribute.placeholder.value1 "
588
- let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
589
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
560
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: [ attrKey1 : attrValue1] )
590
561
591
562
let appendString = " Sample appending. "
592
563
let appendAttrKey = NSAttributedStringKey ( " attribute.appending.key " )
@@ -609,43 +580,36 @@ class TestNSMutableAttributedString : XCTestCase {
609
580
610
581
func test_deleteCharacters( ) {
611
582
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
612
- let mutableAttrString = NSMutableAttributedString ( string: string)
613
-
614
583
let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
615
584
let attrValue1 = " attribute.placeholder.value1 "
616
- let attrRange1 = NSRange ( location: 0 , length: mutableAttrString. length)
617
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
585
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: [ attrKey1 : attrValue1] )
618
586
619
587
let deleteRange = NSRange ( location: 0 , length: 10 )
620
588
mutableAttrString. deleteCharacters ( in: deleteRange)
621
589
622
590
let expectedString = String ( string [ string. startIndex. advanced ( by: 10 ) ... ] )
623
591
XCTAssertEqual ( mutableAttrString. string, expectedString)
624
592
625
- let expectedLongestEffectiveRange = NSRange ( location : 0 , length : expectedString. utf16 . count )
593
+ let expectedLongestEffectiveRange = NSRange ( expectedString . startIndex ... , in : expectedString)
626
594
var longestEffectiveRange = NSRange ( )
627
595
let searchRange = NSRange ( location: 0 , length: mutableAttrString. length)
628
- _ = mutableAttrString. attribute ( attrKey1, at: 0 , longestEffectiveRange: & longestEffectiveRange, in: searchRange)
596
+ guard let value = mutableAttrString. attribute ( attrKey1, at: 0 , longestEffectiveRange: & longestEffectiveRange, in: searchRange) as? NSString else {
597
+ XCTAssert ( false , " attribute not found " )
598
+ return
599
+ }
600
+ XCTAssertEqual ( value, " attribute.placeholder.value1 " )
629
601
XCTAssertEqual ( longestEffectiveRange. location, expectedLongestEffectiveRange. location)
630
602
XCTAssertEqual ( longestEffectiveRange. length, expectedLongestEffectiveRange. length)
631
603
}
632
604
633
605
func test_setAttributedString( ) {
634
606
let string1 = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
635
- let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
636
- let attrValue1 = " attribute.placeholder.value1 "
637
- let attrRange1 = NSRange ( location: 0 , length: string1. utf8. count)
607
+ let attributes1 : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key1 " ) : " attribute.placeholder.value1 " ]
608
+ let mutableAttrString = NSMutableAttributedString ( string: string1, attributes: attributes1)
638
609
639
- let mutableAttrString = NSMutableAttributedString ( string: string1)
640
- mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
641
-
642
610
let string2 = " Sample set attributed string. "
643
- let attrKey2 = NSAttributedStringKey ( " attribute.placeholder.key2 " )
644
- let attrValue2 = " attribute.placeholder.value2 "
645
- let attrRange2 = NSRange ( location: 0 , length: string2. utf8. count)
646
-
647
- let replacementAttrString = NSMutableAttributedString ( string: string2)
648
- replacementAttrString. addAttribute ( attrKey2, value: attrValue2, range: attrRange2)
611
+ let attributes2 : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 " ]
612
+ let replacementAttrString = NSMutableAttributedString ( string: string2, attributes: attributes2)
649
613
650
614
mutableAttrString. setAttributedString ( replacementAttrString)
651
615
XCTAssertTrue ( mutableAttrString. isEqual ( to: replacementAttrString) )
0 commit comments