Skip to content

Commit 54f47e4

Browse files
authored
Merge pull request #1107 from bubski/person-name-components-is-equal
2 parents caacb0e + d46f77a commit 54f47e4

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Foundation/NSPersonNameComponents.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ open class NSPersonNameComponents : NSObject, NSCopying, NSSecureCoding {
6464
}
6565
return copy
6666
}
67+
68+
open override func isEqual(_ object: Any?) -> Bool {
69+
guard let object = object else { return false }
70+
71+
switch object {
72+
case let other as NSPersonNameComponents: return self.isEqual(other)
73+
case let other as PersonNameComponents: return self.isEqual(other._bridgeToObjectiveC())
74+
default: return false
75+
}
76+
}
77+
78+
private func isEqual(_ other: NSPersonNameComponents) -> Bool {
79+
if self === other { return true }
80+
81+
return (self.namePrefix == other.namePrefix
82+
&& self.givenName == other.givenName
83+
&& self.middleName == other.middleName
84+
&& self.familyName == other.familyName
85+
&& self.nameSuffix == other.nameSuffix
86+
&& self.nickname == other.nickname
87+
&& self.phoneticRepresentation == other.phoneticRepresentation)
88+
}
6789

6890
/* The below examples all assume the full name Dr. Johnathan Maple Appleseed Esq., nickname "Johnny" */
6991

TestFoundation/TestNSPersonNameComponents.swift

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,42 @@
1717
import SwiftXCTest
1818
#endif
1919

20+
private func assertEqual(_ lhs:PersonNameComponents,
21+
_ rhs: PersonNameComponents,
22+
file: StaticString = #file,
23+
line: UInt = #line) {
24+
assert(equal: true, lhs, rhs, file: file, line: line)
25+
}
26+
27+
private func assertNotEqual(_ lhs:PersonNameComponents,
28+
_ rhs: PersonNameComponents,
29+
file: StaticString = #file,
30+
line: UInt = #line) {
31+
assert(equal: false, lhs, rhs, file: file, line: line)
32+
}
2033

34+
private func assert(equal: Bool,
35+
_ lhs:PersonNameComponents,
36+
_ rhs: PersonNameComponents,
37+
file: StaticString = #file,
38+
line: UInt = #line) {
39+
if equal {
40+
XCTAssertEqual(lhs, rhs, file: file, line: line)
41+
XCTAssertEqual(lhs._bridgeToObjectiveC(), rhs._bridgeToObjectiveC(), file: file, line: line)
42+
XCTAssertTrue(lhs._bridgeToObjectiveC().isEqual(rhs), file: file, line: line)
43+
} else {
44+
XCTAssertNotEqual(lhs, rhs, file: file, line: line)
45+
XCTAssertNotEqual(lhs._bridgeToObjectiveC(), rhs._bridgeToObjectiveC(), file: file, line: line)
46+
XCTAssertFalse(lhs._bridgeToObjectiveC().isEqual(rhs), file: file, line: line)
47+
}
48+
}
2149

2250
class TestNSPersonNameComponents : XCTestCase {
2351

2452
static var allTests: [(String, (TestNSPersonNameComponents) -> () throws -> Void)] {
2553
return [
2654
("testCopy", testCopy),
55+
("testEquality", testEquality),
2756
]
2857
}
2958

@@ -39,6 +68,78 @@ class TestNSPersonNameComponents : XCTestCase {
3968
XCTAssertEqual(original.phoneticRepresentation!.givenName,copy.phoneticRepresentation!.givenName)
4069
XCTAssertNil(copy.phoneticRepresentation!.phoneticRepresentation)
4170
}
71+
72+
func testEquality() {
73+
do {
74+
let lhs = PersonNameComponents()
75+
let rhs = PersonNameComponents()
76+
assertEqual(lhs, rhs)
77+
}
78+
79+
let lhs = self.makePersonNameComponentsWithTestValues()
80+
do {
81+
let rhs = self.makePersonNameComponentsWithTestValues()
82+
assertEqual(lhs, rhs)
83+
}
84+
do {
85+
var rhs = self.makePersonNameComponentsWithTestValues()
86+
rhs.namePrefix = "differentValue"
87+
assertNotEqual(lhs, rhs)
88+
}
89+
do {
90+
var rhs = self.makePersonNameComponentsWithTestValues()
91+
rhs.givenName = "differentValue"
92+
assertNotEqual(lhs, rhs)
93+
}
94+
do {
95+
var rhs = self.makePersonNameComponentsWithTestValues()
96+
rhs.middleName = "differentValue"
97+
assertNotEqual(lhs, rhs)
98+
}
99+
do {
100+
var rhs = self.makePersonNameComponentsWithTestValues()
101+
rhs.familyName = "differentValue"
102+
assertNotEqual(lhs, rhs)
103+
}
104+
do {
105+
var rhs = self.makePersonNameComponentsWithTestValues()
106+
rhs.nameSuffix = "differentValue"
107+
assertNotEqual(lhs, rhs)
108+
}
109+
do {
110+
var rhs = self.makePersonNameComponentsWithTestValues()
111+
rhs.nickname = "differentValue"
112+
assertNotEqual(lhs, rhs)
113+
}
114+
do {
115+
var rhs = self.makePersonNameComponentsWithTestValues()
116+
rhs.phoneticRepresentation?.namePrefix = "differentValue"
117+
assertNotEqual(lhs, rhs)
118+
}
119+
}
120+
121+
// MARK: - Helpers
122+
123+
private func makePersonNameComponentsWithTestValues() -> PersonNameComponents {
124+
var components = PersonNameComponents()
125+
components.namePrefix = "namePrefix"
126+
components.givenName = "givenName"
127+
components.middleName = "middleName"
128+
components.familyName = "familyName"
129+
components.nameSuffix = "nameSuffix"
130+
components.nickname = "nickname"
131+
components.phoneticRepresentation = {
132+
var components = PersonNameComponents()
133+
components.namePrefix = "phonetic_namePrefix"
134+
components.givenName = "phonetic_givenName"
135+
components.middleName = "phonetic_middleName"
136+
components.familyName = "phonetic_familyName"
137+
components.nameSuffix = "phonetic_nameSuffix"
138+
components.nickname = "phonetic_nickname"
139+
return components
140+
}()
141+
return components
142+
}
42143
}
43144

44145

0 commit comments

Comments
 (0)