From 1c70e5a233b39abef87b0875c35ee9c2e180b046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Pela=CC=81ez?= Date: Sat, 23 Feb 2019 22:24:17 -0600 Subject: [PATCH 1/3] Added += operator --- .../Sources/common/Operators.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/SwiftyAttributes/Sources/common/Operators.swift b/SwiftyAttributes/Sources/common/Operators.swift index 50a00e1..17efba0 100644 --- a/SwiftyAttributes/Sources/common/Operators.swift +++ b/SwiftyAttributes/Sources/common/Operators.swift @@ -16,3 +16,22 @@ public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSMutableAtt combinedString.append(rhs) return combinedString } + +/** + Addition and assign operator. Creates a concatenated string and assigns it to the left value. + */ +public func += (lhs: inout NSAttributedString, rhs: NSAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSMutableAttributedString, rhs: NSAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSAttributedString, rhs: NSMutableAttributedString) { + lhs = lhs + rhs +} + +public func += (lhs: inout NSMutableAttributedString, rhs: NSMutableAttributedString) { + lhs = lhs + rhs +} From 95c6f3c1c8cad19c207f62325d27d4504d149c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Pela=CC=81ez?= Date: Sat, 23 Feb 2019 22:30:49 -0600 Subject: [PATCH 2/3] += operator test --- SwiftyAttributesTests/Operators_Tests.swift | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/SwiftyAttributesTests/Operators_Tests.swift b/SwiftyAttributesTests/Operators_Tests.swift index 41905d8..e28c264 100644 --- a/SwiftyAttributesTests/Operators_Tests.swift +++ b/SwiftyAttributesTests/Operators_Tests.swift @@ -33,5 +33,29 @@ class Operators_Tests: XCTestCase { XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as! [String: NSObject], rightAttributes) #endif } + + func testOverloadedAssignedAdditionOperator() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) + var newString = lhs + newString += rhs + #if swift(>=4.0) + let leftAttributes: [AttributeName: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [AttributeName: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + #else + let leftAttributes = [NSFontAttributeName: Font.systemFont(ofSize: 19)] as [String: NSObject] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as! [String: NSObject], leftAttributes) + let rightAttributes = [NSForegroundColorAttributeName: Color.magenta, + NSBackgroundColorAttributeName: Color.orange, + NSFontAttributeName: Font.boldSystemFont(ofSize: 24)] as [String: NSObject] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as! [String: NSObject], rightAttributes) + #endif + } } From f188feaa83e6f9660f51a327c9a4ece7708215ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Pela=CC=81ez?= Date: Thu, 11 Apr 2019 20:33:05 -0500 Subject: [PATCH 3/3] Added tests for all += implementations --- SwiftyAttributesTests/Operators_Tests.swift | 64 +++++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/SwiftyAttributesTests/Operators_Tests.swift b/SwiftyAttributesTests/Operators_Tests.swift index d38f891..131b481 100644 --- a/SwiftyAttributesTests/Operators_Tests.swift +++ b/SwiftyAttributesTests/Operators_Tests.swift @@ -25,28 +25,68 @@ class Operators_Tests: XCTestCase { XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) } - func testOverloadedAssignedAdditionOperator() { + // NSAttributedString + NSAttributedString + func testOverloadedAssignedAdditionOperator1() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) as NSAttributedString + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) as NSAttributedString + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSMutableAttributedString + NSAttributedString + func testOverloadedAssignedAdditionOperator2() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) as NSAttributedString + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSAttributedString + NSMutableAttributedString + func testOverloadedAssignedAdditionOperator3() { + let lhs = "Hello".withFont(.systemFont(ofSize: 19)) as NSAttributedString + let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) + var newString = lhs + newString += rhs + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] + XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) + let rightAttributes: [NSAttributedString.Key: NSObject] = [ + .foregroundColor: Color.magenta, + .backgroundColor: Color.orange, + .font: Font.boldSystemFont(ofSize: 24) + ] + XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) + } + + // NSMutableAttributedString + NSMutableAttributedString + func testOverloadedAssignedAdditionOperator4() { let lhs = "Hello".withFont(.systemFont(ofSize: 19)) let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24)) var newString = lhs newString += rhs - #if swift(>=4.0) - let leftAttributes: [AttributeName: NSObject] = [.font: Font.systemFont(ofSize: 19)] + let leftAttributes: [NSAttributedString.Key: NSObject] = [.font: Font.systemFont(ofSize: 19)] XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary) - let rightAttributes: [AttributeName: NSObject] = [ + let rightAttributes: [NSAttributedString.Key: NSObject] = [ .foregroundColor: Color.magenta, .backgroundColor: Color.orange, .font: Font.boldSystemFont(ofSize: 24) ] XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary) - #else - let leftAttributes = [NSFontAttributeName: Font.systemFont(ofSize: 19)] as [String: NSObject] - XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as! [String: NSObject], leftAttributes) - let rightAttributes = [NSForegroundColorAttributeName: Color.magenta, - NSBackgroundColorAttributeName: Color.orange, - NSFontAttributeName: Font.boldSystemFont(ofSize: 24)] as [String: NSObject] - XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as! [String: NSObject], rightAttributes) - #endif } }