You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 regels
4.1 KiB

  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. base := getSize(t, `fmt.Print(currency.CLDRVersion)`)
  141. symbols := getSize(t, `fmt.Print(currency.Symbol(currency.USD))`)
  142. if d := symbols - base; d < 2*1024 {
  143. t.Errorf("size(symbols)-size(base) was %d; want > 2K", d)
  144. }
  145. }
  146. func getSize(t *testing.T, main string) int {
  147. size, err := testtext.CodeSize(fmt.Sprintf(body, main))
  148. if err != nil {
  149. t.Skipf("skipping link size test; binary size could not be determined: %v", err)
  150. }
  151. return size
  152. }
  153. func BenchmarkString(b *testing.B) {
  154. for i := 0; i < b.N; i++ {
  155. USD.String()
  156. }
  157. }