Added some tests

This commit is contained in:
cdricms
2025-07-19 13:10:53 +02:00
parent 9ecd335808
commit 9e4323f6a1
2 changed files with 129 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ public enum UnitCategory: String, CaseIterable, CustomStringConvertible {
// Add more categories as needed
public var description: String {
return self.rawValue.capitalized
rawValue.capitalized
}
}
@@ -158,21 +158,19 @@ public enum Unit: String, CaseIterable, CustomStringConvertible {
}
}
public var description: String {
self.rawValue
}
public var description: String { rawValue }
// MARK: - Subscripts for Unit Enum
/// Allows retrieving a Unit by its raw string value.
/// Example: `Unit(rawValue: "kg")` or `Unit["kg"]`
public static subscript(rawValue: String) -> Unit? {
return .init(rawValue: rawValue)
.init(rawValue: rawValue)
}
/// Allows retrieving all Units belonging to a specific UnitCategory.
/// Example: `Unit[.mass]` will return `[.kilogram, .gram, ...]`
public static subscript(category: UnitCategory) -> [Unit] {
return Unit.allCases.filter { $0.category == category }
Unit.allCases.filter { $0.category == category }
}
}
@@ -268,30 +266,30 @@ public struct UnitValue<T: BinaryFloatingPoint>: CustomStringConvertible, Equata
// Allows comparing two UnitValue instances for equality.
// Considers both value and unit.
public static func == (lhs: UnitValue<T>, rhs: UnitValue<T>) -> Bool {
return lhs.value == rhs.value && lhs.unit == rhs.unit
lhs.value == rhs.value && lhs.unit == rhs.unit
}
public static func >= (lhs: UnitValue<T>, rhs: UnitValue<T>) -> Bool {
return lhs.value >= rhs.value && lhs.unit == rhs.unit
lhs.value >= rhs.value && lhs.unit == rhs.unit
}
public static func > (lhs: UnitValue<T>, rhs: UnitValue<T>) -> Bool {
return lhs.value > rhs.value && lhs.unit == rhs.unit
lhs.value > rhs.value && lhs.unit == rhs.unit
}
public static func <= (lhs: UnitValue<T>, rhs: UnitValue<T>) -> Bool {
return lhs.value <= rhs.value && lhs.unit == rhs.unit
lhs.value <= rhs.value && lhs.unit == rhs.unit
}
public static func < (lhs: UnitValue<T>, rhs: UnitValue<T>) -> Bool {
return lhs.value < rhs.value && lhs.unit == rhs.unit
lhs.value < rhs.value && lhs.unit == rhs.unit
}
// MARK: - Subscripts for UnitValue
/// Allows converting the UnitValue to another Unit using subscript syntax.
/// Example: `tenKilos[.pound]`
public subscript(targetUnit: Unit) -> UnitValue<T>? {
return converted(to: targetUnit)
converted(to: targetUnit)
}
/// Allows converting the UnitValue to another Unit using its raw string value.

View File

@@ -1,10 +1,123 @@
import Testing
@testable import Units
@testable import Units // Assuming your library is in a module named 'Units'
@Test func example() async throws {
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
if let tenKilos = 10["kg"] {
print(tenKilos[tenKilos.unit]!)
@Test func unitCreationSubscriptTests() async throws {
// Test creating UnitValue using string subscript on numeric literals
let tenKilosByString = 10.0["kg"]
#expect(tenKilosByString != nil)
#expect(tenKilosByString?.value == 10.0)
#expect(tenKilosByString?.unit == .kilogram)
print("Created 10 kg by string subscript: \(String(describing: tenKilosByString))")
let fiveMetersByString = 5.0["m"]
#expect(fiveMetersByString != nil)
#expect(fiveMetersByString?.value == 5.0)
#expect(fiveMetersByString?.unit == .meter)
print("Created 5 m by string subscript: \(String(describing: fiveMetersByString))")
// Test creating UnitValue using enum subscript on numeric literals
let twentyLitersByEnum = 20.0[.liter]
#expect(twentyLitersByEnum.value == 20.0)
#expect(twentyLitersByEnum.unit == .liter)
print("Created 20 L by enum subscript: \(twentyLitersByEnum)")
let thirtySecondsByEnum = 30.0[.second]
#expect(thirtySecondsByEnum.value == 30.0)
#expect(thirtySecondsByEnum.unit == .second)
print("Created 30 s by enum subscript: \(thirtySecondsByEnum)")
// Test invalid unit string for creation
let invalidUnitCreation = 100.0["xyz"]
#expect(invalidUnitCreation == nil)
print(
"Attempted invalid unit creation (should be nil): \(String(describing: invalidUnitCreation))"
)
}
@Test func unitConversionSubscriptTests() async throws {
// Test conversion using string subscript on UnitValue
if let tenKilos = 10.0["kg"] {
let pounds = tenKilos["lbs"]
#expect(pounds != nil)
#expect(pounds?.value == 22.046226218487757) // Exact conversion value
#expect(pounds?.unit == .pound)
print("10 kg to lbs by string subscript: \(String(describing: pounds))")
} else {
#expect(false, "Failed to create initial 10 kg unit.")
}
// Test conversion using enum subscript on UnitValue
let fiveMeters = 5.0.m
let feet = fiveMeters[.foot]
#expect(feet != nil)
#expect(feet?.value == 16.404199475065617) // Exact conversion value
#expect(feet?.unit == .foot)
print("5 m to ft by enum subscript: \(String(describing: feet))")
// Test chained conversions
if let initialValue = 100.0["km"] {
let miles = initialValue["mi"]
#expect(miles != nil)
#expect(miles?.value == 62.13711922373339) // Exact conversion value
#expect(miles?.unit == .mile)
print("100 km to mi (chained string subscript): \(String(describing: miles))")
let feetFromMiles = miles?["ft"]
#expect(feetFromMiles != nil)
#expect(feetFromMiles?.value == 328083.9895013123) // Exact conversion value (100km -> mi -> ft)
#expect(feetFromMiles?.unit == .foot)
print("100 km to mi to ft (chained string subscript): \(String(describing: feetFromMiles))")
} else {
#expect(false, "Failed to create initial 100 km unit.")
}
// Test invalid conversion (different categories)
let tenKilos = 10.0.kg
let metersFromKilos = tenKilos["m"]
#expect(metersFromKilos == nil)
print(
"Attempted invalid conversion (mass to length, should be nil): \(String(describing: metersFromKilos))"
)
// Test invalid target unit string for conversion
let invalidTargetConversion = tenKilos["xyz"]
#expect(invalidTargetConversion == nil)
print(
"Attempted conversion to invalid unit string (should be nil): \(String(describing: invalidTargetConversion))"
)
}
@Test func temperatureConversionSubscriptTests() async throws {
// Test Celsius to Fahrenheit
let twentyFiveC = 25.0[.celsius]
let fahrenheitFromC = twentyFiveC[.fahrenheit]
#expect(fahrenheitFromC != nil)
#expect(fahrenheitFromC?.value == 77.0)
#expect(fahrenheitFromC?.unit == .fahrenheit)
print("25°C to °F: \(String(describing: fahrenheitFromC))")
// Test Fahrenheit to Celsius
let ninetyEightF = 98.6[.fahrenheit]
let celsiusFromF = ninetyEightF[.celsius]
#expect(celsiusFromF != nil)
#expect(celsiusFromF?.value == 37.0)
#expect(celsiusFromF?.unit == .celsius)
print("98.6°F to °C: \(String(describing: celsiusFromF))")
// Test Celsius to Kelvin
let zeroC = 0.0[.celsius]
let kelvinFromC = zeroC[.kelvin]
#expect(kelvinFromC != nil)
#expect(kelvinFromC?.value == 273.15)
#expect(kelvinFromC?.unit == .kelvin)
print("0°C to K: \(String(describing: kelvinFromC))")
// Test Kelvin to Celsius
let twoSeventyThreeK = 273.15[.kelvin]
let celsiusFromK = twoSeventyThreeK[.celsius]
#expect(celsiusFromK != nil)
#expect(celsiusFromK?.value == 0.0)
#expect(celsiusFromK?.unit == .celsius)
print("273.15 K to °C: \(String(describing: celsiusFromK))")
}