From f38c6963b07beb17921243d383a48c6b9e818a96 Mon Sep 17 00:00:00 2001 From: Piergiuseppe Longo Date: Sat, 10 Jan 2015 17:23:25 +0100 Subject: [PATCH] Add Comparable functions to NSDate --- ExSwift/NSDate.swift | 27 +++++++++++++- ExSwiftTests/ExSwiftNSDateTests.swift | 54 ++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/ExSwift/NSDate.swift b/ExSwift/NSDate.swift index 3fff146..6d05460 100644 --- a/ExSwift/NSDate.swift +++ b/ExSwift/NSDate.swift @@ -226,4 +226,29 @@ public extension NSDate{ let components = calendar.components(component, fromDate: self) return components.valueForComponent(component) } -} \ No newline at end of file +} + +// MARK: Comparable functions + +public func ==(lhs: NSDate, rhs: NSDate) -> Bool { + return lhs.compare(rhs) == NSComparisonResult.OrderedSame +} + +public func <(lhs: NSDate, rhs: NSDate) -> Bool { + return lhs.compare(rhs) == NSComparisonResult.OrderedAscending +} + +public func >(lhs: NSDate, rhs: NSDate) -> Bool { + return !(lhs < rhs) +} + +public func <=(lhs: NSDate, rhs: NSDate) -> Bool { + return lhs < rhs || lhs == rhs +} + +public func >=(lhs: NSDate, rhs: NSDate) -> Bool { + return lhs > rhs || lhs == rhs +} + +extension NSDate: Comparable { +} diff --git a/ExSwiftTests/ExSwiftNSDateTests.swift b/ExSwiftTests/ExSwiftNSDateTests.swift index a918178..3d9cb5c 100644 --- a/ExSwiftTests/ExSwiftNSDateTests.swift +++ b/ExSwiftTests/ExSwiftNSDateTests.swift @@ -148,7 +148,7 @@ class ExSwiftNSDataTests: XCTestCase { XCTAssertFalse(date.isAfter(date), "Past date should be in the past") XCTAssertFalse(pastDate.isAfter(date), "Past date should be in the past") } - + func testIsBefore(){ var date = NSDate() var futureDate = date.addSeconds(42) @@ -159,6 +159,8 @@ class ExSwiftNSDataTests: XCTestCase { } + // MARK: Getter + func testGetter() { XCTAssertEqual(1988, startDate!.year, "Year Mismatch") XCTAssertEqual(11, startDate!.month, "Month Mismatch") @@ -170,4 +172,54 @@ class ExSwiftNSDataTests: XCTestCase { XCTAssertEqual(5, startDate!.weekMonth, "Weekmonth Mismatch") } + // MARK: Comparable + + func testSorting () { + var firstDate = startDate!.addSeconds(0) + var secondDate = startDate!.addSeconds(42) + var thirdDate = startDate!.addSeconds(-42) + var fourthDate = startDate!.addSeconds(-84) + var fifthDate = startDate!.addSeconds(84) + + var dates : [NSDate] = [thirdDate, secondDate, firstDate, fourthDate, fifthDate] + + let expected : [NSDate] = [fifthDate, secondDate, firstDate, thirdDate, fourthDate] + + let expectedReverded = expected.reverse() + + for i in 0 ... 42 { + dates.shuffle() + + dates.sort( { $0 > $1 } ) + XCTAssertEqual(expected, dates, "Sort mismatch") + + dates.sort( { $0 < $1 } ) + XCTAssertEqual(expectedReverded, dates, "Sort mismatch") + } + } + + func testComparable(){ + var date = startDate!.addSeconds(-42) + var anotherDate = startDate!.addSeconds(42) + let shouldBeTheSameDate = NSDate(timeInterval: 0, sinceDate: startDate!) + + + XCTAssertTrue(startDate > date, "Date should be greater") + XCTAssertFalse(startDate > anotherDate, "Date shouldn't be greater") + + XCTAssertTrue(startDate < anotherDate, "Date should be lower") + XCTAssertFalse(startDate < date, "Date shouldn't be lower") + + XCTAssertTrue(startDate >= shouldBeTheSameDate, "Date should be greater or equal") + XCTAssertTrue(startDate >= date, "Date should be greater or equal") + XCTAssertFalse(startDate >= anotherDate, "Date shouldn't be greater or equal") + + XCTAssertTrue(startDate <= shouldBeTheSameDate, "Date should be lower or equal") + XCTAssertTrue(startDate < anotherDate, "Date should be lower") + XCTAssertFalse(startDate <= date, "Date shouldn't be greater or equal") + + XCTAssertFalse(date == startDate, "Date should mismatch") + XCTAssertFalse(anotherDate == startDate, "Date should mismatch") + XCTAssertTrue(shouldBeTheSameDate == startDate, "Date shouldn't mismatch") + } }