dev
dart 128 lines 4.33 KB
Raw
1 import "package:cw_core/amount/money.dart";
2 import "package:cw_core/amount/money_local.dart";
3 import "package:cw_core/crypto_currency.dart";
4 import "package:flutter_test/flutter_test.dart";
5 import "package:intl/intl.dart";
6
7 final _btc012 = Money.fromInt(12345678, CryptoCurrency.btc); // 0.12345678 BTC
8 final _btcTrailing = Money.fromInt(12000000, CryptoCurrency.btc); // 0.12 BTC
9 final _btcGrouping = Money.fromInt(123450000000, CryptoCurrency.btc); // 1234.5 BTC
10 final _xmrHalf = Money.fromInt(500000000000, CryptoCurrency.xmr); // 0.5 XMR
11
12 void main() {
13 group("toLocalStringWithPrecision", () {
14 test("localizes grouping (en_US)", () {
15 expect(_btcGrouping.toLocalStringWithPrecision(locale: "en_US"), "1,234.5");
16 });
17
18 test("no grouping for sub-1 values", () {
19 expect(_btc012.toLocalStringWithPrecision(locale: "en_US"), "0.12345678");
20 });
21
22 test("swaps grouping/decimal separators (de_DE)", () {
23 expect(_btcGrouping.toLocalStringWithPrecision(locale: "de_DE"), "1.234,5");
24 });
25
26 test("base unit renders integer sats with grouping", () {
27 expect(
28 _btc012.toLocalStringWithPrecision(useBaseUnit: true, locale: "en_US"),
29 "12,345,678",
30 );
31 });
32
33 test("fractionalDigits truncates (does not round)", () {
34 // 0.12345678 -> 0.12 (NOT 0.13).
35 expect(_btc012.toLocalStringWithPrecision(fractionalDigits: 2, locale: "en_US"), "0.12");
36 });
37
38 test("trimZeros:false keeps trailing zeros", () {
39 expect(
40 _btcTrailing.toLocalStringWithPrecision(trimZeros: false, locale: "en_US"),
41 "0.12000000",
42 );
43 });
44
45 test("respects a higher-precision currency (XMR)", () {
46 expect(_xmrHalf.toLocalStringWithPrecision(locale: "en_US"), "0.5");
47 });
48
49 test("falls back to the ambient Intl locale when locale is null", () {
50 final previous = Intl.defaultLocale;
51 addTearDown(() => Intl.defaultLocale = previous);
52 Intl.defaultLocale = "de_DE";
53
54 // Proves the extension passes null through to NumberFormat rather than
55 // hardcoding a locale.
56 expect(_btcGrouping.toLocalStringWithPrecision(), "1.234,5");
57 });
58 });
59
60 group("toLocalStringWithSymbol", () {
61 test("suffixes the symbol by default", () {
62 expect(_btcGrouping.toLocalStringWithSymbol(locale: "en_US"), "1,234.5 BTC");
63 });
64
65 test("prefixes the symbol when withSymbolPrefix is true", () {
66 expect(
67 _btcGrouping.toLocalStringWithSymbol(withSymbolPrefix: true, locale: "en_US"),
68 "BTC 1,234.5",
69 );
70 });
71
72 test("suffixes the base-unit ticker", () {
73 expect(
74 _btc012.toLocalStringWithSymbol(useBaseUnit: true, locale: "en_US"),
75 "12,345,678 sats",
76 );
77 });
78
79 test("prefixes the base-unit ticker", () {
80 expect(
81 _btc012.toLocalStringWithSymbol(
82 useBaseUnit: true,
83 withSymbolPrefix: true,
84 locale: "en_US",
85 ),
86 "sats 12,345,678",
87 );
88 });
89
90 test("prefix respects de_DE separators", () {
91 expect(
92 _btcGrouping.toLocalStringWithSymbol(withSymbolPrefix: true, locale: "de_DE"),
93 "BTC 1.234,5",
94 );
95 });
96
97 test("uses the currency symbol for non-BTC", () {
98 expect(_xmrHalf.toLocalStringWithSymbol(locale: "en_US"), "0.5 XMR");
99 });
100
101 test("forwards fractionalDigits to the precision string", () {
102 expect(_btc012.toLocalStringWithSymbol(fractionalDigits: 2, locale: "en_US"), "0.12 BTC");
103 });
104
105 test("forwards trimZeros to the precision string", () {
106 expect(
107 _btcTrailing.toLocalStringWithSymbol(trimZeros: false, locale: "en_US"),
108 "0.12000000 BTC",
109 );
110 });
111 });
112
113 group("negative amounts", () {
114 test("keeps the sign when the integer part is non-zero", () {
115 expect((-_btcGrouping).toLocalStringWithSymbol(locale: "en_US"), "-1,234.5 BTC");
116 });
117
118 // NOTE: currently fails. For |amount| < 1 the sign is dropped:
119 // formatFixed yields "-0.5", but _withLocalSeparator re-parses the integer
120 // part and int.tryParse("-0") == 0, so the "-" is lost -> "0.5 XMR".
121 // Fix in _withLocalSeparator by capturing the sign before parsing, e.g.
122 // final negative = amount.startsWith("-");
123 // ... then re-apply it to the formatted result.
124 test("keeps the sign for sub-1 amounts", () {
125 expect((-_xmrHalf).toLocalStringWithSymbol(locale: "en_US"), "-0.5 XMR");
126 });
127 });
128 }