Skip to content

Implementation of NSDateComponents.copy(with: zone) #564

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

Merged
merged 1 commit into from
Sep 12, 2016
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
22 changes: 21 additions & 1 deletion Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,27 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
}

open func copy(with zone: NSZone? = nil) -> Any {
NSUnimplemented()
let newObj = NSDateComponents()
newObj.calendar = calendar
newObj.timeZone = timeZone
newObj.era = era
newObj.year = year
newObj.month = month
newObj.day = day
newObj.hour = hour
newObj.minute = minute
newObj.second = second
newObj.nanosecond = nanosecond
newObj.weekOfYear = weekOfYear
newObj.weekOfMonth = weekOfMonth
newObj.yearForWeekOfYear = yearForWeekOfYear
newObj.weekday = weekday
newObj.weekdayOrdinal = weekdayOrdinal
newObj.quarter = quarter
if leapMonthSet {
newObj.isLeapMonth = isLeapMonth
}
return newObj
}

/*@NSCopying*/ open var calendar: Calendar? {
Expand Down
31 changes: 31 additions & 0 deletions TestFoundation/TestNSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,34 @@ class TestNSCalendar: XCTestCase {
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
}
}

class TestNSDateComponents: XCTestCase {

static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
return [
("test_copyNSDateComponents", test_copyNSDateComponents),
]
}

func test_copyNSDateComponents() {
let components = NSDateComponents()
components.year = 1987
components.month = 3
components.day = 17
components.hour = 14
components.minute = 20
components.second = 0
let copy = components.copy(with: nil) as! NSDateComponents
XCTAssertTrue(components.isEqual(copy))
XCTAssertTrue(components == copy)
XCTAssertFalse(components === copy)
XCTAssertEqual(copy.year, 1987)
XCTAssertEqual(copy.month, 3)
XCTAssertEqual(copy.day, 17)
XCTAssertEqual(copy.isLeapMonth, false)
//Mutate NSDateComponents and verify that it does not reflect in the copy
components.hour = 12
XCTAssertEqual(components.hour, 12)
XCTAssertEqual(copy.hour, 14)
}
}
1 change: 1 addition & 0 deletions TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ XCTMain([
testCase(TestNSCompoundPredicate.allTests),
testCase(TestNSData.allTests),
testCase(TestNSDate.allTests),
testCase(TestNSDateComponents.allTests),
testCase(TestNSDateFormatter.allTests),
testCase(TestNSDictionary.allTests),
testCase(TestNSFileManager.allTests),
Expand Down