number_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2016 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 number
  5. import (
  6. "fmt"
  7. "testing"
  8. "golang.org/x/text/internal/testtext"
  9. "golang.org/x/text/language"
  10. )
  11. func TestInfo(t *testing.T) {
  12. testCases := []struct {
  13. lang string
  14. sym SymbolType
  15. wantSym string
  16. wantNine rune
  17. }{
  18. {"und", SymDecimal, ".", '9'},
  19. {"de", SymGroup, ".", '9'},
  20. {"de-BE", SymGroup, ".", '9'}, // inherits from de (no number data in CLDR)
  21. {"de-BE-oxendict", SymGroup, ".", '9'}, // inherits from de (no compact index)
  22. // U+096F DEVANAGARI DIGIT NINE ('९')
  23. {"de-BE-u-nu-deva", SymGroup, ".", '\u096f'}, // miss -> latn -> de
  24. {"de-Cyrl-BE", SymGroup, ",", '9'}, // inherits from root
  25. {"de-CH", SymGroup, "’", '9'}, // overrides values in de
  26. {"de-CH-oxendict", SymGroup, "’", '9'}, // inherits from de-CH (no compact index)
  27. {"de-CH-u-nu-deva", SymGroup, "’", '\u096f'}, // miss -> latn -> de-CH
  28. {"bn-u-nu-beng", SymGroup, ",", '\u09ef'},
  29. {"bn-u-nu-deva", SymGroup, ",", '\u096f'},
  30. {"bn-u-nu-latn", SymGroup, ",", '9'},
  31. {"pa", SymExponential, "E", '9'},
  32. // "×۱۰^" -> U+00d7 U+06f1 U+06f0^"
  33. // U+06F0 EXTENDED ARABIC-INDIC DIGIT ZERO
  34. // U+06F1 EXTENDED ARABIC-INDIC DIGIT ONE
  35. // U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE
  36. {"pa-u-nu-arabext", SymExponential, "\u00d7\u06f1\u06f0^", '\u06f9'},
  37. // "གྲངས་མེད" - > U+0f42 U+0fb2 U+0f44 U+0f66 U+0f0b U+0f58 U+0f7a U+0f51
  38. // Examples:
  39. // U+0F29 TIBETAN DIGIT NINE (༩)
  40. {"dz", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'}, // defaults to tibt
  41. {"dz-u-nu-latn", SymInfinity, "∞", '9'}, // select alternative
  42. {"dz-u-nu-tibt", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
  43. {"en-u-nu-tibt", SymInfinity, "∞", '\u0f29'},
  44. // algorithmic number systems fall back to ASCII if Digits is used.
  45. {"en-u-nu-hanidec", SymPlusSign, "+", '9'},
  46. {"en-u-nu-roman", SymPlusSign, "+", '9'},
  47. }
  48. for _, tc := range testCases {
  49. t.Run(fmt.Sprintf("%s:%v", tc.lang, tc.sym), func(t *testing.T) {
  50. info := InfoFromTag(language.MustParse(tc.lang))
  51. if got := info.Symbol(tc.sym); got != tc.wantSym {
  52. t.Errorf("sym: got %q; want %q", got, tc.wantSym)
  53. }
  54. if got := info.Digit('9'); got != tc.wantNine {
  55. t.Errorf("Digit(9): got %+q; want %+q", got, tc.wantNine)
  56. }
  57. var buf [4]byte
  58. if got := string(buf[:info.WriteDigit(buf[:], '9')]); got != string(tc.wantNine) {
  59. t.Errorf("WriteDigit(9): got %+q; want %+q", got, tc.wantNine)
  60. }
  61. if got := string(info.AppendDigit([]byte{}, 9)); got != string(tc.wantNine) {
  62. t.Errorf("AppendDigit(9): got %+q; want %+q", got, tc.wantNine)
  63. }
  64. })
  65. }
  66. }
  67. func TestFormats(t *testing.T) {
  68. testCases := []struct {
  69. lang string
  70. pattern string
  71. index []byte
  72. }{
  73. {"en", "#,##0.###", tagToDecimal},
  74. {"de", "#,##0.###", tagToDecimal},
  75. {"de-CH", "#,##0.###", tagToDecimal},
  76. {"pa", "#,##,##0.###", tagToDecimal},
  77. {"pa-Arab", "#,##0.###", tagToDecimal}, // Does NOT inherit from pa!
  78. {"mr", "#,##,##0.###", tagToDecimal},
  79. {"mr-IN", "#,##,##0.###", tagToDecimal}, // Inherits from mr.
  80. {"nl", "#E0", tagToScientific},
  81. {"nl-MX", "#E0", tagToScientific}, // Inherits through Tag.Parent.
  82. {"zgh", "#,##0 %", tagToPercent},
  83. }
  84. for _, tc := range testCases {
  85. testtext.Run(t, tc.lang, func(t *testing.T) {
  86. got := formatForLang(language.MustParse(tc.lang), tc.index)
  87. want, _ := ParsePattern(tc.pattern)
  88. if *got != *want {
  89. t.Errorf("\ngot %#v;\nwant %#v", got, want)
  90. }
  91. })
  92. }
  93. }