@@ -355,6 +355,8 @@ class TestNSMutableAttributedString : XCTestCase {
355
355
static var allTests : [ ( String , ( TestNSMutableAttributedString ) -> ( ) throws -> Void ) ] {
356
356
return [
357
357
( " test_initWithString " , test_initWithString) ,
358
+ ( " test_initWithStringAndAttributes " , test_initWithStringAndAttributes) ,
359
+ ( " test_initWithAttributedString " , test_initWithAttributedString) ,
358
360
( " test_addAttribute " , test_addAttribute) ,
359
361
( " test_addAttributes " , test_addAttributes) ,
360
362
( " test_setAttributes " , test_setAttributes) ,
@@ -373,6 +375,56 @@ class TestNSMutableAttributedString : XCTestCase {
373
375
XCTAssertEqual ( mutableAttrString. mutableString, NSMutableString ( string: string) )
374
376
}
375
377
378
+ func test_initWithStringAndAttributes( ) {
379
+ let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
380
+ let attributes : [ NSAttributedStringKey : Any ] = [ NSAttributedStringKey ( " attribute.placeholder.key " ) : " attribute.placeholder.value " ]
381
+
382
+ let mutableAttrString = NSMutableAttributedString ( string: string, attributes: attributes)
383
+ XCTAssertEqual ( mutableAttrString. mutableString, NSMutableString ( string: string) )
384
+ XCTAssertEqual ( mutableAttrString. length, string. utf16. count)
385
+
386
+ var range = NSRange ( )
387
+ let attrs = mutableAttrString. attributes ( at: 0 , effectiveRange: & range)
388
+ guard let value = attrs [ NSAttributedStringKey ( " attribute.placeholder.key " ) ] as? String else {
389
+ XCTAssert ( false , " attribute value not found " )
390
+ return
391
+ }
392
+ XCTAssertEqual ( range. location, 0 )
393
+ XCTAssertEqual ( range. length, mutableAttrString. length)
394
+ XCTAssertEqual ( value, " attribute.placeholder.value " )
395
+
396
+ let invalidAttribute = mutableAttrString. attribute ( NSAttributedStringKey ( " invalid " ) , at: 0 , effectiveRange: & range)
397
+ XCTAssertNil ( invalidAttribute)
398
+ XCTAssertEqual ( range. location, 0 )
399
+ XCTAssertEqual ( range. length, string. utf16. count)
400
+
401
+ let attribute = mutableAttrString. attribute ( NSAttributedStringKey ( " attribute.placeholder.key " ) , at: 0 , effectiveRange: & range)
402
+ XCTAssertEqual ( range. location, 0 )
403
+ XCTAssertEqual ( range. length, mutableAttrString. length)
404
+ guard let validAttribute = attribute as? NSString else {
405
+ XCTAssert ( false , " attribute not found " )
406
+ return
407
+ }
408
+ XCTAssertEqual ( validAttribute, " attribute.placeholder.value " )
409
+ }
410
+
411
+ 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)
419
+
420
+ let initializedMutableAttrString = NSMutableAttributedString ( attributedString: mutableAttrString)
421
+ XCTAssertTrue ( initializedMutableAttrString. isEqual ( to: mutableAttrString) )
422
+
423
+ // changing the mutable attr string should not affect the initialized attr string
424
+ mutableAttrString. append ( mutableAttrString)
425
+ XCTAssertEqual ( initializedMutableAttrString. mutableString, NSMutableString ( string: string1) )
426
+ }
427
+
376
428
func test_addAttribute( ) {
377
429
let string = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. "
378
430
let mutableAttrString = NSMutableAttributedString ( string: string)
0 commit comments