format_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package currency
  5. import (
  6. "testing"
  7. "golang.org/x/text/language"
  8. "golang.org/x/text/message"
  9. )
  10. var (
  11. en = language.English
  12. fr = language.French
  13. en_US = language.AmericanEnglish
  14. en_GB = language.BritishEnglish
  15. en_AU = language.MustParse("en-AU")
  16. und = language.Und
  17. )
  18. func TestFormatting(t *testing.T) {
  19. testCases := []struct {
  20. tag language.Tag
  21. value interface{}
  22. format Formatter
  23. want string
  24. }{
  25. 0: {en, USD.Amount(0.1), nil, "USD 0.10"},
  26. 1: {en, XPT.Amount(1.0), Symbol, "XPT 1.00"},
  27. 2: {en, USD.Amount(2.0), ISO, "USD 2.00"},
  28. 3: {und, USD.Amount(3.0), Symbol, "US$ 3.00"},
  29. 4: {en, USD.Amount(4.0), Symbol, "$ 4.00"},
  30. 5: {en, USD.Amount(5.20), NarrowSymbol, "$ 5.20"},
  31. 6: {en, AUD.Amount(6.20), Symbol, "A$ 6.20"},
  32. 7: {en_AU, AUD.Amount(7.20), Symbol, "$ 7.20"},
  33. 8: {en_GB, USD.Amount(8.20), Symbol, "US$ 8.20"},
  34. 9: {en, 9.0, Symbol.Default(EUR), "€ 9.00"},
  35. 10: {en, 10.123, Symbol.Default(KRW), "₩ 10"},
  36. 11: {fr, 11.52, Symbol.Default(TWD), "TWD 11.52"},
  37. 12: {en, 12.123, Symbol.Default(czk), "CZK 12.12"},
  38. 13: {en, 13.123, Symbol.Default(czk).Kind(Cash), "CZK 13"},
  39. 14: {en, 14.12345, ISO.Default(MustParseISO("CLF")), "CLF 14.1235"},
  40. 15: {en, USD.Amount(15.00), ISO.Default(TWD), "USD 15.00"},
  41. 16: {en, KRW.Amount(16.00), ISO.Kind(Cash), "KRW 16"},
  42. // TODO: support integers as well.
  43. 17: {en, USD, nil, "USD"},
  44. 18: {en, USD, ISO, "USD"},
  45. 19: {en, USD, Symbol, "$"},
  46. 20: {en_GB, USD, Symbol, "US$"},
  47. 21: {en_AU, USD, NarrowSymbol, "$"},
  48. }
  49. for i, tc := range testCases {
  50. p := message.NewPrinter(tc.tag)
  51. v := tc.value
  52. if tc.format != nil {
  53. v = tc.format(v)
  54. }
  55. if got := p.Sprint(v); got != tc.want {
  56. t.Errorf("%d: got %q; want %q", i, got, tc.want)
  57. }
  58. }
  59. }