gen_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2017 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 date
  5. import (
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "golang.org/x/text/internal/cldrtree"
  10. "golang.org/x/text/internal/gen"
  11. "golang.org/x/text/internal/language/compact"
  12. "golang.org/x/text/internal/testtext"
  13. "golang.org/x/text/language"
  14. "golang.org/x/text/unicode/cldr"
  15. )
  16. func TestTables(t *testing.T) {
  17. testtext.SkipIfNotLong(t)
  18. r := gen.OpenCLDRCoreZip()
  19. defer r.Close()
  20. d := &cldr.Decoder{}
  21. d.SetDirFilter("supplemental", "main")
  22. d.SetSectionFilter("dates")
  23. data, err := d.DecodeZip(r)
  24. if err != nil {
  25. t.Fatalf("DecodeZip: %v", err)
  26. }
  27. count := 0
  28. for _, lang := range data.Locales() {
  29. ldml := data.RawLDML(lang)
  30. if ldml.Dates == nil {
  31. continue
  32. }
  33. tag, _ := compact.RegionalID(compact.Tag(language.MustParse(lang)))
  34. test := func(want cldrtree.Element, path ...string) {
  35. if count > 30 {
  36. return
  37. }
  38. t.Run(lang+"/"+strings.Join(path, "/"), func(t *testing.T) {
  39. p := make([]uint16, len(path))
  40. for i, s := range path {
  41. if v, err := strconv.Atoi(s); err == nil {
  42. p[i] = uint16(v)
  43. } else if v, ok := enumMap[s]; ok {
  44. p[i] = v
  45. } else {
  46. count++
  47. t.Fatalf("Unknown key %q", s)
  48. }
  49. }
  50. wantStr := want.GetCommon().Data()
  51. if got := tree.Lookup(tag, p...); got != wantStr {
  52. count++
  53. t.Errorf("got %q; want %q", got, wantStr)
  54. }
  55. })
  56. }
  57. width := func(s string) string { return "width" + strings.Title(s) }
  58. if ldml.Dates.Calendars != nil {
  59. for _, cal := range ldml.Dates.Calendars.Calendar {
  60. if cal.Months != nil {
  61. for _, mc := range cal.Months.MonthContext {
  62. for _, mw := range mc.MonthWidth {
  63. for _, m := range mw.Month {
  64. test(m, "calendars", cal.Type, "months", mc.Type, width(mw.Type), m.Yeartype+m.Type)
  65. }
  66. }
  67. }
  68. }
  69. if cal.MonthPatterns != nil {
  70. for _, mc := range cal.MonthPatterns.MonthPatternContext {
  71. for _, mw := range mc.MonthPatternWidth {
  72. for _, m := range mw.MonthPattern {
  73. test(m, "calendars", cal.Type, "monthPatterns", mc.Type, width(mw.Type))
  74. }
  75. }
  76. }
  77. }
  78. if cal.CyclicNameSets != nil {
  79. for _, cns := range cal.CyclicNameSets.CyclicNameSet {
  80. for _, cc := range cns.CyclicNameContext {
  81. for _, cw := range cc.CyclicNameWidth {
  82. for _, c := range cw.CyclicName {
  83. test(c, "calendars", cal.Type, "cyclicNameSets", cns.Type+"CycleType", cc.Type, width(cw.Type), c.Type)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. if cal.Days != nil {
  90. for _, dc := range cal.Days.DayContext {
  91. for _, dw := range dc.DayWidth {
  92. for _, d := range dw.Day {
  93. test(d, "calendars", cal.Type, "days", dc.Type, width(dw.Type), d.Type)
  94. }
  95. }
  96. }
  97. }
  98. if cal.Quarters != nil {
  99. for _, qc := range cal.Quarters.QuarterContext {
  100. for _, qw := range qc.QuarterWidth {
  101. for _, q := range qw.Quarter {
  102. test(q, "calendars", cal.Type, "quarters", qc.Type, width(qw.Type), q.Type)
  103. }
  104. }
  105. }
  106. }
  107. if cal.DayPeriods != nil {
  108. for _, dc := range cal.DayPeriods.DayPeriodContext {
  109. for _, dw := range dc.DayPeriodWidth {
  110. for _, d := range dw.DayPeriod {
  111. test(d, "calendars", cal.Type, "dayPeriods", dc.Type, width(dw.Type), d.Type, d.Alt)
  112. }
  113. }
  114. }
  115. }
  116. if cal.Eras != nil {
  117. if cal.Eras.EraNames != nil {
  118. for _, e := range cal.Eras.EraNames.Era {
  119. test(e, "calendars", cal.Type, "eras", "widthWide", e.Alt, e.Type)
  120. }
  121. }
  122. if cal.Eras.EraAbbr != nil {
  123. for _, e := range cal.Eras.EraAbbr.Era {
  124. test(e, "calendars", cal.Type, "eras", "widthAbbreviated", e.Alt, e.Type)
  125. }
  126. }
  127. if cal.Eras.EraNarrow != nil {
  128. for _, e := range cal.Eras.EraNarrow.Era {
  129. test(e, "calendars", cal.Type, "eras", "widthNarrow", e.Alt, e.Type)
  130. }
  131. }
  132. }
  133. if cal.DateFormats != nil {
  134. for _, dfl := range cal.DateFormats.DateFormatLength {
  135. for _, df := range dfl.DateFormat {
  136. for _, p := range df.Pattern {
  137. test(p, "calendars", cal.Type, "dateFormats", dfl.Type, p.Alt)
  138. }
  139. }
  140. }
  141. }
  142. if cal.TimeFormats != nil {
  143. for _, tfl := range cal.TimeFormats.TimeFormatLength {
  144. for _, tf := range tfl.TimeFormat {
  145. for _, p := range tf.Pattern {
  146. test(p, "calendars", cal.Type, "timeFormats", tfl.Type, p.Alt)
  147. }
  148. }
  149. }
  150. }
  151. if cal.DateTimeFormats != nil {
  152. for _, dtfl := range cal.DateTimeFormats.DateTimeFormatLength {
  153. for _, dtf := range dtfl.DateTimeFormat {
  154. for _, p := range dtf.Pattern {
  155. test(p, "calendars", cal.Type, "dateTimeFormats", dtfl.Type, p.Alt)
  156. }
  157. }
  158. }
  159. // TODO:
  160. // - appendItems
  161. // - intervalFormats
  162. }
  163. }
  164. }
  165. // TODO: this is a lot of data and is probably relatively little used.
  166. // Store this somewhere else.
  167. if ldml.Dates.Fields != nil {
  168. for _, f := range ldml.Dates.Fields.Field {
  169. field := f.Type + "Field"
  170. for _, d := range f.DisplayName {
  171. test(d, "fields", field, "displayName", d.Alt)
  172. }
  173. for _, r := range f.Relative {
  174. i, _ := strconv.Atoi(r.Type)
  175. v := []string{"before2", "before1", "current", "after1", "after2", "after3"}[i+2]
  176. test(r, "fields", field, "relative", v)
  177. }
  178. for _, rt := range f.RelativeTime {
  179. for _, p := range rt.RelativeTimePattern {
  180. test(p, "fields", field, "relativeTime", rt.Type, p.Count)
  181. }
  182. }
  183. for _, rp := range f.RelativePeriod {
  184. test(rp, "fields", field, "relativePeriod", rp.Alt)
  185. }
  186. }
  187. }
  188. if ldml.Dates.TimeZoneNames != nil {
  189. for _, h := range ldml.Dates.TimeZoneNames.HourFormat {
  190. test(h, "timeZoneNames", "zoneFormat", h.Element())
  191. }
  192. for _, g := range ldml.Dates.TimeZoneNames.GmtFormat {
  193. test(g, "timeZoneNames", "zoneFormat", g.Element())
  194. }
  195. for _, g := range ldml.Dates.TimeZoneNames.GmtZeroFormat {
  196. test(g, "timeZoneNames", "zoneFormat", g.Element())
  197. }
  198. for _, r := range ldml.Dates.TimeZoneNames.RegionFormat {
  199. s := r.Type
  200. if s == "" {
  201. s = "generic"
  202. }
  203. test(r, "timeZoneNames", "regionFormat", s+"Time")
  204. }
  205. testZone := func(zoneType, zoneWidth, zone string, a ...[]*cldr.Common) {
  206. for _, e := range a {
  207. for _, n := range e {
  208. test(n, "timeZoneNames", zoneType, zoneWidth, n.Element()+"Time", zone)
  209. }
  210. }
  211. }
  212. for _, z := range ldml.Dates.TimeZoneNames.Zone {
  213. for _, l := range z.Long {
  214. testZone("zone", l.Element(), z.Type, l.Generic, l.Standard, l.Daylight)
  215. }
  216. for _, l := range z.Short {
  217. testZone("zone", l.Element(), z.Type, l.Generic, l.Standard, l.Daylight)
  218. }
  219. }
  220. for _, z := range ldml.Dates.TimeZoneNames.Metazone {
  221. for _, l := range z.Long {
  222. testZone("metaZone", l.Element(), z.Type, l.Generic, l.Standard, l.Daylight)
  223. }
  224. for _, l := range z.Short {
  225. testZone("metaZone", l.Element(), z.Type, l.Generic, l.Standard, l.Daylight)
  226. }
  227. }
  228. }
  229. }
  230. }