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