Skip to content

Commit 6d50f8e

Browse files
Merge pull request #11 from NeedleInAJayStack/test/compositeUnitDefine
Adds composite unit definition test
2 parents c63265c + 2bfa069 commit 6d50f8e

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

Sources/Units/Registry.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// UnitRegistry defines a structure that contains all defined units. This ensures
22
/// that we are able to parse to and from unit symbol representations.
3-
internal class Registry {
3+
class Registry {
44
// TODO: Should we eliminate this singleton and make clients keep track?
5-
internal static let instance = Registry()
5+
static let instance = Registry()
66

77
// Quick access based on symbol
88
private var symbolMap: [String: DefinedUnit]
@@ -29,7 +29,7 @@ internal class Registry {
2929

3030
/// Returns a list of defined units and their exponents, given a composite unit symbol. It is expected that the caller has
3131
/// verified that this is a composite unit.
32-
internal func compositeUnitsFromSymbol(symbol: String) throws -> [DefinedUnit: Int] {
32+
func compositeUnitsFromSymbol(symbol: String) throws -> [DefinedUnit: Int] {
3333
let symbolsAndExponents = try deserializeSymbolicEquation(symbol)
3434

3535
var compositeUnits = [DefinedUnit: Int]()
@@ -45,7 +45,7 @@ internal class Registry {
4545

4646
/// Returns a defined unit given a defined unit symbol. It is expected that the caller has
4747
/// verified that this is not a composite unit.
48-
internal func getUnit(bySymbol symbol: String) throws -> DefinedUnit {
48+
func getUnit(bySymbol symbol: String) throws -> DefinedUnit {
4949
guard let definedUnit = symbolMap[symbol] else {
5050
throw UnitError.unitNotFound(message: "Symbol '\(symbol)' not recognized")
5151
}
@@ -54,7 +54,7 @@ internal class Registry {
5454

5555
/// Returns a defined unit given a defined unit name. It is expected that the caller has
5656
/// verified that this is not a composite unit.
57-
internal func getUnit(byName name: String) throws -> DefinedUnit {
57+
func getUnit(byName name: String) throws -> DefinedUnit {
5858
guard let definedUnit = nameMap[name] else {
5959
throw UnitError.unitNotFound(message: "Name '\(name)' not recognized")
6060
}
@@ -67,7 +67,7 @@ internal class Registry {
6767
/// - parameter dimension: The unit dimensionality as a dictionary of quantities and their respective exponents.
6868
/// - parameter coefficient: The value to multiply a base unit of this dimension when converting it to this unit. For base units, this is 1.
6969
/// - parameter constant: The value to add to a base unit when converting it to this unit. This is added after the coefficient is multiplied according to order-of-operations.
70-
internal func addUnit(
70+
func addUnit(
7171
name: String,
7272
symbol: String,
7373
dimension: [Quantity: Int],
@@ -95,7 +95,7 @@ internal class Registry {
9595
}
9696

9797
/// Returns all units currently defined by the registry
98-
internal func allUnits() -> [Unit] {
98+
func allUnits() -> [Unit] {
9999
var allUnits = [Unit]()
100100
for (_, unit) in symbolMap {
101101
allUnits.append(Unit(definedBy: unit))

Sources/Units/Unit/Unit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public struct Unit {
4848

4949
/// Create a unit from the defined unit object.
5050
/// - Parameter definedBy: A defined unit to wrap
51-
internal init(definedBy: DefinedUnit) {
51+
init(definedBy: DefinedUnit) {
5252
type = .defined(definedBy)
5353
}
5454

5555
/// Create a new from the sub-unit dictionary.
5656
/// - Parameter subUnits: A dictionary of defined units and exponents. If this dictionary has only a single unit with an exponent of one,
5757
/// we return that defined unit directly.
58-
internal init(composedOf subUnits: [DefinedUnit: Int]) {
58+
init(composedOf subUnits: [DefinedUnit: Int]) {
5959
if subUnits.count == 1, let subUnit = subUnits.first, subUnit.value == 1 {
6060
type = .defined(subUnit.key)
6161
} else {

Tests/UnitsTests/MeasurementTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,41 @@ final class MeasurementTests: XCTestCase {
427427
)
428428
}
429429

430+
func testCompositeUnitDefine() throws {
431+
let ampereHour = Unit.ampere * .hour
432+
433+
// Test conversion from custom unit
434+
XCTAssertEqual(
435+
try 1.measured(in: ampereHour).convert(to: .coulomb),
436+
3600.measured(in: .coulomb),
437+
accuracy: accuracy
438+
)
439+
440+
// Test conversion to custom unit
441+
XCTAssertEqual(
442+
try 3600.measured(in: .coulomb).convert(to: ampereHour),
443+
1.measured(in: ampereHour),
444+
accuracy: accuracy
445+
)
446+
447+
let ampereSquareMeters = Unit.ampere * .meter.pow(2)
448+
let joulePerTesla = Unit.joule / .tesla
449+
450+
// Test complex conversion from custom unit
451+
XCTAssertEqual(
452+
try 1.measured(in: ampereSquareMeters).convert(to: joulePerTesla),
453+
1.measured(in: joulePerTesla),
454+
accuracy: accuracy
455+
)
456+
457+
// Test complex conversion to custom unit
458+
XCTAssertEqual(
459+
try 1.measured(in: joulePerTesla).convert(to: ampereSquareMeters),
460+
1.measured(in: ampereSquareMeters),
461+
accuracy: accuracy
462+
)
463+
}
464+
430465
func testUnitRegister() throws {
431466
try Unit.register(
432467
name: "centiinch",

Tests/UnitsTests/XCTest+Measurement.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Units
2+
23
// import Foundation
34
import XCTest
45

0 commit comments

Comments
 (0)