github.com/tawesoft/golib/v2@v2.10.0/legacy/humanize/format_test.go (about)

     1  package humanize
     2  
     3  import (
     4      "testing"
     5  
     6      "golang.org/x/text/language"
     7  )
     8  
     9  func TestFormatParts(t *testing.T) {
    10  
    11      factors := Factors{
    12          Factors:    []Factor{
    13              { 0.10, Unit{"small",  "small(ascii)"},  FactorModeUnitPrefix},
    14              { 1.00, Unit{"", ""}, FactorModeIdentity},
    15              {10.00, Unit{"big",    "big(ascii)"},    FactorModeUnitPrefix},
    16          },
    17      }
    18  
    19      factors2 := Factors{
    20          Components: 2,
    21          Factors:    []Factor{
    22              { 0.10, Unit{"small",  "small(ascii)"},  FactorModeUnitPrefix},
    23              { 1.00, Unit{"", ""}, FactorModeIdentity},
    24              {10.00, Unit{"big",    "big(ascii)"},    FactorModeUnitPrefix},
    25          },
    26      }
    27  
    28      type test struct {
    29          factors Factors
    30          value float64
    31          unit Unit
    32          expectedParts []Part
    33      }
    34  
    35      tests := []test{
    36          {factors, 0.01, Unit{"unit", "unit(ascii)"}, []Part{{0.1, Unit{"smallunit", "small(ascii)unit(ascii)"}}}},
    37          {factors, 0.15, Unit{"unit", "unit(ascii)"}, []Part{{1.5, Unit{"smallunit", "small(ascii)unit(ascii)"}}}},
    38          {factors, 1.00, Unit{"unit", "unit(ascii)"}, []Part{{1.0, Unit{"unit", "unit(ascii)"}}}},
    39          {factors, 1.50, Unit{"unit", "unit(ascii)"}, []Part{{1.5, Unit{"unit", "unit(ascii)"}}}},
    40          {factors, 15.0, Unit{"unit", "unit(ascii)"}, []Part{{1.5, Unit{"bigunit", "big(ascii)unit(ascii)"}}}},
    41  
    42          {factors2, 15.0, Unit{"unit", "unit(ascii)"}, []Part{
    43              {1.0, Unit{"big", "big(ascii)"}},
    44              {5.0, Unit{"unit", "unit(ascii)"}},
    45          }},
    46  
    47          {CommonFactors.Time, 0, Unit{"s", "s"}, []Part{
    48              {0.0, Unit{"s", "s"}},
    49          }},
    50  
    51          {CommonFactors.Time, 60 * 2.5, Unit{"s", "s"}, []Part{
    52              { 2.0, Unit{"min", "min"}},
    53              {30.0, Unit{"s", "s"}},
    54          }},
    55  
    56          // skips zero units
    57          {CommonFactors.Time, 60 * 2.0, Unit{"s", "s"}, []Part{
    58              { 2.0, Unit{"min", "min"}},
    59          }},
    60          {CommonFactors.Time, 1 + (60 * 60 * 2.0), Unit{"s", "s"}, []Part{
    61              { 2.0, Unit{"h", "h"}},
    62              { 1.0, Unit{"s", "s"}},
    63          }},
    64      }
    65  
    66      const epsilon = 0.01
    67  
    68      for _, test := range tests {
    69          parts := FormatParts(test.value, test.unit, test.factors)
    70          if !partsEqual(parts, test.expectedParts, epsilon) {
    71              t.Errorf("FormatParts(%f, %q, factors): got %v but expected %v",
    72                  test.value, test.unit, parts, test.expectedParts)
    73          }
    74      }
    75  }
    76  
    77  func TestFormat(t *testing.T) {
    78      english := NewHumanizer(language.English)
    79      danish  := NewHumanizer(language.Danish)
    80  
    81      type test struct {
    82          humanizer Humanizer
    83          humanizerName string
    84          factors Factors
    85          value float64
    86          unit Unit
    87          expected string
    88      }
    89  
    90      tests := []test{
    91          {english, "english", CommonFactors.Distance, 1500 * 1000, Unit{"m", "m"}, "1,500 km"},
    92          {danish,  "danish", CommonFactors.Distance, 1500 * 1000, Unit{"m", "m"}, "1.500 km"},
    93      }
    94  
    95      for _, test := range tests {
    96          str := test.humanizer.Format(test.value, test.unit, test.factors)
    97          if str.Utf8 != test.expected {
    98              t.Errorf("humanizer<%s>.FormatParts(%f, %q, factors): got %v but expected %v",
    99                  test.humanizerName, test.value, test.unit, str, test.expected)
   100          }
   101      }
   102  }