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.