Skip to content

Commit 96b30ef

Browse files
committed
Implementation of NSDateComponents.copy(with: zone)
1 parent ff652d6 commit 96b30ef

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Foundation/NSCalendar.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,11 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
14281428
}
14291429

14301430
open func copy(with zone: NSZone? = nil) -> Any {
1431-
NSUnimplemented()
1431+
let copy = NSDateComponents()
1432+
copy._values = self._values
1433+
copy._calendar = self._calendar
1434+
copy._timeZone = self._timeZone
1435+
return copy
14321436
}
14331437

14341438
/*@NSCopying*/ open var calendar: Calendar? {

TestFoundation/TestNSCalendar.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,34 @@ class TestNSCalendar: XCTestCase {
9090
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
9191
}
9292
}
93+
94+
class TestNSDateComponents: XCTestCase {
95+
96+
static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
97+
return [
98+
("test_copyNSDateComponents", test_copyNSDateComponents),
99+
]
100+
}
101+
102+
func test_copyNSDateComponents() {
103+
let components = NSDateComponents()
104+
components.year = 1987
105+
components.month = 3
106+
components.day = 17
107+
components.hour = 14
108+
components.minute = 20
109+
components.second = 0
110+
let copy = components.copy(with: nil) as! NSDateComponents
111+
XCTAssertTrue(components.isEqual(copy))
112+
XCTAssertTrue(components == copy)
113+
XCTAssertFalse(components === copy)
114+
XCTAssertEqual(copy.year, 1987)
115+
XCTAssertEqual(copy.month, 3)
116+
XCTAssertEqual(copy.day, 17)
117+
XCTAssertEqual(copy.isLeapMonth, false)
118+
//Mutate NSDateComponents and verify that it does not reflect in the copy
119+
components.hour = 12
120+
XCTAssertEqual(components.hour, 12)
121+
XCTAssertEqual(copy.hour, 14)
122+
}
123+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ XCTMain([
3030
testCase(TestNSCompoundPredicate.allTests),
3131
testCase(TestNSData.allTests),
3232
testCase(TestNSDate.allTests),
33+
testCase(TestNSDateComponents.allTests),
3334
testCase(TestNSDateFormatter.allTests),
3435
testCase(TestNSDictionary.allTests),
3536
testCase(TestNSFileManager.allTests),

0 commit comments

Comments
 (0)