tables_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "flag"
  7. "log"
  8. "reflect"
  9. "testing"
  10. "golang.org/x/text/internal/gen"
  11. "golang.org/x/text/internal/language"
  12. "golang.org/x/text/internal/language/compact"
  13. "golang.org/x/text/internal/testtext"
  14. "golang.org/x/text/unicode/cldr"
  15. )
  16. var draft = flag.String("draft",
  17. "contributed",
  18. `Minimal draft requirements (approved, contributed, provisional, unconfirmed).`)
  19. func TestNumberSystems(t *testing.T) {
  20. testtext.SkipIfNotLong(t)
  21. r := gen.OpenCLDRCoreZip()
  22. defer r.Close()
  23. d := &cldr.Decoder{}
  24. d.SetDirFilter("supplemental")
  25. d.SetSectionFilter("numberingSystem")
  26. data, err := d.DecodeZip(r)
  27. if err != nil {
  28. t.Fatalf("DecodeZip: %v", err)
  29. }
  30. for _, ns := range data.Supplemental().NumberingSystems.NumberingSystem {
  31. n := systemMap[ns.Id]
  32. if int(n) >= len(numSysData) {
  33. continue
  34. }
  35. info := InfoFromLangID(0, ns.Id)
  36. val := '0'
  37. for _, rWant := range ns.Digits {
  38. if rGot := info.Digit(val); rGot != rWant {
  39. t.Errorf("%s:%d: got %U; want %U", ns.Id, val, rGot, rWant)
  40. }
  41. val++
  42. }
  43. }
  44. }
  45. func TestSymbols(t *testing.T) {
  46. testtext.SkipIfNotLong(t)
  47. draft, err := cldr.ParseDraft(*draft)
  48. if err != nil {
  49. log.Fatalf("invalid draft level: %v", err)
  50. }
  51. r := gen.OpenCLDRCoreZip()
  52. defer r.Close()
  53. d := &cldr.Decoder{}
  54. d.SetDirFilter("main")
  55. d.SetSectionFilter("numbers")
  56. data, err := d.DecodeZip(r)
  57. if err != nil {
  58. t.Fatalf("DecodeZip: %v", err)
  59. }
  60. for _, lang := range data.Locales() {
  61. ldml := data.RawLDML(lang)
  62. if ldml.Numbers == nil {
  63. continue
  64. }
  65. langIndex, ok := compact.FromTag(language.MustParse(lang))
  66. if !ok {
  67. t.Fatalf("No compact index for language %s", lang)
  68. }
  69. syms := cldr.MakeSlice(&ldml.Numbers.Symbols)
  70. syms.SelectDraft(draft)
  71. for _, sym := range ldml.Numbers.Symbols {
  72. if sym.NumberSystem == "" {
  73. continue
  74. }
  75. testCases := []struct {
  76. name string
  77. st SymbolType
  78. x interface{}
  79. }{
  80. {"Decimal", SymDecimal, sym.Decimal},
  81. {"Group", SymGroup, sym.Group},
  82. {"List", SymList, sym.List},
  83. {"PercentSign", SymPercentSign, sym.PercentSign},
  84. {"PlusSign", SymPlusSign, sym.PlusSign},
  85. {"MinusSign", SymMinusSign, sym.MinusSign},
  86. {"Exponential", SymExponential, sym.Exponential},
  87. {"SuperscriptingExponent", SymSuperscriptingExponent, sym.SuperscriptingExponent},
  88. {"PerMille", SymPerMille, sym.PerMille},
  89. {"Infinity", SymInfinity, sym.Infinity},
  90. {"NaN", SymNan, sym.Nan},
  91. {"TimeSeparator", SymTimeSeparator, sym.TimeSeparator},
  92. }
  93. info := InfoFromLangID(langIndex, sym.NumberSystem)
  94. for _, tc := range testCases {
  95. // Extract the wanted value.
  96. v := reflect.ValueOf(tc.x)
  97. if v.Len() == 0 {
  98. return
  99. }
  100. if v.Len() > 1 {
  101. t.Fatalf("Multiple values of %q within single symbol not supported.", tc.name)
  102. }
  103. want := v.Index(0).MethodByName("Data").Call(nil)[0].String()
  104. got := info.Symbol(tc.st)
  105. if got != want {
  106. t.Errorf("%s:%s:%s: got %q; want %q", lang, sym.NumberSystem, tc.name, got, want)
  107. }
  108. }
  109. }
  110. }
  111. }