1
2
3
4
5 package number
6
7 import (
8 "fmt"
9 "testing"
10
11 "golang.org/x/text/internal/testtext"
12 "golang.org/x/text/language"
13 )
14
15 func TestInfo(t *testing.T) {
16 testCases := []struct {
17 lang string
18 sym SymbolType
19 wantSym string
20 wantNine rune
21 }{
22 {"und", SymDecimal, ".", '9'},
23 {"de", SymGroup, ".", '9'},
24 {"de-BE", SymGroup, ".", '9'},
25 {"de-BE-oxendict", SymGroup, ".", '9'},
26
27
28 {"de-BE-u-nu-deva", SymGroup, ".", '\u096f'},
29 {"de-Cyrl-BE", SymGroup, ",", '9'},
30 {"de-CH", SymGroup, "’", '9'},
31 {"de-CH-oxendict", SymGroup, "’", '9'},
32 {"de-CH-u-nu-deva", SymGroup, "’", '\u096f'},
33
34 {"bn-u-nu-beng", SymGroup, ",", '\u09ef'},
35 {"bn-u-nu-deva", SymGroup, ",", '\u096f'},
36 {"bn-u-nu-latn", SymGroup, ",", '9'},
37
38 {"pa", SymExponential, "E", '9'},
39
40
41
42
43
44 {"pa-u-nu-arabext", SymExponential, "\u00d7\u06f1\u06f0^", '\u06f9'},
45
46
47
48
49 {"dz", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
50 {"dz-u-nu-latn", SymInfinity, "∞", '9'},
51 {"dz-u-nu-tibt", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
52 {"en-u-nu-tibt", SymInfinity, "∞", '\u0f29'},
53
54
55 {"en-u-nu-hanidec", SymPlusSign, "+", '9'},
56 {"en-u-nu-roman", SymPlusSign, "+", '9'},
57 }
58 for _, tc := range testCases {
59 t.Run(fmt.Sprintf("%s:%v", tc.lang, tc.sym), func(t *testing.T) {
60 info := InfoFromTag(language.MustParse(tc.lang))
61 if got := info.Symbol(tc.sym); got != tc.wantSym {
62 t.Errorf("sym: got %q; want %q", got, tc.wantSym)
63 }
64 if got := info.Digit('9'); got != tc.wantNine {
65 t.Errorf("Digit(9): got %+q; want %+q", got, tc.wantNine)
66 }
67 var buf [4]byte
68 if got := string(buf[:info.WriteDigit(buf[:], '9')]); got != string(tc.wantNine) {
69 t.Errorf("WriteDigit(9): got %+q; want %+q", got, tc.wantNine)
70 }
71 if got := string(info.AppendDigit([]byte{}, 9)); got != string(tc.wantNine) {
72 t.Errorf("AppendDigit(9): got %+q; want %+q", got, tc.wantNine)
73 }
74 })
75 }
76 }
77
78 func TestFormats(t *testing.T) {
79 testCases := []struct {
80 lang string
81 pattern string
82 index []byte
83 }{
84 {"en", "#,##0.###", tagToDecimal},
85 {"de", "#,##0.###", tagToDecimal},
86 {"de-CH", "#,##0.###", tagToDecimal},
87 {"pa", "#,##,##0.###", tagToDecimal},
88 {"pa-Arab", "#,##0.###", tagToDecimal},
89 {"mr", "#,##,##0.###", tagToDecimal},
90 {"mr-IN", "#,##,##0.###", tagToDecimal},
91 {"nl", "#E0", tagToScientific},
92 {"nl-MX", "#E0", tagToScientific},
93 {"zgh", "#,##0 %", tagToPercent},
94 }
95 for _, tc := range testCases {
96 testtext.Run(t, tc.lang, func(t *testing.T) {
97 got := formatForLang(language.MustParse(tc.lang), tc.index)
98 want, _ := ParsePattern(tc.pattern)
99 if *got != *want {
100 t.Errorf("\ngot %#v;\nwant %#v", got, want)
101 }
102 })
103 }
104 }
105
View as plain text