Skip to content

Commit f43e647

Browse files
committed
override initializers in mutable attr string and add tests
1 parent 3e541f9 commit f43e647

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Foundation/NSAttributedString.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,19 @@ open class NSMutableAttributedString : NSAttributedString {
388388
return NSAttributedString(attributedString: self)
389389
}
390390

391-
public override init(string str: String) {
392-
super.init(string: str)
393-
_string = NSMutableString(string: str)
391+
public override init(string: String) {
392+
super.init(string: string)
393+
_string = NSMutableString(string: string)
394+
}
395+
396+
public override init(string: String, attributes attrs: [NSAttributedStringKey : Any]? = nil) {
397+
super.init(string: string, attributes: attrs)
398+
_string = NSMutableString(string: string)
399+
}
400+
401+
public override init(attributedString: NSAttributedString) {
402+
super.init(attributedString: attributedString)
403+
_string = NSMutableString(string: attributedString.string)
394404
}
395405

396406
public required init?(coder aDecoder: NSCoder) {

TestFoundation/TestNSAttributedString.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ class TestNSMutableAttributedString : XCTestCase {
355355
static var allTests: [(String, (TestNSMutableAttributedString) -> () throws -> Void)] {
356356
return [
357357
("test_initWithString", test_initWithString),
358+
("test_initWithStringAndAttributes", test_initWithStringAndAttributes),
359+
("test_initWithAttributedString", test_initWithAttributedString),
358360
("test_addAttribute", test_addAttribute),
359361
("test_addAttributes", test_addAttributes),
360362
("test_setAttributes", test_setAttributes),
@@ -373,6 +375,56 @@ class TestNSMutableAttributedString : XCTestCase {
373375
XCTAssertEqual(mutableAttrString.mutableString, NSMutableString(string: string))
374376
}
375377

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+
376428
func test_addAttribute() {
377429
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
378430
let mutableAttrString = NSMutableAttributedString(string: string)

0 commit comments

Comments
 (0)