Skip to content

Commit fba70bb

Browse files
committed
cleanup tests
1 parent ec1b067 commit fba70bb

File tree

1 file changed

+42
-78
lines changed

1 file changed

+42
-78
lines changed

TestFoundation/TestNSAttributedString.swift

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,16 @@ class TestNSAttributedString : XCTestCase {
8787
}
8888

8989
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)
90+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
91+
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
92+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: attributes)
9793

9894
let initializedAttrString = NSAttributedString(attributedString: mutableAttrString)
9995
XCTAssertTrue(initializedAttrString.isEqual(to: mutableAttrString))
10096

10197
// changing the mutable attr string should not affect the initialized attr string
10298
mutableAttrString.append(mutableAttrString)
103-
XCTAssertEqual(initializedAttrString.string, string1)
99+
XCTAssertEqual(initializedAttrString.string, string)
104100
}
105101

106102
func test_attributedSubstring() {
@@ -263,18 +259,15 @@ class TestNSAttributedString : XCTestCase {
263259

264260
func test_copy() {
265261
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])
262+
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
263+
264+
let originalAttrString = NSAttributedString(string: string, attributes: attributes)
271265
let attrStringCopy = originalAttrString.copy()
272266
XCTAssertTrue(attrStringCopy is NSAttributedString)
273267
XCTAssertFalse(attrStringCopy is NSMutableAttributedString)
274268
XCTAssertTrue((attrStringCopy as! NSAttributedString).isEqual(to: originalAttrString))
275269

276-
let originalMutableAttrString = NSMutableAttributedString(string: string)
277-
originalMutableAttrString.addAttribute(attrKey, value: attrValue, range: attrRange)
270+
let originalMutableAttrString = NSMutableAttributedString(string: string, attributes: attributes)
278271
let mutableAttrStringCopy = originalMutableAttrString.copy()
279272
XCTAssertTrue(mutableAttrStringCopy is NSAttributedString)
280273
XCTAssertFalse(mutableAttrStringCopy is NSMutableAttributedString)
@@ -283,42 +276,35 @@ class TestNSAttributedString : XCTestCase {
283276

284277
func test_mutableCopy() {
285278
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)
279+
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
289280

290-
let originalAttrString = NSAttributedString(string: string, attributes: [attrKey : attrValue])
281+
let originalAttrString = NSAttributedString(string: string, attributes: attributes)
291282
let attrStringMutableCopy = originalAttrString.mutableCopy()
292283
XCTAssertTrue(attrStringMutableCopy is NSMutableAttributedString)
293284
XCTAssertTrue((attrStringMutableCopy as! NSMutableAttributedString).isEqual(to: originalAttrString))
294285

295-
let originalMutableAttrString = NSMutableAttributedString(string: string)
296-
originalMutableAttrString.addAttribute(attrKey, value: attrValue, range: attrRange)
286+
let originalMutableAttrString = NSMutableAttributedString(string: string, attributes: attributes)
297287
let mutableAttrStringMutableCopy = originalMutableAttrString.mutableCopy()
298288
XCTAssertTrue(mutableAttrStringMutableCopy is NSMutableAttributedString)
299289
XCTAssertTrue((mutableAttrStringMutableCopy as! NSMutableAttributedString).isEqual(to: originalMutableAttrString))
300290
}
301291

302292
func test_isEqual() {
303293
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)
294+
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
307295

308-
let attrString = NSAttributedString(string: string, attributes: [attrKey : attrValue])
296+
let attrString = NSAttributedString(string: string, attributes: attributes)
309297

310-
let attrString2 = NSAttributedString(string: string, attributes: [attrKey : attrValue])
298+
let attrString2 = NSAttributedString(string: string, attributes: attributes)
311299
XCTAssertTrue(attrString.isEqual(to: attrString2))
312300

313-
let mutableAttrString = NSMutableAttributedString(string: string)
314-
mutableAttrString.addAttribute(attrKey, value: attrValue, range: attrRange)
301+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: attributes)
315302
XCTAssertTrue(attrString.isEqual(to: mutableAttrString))
316303
XCTAssertTrue(mutableAttrString.isEqual(to: attrString))
317304

318-
let newAttrs: [NSAttributedStringKey : Any] = [
319-
NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2"
320-
]
321-
mutableAttrString.addAttributes(newAttrs, range: attrRange)
305+
let newAttrs: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2"]
306+
let newAttrsRange = NSRange(string.startIndex..., in: string)
307+
mutableAttrString.addAttributes(newAttrs, range: newAttrsRange)
322308
XCTAssertFalse(attrString.isEqual(to: mutableAttrString))
323309
XCTAssertFalse(mutableAttrString.isEqual(to: attrString))
324310
}
@@ -409,20 +395,17 @@ class TestNSMutableAttributedString : XCTestCase {
409395
}
410396

411397
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)
398+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
399+
let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key") : "attribute.placeholder.value"]
400+
401+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: attributes)
419402

420403
let initializedMutableAttrString = NSMutableAttributedString(attributedString: mutableAttrString)
421404
XCTAssertTrue(initializedMutableAttrString.isEqual(to: mutableAttrString))
422405

423406
// changing the mutable attr string should not affect the initialized attr string
424407
mutableAttrString.append(mutableAttrString)
425-
XCTAssertEqual(initializedMutableAttrString.mutableString, NSMutableString(string: string1))
408+
XCTAssertEqual(initializedMutableAttrString.mutableString, NSMutableString(string: string))
426409
}
427410

428411
func test_addAttribute() {
@@ -497,15 +480,12 @@ class TestNSMutableAttributedString : XCTestCase {
497480

498481
func test_replaceCharactersWithString() {
499482
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
500-
let mutableAttrString = NSMutableAttributedString(string: string)
501-
502483
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
503484
let attrValue1 = "attribute.placeholder.value1"
504-
let attrRange1 = NSRange(location: 0, length: mutableAttrString.length)
505-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
506-
485+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: [attrKey1 : attrValue1])
486+
507487
let replacement = "Sample replacement "
508-
let replacementRange = NSRange(location: 0, length: replacement.utf16.count)
488+
let replacementRange = NSRange(replacement.startIndex..., in: replacement)
509489

510490
mutableAttrString.replaceCharacters(in: replacementRange, with: replacement)
511491

@@ -522,13 +502,10 @@ class TestNSMutableAttributedString : XCTestCase {
522502

523503
func test_replaceCharactersWithAttributedString() {
524504
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
525-
let mutableAttrString = NSMutableAttributedString(string: string)
526-
527505
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
528506
let attrValue1 = "attribute.placeholder.value1"
529-
let attrRange1 = NSRange(location: 0, length: mutableAttrString.length)
530-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
531-
507+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: [attrKey1 : attrValue1])
508+
532509
let replacement = "Sample replacement "
533510
let replacementAttrKey = NSAttributedStringKey("attribute.replacement.key")
534511
let replacementAttributes: [NSAttributedStringKey : Any] = [replacementAttrKey : "attribute.replacement.value"]
@@ -553,13 +530,10 @@ class TestNSMutableAttributedString : XCTestCase {
553530

554531
func test_insert() {
555532
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
556-
let mutableAttrString = NSMutableAttributedString(string: string)
557-
558533
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
559534
let attrValue1 = "attribute.placeholder.value1"
560-
let attrRange1 = NSRange(location: 0, length: mutableAttrString.length)
561-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
562-
535+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: [attrKey1 : attrValue1])
536+
563537
let insertString = "Sample insertion. "
564538
let insertAttrKey = NSAttributedStringKey("attribute.insertion.key")
565539
let insertAttributes: [NSAttributedStringKey : Any] = [insertAttrKey : "attribute.insertion.value"]
@@ -581,12 +555,9 @@ class TestNSMutableAttributedString : XCTestCase {
581555

582556
func test_append() {
583557
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
584-
let mutableAttrString = NSMutableAttributedString(string: string)
585-
586558
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
587559
let attrValue1 = "attribute.placeholder.value1"
588-
let attrRange1 = NSRange(location: 0, length: mutableAttrString.length)
589-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
560+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: [attrKey1 : attrValue1])
590561

591562
let appendString = " Sample appending."
592563
let appendAttrKey = NSAttributedStringKey("attribute.appending.key")
@@ -609,43 +580,36 @@ class TestNSMutableAttributedString : XCTestCase {
609580

610581
func test_deleteCharacters() {
611582
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
612-
let mutableAttrString = NSMutableAttributedString(string: string)
613-
614583
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
615584
let attrValue1 = "attribute.placeholder.value1"
616-
let attrRange1 = NSRange(location: 0, length: mutableAttrString.length)
617-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
585+
let mutableAttrString = NSMutableAttributedString(string: string, attributes: [attrKey1 : attrValue1])
618586

619587
let deleteRange = NSRange(location: 0, length: 10)
620588
mutableAttrString.deleteCharacters(in: deleteRange)
621589

622590
let expectedString = String(string[string.startIndex.advanced(by: 10)...])
623591
XCTAssertEqual(mutableAttrString.string, expectedString)
624592

625-
let expectedLongestEffectiveRange = NSRange(location: 0, length: expectedString.utf16.count)
593+
let expectedLongestEffectiveRange = NSRange(expectedString.startIndex..., in: expectedString)
626594
var longestEffectiveRange = NSRange()
627595
let searchRange = NSRange(location: 0, length: mutableAttrString.length)
628-
_ = mutableAttrString.attribute(attrKey1, at: 0, longestEffectiveRange: &longestEffectiveRange, in: searchRange)
596+
guard let value = mutableAttrString.attribute(attrKey1, at: 0, longestEffectiveRange: &longestEffectiveRange, in: searchRange) as? NSString else {
597+
XCTAssert(false, "attribute not found")
598+
return
599+
}
600+
XCTAssertEqual(value, "attribute.placeholder.value1")
629601
XCTAssertEqual(longestEffectiveRange.location, expectedLongestEffectiveRange.location)
630602
XCTAssertEqual(longestEffectiveRange.length, expectedLongestEffectiveRange.length)
631603
}
632604

633605
func test_setAttributedString() {
634606
let string1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
635-
let attrKey1 = NSAttributedStringKey("attribute.placeholder.key1")
636-
let attrValue1 = "attribute.placeholder.value1"
637-
let attrRange1 = NSRange(location: 0, length: string1.utf8.count)
607+
let attributes1: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key1") : "attribute.placeholder.value1"]
608+
let mutableAttrString = NSMutableAttributedString(string: string1, attributes: attributes1)
638609

639-
let mutableAttrString = NSMutableAttributedString(string: string1)
640-
mutableAttrString.addAttribute(attrKey1, value: attrValue1, range: attrRange1)
641-
642610
let string2 = "Sample set attributed string."
643-
let attrKey2 = NSAttributedStringKey("attribute.placeholder.key2")
644-
let attrValue2 = "attribute.placeholder.value2"
645-
let attrRange2 = NSRange(location: 0, length: string2.utf8.count)
646-
647-
let replacementAttrString = NSMutableAttributedString(string: string2)
648-
replacementAttrString.addAttribute(attrKey2, value: attrValue2, range: attrRange2)
611+
let attributes2: [NSAttributedStringKey : Any] = [NSAttributedStringKey("attribute.placeholder.key2") : "attribute.placeholder.value2"]
612+
let replacementAttrString = NSMutableAttributedString(string: string2, attributes: attributes2)
649613

650614
mutableAttrString.setAttributedString(replacementAttrString)
651615
XCTAssertTrue(mutableAttrString.isEqual(to: replacementAttrString))

0 commit comments

Comments
 (0)