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