currency_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "fmt"
  7. "testing"
  8. "golang.org/x/text/internal/testtext"
  9. "golang.org/x/text/language"
  10. )
  11. var (
  12. cup = MustParseISO("CUP")
  13. czk = MustParseISO("CZK")
  14. xcd = MustParseISO("XCD")
  15. zwr = MustParseISO("ZWR")
  16. )
  17. func TestParseISO(t *testing.T) {
  18. testCases := []struct {
  19. in string
  20. out Unit
  21. ok bool
  22. }{
  23. {"USD", USD, true},
  24. {"xxx", XXX, true},
  25. {"xts", XTS, true},
  26. {"XX", XXX, false},
  27. {"XXXX", XXX, false},
  28. {"", XXX, false}, // not well-formed
  29. {"UUU", XXX, false}, // unknown
  30. {"\u22A9", XXX, false}, // non-ASCII, printable
  31. {"aaa", XXX, false},
  32. {"zzz", XXX, false},
  33. {"000", XXX, false},
  34. {"999", XXX, false},
  35. {"---", XXX, false},
  36. {"\x00\x00\x00", XXX, false},
  37. {"\xff\xff\xff", XXX, false},
  38. }
  39. for i, tc := range testCases {
  40. if x, err := ParseISO(tc.in); x != tc.out || err == nil != tc.ok {
  41. t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tc.in, x, err == nil, tc.out, tc.ok)
  42. }
  43. }
  44. }
  45. func TestFromRegion(t *testing.T) {
  46. testCases := []struct {
  47. region string
  48. currency Unit
  49. ok bool
  50. }{
  51. {"NL", EUR, true},
  52. {"BE", EUR, true},
  53. {"AG", xcd, true},
  54. {"CH", CHF, true},
  55. {"CU", cup, true}, // first of multiple
  56. {"DG", USD, true}, // does not have M49 code
  57. {"150", XXX, false}, // implicit false
  58. {"CP", XXX, false}, // explicit false in CLDR
  59. {"CS", XXX, false}, // all expired
  60. {"ZZ", XXX, false}, // none match
  61. }
  62. for _, tc := range testCases {
  63. cur, ok := FromRegion(language.MustParseRegion(tc.region))
  64. if cur != tc.currency || ok != tc.ok {
  65. t.Errorf("%s: got %v, %v; want %v, %v", tc.region, cur, ok, tc.currency, tc.ok)
  66. }
  67. }
  68. }
  69. func TestFromTag(t *testing.T) {
  70. testCases := []struct {
  71. tag string
  72. currency Unit
  73. conf language.Confidence
  74. }{
  75. {"nl", EUR, language.Low}, // nl also spoken outside Euro land.
  76. {"nl-BE", EUR, language.Exact}, // region is known
  77. {"pt", BRL, language.Low},
  78. {"en", USD, language.Low},
  79. {"en-u-cu-eur", EUR, language.Exact},
  80. {"tlh", XXX, language.No}, // Klingon has no country.
  81. {"es-419", XXX, language.No},
  82. {"und", USD, language.Low},
  83. }
  84. for _, tc := range testCases {
  85. cur, conf := FromTag(language.MustParse(tc.tag))
  86. if cur != tc.currency || conf != tc.conf {
  87. t.Errorf("%s: got %v, %v; want %v, %v", tc.tag, cur, conf, tc.currency, tc.conf)
  88. }
  89. }
  90. }
  91. func TestTable(t *testing.T) {
  92. for i := 4; i < len(currency); i += 4 {
  93. if a, b := currency[i-4:i-1], currency[i:i+3]; a >= b {
  94. t.Errorf("currency unordered at element %d: %s >= %s", i, a, b)
  95. }
  96. }
  97. // First currency has index 1, last is numCurrencies.
  98. if c := currency.Elem(1)[:3]; c != "ADP" {
  99. t.Errorf("first was %q; want ADP", c)
  100. }
  101. if c := currency.Elem(numCurrencies)[:3]; c != "ZWR" {
  102. t.Errorf("last was %q; want ZWR", c)
  103. }
  104. }
  105. func TestKindRounding(t *testing.T) {
  106. testCases := []struct {
  107. kind Kind
  108. cur Unit
  109. scale int
  110. inc int
  111. }{
  112. {Standard, USD, 2, 1},
  113. {Standard, CHF, 2, 1},
  114. {Cash, CHF, 2, 5},
  115. {Standard, TWD, 2, 1},
  116. {Cash, TWD, 0, 1},
  117. {Standard, czk, 2, 1},
  118. {Cash, czk, 0, 1},
  119. {Standard, zwr, 2, 1},
  120. {Cash, zwr, 0, 1},
  121. {Standard, KRW, 0, 1},
  122. {Cash, KRW, 0, 1}, // Cash defaults to standard.
  123. }
  124. for i, tc := range testCases {
  125. if scale, inc := tc.kind.Rounding(tc.cur); scale != tc.scale && inc != tc.inc {
  126. t.Errorf("%d: got %d, %d; want %d, %d", i, scale, inc, tc.scale, tc.inc)
  127. }
  128. }
  129. }
  130. const body = `package main
  131. import (
  132. "fmt"
  133. "golang.org/x/text/currency"
  134. )
  135. func main() {
  136. %s
  137. }
  138. `
  139. func TestLinking(t *testing.T) {
  140. t.Skip("skipping flaky test; see golang.org/issue/17538")
  141. base := getSize(t, `fmt.Print(currency.CLDRVersion)`)
  142. symbols := getSize(t, `fmt.Print(currency.Symbol(currency.USD))`)
  143. if d := symbols - base; d < 2*1024 {
  144. t.Errorf("size(symbols)-size(base) was %d; want > 2K", d)
  145. }
  146. }
  147. func getSize(t *testing.T, main string) int {
  148. size, err := testtext.CodeSize(fmt.Sprintf(body, main))
  149. if err != nil {
  150. t.Skipf("skipping link size test; binary size could not be determined: %v", err)
  151. }
  152. return size
  153. }
  154. func BenchmarkString(b *testing.B) {
  155. for i := 0; i < b.N; i++ {
  156. USD.String()
  157. }
  158. }