Skip to content

Commit 722ed72

Browse files
committed
add tests
1 parent 1c5b007 commit 722ed72

File tree

2 files changed

+167
-19
lines changed

2 files changed

+167
-19
lines changed

Foundation/NSAttributedString.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,26 +342,13 @@ open class NSMutableAttributedString : NSAttributedString {
342342
}
343343

344344
open func insert(_ attrString: NSAttributedString, at loc: Int) {
345-
mutableString.insert(attrString.string, at: loc)
346-
347-
let fullStringRange = NSRange(location: 0, length: attrString._string.length)
348-
attrString.enumerateAttributes(in: fullStringRange) { attribute, range, _ in
349-
var rangeAfterInserting = range
350-
rangeAfterInserting.location += loc
351-
addAttributes(attribute, range: rangeAfterInserting)
352-
}
345+
let insertRange = NSRange(location: loc, length: 0)
346+
replaceCharacters(in: insertRange, with: attrString)
353347
}
354348

355349
open func append(_ attrString: NSAttributedString) {
356-
let lengthBeforeAppending = string.length
357-
mutableString.append(attrString.string)
358-
359-
let fullStringRange = NSRange(location: 0, length: attrString._string.length)
360-
attrString.enumerateAttributes(in: fullStringRange) { attribute, range, _ in
361-
var rangeAfterAppending = range
362-
rangeAfterAppending.location += lengthBeforeAppending
363-
addAttributes(attribute, range: rangeAfterAppending)
364-
}
350+
let appendRange = NSRange(location: length, length: 0)
351+
replaceCharacters(in: appendRange, with: attrString)
365352
}
366353

367354
open func deleteCharacters(in range: NSRange) {

TestFoundation/TestNSAttributedString.swift

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestNSAttributedString : XCTestCase {
2323
static var allTests: [(String, (TestNSAttributedString) -> () throws -> Void)] {
2424
return [
2525
("test_initWithString", test_initWithString),
26+
("test_attributedSubstring", test_attributedSubstring),
2627
("test_initWithStringAndAttributes", test_initWithStringAndAttributes),
2728
("test_longestEffectiveRange", test_longestEffectiveRange),
2829
("test_enumerateAttributeWithName", test_enumerateAttributeWithName),
@@ -48,9 +49,29 @@ class TestNSAttributedString : XCTestCase {
4849
XCTAssertEqual(range.length, string.utf16.count)
4950
}
5051

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+
5172
func test_initWithStringAndAttributes() {
5273
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"]
5475

5576
let attrString = NSAttributedString(string: string, attributes: attributes)
5677
XCTAssertEqual(attrString.string, string)
@@ -85,7 +106,7 @@ class TestNSAttributedString : XCTestCase {
85106
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
86107

87108
let attrKey = NSAttributedStringKey("attribute.placeholder.key")
88-
let attrValue = "attribute.placeholder.value" as NSString
109+
let attrValue = "attribute.placeholder.value"
89110

90111
let attrRange1 = NSRange(location: 0, length: 20)
91112
let attrRange2 = NSRange(location: 18, length: 10)
@@ -254,6 +275,11 @@ class TestNSMutableAttributedString : XCTestCase {
254275
("test_addAttribute", test_addAttribute),
255276
("test_addAttributes", test_addAttributes),
256277
("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),
257283
]
258284
}
259285

@@ -332,4 +358,139 @@ class TestNSMutableAttributedString : XCTestCase {
332358
let emptyResult = mutableAttrString.attributes(at: 10, effectiveRange: nil)
333359
XCTAssertTrue(emptyResult.isEmpty)
334360
}
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+
}
335496
}

0 commit comments

Comments
 (0)