Skip to content

Commit d193e77

Browse files
committed
NSPersonNameComponents.isEqual implemented
1 parent 31d1d41 commit d193e77

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TestNSPersonNameComponents : XCTestCase {
2424
static var allTests: [(String, (TestNSPersonNameComponents) -> () throws -> Void)] {
2525
return [
2626
("testCopy", testCopy),
27+
("testEquality", testEquality),
2728
]
2829
}
2930

@@ -39,6 +40,82 @@ class TestNSPersonNameComponents : XCTestCase {
3940
XCTAssertEqual(original.phoneticRepresentation!.givenName,copy.phoneticRepresentation!.givenName)
4041
XCTAssertNil(copy.phoneticRepresentation!.phoneticRepresentation)
4142
}
43+
44+
private func makePersonNameComponentsWithTestValues() -> PersonNameComponents {
45+
var components = PersonNameComponents()
46+
components.namePrefix = "namePrefix"
47+
components.givenName = "givenName"
48+
components.middleName = "middleName"
49+
components.familyName = "familyName"
50+
components.nameSuffix = "nameSuffix"
51+
components.nickname = "nickname"
52+
components.phoneticRepresentation = {
53+
var components = PersonNameComponents()
54+
components.namePrefix = "phonetic_namePrefix"
55+
components.givenName = "phonetic_givenName"
56+
components.middleName = "phonetic_middleName"
57+
components.familyName = "phonetic_familyName"
58+
components.nameSuffix = "phonetic_nameSuffix"
59+
components.nickname = "phonetic_nickname"
60+
return components
61+
}()
62+
return components
63+
}
64+
65+
func testEquality() {
66+
do {
67+
let lhs = PersonNameComponents()
68+
let rhs = PersonNameComponents()
69+
XCTAssertEqual(lhs, rhs)
70+
}
71+
do {
72+
let lhs = self.makePersonNameComponentsWithTestValues()
73+
let rhs = self.makePersonNameComponentsWithTestValues()
74+
XCTAssertEqual(lhs, rhs)
75+
}
76+
do {
77+
var lhs = self.makePersonNameComponentsWithTestValues()
78+
lhs.namePrefix = "differentValue"
79+
let rhs = self.makePersonNameComponentsWithTestValues()
80+
XCTAssertNotEqual(lhs, rhs)
81+
}
82+
do {
83+
var lhs = self.makePersonNameComponentsWithTestValues()
84+
lhs.givenName = "differentValue"
85+
let rhs = self.makePersonNameComponentsWithTestValues()
86+
XCTAssertNotEqual(lhs, rhs)
87+
}
88+
do {
89+
var lhs = self.makePersonNameComponentsWithTestValues()
90+
lhs.middleName = "differentValue"
91+
let rhs = self.makePersonNameComponentsWithTestValues()
92+
XCTAssertNotEqual(lhs, rhs)
93+
}
94+
do {
95+
var lhs = self.makePersonNameComponentsWithTestValues()
96+
lhs.familyName = "differentValue"
97+
let rhs = self.makePersonNameComponentsWithTestValues()
98+
XCTAssertNotEqual(lhs, rhs)
99+
}
100+
do {
101+
var lhs = self.makePersonNameComponentsWithTestValues()
102+
lhs.nameSuffix = "differentValue"
103+
let rhs = self.makePersonNameComponentsWithTestValues()
104+
XCTAssertNotEqual(lhs, rhs)
105+
}
106+
do {
107+
var lhs = self.makePersonNameComponentsWithTestValues()
108+
lhs.nickname = "differentValue"
109+
let rhs = self.makePersonNameComponentsWithTestValues()
110+
XCTAssertNotEqual(lhs, rhs)
111+
}
112+
do {
113+
var lhs = self.makePersonNameComponentsWithTestValues()
114+
lhs.phoneticRepresentation?.namePrefix = "differentValue"
115+
let rhs = self.makePersonNameComponentsWithTestValues()
116+
XCTAssertNotEqual(lhs, rhs)
117+
}
118+
}
42119
}
43120

44121

0 commit comments

Comments
 (0)