Skip to content

Commit

Permalink
Add test case for custom UnitSpeed
Browse files Browse the repository at this point in the history
  • Loading branch information
brantonb committed May 15, 2024
1 parent 018441a commit c284c98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/RunningPace/UnitSpeedExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public extension UnitSpeed {
converter: UnitConverterInverse(constant: 16.666666666667))
}

private class UnitConverterInverse: UnitConverter {
class UnitConverterInverse: UnitConverter {
let constant: Double

init(constant: Double) {
Expand Down
11 changes: 10 additions & 1 deletion Tests/RunningPaceTests/OperatorsTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
import RunningPace
@testable import RunningPace

final class OperatorsTests: XCTestCase {

Expand Down Expand Up @@ -64,6 +64,8 @@ final class OperatorsTests: XCTestCase {
}

func testMultiplyingSpeedByDurationHasProperUnits() {
let secondsPerMeter = UnitSpeed(symbol: "\(UnitDuration.seconds.symbol)/\(UnitLength.meters.symbol)",
converter: UnitConverterInverse(constant: 0.277778))
let conversions: [(unitSpeed: UnitSpeed, unitLength: UnitLength)] = [
(.minutesPerMile, .miles),
(.milesPerHour, .miles),
Expand All @@ -73,6 +75,9 @@ final class OperatorsTests: XCTestCase {

(.knots, .nauticalMiles),
(.metersPerSecond, .meters),

// Default should be kilometers for any unknown types like this custom one
(secondsPerMeter, .kilometers),
]

for (unitSpeed, unitLength) in conversions {
Expand Down Expand Up @@ -211,4 +216,8 @@ private func assertEqualDurations(_ expected: Duration, _ actual: Duration) {
let actualSeconds = actual.converted(to: .seconds).value
XCTAssertEqual(expectedSeconds, actualSeconds, accuracy: tolerance)
}

private extension UnitSpeed {
static let secondsPerMeter = UnitSpeed(symbol: "\(UnitDuration.seconds.symbol)/\(UnitLength.meters.symbol)",
converter: UnitConverterInverse(constant: 0.277778))
}

0 comments on commit c284c98

Please sign in to comment.