github.com/tawesoft/golib/v2@v2.10.0/legacy/localize/numberformat_test.go (about)

     1  package localize
     2  
     3  import (
     4      "reflect"
     5      "testing"
     6  
     7      "golang.org/x/text/language"
     8  )
     9  
    10  // TestDecimalNumberFormatLocale tests that the decimalFormat correctly detects the
    11  // correct group and decimal separators and digits for a given locale.
    12  //
    13  // It is possible that these tests may become stale and fail with newer
    14  // versions of Unicode or newer versions of golang.org/x/text. In that case,
    15  // the test is incorrect, not the code.
    16  //
    17  // It's also possible that these tests are incorrect due to my own
    18  // misunderstanding of certain locales. Corrections are welcome!
    19  func TestDecimalNumberFormatLocale(t *testing.T) {
    20      type test struct {
    21          lang language.Tag
    22          expectedGroupSeparator rune
    23          expectedPoint rune
    24          expectedDigits []rune
    25      }
    26  
    27      digitsLatin   := []rune("0123456789")
    28      digitsArabic  := []rune("٠١٢٣٤٥٦٧٨٩")
    29      digitsBengali := []rune("০১২৩৪৫৬৭৮৯")
    30  
    31      var tests = []test{
    32          {language.BritishEnglish,   ',',        '.',      digitsLatin},
    33          {language.French,           '\u00a0',   ',',      digitsLatin},
    34          {language.Dutch,            '.',        ',',      digitsLatin},
    35          {language.Arabic,           '\u066c',   '\u066b', digitsArabic},
    36          {language.Malayalam,        ',',        '.',      digitsLatin},
    37          {language.Bengali,          ',',        '.',      digitsBengali},
    38      }
    39  
    40      for _, test := range tests {
    41          f := NewDecimalFormat(test.lang).(decimalFormat)
    42  
    43          if f.GroupSeparator != test.expectedGroupSeparator {
    44              t.Errorf("expected group separator %c (0x%x) for %s, but got %c (0x%x)",
    45                  test.expectedGroupSeparator, test.expectedGroupSeparator,
    46                  test.lang,
    47                  f.GroupSeparator, f.GroupSeparator)
    48          }
    49  
    50          if f.Point != test.expectedPoint {
    51              t.Errorf("expected point %c (0x%x) for %s, but got %c (0x%x)",
    52                  test.expectedPoint, test.expectedPoint,
    53                  test.lang,
    54                  f.Point, f.Point)
    55          }
    56  
    57          if !reflect.DeepEqual(f.Digits[:], test.expectedDigits) {
    58              t.Errorf("expected digits %s (%+v) for %s, but got %s (%+v)",
    59                  string(test.expectedDigits), test.expectedDigits,
    60                  test.lang,
    61                  string(f.Digits[:]), f.Digits)
    62          }
    63      }
    64  }
    65  
    66  // TestDecimalNumberFormatParse tests parsing of decimal numbers in different
    67  // locales.
    68  func TestDecimalNumberFormatParse(t *testing.T) {
    69      type test struct {
    70          lang language.Tag
    71          in string
    72  
    73          expectedIntValue int64
    74          expectedIntLen   int
    75  
    76          expectedFloatValue float64
    77          expectedFloatLen int
    78      }
    79  
    80      var tests = []test{
    81          {language.BritishEnglish, "1,234.56", 1234, 5, 1234.56, 8},
    82          {language.French,         "1 234,56", 1234, 5, 1234.56, 8},
    83          {language.Arabic,         "١\u066c٢٣٤\u066b٥٦", 1234, 10, 1234.56, 16},
    84      }
    85  
    86      for _, test := range tests {
    87          f := NewDecimalFormat(test.lang)
    88  
    89          {
    90              value, length, err := f.AcceptInt(test.in)
    91              if err != nil {
    92                  t.Errorf("unexpected error for %s %v: %v", test.lang, test.in, err)
    93                  continue
    94              }
    95  
    96              if length != test.expectedIntLen {
    97                  t.Errorf("expected int len %d for %s %v but got %d",
    98                  test.expectedIntLen, test.lang, test.in, length)
    99              }
   100  
   101              if value != test.expectedIntValue {
   102                  t.Errorf("expected int value %d for %s %v but got %d",
   103                  test.expectedIntValue, test.lang, test.in, value)
   104              }
   105          }
   106  
   107          {
   108              value, length, err := f.AcceptFloat(test.in)
   109              if err != nil {
   110                  t.Errorf("unexpected error for %s %v: %v", test.lang, test.in, err)
   111                  continue
   112              }
   113  
   114              if length != test.expectedFloatLen {
   115                  t.Errorf("expected float len %d for %s %v but got %d",
   116                  test.expectedFloatLen, test.lang, test.in, length)
   117              }
   118  
   119              if value != test.expectedFloatValue {
   120                  t.Errorf("expected float value %f for %s %v but got %f",
   121                  test.expectedFloatValue, test.lang, test.in, value)
   122              }
   123          }
   124      }
   125  }