Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXFinancial/Tests/ORIXFinancialTests/TestSupport.swift3// Description: Test helpers — loader for the Python-generated reference values and tolerance/error assertions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXFinancial1011typealias JSON = [String: Any]1213/// Reference values produced by Reference/generate_reference.py (bundled copy of Reference/reference.json).14enum Ref {15 static func load() -> JSON {16 guard let url = Bundle.module.url(forResource: "reference", withExtension: "json"),17 let data = try? Data(contentsOf: url),18 let object = try? JSONSerialization.jsonObject(with: data) as? JSON else {19 fatalError("reference.json is missing from the test bundle; run python3 Reference/generate_reference.py")20 }21 return object22 }23 static func list(_ key: String) -> [JSON] { load()[key] as? [JSON] ?? [] }24 static func object(_ key: String) -> JSON { load()[key] as? JSON ?? [:] }25}2627extension Dictionary where Key == String, Value == Any {28 func num(_ key: String) -> Double {29 if let n = self[key] as? NSNumber { return n.doubleValue }30 if let s = self[key] as? String, let d = Double(s) { return d }31 fatalError("missing number \(key)")32 }33 func optionalNum(_ key: String) -> Double? { (self[key] as? NSNumber)?.doubleValue }34 func int(_ key: String) -> Int { Int(num(key)) }35 func str(_ key: String) -> String { self[key] as? String ?? "" }36 func bool(_ key: String) -> Bool { (self[key] as? NSNumber)?.boolValue ?? false }37 func nums(_ key: String) -> [Double] { (self[key] as? [NSNumber])?.map(\.doubleValue) ?? [] }38}3940/// |a − b| ≤ rel·max(1, |b|) — relative for large values, absolute near zero.41func close(_ a: Double, _ b: Double, _ rel: Double = 1e-9) -> Bool { abs(a - b) <= rel * max(1, abs(b)) }4243func date(_ iso: String) -> FinancialDate {44 let p = iso.split(separator: "-").map { Int($0)! }45 return try! FinancialDate(year: p[0], month: p[1], day: p[2])46}4748/// Runs `body`, which must throw a `FinancialError` accepted by `match`.49func expectFinancialError(_ body: () throws -> Void, _ match: (FinancialError) -> Bool, sourceLocation: SourceLocation = #_sourceLocation) {50 do {51 try body()52 Issue.record("expected a FinancialError, none thrown", sourceLocation: sourceLocation)53 } catch let e as FinancialError {54 #expect(match(e), "unexpected error: \(e)", sourceLocation: sourceLocation)55 #expect(!e.description.isEmpty, sourceLocation: sourceLocation)56 } catch {57 Issue.record("unexpected error type: \(error)", sourceLocation: sourceLocation)58 }59}6061func compounding(_ s: String) -> CompoundingConvention {62 switch s {63 case "usMonthly": .usMonthly64 case "canadianFixed": .canadianFixed65 case "canadianVariable": .canadianVariable66 case "continuous": .continuous67 default: .periodic(perYear: Int(s)!)68 }69}7071@Test func bundledReferenceMatchesTheGeneratedFile() throws {72 // Tests/ORIXFinancialTests/TestSupport.swift → package root → Reference/reference.json73 let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()74 guard let generated = try? Data(contentsOf: root.appendingPathComponent("Reference/reference.json")),75 let bundled = Bundle.module.url(forResource: "reference", withExtension: "json").flatMap({ try? Data(contentsOf: $0) }) else {76 return // sources not available at run time (e.g. on a device): nothing to compare77 }78 #expect(generated == bundled, "Tests/…/Resources/reference.json is stale; run python3 Reference/generate_reference.py")79}80