Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created += operator #31

Merged
merged 4 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions SwiftyAttributes/Sources/common/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
64 changes: 64 additions & 0 deletions SwiftyAttributesTests/Operators_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,69 @@ class Operators_Tests: XCTestCase {
]
XCTAssertEqual(newString.attributes(at: lhs.length + 1, effectiveRange: nil) as NSDictionary, rightAttributes as NSDictionary)
}

// 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
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)
}

}