Repository Public
1import Foundation2import Testing3@testable import VQuantKit45@Suite struct FormatTests {6 /// Every case of the golden file was produced by the web app's own `client/src/lib/format.ts` (Scripts/format-golden.mjs).7 @Test func matchesTheWebFormatter() throws {8 let golden = try Fixture.json("format_golden.json")9 let tz = TimeZone(identifier: golden.str("tz", "America/Toronto"))!10 var checked = 011 var failures: [String] = []12 for c in golden.list("cases") {13 let locale = VQNumberLocale(preference: c.str("locale"))14 let a = c.list("args")15 let v = a.first?.doubleValue16 let expected = c.str("result")17 func int(_ i: Int) -> Int { a[i].intValue ?? 0 }18 func flag(_ i: Int) -> Bool { a[i].boolValue ?? false }19 func date(_ i: Int) -> Date? { a[i].doubleValue.map { Date(timeIntervalSince1970: $0 / 1000) } }20 let got: String21 switch c.str("fn") {22 case "number": got = VQFormat.number(v, decimals: int(1), signed: flag(2), locale: locale)23 case "percent": got = VQFormat.percent(v, decimals: int(1), signed: flag(2), locale: locale)24 case "percentValue": got = VQFormat.percentValue(v, decimals: int(1), signed: flag(2), locale: locale)25 case "compact": got = VQFormat.compact(v, decimals: int(1), signed: flag(2), locale: locale)26 case "bps": got = VQFormat.bps(v, decimals: int(1), signed: flag(2), locale: locale)27 case "delta": got = VQFormat.delta(v, decimals: int(1), locale: locale)28 case "int": got = VQFormat.int(v, locale: locale)29 case "currency": got = VQFormat.currency(v, a[1].stringValue ?? "USD", decimals: int(2), signed: flag(3), compact: flag(4), locale: locale)30 case "byUnit":31 // the web's third variant passes { decimals: 1, signed: true }32 let decimals = a[2].intValue33 // the golden strings are the web formatter's, whose interface is French: the day unit is "j" there34 got = VQFormat.byUnit(v, VQValueUnit(a[1].stringValue), decimals: decimals, signed: decimals != nil, compact: flag(3), locale: locale, language: .fr)35 case "direction": got = VQFormat.direction(v, decimals: int(1)).rawValue36 case "duration": got = VQFormat.duration(ms: v, locale: locale)37 case "tokens": got = VQFormat.tokens(v, locale: locale)38 case "cost": got = VQFormat.cost(v, locale: locale)39 case "time": got = VQFormat.time(date(0), timeZone: tz)40 case "dateIso": got = VQFormat.dateISO(date(0), timeZone: tz)41 case "date": got = VQFormat.date(date(0), timeZone: tz)42 case "dateTime": got = VQFormat.dateTime(date(0), timeZone: tz)43 case "asOf": got = VQFormat.asOf(date(0), now: date(1)!, timeZone: tz)44 case "relative": got = VQFormat.relative(date(0), now: date(1)!, timeZone: tz)45 default: continue46 }47 checked += 148 if got != expected, failures.count < 25 { failures.append("\(c.str("locale")) \(c.str("fn"))\(VQJSON.array(a).text()): got “\(got)”, web “\(expected)”") }49 }50 #expect(checked > 4000)51 #expect(failures.isEmpty, "\(failures.joined(separator: "\n"))")52 }5354 @Test func missingValuesAreAnEmDash() {55 for v in [nil, Double.nan, Double.infinity] as [Double?] {56 #expect(VQFormat.number(v) == "—")57 #expect(VQFormat.percent(v) == "—")58 #expect(VQFormat.currency(v) == "—")59 #expect(VQFormat.byUnit(v, .bps) == "—")60 #expect(VQFormat.compact(v) == "—")61 }62 #expect(VQFormat.date(nil) == "—")63 }6465 @Test func examplesOfTheDesignSystem() {66 #expect(VQFormat.percent(0.1234) == "12,34\u{00A0}%")67 #expect(VQFormat.bps(0.0025) == "25\u{00A0}pb")68 #expect(VQFormat.number(1234.5) == "1\u{00A0}234,50")69 #expect(VQFormat.number(1234.5, locale: .enCA) == "1,234.50")70 #expect(VQFormat.compact(2.5e9) == "2,50\u{00A0}G")71 #expect(VQFormat.delta(-0.2) == "-0,20")72 #expect(VQFormat.currency(1234.5, "USD") == "1\u{00A0}234,50\u{00A0}$\u{00A0}US")73 }7475 /// The day unit follows the language, not the number format: "d" in English, "j" in French.76 @Test func dayUnitFollowsTheLanguage() {77 #expect(VQFormat.byUnit(30, .days, locale: .frCA, language: .en) == "30\u{00A0}d")78 #expect(VQFormat.byUnit(30, .days, locale: .enCA, language: .fr) == "30\u{00A0}j")79 }8081 @Test func freshnessNeverSaysLive() throws {82 for kind in VQFreshness.Kind.allCases {83 for language in VQLanguage.allCases {84 #expect(!kind.label(language).lowercased().contains("live"))85 #expect(!kind.explanation(language).lowercased().contains("live"))86 #expect(!kind.label(language).lowercased().contains("direct"))87 }88 }89 // a server that would one day send an unknown kind — even "live" — is not echoed as such90 let odd = VQFreshness(json: ["kind": "live", "provider": "fmp"])91 #expect(!odd.displayText(.en).lowercased().contains("live"))92 let future = VQFreshness(json: ["kind": "consolidated", "provider": "fmp"])93 #expect(future.kindLabel(.en) == "Consolidated")9495 let now = Date(timeIntervalSince1970: 1_795_211_400)96 let f = try Fixture.json("quotes.json")97 let fresh = VQQuotes(json: f).freshness!98 let text = fresh.displayText(.fr, now: now, timeZone: TimeZone(identifier: "America/Toronto")!)99 #expect(text == "Interrogé · FMP · 16\u{00A0}h\u{00A0}49")100 }101102 @Test func isoDates() {103 #expect(VQISODate.parse("2026-11-20")?.timeIntervalSince1970 == 1_795_132_800)104 #expect(VQISODate.parse("2026-11-20T10:00:00Z")?.timeIntervalSince1970 == 1_795_168_800)105 #expect(VQISODate.parse("2026-11-20T21:49:25+00:00")?.timeIntervalSince1970 == 1_795_211_365)106 #expect(VQISODate.parse("2026-11-20T05:00:00.250-05:00")?.timeIntervalSince1970 == 1_795_168_800.25)107 #expect(VQISODate.parse("nope") == nil)108 #expect(VQISODate.parse("AAPL 2024-02-01") == nil)109 }110}111