From 5c5e931f7cad7994bfc029d729c029c27913d9b9 Mon Sep 17 00:00:00 2001 From: Quentin Date: Mon, 27 Aug 2018 13:33:10 +0800 Subject: [PATCH] Make Interval conform Comparable --- Schedule.xcodeproj/project.pbxproj | 22 ---------------------- Sources/Schedule/Interval.swift | 11 +++++++++++ Tests/ScheduleTests/DateTimeTests.swift | 4 ++++ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Schedule.xcodeproj/project.pbxproj b/Schedule.xcodeproj/project.pbxproj index f3d5690..79aeb0c 100644 --- a/Schedule.xcodeproj/project.pbxproj +++ b/Schedule.xcodeproj/project.pbxproj @@ -70,17 +70,10 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 626D761B2113F9D100FCAFFE /* README.zh_cn.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.zh_cn.md; sourceTree = ""; }; 62AA63012133970A00A442A5 /* Calendar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Calendar.swift; sourceTree = ""; }; 62AA63032133A0FB00A442A5 /* CalendarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarTests.swift; sourceTree = ""; }; 667D2DF62132C5390071DC89 /* DeinitObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeinitObserver.swift; sourceTree = ""; }; 667D2DF82132C95D0071DC89 /* DeinitObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeinitObserverTests.swift; sourceTree = ""; }; - 668685ED210DCC0E009305C3 /* .swift-version */ = {isa = PBXFileReference; lastKnownFileType = text; path = ".swift-version"; sourceTree = ""; }; - 668685EE210DCC0E009305C3 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; - 668685EF210DCC0E009305C3 /* .codecov.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .codecov.yml; sourceTree = ""; }; - 668685F0210DCC0E009305C3 /* Schedule.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = Schedule.podspec; sourceTree = ""; }; - 668685F1210DCC0F009305C3 /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .travis.yml; sourceTree = ""; }; - 668685F2210DCC0F009305C3 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .swiftlint.yml; sourceTree = ""; }; 668685F3210DCEB0009305C3 /* Schedule.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Schedule.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; OBJ_10 /* Bucket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bucket.swift; sourceTree = ""; }; OBJ_11 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; @@ -138,20 +131,6 @@ name = Platforms; sourceTree = ""; }; - 668685EC210DCBD4009305C3 /* Related */ = { - isa = PBXGroup; - children = ( - 668685EF210DCC0E009305C3 /* .codecov.yml */, - 668685ED210DCC0E009305C3 /* .swift-version */, - 668685F2210DCC0F009305C3 /* .swiftlint.yml */, - 668685F1210DCC0F009305C3 /* .travis.yml */, - 668685EE210DCC0E009305C3 /* README.md */, - 668685F0210DCC0E009305C3 /* Schedule.podspec */, - 626D761B2113F9D100FCAFFE /* README.zh_cn.md */, - ); - name = Related; - sourceTree = ""; - }; 668685F4210DD21A009305C3 /* Utils */ = { isa = PBXGroup; children = ( @@ -225,7 +204,6 @@ 668685F3210DCEB0009305C3 /* Schedule.playground */, OBJ_6 /* Package.swift */, OBJ_37 /* Products */, - 668685EC210DCBD4009305C3 /* Related */, OBJ_7 /* Sources */, OBJ_24 /* Tests */, ); diff --git a/Sources/Schedule/Interval.swift b/Sources/Schedule/Interval.swift index d23c81c..53f3838 100644 --- a/Sources/Schedule/Interval.swift +++ b/Sources/Schedule/Interval.swift @@ -116,6 +116,17 @@ extension Interval { } } +extension Interval: Comparable { + + /// Returns a Boolean value indicating whether the first interval is + /// less than the second interval. + /// + /// A negative interval is always less than a positive interval. + public static func < (lhs: Interval, rhs: Interval) -> Bool { + return lhs.compare(rhs) == .orderedAscending + } +} + // MARK: - Adding & Subtracting extension Interval { /// Returns a new interval by multipling this interval by the given number. diff --git a/Tests/ScheduleTests/DateTimeTests.swift b/Tests/ScheduleTests/DateTimeTests.swift index 7e0864d..2cbbf95 100644 --- a/Tests/ScheduleTests/DateTimeTests.swift +++ b/Tests/ScheduleTests/DateTimeTests.swift @@ -21,6 +21,10 @@ final class DateTimeTests: XCTestCase { XCTAssertEqual(7.day, 1.week) XCTAssertEqual((-2).seconds.compare(1.second), .orderedAscending) + XCTAssertTrue(1.1.second > 1.0.second) + XCTAssertTrue(3.days < 1.week) + XCTAssertTrue(4.day >= 4.days) + XCTAssertTrue(-2.seconds < 1.seconds) XCTAssertTrue(1.1.second.isLonger(than: 1.0.second)) XCTAssertTrue(3.days.isShorter(than: 1.week))