OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / text / internal / number / number_test.go
1 // Copyright 2016 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
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'},          // inherits from de (no number data in CLDR)
25                 {"de-BE-oxendict", SymGroup, ".", '9'}, // inherits from de (no compact index)
26
27                 // U+096F DEVANAGARI DIGIT NINE ('९')
28                 {"de-BE-u-nu-deva", SymGroup, ".", '\u096f'}, // miss -> latn -> de
29                 {"de-Cyrl-BE", SymGroup, ",", '9'},           // inherits from root
30                 {"de-CH", SymGroup, "’", '9'},                // overrides values in de
31                 {"de-CH-oxendict", SymGroup, "’", '9'},       // inherits from de-CH (no compact index)
32                 {"de-CH-u-nu-deva", SymGroup, "’", '\u096f'}, // miss -> latn -> de-CH
33
34                 {"pa", SymExponential, "E", '9'},
35
36                 // "×۱۰^" -> U+00d7 U+06f1 U+06f0^"
37                 // U+06F0 EXTENDED ARABIC-INDIC DIGIT ZERO
38                 // U+06F1 EXTENDED ARABIC-INDIC DIGIT ONE
39                 // U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE
40                 {"pa-u-nu-arabext", SymExponential, "\u00d7\u06f1\u06f0^", '\u06f9'},
41
42                 //  "གྲངས་མེད" - > U+0f42 U+0fb2 U+0f44 U+0f66 U+0f0b U+0f58 U+0f7a U+0f51
43                 // Examples:
44                 // U+0F29 TIBETAN DIGIT NINE (༩)
45                 {"dz", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'}, // defaults to tibt
46                 {"dz-u-nu-latn", SymInfinity, "∞", '9'},                                           // select alternative
47                 {"dz-u-nu-tibt", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
48                 {"en-u-nu-tibt", SymInfinity, "∞", '\u0f29'},
49
50                 // algorithmic number systems fall back to ASCII if Digits is used.
51                 {"en-u-nu-hanidec", SymPlusSign, "+", '9'},
52                 {"en-u-nu-roman", SymPlusSign, "+", '9'},
53         }
54         for _, tc := range testCases {
55                 t.Run(fmt.Sprintf("%s:%v", tc.lang, tc.sym), func(t *testing.T) {
56                         info := InfoFromTag(language.MustParse(tc.lang))
57                         if got := info.Symbol(tc.sym); got != tc.wantSym {
58                                 t.Errorf("sym: got %q; want %q", got, tc.wantSym)
59                         }
60                         if got := info.Digit('9'); got != tc.wantNine {
61                                 t.Errorf("Digit(9): got %+q; want %+q", got, tc.wantNine)
62                         }
63                         var buf [4]byte
64                         if got := string(buf[:info.WriteDigit(buf[:], '9')]); got != string(tc.wantNine) {
65                                 t.Errorf("WriteDigit(9): got %+q; want %+q", got, tc.wantNine)
66                         }
67                         if got := string(info.AppendDigit([]byte{}, 9)); got != string(tc.wantNine) {
68                                 t.Errorf("AppendDigit(9): got %+q; want %+q", got, tc.wantNine)
69                         }
70                 })
71         }
72 }
73
74 func TestFormats(t *testing.T) {
75         testCases := []struct {
76                 lang    string
77                 pattern string
78                 index   []byte
79         }{
80                 {"en", "#,##0.###", tagToDecimal},
81                 {"de", "#,##0.###", tagToDecimal},
82                 {"de-CH", "#,##0.###", tagToDecimal},
83                 {"pa", "#,##,##0.###", tagToDecimal},
84                 {"pa-Arab", "#,##0.###", tagToDecimal}, // Does NOT inherit from pa!
85                 {"mr", "#,##,##0.###", tagToDecimal},
86                 {"mr-IN", "#,##,##0.###", tagToDecimal}, // Inherits from mr.
87                 {"nl", "#E0", tagToScientific},
88                 {"nl-MX", "#E0", tagToScientific}, // Inherits through Tag.Parent.
89                 {"zgh", "#,##0 %", tagToPercent},
90         }
91         for _, tc := range testCases {
92                 testtext.Run(t, tc.lang, func(t *testing.T) {
93                         got := formatForLang(language.MustParse(tc.lang), tc.index)
94                         want, _ := ParsePattern(tc.pattern)
95                         if *got != *want {
96                                 t.Errorf("\ngot  %#v;\nwant %#v", got, want)
97                         }
98                 })
99         }
100 }