Skip to content

Commit db3aeaa

Browse files
committed
add tests
1 parent eb220dd commit db3aeaa

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

Foundation/NSAttributedString.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,11 @@ open class NSMutableAttributedString : NSAttributedString {
309309
open func replaceCharacters(in range: NSRange, with str: String) { NSUnimplemented() }
310310

311311
open func setAttributes(_ attrs: [NSAttributedStringKey : Any]?, range: NSRange) {
312-
CFAttributedStringSetAttributes(_cfMutableObject, CFRange(range), attrs?._cfObject, true)
312+
guard let attrs = attrs else {
313+
CFAttributedStringSetAttributes(_cfMutableObject, CFRange(range), nil, true)
314+
return
315+
}
316+
CFAttributedStringSetAttributes(_cfMutableObject, CFRange(range), attributesCFDictionary(from: attrs), true)
313317
}
314318

315319
open var mutableString: NSMutableString {
@@ -321,7 +325,7 @@ open class NSMutableAttributedString : NSAttributedString {
321325
}
322326

323327
open func addAttributes(_ attrs: [NSAttributedStringKey : Any], range: NSRange) {
324-
CFAttributedStringSetAttributes(_cfMutableObject, CFRange(range), attrs._cfObject, false)
328+
CFAttributedStringSetAttributes(_cfMutableObject, CFRange(range), attributesCFDictionary(from: attrs), false)
325329
}
326330

327331
open func removeAttribute(_ name: NSAttributedStringKey, range: NSRange) {
@@ -351,3 +355,14 @@ open class NSMutableAttributedString : NSAttributedString {
351355
extension NSMutableAttributedString {
352356
internal var _cfMutableObject: CFMutableAttributedString { return unsafeBitCast(self, to: CFMutableAttributedString.self) }
353357
}
358+
359+
private extension NSMutableAttributedString {
360+
361+
func attributesCFDictionary(from attrs: [NSAttributedStringKey : Any]) -> CFDictionary {
362+
var attributesDictionary = [String : Any]()
363+
for (key, value) in attrs {
364+
attributesDictionary[key.rawValue] = value
365+
}
366+
return attributesDictionary._cfObject
367+
}
368+
}

TestFoundation/TestNSAttributedString.swift

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,85 @@ class TestNSMutableAttributedString : XCTestCase {
251251
static var allTests: [(String, (TestNSMutableAttributedString) -> () throws -> Void)] {
252252
return [
253253
("test_initWithString", test_initWithString),
254+
("test_addAttribute", test_addAttribute),
255+
("test_addAttributes", test_addAttributes),
256+
("test_setAttributes", test_setAttributes),
254257
]
255258
}
256259

257260
func test_initWithString() {
258261
let string = "Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ"
259262
let mutableAttrString = NSMutableAttributedString(string: string)
260263
XCTAssertEqual(mutableAttrString.mutableString, NSMutableString(string: string))
261-
}
264+
}
265+
266+
func test_addAttribute() {
267+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
268+
let mutableAttrString = NSMutableAttributedString(string: string)
269+
270+
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
271+
let attrValue1 = "attribute.placeholder.value1"
272+
let attrRange1 = NSRange(location: 0, length: 20)
273+
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
274+
275+
let attrValue = mutableAttrString.attribute(attrKey1, at: 10, effectiveRange: nil)
276+
guard let validAttribute = attrValue as? NSString else {
277+
XCTAssert(false, "attribute not found")
278+
return
279+
}
280+
XCTAssertEqual(validAttribute, "attribute.placeholder.value1")
281+
}
282+
283+
func test_addAttributes() {
284+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
285+
let mutableAttrString = NSMutableAttributedString(string: string)
286+
287+
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
288+
let attrValue1 = "attribute.placeholder.value1"
289+
let attrRange1 = NSRange(location: 0, length: 20)
290+
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
291+
292+
let attrs2 = [
293+
NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2",
294+
NSAttributedStringKey("attribute.placeholder.key3") : "attribute.placeholder.value3",
295+
]
296+
let attrRange2 = NSRange(location: 0, length: 20)
297+
mutableAttrString.addAttributes(attrs2, range: attrRange2)
298+
299+
let result = mutableAttrString.attributes(at: 10, effectiveRange: nil) as? [NSAttributedStringKey : String]
300+
let expectedResult: [NSAttributedStringKey : String] = [
301+
NSAttributedStringKey("attribute.placeholder.key1") : "attribute.placeholder.value1",
302+
NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2",
303+
NSAttributedStringKey("attribute.placeholder.key3") : "attribute.placeholder.value3",
304+
]
305+
XCTAssertEqual(result, expectedResult)
306+
}
307+
308+
func test_setAttributes() {
309+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
310+
let mutableAttrString = NSMutableAttributedString(string: string)
311+
312+
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
313+
let attrValue1 = "attribute.placeholder.value1"
314+
let attrRange1 = NSRange(location: 0, length: 20)
315+
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
316+
317+
let attrs2 = [
318+
NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2",
319+
NSAttributedStringKey("attribute.placeholder.key3") : "attribute.placeholder.value3",
320+
]
321+
let attrRange2 = NSRange(location: 0, length: 20)
322+
mutableAttrString.setAttributes(attrs2, range: attrRange2)
323+
324+
let result = mutableAttrString.attributes(at: 10, effectiveRange: nil) as? [NSAttributedStringKey : String]
325+
let expectedResult: [NSAttributedStringKey : String] = [
326+
NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2",
327+
NSAttributedStringKey("attribute.placeholder.key3") : "attribute.placeholder.value3",
328+
]
329+
XCTAssertEqual(result, expectedResult)
330+
331+
mutableAttrString.setAttributes(nil, range: attrRange2)
332+
let emptyResult = mutableAttrString.attributes(at: 10, effectiveRange: nil)
333+
XCTAssertTrue(emptyResult.isEmpty)
334+
}
262335
}

0 commit comments

Comments
 (0)