@@ -23,14 +23,18 @@ 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) ,
27
26
( " test_initWithStringAndAttributes " , test_initWithStringAndAttributes) ,
27
+ ( " test_initWithAttributedString " , test_initWithAttributedString) ,
28
+ ( " test_attributedSubstring " , test_attributedSubstring) ,
28
29
( " test_longestEffectiveRange " , test_longestEffectiveRange) ,
29
30
( " test_enumerateAttributeWithName " , test_enumerateAttributeWithName) ,
30
31
( " test_enumerateAttributes " , test_enumerateAttributes) ,
32
+ ( " test_copy " , test_copy) ,
33
+ ( " test_mutableCopy " , test_mutableCopy) ,
34
+ ( " test_isEqual " , test_isEqual) ,
31
35
]
32
36
}
33
-
37
+
34
38
func test_initWithString( ) {
35
39
let string = " Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ "
36
40
let attrString = NSAttributedString ( string: string)
@@ -49,26 +53,6 @@ class TestNSAttributedString : XCTestCase {
49
53
XCTAssertEqual ( range. length, string. utf16. count)
50
54
}
51
55
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
-
72
56
func test_initWithStringAndAttributes( ) {
73
57
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
74
58
let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
@@ -102,6 +86,43 @@ class TestNSAttributedString : XCTestCase {
102
86
XCTAssertEqual ( validAttribute, " attribute.placeholder.value " )
103
87
}
104
88
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)
97
+
98
+ let initializedAttrString = NSAttributedString ( attributedString: mutableAttrString)
99
+ XCTAssertTrue ( initializedAttrString. isEqual ( to: mutableAttrString) )
100
+
101
+ // changing the mutable attr string should not affect the initialized attr string
102
+ mutableAttrString. append ( mutableAttrString)
103
+ XCTAssertEqual ( initializedAttrString. string, string1)
104
+ }
105
+
106
+ func test_attributedSubstring( ) {
107
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
108
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
109
+
110
+ let attrString = NSAttributedString ( string: string, attributes: attributes)
111
+ let subStringRange = NSRange ( location: 0 , length: 26 )
112
+ let substring = attrString. attributedSubstring ( from: subStringRange)
113
+ XCTAssertEqual ( substring. string, " Lorem ipsum dolor sit amet " )
114
+
115
+ var range = NSRange ( )
116
+ let attrs = attrString. attributes ( at: 0 , effectiveRange: & range)
117
+ guard let value = attrs [ NSAttributedStringKey ( " attribute.placeholder.key " ) ] as? String else {
118
+ XCTAssert ( false , " attribute value not found " )
119
+ return
120
+ }
121
+ XCTAssertEqual ( range. location, 0 )
122
+ XCTAssertEqual ( range. length, attrString. length)
123
+ XCTAssertEqual ( value, " attribute.placeholder.value " )
124
+ }
125
+
105
126
func test_longestEffectiveRange( ) {
106
127
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
107
128
@@ -239,6 +260,68 @@ class TestNSAttributedString : XCTestCase {
239
260
XCTAssertEqual ( attrsDescriptionString, " [attribute.placeholder.key1:attribute.placeholder.value1] " )
240
261
#endif
241
262
}
263
+
264
+ func test_copy( ) {
265
+ 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] )
271
+ let attrStringCopy = originalAttrString. copy ( )
272
+ XCTAssertTrue ( attrStringCopy is NSAttributedString )
273
+ XCTAssertFalse ( attrStringCopy is NSMutableAttributedString )
274
+ XCTAssertTrue ( ( attrStringCopy as! NSAttributedString ) . isEqual ( to: originalAttrString) )
275
+
276
+ let originalMutableAttrString = NSMutableAttributedString ( string: string)
277
+ originalMutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
278
+ let mutableAttrStringCopy = originalMutableAttrString. copy ( )
279
+ XCTAssertTrue ( mutableAttrStringCopy is NSAttributedString )
280
+ XCTAssertFalse ( mutableAttrStringCopy is NSMutableAttributedString )
281
+ XCTAssertTrue ( ( mutableAttrStringCopy as! NSAttributedString ) . isEqual ( to: originalMutableAttrString) )
282
+ }
283
+
284
+ func test_mutableCopy( ) {
285
+ 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)
289
+
290
+ let originalAttrString = NSAttributedString ( string: string, attributes: [ attrKey : attrValue] )
291
+ let attrStringMutableCopy = originalAttrString. mutableCopy ( )
292
+ XCTAssertTrue ( attrStringMutableCopy is NSMutableAttributedString )
293
+ XCTAssertTrue ( ( attrStringMutableCopy as! NSMutableAttributedString ) . isEqual ( to: originalAttrString) )
294
+
295
+ let originalMutableAttrString = NSMutableAttributedString ( string: string)
296
+ originalMutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
297
+ let mutableAttrStringMutableCopy = originalMutableAttrString. mutableCopy ( )
298
+ XCTAssertTrue ( mutableAttrStringMutableCopy is NSMutableAttributedString )
299
+ XCTAssertTrue ( ( mutableAttrStringMutableCopy as! NSMutableAttributedString ) . isEqual ( to: originalMutableAttrString) )
300
+ }
301
+
302
+ func test_isEqual( ) {
303
+ 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)
307
+
308
+ let attrString = NSAttributedString ( string: string, attributes: [ attrKey : attrValue] )
309
+
310
+ let attrString2 = NSAttributedString ( string: string, attributes: [ attrKey : attrValue] )
311
+ XCTAssertTrue ( attrString. isEqual ( to: attrString2) )
312
+
313
+ let mutableAttrString = NSMutableAttributedString ( string: string)
314
+ mutableAttrString. addAttribute ( attrKey, value: attrValue, range: attrRange)
315
+ XCTAssertTrue ( attrString. isEqual ( to: mutableAttrString) )
316
+ XCTAssertTrue ( mutableAttrString. isEqual ( to: attrString) )
317
+
318
+ let newAttrs : [ NSAttributedStringKey : Any ] = [
319
+ NSAttributedStringKey ( " attribute.placeholder.key2 " ) : " attribute.placeholder.value2 "
320
+ ]
321
+ mutableAttrString. addAttributes ( newAttrs, range: attrRange)
322
+ XCTAssertFalse ( attrString. isEqual ( to: mutableAttrString) )
323
+ XCTAssertFalse ( mutableAttrString. isEqual ( to: attrString) )
324
+ }
242
325
}
243
326
244
327
fileprivate extension TestNSAttributedString {
@@ -280,6 +363,7 @@ class TestNSMutableAttributedString : XCTestCase {
280
363
( " test_insert " , test_insert) ,
281
364
( " test_append " , test_append) ,
282
365
( " test_deleteCharacters " , test_deleteCharacters) ,
366
+ ( " test_setAttributedString " , test_setAttributedString) ,
283
367
]
284
368
}
285
369
@@ -493,4 +577,29 @@ class TestNSMutableAttributedString : XCTestCase {
493
577
XCTAssertEqual ( longestEffectiveRange. location, expectedLongestEffectiveRange. location)
494
578
XCTAssertEqual ( longestEffectiveRange. length, expectedLongestEffectiveRange. length)
495
579
}
580
+
581
+ func test_setAttributedString( ) {
582
+ let string1 = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
583
+ let attrKey1 = NSAttributedStringKey ( " attribute.placeholder.key1 " )
584
+ let attrValue1 = " attribute.placeholder.value1 "
585
+ let attrRange1 = NSRange ( location: 0 , length: string1. utf8. count)
586
+
587
+ let mutableAttrString = NSMutableAttributedString ( string: string1)
588
+ mutableAttrString. addAttribute ( attrKey1, value: attrValue1, range: attrRange1)
589
+
590
+ let string2 = " Sample set attributed string. "
591
+ let attrKey2 = NSAttributedStringKey ( " attribute.placeholder.key2 " )
592
+ let attrValue2 = " attribute.placeholder.value2 "
593
+ let attrRange2 = NSRange ( location: 0 , length: string2. utf8. count)
594
+
595
+ let replacementAttrString = NSMutableAttributedString ( string: string2)
596
+ replacementAttrString. addAttribute ( attrKey2, value: attrValue2, range: attrRange2)
597
+
598
+ mutableAttrString. setAttributedString ( replacementAttrString)
599
+ XCTAssertTrue ( mutableAttrString. isEqual ( to: replacementAttrString) )
600
+
601
+ // changing the replacement attr string should not affect the replaced attr string
602
+ replacementAttrString. append ( replacementAttrString)
603
+ XCTAssertEqual ( mutableAttrString. string, string2)
604
+ }
496
605
}
0 commit comments