Skip to content

Commit

Permalink
Merge pull request #31 from EmilioPelaez/master
Browse files Browse the repository at this point in the history
Created += operator
  • Loading branch information
eddiekaiger authored Apr 23, 2019
2 parents e511246 + f188fea commit a653915
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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)
}

}

0 comments on commit a653915

Please sign in to comment.