github.com/go-xe2/third@v1.0.3/golang.org/x/text/internal/number/tables_test.go (about)

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