...

Source file src/golang.org/x/text/internal/number/tables_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  	"flag"
     9  	"log"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"golang.org/x/text/internal/gen"
    14  	"golang.org/x/text/internal/language"
    15  	"golang.org/x/text/internal/language/compact"
    16  	"golang.org/x/text/internal/testtext"
    17  	"golang.org/x/text/unicode/cldr"
    18  )
    19  
    20  var draft = flag.String("draft",
    21  	"contributed",
    22  	`Minimal draft requirements (approved, contributed, provisional, unconfirmed).`)
    23  
    24  func TestNumberSystems(t *testing.T) {
    25  	testtext.SkipIfNotLong(t)
    26  
    27  	r := gen.OpenCLDRCoreZip()
    28  	defer r.Close()
    29  
    30  	d := &cldr.Decoder{}
    31  	d.SetDirFilter("supplemental")
    32  	d.SetSectionFilter("numberingSystem")
    33  	data, err := d.DecodeZip(r)
    34  	if err != nil {
    35  		t.Fatalf("DecodeZip: %v", err)
    36  	}
    37  
    38  	for _, ns := range data.Supplemental().NumberingSystems.NumberingSystem {
    39  		n := systemMap[ns.Id]
    40  		if int(n) >= len(numSysData) {
    41  			continue
    42  		}
    43  		info := InfoFromLangID(0, ns.Id)
    44  		val := '0'
    45  		for _, rWant := range ns.Digits {
    46  			if rGot := info.Digit(val); rGot != rWant {
    47  				t.Errorf("%s:%d: got %U; want %U", ns.Id, val, rGot, rWant)
    48  			}
    49  			val++
    50  		}
    51  	}
    52  }
    53  
    54  func TestSymbols(t *testing.T) {
    55  	testtext.SkipIfNotLong(t)
    56  
    57  	draft, err := cldr.ParseDraft(*draft)
    58  	if err != nil {
    59  		log.Fatalf("invalid draft level: %v", err)
    60  	}
    61  
    62  	r := gen.OpenCLDRCoreZip()
    63  	defer r.Close()
    64  
    65  	d := &cldr.Decoder{}
    66  	d.SetDirFilter("main")
    67  	d.SetSectionFilter("numbers")
    68  	data, err := d.DecodeZip(r)
    69  	if err != nil {
    70  		t.Fatalf("DecodeZip: %v", err)
    71  	}
    72  
    73  	for _, lang := range data.Locales() {
    74  		ldml := data.RawLDML(lang)
    75  		if ldml.Numbers == nil {
    76  			continue
    77  		}
    78  		langIndex, ok := compact.FromTag(language.MustParse(lang))
    79  		if !ok {
    80  			t.Fatalf("No compact index for language %s", lang)
    81  		}
    82  
    83  		syms := cldr.MakeSlice(&ldml.Numbers.Symbols)
    84  		syms.SelectDraft(draft)
    85  
    86  		for _, sym := range ldml.Numbers.Symbols {
    87  			if sym.NumberSystem == "" {
    88  				continue
    89  			}
    90  			testCases := []struct {
    91  				name string
    92  				st   SymbolType
    93  				x    interface{}
    94  			}{
    95  				{"Decimal", SymDecimal, sym.Decimal},
    96  				{"Group", SymGroup, sym.Group},
    97  				{"List", SymList, sym.List},
    98  				{"PercentSign", SymPercentSign, sym.PercentSign},
    99  				{"PlusSign", SymPlusSign, sym.PlusSign},
   100  				{"MinusSign", SymMinusSign, sym.MinusSign},
   101  				{"Exponential", SymExponential, sym.Exponential},
   102  				{"SuperscriptingExponent", SymSuperscriptingExponent, sym.SuperscriptingExponent},
   103  				{"PerMille", SymPerMille, sym.PerMille},
   104  				{"Infinity", SymInfinity, sym.Infinity},
   105  				{"NaN", SymNan, sym.Nan},
   106  				{"TimeSeparator", SymTimeSeparator, sym.TimeSeparator},
   107  			}
   108  			info := InfoFromLangID(langIndex, sym.NumberSystem)
   109  			for _, tc := range testCases {
   110  				// Extract the wanted value.
   111  				v := reflect.ValueOf(tc.x)
   112  				if v.Len() == 0 {
   113  					return
   114  				}
   115  				if v.Len() > 1 {
   116  					t.Fatalf("Multiple values of %q within single symbol not supported.", tc.name)
   117  				}
   118  				want := v.Index(0).MethodByName("Data").Call(nil)[0].String()
   119  				got := info.Symbol(tc.st)
   120  				if got != want {
   121  					t.Errorf("%s:%s:%s: got %q; want %q", lang, sym.NumberSystem, tc.name, got, want)
   122  				}
   123  			}
   124  		}
   125  	}
   126  }
   127  

View as plain text