tables_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package currency
  2. import (
  3. "flag"
  4. "strings"
  5. "testing"
  6. "time"
  7. "golang.org/x/text/internal/gen"
  8. "golang.org/x/text/internal/testtext"
  9. "golang.org/x/text/language"
  10. "golang.org/x/text/message"
  11. "golang.org/x/text/unicode/cldr"
  12. )
  13. var draft = flag.String("draft",
  14. "contributed",
  15. `Minimal draft requirements (approved, contributed, provisional, unconfirmed).`)
  16. func TestTables(t *testing.T) {
  17. testtext.SkipIfNotLong(t)
  18. // Read the CLDR zip file.
  19. r := gen.OpenCLDRCoreZip()
  20. defer r.Close()
  21. d := &cldr.Decoder{}
  22. d.SetDirFilter("supplemental", "main")
  23. d.SetSectionFilter("numbers")
  24. data, err := d.DecodeZip(r)
  25. if err != nil {
  26. t.Fatalf("DecodeZip: %v", err)
  27. }
  28. dr, err := cldr.ParseDraft(*draft)
  29. if err != nil {
  30. t.Fatalf("filter: %v", err)
  31. }
  32. for _, lang := range data.Locales() {
  33. p := message.NewPrinter(language.MustParse(lang))
  34. ldml := data.RawLDML(lang)
  35. if ldml.Numbers == nil || ldml.Numbers.Currencies == nil {
  36. continue
  37. }
  38. for _, c := range ldml.Numbers.Currencies.Currency {
  39. syms := cldr.MakeSlice(&c.Symbol)
  40. syms.SelectDraft(dr)
  41. for _, sym := range c.Symbol {
  42. cur, err := ParseISO(c.Type)
  43. if err != nil {
  44. continue
  45. }
  46. formatter := Symbol
  47. switch sym.Alt {
  48. case "":
  49. case "narrow":
  50. formatter = NarrowSymbol
  51. default:
  52. continue
  53. }
  54. want := sym.Data()
  55. if got := p.Sprint(formatter(cur)); got != want {
  56. t.Errorf("%s:%sSymbol(%s) = %s; want %s", lang, strings.Title(sym.Alt), c.Type, got, want)
  57. }
  58. }
  59. }
  60. }
  61. for _, reg := range data.Supplemental().CurrencyData.Region {
  62. i := 0
  63. for ; regionData[i].Region().String() != reg.Iso3166; i++ {
  64. }
  65. it := Query(Historical, NonTender, Region(language.MustParseRegion(reg.Iso3166)))
  66. for _, cur := range reg.Currency {
  67. from, _ := time.Parse("2006-01-02", cur.From)
  68. to, _ := time.Parse("2006-01-02", cur.To)
  69. it.Next()
  70. for j, r := range []QueryIter{&iter{regionInfo: &regionData[i]}, it} {
  71. if got, _ := r.From(); from != got {
  72. t.Errorf("%d:%s:%s:from: got %v; want %v", j, reg.Iso3166, cur.Iso4217, got, from)
  73. }
  74. if got, _ := r.To(); to != got {
  75. t.Errorf("%d:%s:%s:to: got %v; want %v", j, reg.Iso3166, cur.Iso4217, got, to)
  76. }
  77. }
  78. i++
  79. }
  80. }
  81. }