...

Source file src/golang.org/x/text/internal/number/number_test.go

Documentation: golang.org/x/text/internal/number

     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  		{"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  		// "×۱۰^" -> U+00d7 U+06f1 U+06f0^"
    41  		// U+06F0 EXTENDED ARABIC-INDIC DIGIT ZERO
    42  		// U+06F1 EXTENDED ARABIC-INDIC DIGIT ONE
    43  		// U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE
    44  		{"pa-u-nu-arabext", SymExponential, "\u00d7\u06f1\u06f0^", '\u06f9'},
    45  
    46  		//  "གྲངས་མེད" - > U+0f42 U+0fb2 U+0f44 U+0f66 U+0f0b U+0f58 U+0f7a U+0f51
    47  		// Examples:
    48  		// U+0F29 TIBETAN DIGIT NINE (༩)
    49  		{"dz", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'}, // defaults to tibt
    50  		{"dz-u-nu-latn", SymInfinity, "∞", '9'},                                           // select alternative
    51  		{"dz-u-nu-tibt", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
    52  		{"en-u-nu-tibt", SymInfinity, "∞", '\u0f29'},
    53  
    54  		// algorithmic number systems fall back to ASCII if Digits is used.
    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}, // Does NOT inherit from pa!
    89  		{"mr", "#,##,##0.###", tagToDecimal},
    90  		{"mr-IN", "#,##,##0.###", tagToDecimal}, // Inherits from mr.
    91  		{"nl", "#E0", tagToScientific},
    92  		{"nl-MX", "#E0", tagToScientific}, // Inherits through Tag.Parent.
    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