Skip to content

Commit

Permalink
Skip testing Duration conversions on older OSs
Browse files Browse the repository at this point in the history
Because apparently the @available check causes a runtime failure?

Also adds ability to mulitple a NimbleTimeInterval
  • Loading branch information
younata committed Jul 13, 2023
1 parent 0c27058 commit 529efe1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
37 changes: 29 additions & 8 deletions Sources/Nimble/Utils/NimbleTimeInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ extension NimbleTimeInterval: CustomStringConvertible {
case let .nanoseconds(val): return "\(Float(val)/1_000_000_000) seconds"
}
}

public static func * (lhs: NimbleTimeInterval, rhs: Int) -> NimbleTimeInterval {
lhs.multiply(by: rhs)
}

public static func * (lhs: Int, rhs: NimbleTimeInterval) -> NimbleTimeInterval {
rhs.multiply(by: lhs)
}

private func multiply(by value: Int) -> NimbleTimeInterval {
switch self {
case let .seconds(int):
return .seconds(int * value)
case let .milliseconds(int):
return .milliseconds(int * value)
case let .microseconds(int):
return .microseconds(int * value)
case let .nanoseconds(int):
return .nanoseconds(int * value)
}
}
}

#if canImport(Foundation)
Expand All @@ -49,13 +70,13 @@ import CDispatch
extension NimbleTimeInterval {
public var dispatchTimeInterval: DispatchTimeInterval {
switch self {
case .seconds(let int):
case let .seconds(int):
return .seconds(int)
case .milliseconds(let int):
case let .milliseconds(int):
return .milliseconds(int)
case .microseconds(let int):
case let .microseconds(int):
return .microseconds(int)
case .nanoseconds(let int):
case let .nanoseconds(int):
return .nanoseconds(int)
}
}
Expand All @@ -65,13 +86,13 @@ extension NimbleTimeInterval {
extension NimbleTimeInterval {
public var duration: Duration {
switch self {
case .seconds(let int):
case let .seconds(int):
return .seconds(int)
case .milliseconds(let int):
case let .milliseconds(int):
return .milliseconds(int)
case .microseconds(let int):
case let .microseconds(int):
return .microseconds(int)
case .nanoseconds(let int):
case let .nanoseconds(int):
return .nanoseconds(int)
}
}
Expand Down
22 changes: 19 additions & 3 deletions Tests/NimbleTests/NimbleTimeIntervalTests.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import Nimble
import XCTest

@available(macOS 13, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
final class NimbleTimeIntervalTests: XCTestCase {
func testToDuration() {
func testMultiply() {
expect(NimbleTimeInterval.seconds(1) * 2) == NimbleTimeInterval.seconds(2)
expect(2 * NimbleTimeInterval.seconds(1)) == NimbleTimeInterval.seconds(2)

expect(NimbleTimeInterval.milliseconds(1) * 2) == NimbleTimeInterval.milliseconds(2)
expect(2 * NimbleTimeInterval.milliseconds(1)) == NimbleTimeInterval.milliseconds(2)

expect(NimbleTimeInterval.microseconds(1) * 2) == NimbleTimeInterval.microseconds(2)
expect(2 * NimbleTimeInterval.microseconds(1)) == NimbleTimeInterval.microseconds(2)

expect(NimbleTimeInterval.nanoseconds(1) * 2) == NimbleTimeInterval.nanoseconds(2)
expect(2 * NimbleTimeInterval.nanoseconds(1)) == NimbleTimeInterval.nanoseconds(2)
}

func testToDuration() throws {
guard #available(macOS 13, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip() }
expect(NimbleTimeInterval.seconds(1).duration).to(equal(Duration.seconds(1)))
expect(NimbleTimeInterval.milliseconds(10).duration).to(equal(Duration.milliseconds(10)))
expect(NimbleTimeInterval.microseconds(20).duration).to(equal(Duration.microseconds(20)))
expect(NimbleTimeInterval.nanoseconds(30).duration).to(equal(Duration.nanoseconds(30)))
}

func testFromDuration() {
func testFromDuration() throws {
guard #available(macOS 13, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip() }

expect(NimbleTimeInterval(duration: Duration.seconds(10))).to(equal(.seconds(10)))

expect(NimbleTimeInterval(duration: Duration.milliseconds(1000))).to(equal(.seconds(1)))
Expand Down

0 comments on commit 529efe1

Please sign in to comment.