summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/cldrtree/cldrtree_test.go
blob: d93c763370006b635afc0e0a7b9751550b8baaa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cldrtree

import (
	"bytes"
	"flag"
	"io/ioutil"
	"log"
	"math/rand"
	"path/filepath"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"testing"

	"golang.org/x/text/internal/gen"
	"golang.org/x/text/language"
	"golang.org/x/text/unicode/cldr"
)

var genOutput = flag.Bool("gen", false, "generate output files")

func TestAliasRegexp(t *testing.T) {
	testCases := []struct {
		alias string
		want  []string
	}{{
		alias: "miscPatterns[@numberSystem='latn']",
		want: []string{
			"miscPatterns[@numberSystem='latn']",
			"miscPatterns",
			"[@numberSystem='latn']",
			"numberSystem",
			"latn",
		},
	}, {
		alias: `calendar[@type='greg-foo']/days/`,
		want: []string{
			"calendar[@type='greg-foo']",
			"calendar",
			"[@type='greg-foo']",
			"type",
			"greg-foo",
		},
	}, {
		alias: "eraAbbr",
		want: []string{
			"eraAbbr",
			"eraAbbr",
			"",
			"",
			"",
		},
	}, {
		// match must be anchored at beginning.
		alias: `../calendar[@type='gregorian']/days/`,
	}}
	for _, tc := range testCases {
		t.Run(tc.alias, func(t *testing.T) {
			got := aliasRe.FindStringSubmatch(tc.alias)
			if !reflect.DeepEqual(got, tc.want) {
				t.Errorf("got %v; want %v", got, tc.want)
			}
		})
	}
}

func TestBuild(t *testing.T) {
	tree1, _ := loadTestdata(t, "test1")
	tree2, _ := loadTestdata(t, "test2")

	// Constants for second test test
	const (
		calendar = iota
		field
	)
	const (
		month = iota
		era
		filler
		cyclicNameSet
	)
	const (
		abbreviated = iota
		narrow
		wide
	)

	testCases := []struct {
		desc      string
		tree      *Tree
		locale    string
		path      []uint16
		isFeature bool
		result    string
	}{{
		desc:   "und/chinese month format wide m1",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 0, month, 0, wide, 1),
		result: "cM01",
	}, {
		desc:   "und/chinese month format wide m12",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 0, month, 0, wide, 12),
		result: "cM12",
	}, {
		desc:   "und/non-existing value",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 0, month, 0, wide, 13),
		result: "",
	}, {
		desc:   "und/dangi:chinese month format wide",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 1, month, 0, wide, 1),
		result: "cM01",
	}, {
		desc:   "und/chinese month format abbreviated:wide",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 0, month, 0, abbreviated, 1),
		result: "cM01",
	}, {
		desc:   "und/chinese month format narrow:wide",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 0, month, 0, narrow, 1),
		result: "cM01",
	}, {
		desc:   "und/gregorian month format wide",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 2, month, 0, wide, 2),
		result: "gM02",
	}, {
		desc:   "und/gregorian month format:stand-alone narrow",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 2, month, 0, narrow, 1),
		result: "1",
	}, {
		desc:   "und/gregorian month stand-alone:format abbreviated",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 2, month, 1, abbreviated, 1),
		result: "gM01",
	}, {
		desc:   "und/gregorian month stand-alone:format wide ",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 2, month, 1, abbreviated, 1),
		result: "gM01",
	}, {
		desc:   "und/dangi:chinese month format narrow:wide ",
		tree:   tree1,
		locale: "und",
		path:   path(calendar, 1, month, 0, narrow, 4),
		result: "cM04",
	}, {
		desc:   "und/field era displayname 0",
		tree:   tree2,
		locale: "und",
		path:   path(field, 0, 0, 0),
		result: "Era",
	}, {
		desc:   "en/field era displayname 0",
		tree:   tree2,
		locale: "en",
		path:   path(field, 0, 0, 0),
		result: "era",
	}, {
		desc:   "und/calendar hebrew format wide 7-leap",
		tree:   tree2,
		locale: "und",
		path:   path(calendar, 7, month, 0, wide, 0),
		result: "Adar II",
	}, {
		desc:   "en-GB:en-001:en:und/calendar hebrew format wide 7-leap",
		tree:   tree2,
		locale: "en-GB",
		path:   path(calendar, 7, month, 0, wide, 0),
		result: "Adar II",
	}, {
		desc:   "und/buddhist month format wide 11",
		tree:   tree2,
		locale: "und",
		path:   path(calendar, 0, month, 0, wide, 12),
		result: "genWideM12",
	}, {
		desc:   "en-GB/gregorian month stand-alone narrow 2",
		tree:   tree2,
		locale: "en-GB",
		path:   path(calendar, 6, month, 1, narrow, 3),
		result: "gbNarrowM3",
	}, {
		desc:   "en-GB/gregorian month format narrow 3/missing in en-GB",
		tree:   tree2,
		locale: "en-GB",
		path:   path(calendar, 6, month, 0, narrow, 4),
		result: "enNarrowM4",
	}, {
		desc:   "en-GB/gregorian month format narrow 3/missing in en and en-GB",
		tree:   tree2,
		locale: "en-GB",
		path:   path(calendar, 6, month, 0, narrow, 7),
		result: "gregNarrowM7",
	}, {
		desc:   "en-GB/gregorian month format narrow 3/missing in en and en-GB",
		tree:   tree2,
		locale: "en-GB",
		path:   path(calendar, 6, month, 0, narrow, 7),
		result: "gregNarrowM7",
	}, {
		desc:      "en-GB/gregorian era narrow",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(calendar, 6, era, abbreviated, 0, 1),
		isFeature: true,
		result:    "AD",
	}, {
		desc:      "en-GB/gregorian era narrow",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(calendar, 6, era, narrow, 0, 0),
		isFeature: true,
		result:    "BC",
	}, {
		desc:      "en-GB/gregorian era narrow",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(calendar, 6, era, wide, 1, 0),
		isFeature: true,
		result:    "Before Common Era",
	}, {
		desc:      "en-GB/dangi:chinese cyclicName, months, format, narrow:abbreviated 2",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(calendar, 1, cyclicNameSet, 3, 0, 1, 2),
		isFeature: true,
		result:    "year2",
	}, {
		desc:   "en-GB/field era-narrow ",
		tree:   tree2,
		locale: "en-GB",
		path:   path(field, 2, 0, 0),
		result: "era",
	}, {
		desc:      "en-GB/field month-narrow relativeTime future one",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(field, 5, 2, 0, 1),
		isFeature: true,
		result:    "001NarrowFutMOne",
	}, {
		// Don't fall back to the one of "en".
		desc:      "en-GB/field month-short relativeTime past one:other",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(field, 4, 2, 1, 1),
		isFeature: true,
		result:    "001ShortPastMOther",
	}, {
		desc:      "en-GB/field month relativeTime future two:other",
		tree:      tree2,
		locale:    "en-GB",
		path:      path(field, 3, 2, 0, 2),
		isFeature: true,
		result:    "enFutMOther",
	}}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			tag, _ := language.CompactIndex(language.MustParse(tc.locale))
			s := tc.tree.lookup(tag, tc.isFeature, tc.path...)
			if s != tc.result {
				t.Errorf("got %q; want %q", s, tc.result)
			}
		})
	}
}

func path(e ...uint16) []uint16 { return e }

func TestGen(t *testing.T) {
	testCases := []string{"test1", "test2"}
	for _, tc := range testCases {
		t.Run(tc, func(t *testing.T) {
			_, got := loadTestdata(t, tc)

			// Remove sizes that may vary per architecture.
			re := regexp.MustCompile("// Size: [0-9]*")
			got = re.ReplaceAllLiteral(got, []byte("// Size: xxxx"))
			re = regexp.MustCompile("// Total table size [0-9]*")
			got = re.ReplaceAllLiteral(got, []byte("// Total table size: xxxx"))

			file := filepath.Join("testdata", tc, "output.go")
			if *genOutput {
				ioutil.WriteFile(file, got, 0700)
				t.SkipNow()
			}

			b, err := ioutil.ReadFile(file)
			if err != nil {
				t.Fatalf("failed to open file: %v", err)
			}
			if want := string(b); string(got) != want {
				t.Log(string(got))
				t.Errorf("files differ")
			}
		})
	}
}

func loadTestdata(t *testing.T, test string) (tree *Tree, file []byte) {
	b := New("test")

	var d cldr.Decoder

	data, err := d.DecodePath(filepath.Join("testdata", test))
	if err != nil {
		t.Fatalf("error decoding testdata: %v", err)
	}

	context := Enum("context")
	widthMap := func(s string) string {
		// Align era with width values.
		if r, ok := map[string]string{
			"eraAbbr":   "abbreviated",
			"eraNarrow": "narrow",
			"eraNames":  "wide",
		}[s]; ok {
			s = r
		}
		return "w" + strings.Title(s)
	}
	width := EnumFunc("width", widthMap, "abbreviated", "narrow", "wide")
	month := Enum("month", "leap7")
	relative := EnumFunc("relative", func(s string) string {
		x, err := strconv.ParseInt(s, 10, 8)
		if err != nil {
			log.Fatal("Invalid number:", err)
		}
		return []string{
			"before1",
			"current",
			"after1",
		}[x+1]
	})
	cycleType := EnumFunc("cycleType", func(s string) string {
		return "cyc" + strings.Title(s)
	})
	r := rand.New(rand.NewSource(0))

	for _, loc := range data.Locales() {
		ldml := data.RawLDML(loc)
		x := b.Locale(language.Make(loc))

		if x := x.Index(ldml.Dates.Calendars); x != nil {
			for _, cal := range ldml.Dates.Calendars.Calendar {
				x := x.IndexFromType(cal)
				if x := x.Index(cal.Months); x != nil {
					for _, mc := range cal.Months.MonthContext {
						x := x.IndexFromType(mc, context)
						for _, mw := range mc.MonthWidth {
							x := x.IndexFromType(mw, width)
							for _, m := range mw.Month {
								x.SetValue(m.Yeartype+m.Type, m, month)
							}
						}
					}
				}
				if x := x.Index(cal.CyclicNameSets); x != nil {
					for _, cns := range cal.CyclicNameSets.CyclicNameSet {
						x := x.IndexFromType(cns, cycleType)
						for _, cc := range cns.CyclicNameContext {
							x := x.IndexFromType(cc, context)
							for _, cw := range cc.CyclicNameWidth {
								x := x.IndexFromType(cw, width)
								for _, c := range cw.CyclicName {
									x.SetValue(c.Type, c)
								}
							}
						}
					}
				}
				if x := x.Index(cal.Eras); x != nil {
					opts := []Option{width, SharedType()}
					if x := x.Index(cal.Eras.EraNames, opts...); x != nil {
						for _, e := range cal.Eras.EraNames.Era {
							x.IndexFromAlt(e).SetValue(e.Type, e)
						}
					}
					if x := x.Index(cal.Eras.EraAbbr, opts...); x != nil {
						for _, e := range cal.Eras.EraAbbr.Era {
							x.IndexFromAlt(e).SetValue(e.Type, e)
						}
					}
					if x := x.Index(cal.Eras.EraNarrow, opts...); x != nil {
						for _, e := range cal.Eras.EraNarrow.Era {
							x.IndexFromAlt(e).SetValue(e.Type, e)
						}
					}
				}
				{
					// Ensure having more than 2 buckets.
					f := x.IndexWithName("filler")
					b := make([]byte, maxStrlen)
					opt := &options{parent: x}
					r.Read(b)
					f.setValue("0", string(b), opt)
				}
			}
		}
		if x := x.Index(ldml.Dates.Fields); x != nil {
			for _, f := range ldml.Dates.Fields.Field {
				x := x.IndexFromType(f)
				for _, d := range f.DisplayName {
					x.Index(d).SetValue("", d)
				}
				for _, r := range f.Relative {
					x.Index(r).SetValue(r.Type, r, relative)
				}
				for _, rt := range f.RelativeTime {
					x := x.Index(rt).IndexFromType(rt)
					for _, p := range rt.RelativeTimePattern {
						x.SetValue(p.Count, p)
					}
				}
				for _, rp := range f.RelativePeriod {
					x.Index(rp).SetValue("", rp)
				}
			}
		}
	}

	tree, err = build(b)
	if err != nil {
		t.Fatal("error building tree:", err)
	}
	w := gen.NewCodeWriter()
	generate(b, tree, w)
	generateTestData(b, w)
	buf := &bytes.Buffer{}
	if _, err = w.WriteGo(buf, "test", ""); err != nil {
		t.Log(buf.String())
		t.Fatal("error generating code:", err)
	}
	return tree, buf.Bytes()
}