|
| 1 | +@testable import Bitkit |
| 2 | +import XCTest |
| 3 | + |
| 4 | +/// Regression coverage for the shared fiat formatting helpers used by both the app and the |
| 5 | +/// WidgetKit extension, plus the App Group symbol sync. Guards against the home-screen weather |
| 6 | +/// widget rendering the locale-disambiguated "US$" instead of "$". |
| 7 | +final class FiatFormattingTests: XCTestCase { |
| 8 | + // MARK: - formatFiatAmount |
| 9 | + |
| 10 | + func testFormatFiatAmount_TwoFractionDigits() throws { |
| 11 | + XCTAssertEqual(try formatFiatAmount(XCTUnwrap(Decimal(string: "0.5"))), "0.50") |
| 12 | + } |
| 13 | + |
| 14 | + func testFormatFiatAmount_RoundsToTwoDigits() throws { |
| 15 | + XCTAssertEqual(try formatFiatAmount(XCTUnwrap(Decimal(string: "0.526"))), "0.53") |
| 16 | + } |
| 17 | + |
| 18 | + func testFormatFiatAmount_GroupsThousandsWithComma() throws { |
| 19 | + XCTAssertEqual(try formatFiatAmount(XCTUnwrap(Decimal(string: "1234.5"))), "1,234.50") |
| 20 | + } |
| 21 | + |
| 22 | + func testFormatFiatAmount_Zero() { |
| 23 | + XCTAssertEqual(formatFiatAmount(0), "0.00") |
| 24 | + } |
| 25 | + |
| 26 | + // MARK: - formatFiatWithSymbol |
| 27 | + |
| 28 | + func testFormatFiatWithSymbol_PrefixCurrency() { |
| 29 | + XCTAssertEqual(formatFiatWithSymbol(formatted: "0.52", symbol: "$", currencyCode: "USD"), "$0.52") |
| 30 | + } |
| 31 | + |
| 32 | + func testFormatFiatWithSymbol_PrefixCurrency_WithSpace() { |
| 33 | + XCTAssertEqual(formatFiatWithSymbol(formatted: "0.52", symbol: "$", currencyCode: "USD", withSpace: true), "$ 0.52") |
| 34 | + } |
| 35 | + |
| 36 | + func testFormatFiatWithSymbol_SuffixCurrency() { |
| 37 | + XCTAssertEqual(formatFiatWithSymbol(formatted: "0.52", symbol: "kr", currencyCode: "SEK"), "0.52kr") |
| 38 | + } |
| 39 | + |
| 40 | + func testFormatFiatWithSymbol_SuffixCurrency_WithSpace() { |
| 41 | + XCTAssertEqual(formatFiatWithSymbol(formatted: "0.52", symbol: "kr", currencyCode: "SEK", withSpace: true), "0.52 kr") |
| 42 | + } |
| 43 | + |
| 44 | + func testFormatFiatWithSymbol_PlaceholderDash() { |
| 45 | + XCTAssertEqual(formatFiatWithSymbol(formatted: "—", symbol: "$", currencyCode: "USD", withSpace: true), "$ —") |
| 46 | + } |
| 47 | + |
| 48 | + // MARK: - Regression guard: never "US$" |
| 49 | + |
| 50 | + func testUsdFormatting_UsesDollarSignNotExtendedSymbol() throws { |
| 51 | + let amount = try formatFiatAmount(XCTUnwrap(Decimal(string: "0.52"))) |
| 52 | + let result = formatFiatWithSymbol(formatted: amount, symbol: "$", currencyCode: "USD", withSpace: true) |
| 53 | + XCTAssertEqual(result, "$ 0.52") |
| 54 | + XCTAssertFalse(result.contains("US$"), "Weather widget must show \"$\", never the disambiguated \"US$\"") |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: - WeatherCurrencyAppGroupStore symbol sync |
| 58 | + |
| 59 | + private static let suiteName = "group.bitkit" |
| 60 | + private static let codeKey = "home_screen_display_currency_code_v1" |
| 61 | + private static let symbolKey = "home_screen_display_currency_symbol_v1" |
| 62 | + |
| 63 | + private var savedCode: String? |
| 64 | + private var savedSymbol: String? |
| 65 | + |
| 66 | + override func setUp() { |
| 67 | + super.setUp() |
| 68 | + let defaults = UserDefaults(suiteName: Self.suiteName) |
| 69 | + savedCode = defaults?.string(forKey: Self.codeKey) |
| 70 | + savedSymbol = defaults?.string(forKey: Self.symbolKey) |
| 71 | + } |
| 72 | + |
| 73 | + override func tearDown() { |
| 74 | + let defaults = UserDefaults(suiteName: Self.suiteName) |
| 75 | + if let savedCode { defaults?.set(savedCode, forKey: Self.codeKey) } else { defaults?.removeObject(forKey: Self.codeKey) } |
| 76 | + if let savedSymbol { defaults?.set(savedSymbol, forKey: Self.symbolKey) } else { defaults?.removeObject(forKey: Self.symbolKey) } |
| 77 | + super.tearDown() |
| 78 | + } |
| 79 | + |
| 80 | + func testStore_RoundTripsCodeAndSymbol() { |
| 81 | + WeatherCurrencyAppGroupStore.save(code: "EUR", symbol: "€") |
| 82 | + XCTAssertEqual(WeatherCurrencyAppGroupStore.load(), "EUR") |
| 83 | + XCTAssertEqual(WeatherCurrencyAppGroupStore.loadSymbol(), "€") |
| 84 | + } |
| 85 | + |
| 86 | + func testStore_LoadSymbolFallsBackOnEmpty() { |
| 87 | + WeatherCurrencyAppGroupStore.save(code: "USD", symbol: "") |
| 88 | + XCTAssertEqual(WeatherCurrencyAppGroupStore.loadSymbol(), WeatherCurrencyAppGroupStore.fallbackSymbol) |
| 89 | + XCTAssertEqual(WeatherCurrencyAppGroupStore.fallbackSymbol, "$") |
| 90 | + } |
| 91 | + |
| 92 | + func testStore_PreservesBackendSymbolForUsd() { |
| 93 | + // The app syncs FxRate.currencySymbol ("$" for USD); the widget must read it back verbatim. |
| 94 | + WeatherCurrencyAppGroupStore.save(code: "USD", symbol: "$") |
| 95 | + XCTAssertEqual(WeatherCurrencyAppGroupStore.loadSymbol(), "$") |
| 96 | + } |
| 97 | +} |
0 commit comments