From c284c98847d15449300136d5c41b9dd530b742fc Mon Sep 17 00:00:00 2001 From: Branton Boehm Date: Wed, 15 May 2024 09:33:27 -0700 Subject: [PATCH] Add test case for custom UnitSpeed --- Sources/RunningPace/UnitSpeedExtensions.swift | 2 +- Tests/RunningPaceTests/OperatorsTests.swift | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/RunningPace/UnitSpeedExtensions.swift b/Sources/RunningPace/UnitSpeedExtensions.swift index 95579cd..7bae4d1 100644 --- a/Sources/RunningPace/UnitSpeedExtensions.swift +++ b/Sources/RunningPace/UnitSpeedExtensions.swift @@ -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) { diff --git a/Tests/RunningPaceTests/OperatorsTests.swift b/Tests/RunningPaceTests/OperatorsTests.swift index 46e1f3c..190e8fb 100644 --- a/Tests/RunningPaceTests/OperatorsTests.swift +++ b/Tests/RunningPaceTests/OperatorsTests.swift @@ -1,5 +1,5 @@ import XCTest -import RunningPace +@testable import RunningPace final class OperatorsTests: XCTestCase { @@ -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), @@ -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 { @@ -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)) }