1
2
3
4
5 package currency
6
7 import (
8 "testing"
9
10 "golang.org/x/text/language"
11 "golang.org/x/text/message"
12 )
13
14 var (
15 de = language.German
16 en = language.English
17 fr = language.French
18 de_CH = language.MustParse("de-CH")
19 en_US = language.AmericanEnglish
20 en_GB = language.BritishEnglish
21 en_AU = language.MustParse("en-AU")
22 und = language.Und
23 )
24
25 func TestFormatting(t *testing.T) {
26 testCases := []struct {
27 tag language.Tag
28 value interface{}
29 format Formatter
30 want string
31 }{
32 0: {en, USD.Amount(0.1), nil, "USD 0.10"},
33 1: {en, XPT.Amount(1.0), Symbol, "XPT 1.00"},
34
35 2: {en, USD.Amount(2.0), ISO, "USD 2.00"},
36 3: {und, USD.Amount(3.0), Symbol, "US$ 3.00"},
37 4: {en, USD.Amount(4.0), Symbol, "$ 4.00"},
38
39 5: {en, USD.Amount(5.20), NarrowSymbol, "$ 5.20"},
40 6: {en, AUD.Amount(6.20), Symbol, "A$ 6.20"},
41
42 7: {en_AU, AUD.Amount(7.20), Symbol, "$ 7.20"},
43 8: {en_GB, USD.Amount(8.20), Symbol, "US$ 8.20"},
44
45 9: {en, 9.0, Symbol.Default(EUR), "€ 9.00"},
46 10: {en, 10.123, Symbol.Default(KRW), "₩ 10"},
47 11: {fr, 11.52, Symbol.Default(TWD), "TWD 11,52"},
48 12: {en, 12.123, Symbol.Default(czk), "CZK 12.12"},
49 13: {en, 13.123, Symbol.Default(czk).Kind(Cash), "CZK 13"},
50 14: {en, 14.12345, ISO.Default(MustParseISO("CLF")), "CLF 14.1235"},
51 15: {en, USD.Amount(15.00), ISO.Default(TWD), "USD 15.00"},
52 16: {en, KRW.Amount(16.00), ISO.Kind(Cash), "KRW 16"},
53
54 17: {en, USD, nil, "USD"},
55 18: {en, USD, ISO, "USD"},
56 19: {en, USD, Symbol, "$"},
57 20: {en_GB, USD, Symbol, "US$"},
58 21: {en_AU, USD, NarrowSymbol, "$"},
59
60
61 22: {de, EUR.Amount(1234567.89), nil, "EUR 1.234.567,89"},
62 23: {fr, EUR.Amount(1234567.89), nil, "EUR 1\u00a0234\u00a0567,89"},
63 24: {en_AU, EUR.Amount(1234567.89), nil, "EUR 1,234,567.89"},
64 25: {de_CH, EUR.Amount(1234567.89), nil, "EUR 1’234’567.89"},
65
66
67 26: {de, NOK.Amount(2.49), ISO.Kind(Cash), "NOK 2"},
68 27: {de, NOK.Amount(2.50), ISO.Kind(Cash), "NOK 3"},
69 28: {de, DKK.Amount(0.24), ISO.Kind(Cash), "DKK 0,00"},
70 29: {de, DKK.Amount(0.25), ISO.Kind(Cash), "DKK 0,50"},
71
72
73 30: {de, EUR.Amount(1234567), nil, "EUR 1.234.567,00"},
74 31: {en, CNY.Amount(0), NarrowSymbol, "¥ 0.00"},
75 32: {en, CNY.Amount(0), Symbol, "CN¥ 0.00"},
76 }
77 for i, tc := range testCases {
78 p := message.NewPrinter(tc.tag)
79 v := tc.value
80 if tc.format != nil {
81 v = tc.format(v)
82 }
83 if got := p.Sprint(v); got != tc.want {
84 t.Errorf("%d: got %q; want %q", i, got, tc.want)
85 }
86 }
87 }
88
View as plain text