Skip to content

Commit 3e541f9

Browse files
committed
add tests
1 parent df18084 commit 3e541f9

File tree

2 files changed

+140
-23
lines changed

2 files changed

+140
-23
lines changed

Foundation/NSAttributedString.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
115115

116116
/// Returns a Boolean value that indicates whether the receiver is equal to another given attributed string.
117117
open func isEqual(to other: NSAttributedString) -> Bool {
118-
return CFEqual(_cfObject, other._cfObject)
118+
guard let runtimeClass = _CFRuntimeGetClassWithTypeID(CFAttributedStringGetTypeID()) else {
119+
fatalError("Could not obtain CFRuntimeClass of CFAttributedString")
120+
}
121+
122+
guard let equalFunction = runtimeClass.pointee.equal else {
123+
fatalError("Could not obtain equal function from CFRuntimeClass of CFAttributedString")
124+
}
125+
126+
return equalFunction(_cfObject, other._cfObject) == true
119127
}
120128

121129
/// Returns an NSAttributedString object initialized with the characters of a given string and no attribute information.

TestFoundation/TestNSAttributedString.swift

+131-22
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ class TestNSAttributedString : XCTestCase {
2323
static var allTests: [(String, (TestNSAttributedString) -> () throws -> Void)] {
2424
return [
2525
("test_initWithString", test_initWithString),
26-
("test_attributedSubstring", test_attributedSubstring),
2726
("test_initWithStringAndAttributes", test_initWithStringAndAttributes),
27+
("test_initWithAttributedString", test_initWithAttributedString),
28+
("test_attributedSubstring", test_attributedSubstring),
2829
("test_longestEffectiveRange", test_longestEffectiveRange),
2930
("test_enumerateAttributeWithName", test_enumerateAttributeWithName),
3031
("test_enumerateAttributes", test_enumerateAttributes),
32+
("test_copy", test_copy),
33+
("test_mutableCopy", test_mutableCopy),
34+
("test_isEqual", test_isEqual),
3135
]
3236
}
33-
37+
3438
func test_initWithString() {
3539
let string = "Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ"
3640
let attrString = NSAttributedString(string: string)
@@ -49,26 +53,6 @@ class TestNSAttributedString : XCTestCase {
4953
XCTAssertEqual(range.length, string.utf16.count)
5054
}
5155

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-
7256
func test_initWithStringAndAttributes() {
7357
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
7458
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
@@ -102,6 +86,43 @@ class TestNSAttributedString : XCTestCase {
10286
XCTAssertEqual(validAttribute, "attribute.placeholder.value")
10387
}
10488

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+
105126
func test_longestEffectiveRange() {
106127
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
107128

@@ -239,6 +260,68 @@ class TestNSAttributedString : XCTestCase {
239260
XCTAssertEqual(attrsDescriptionString, "[attribute.placeholder.key1:attribute.placeholder.value1]")
240261
#endif
241262
}
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+
}
242325
}
243326

244327
fileprivate extension TestNSAttributedString {
@@ -280,6 +363,7 @@ class TestNSMutableAttributedString : XCTestCase {
280363
("test_insert", test_insert),
281364
("test_append", test_append),
282365
("test_deleteCharacters", test_deleteCharacters),
366+
("test_setAttributedString", test_setAttributedString),
283367
]
284368
}
285369

@@ -493,4 +577,29 @@ class TestNSMutableAttributedString : XCTestCase {
493577
XCTAssertEqual(longestEffectiveRange.location, expectedLongestEffectiveRange.location)
494578
XCTAssertEqual(longestEffectiveRange.length, expectedLongestEffectiveRange.length)
495579
}
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+
}
496605
}

0 commit comments

Comments
 (0)