github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/man/number_test.go (about)

     1  package man
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  type TestStruct struct {
     9  	name      string
    10  	format    string
    11  	num       float64
    12  	formatted string
    13  }
    14  
    15  func TestFormatFloat(t *testing.T) {
    16  	tests := []TestStruct{
    17  		{"default", "", 12345.6789, "12,345.68"},
    18  		{"#", "#", 12345.6789, "12345.678900000"},
    19  		{"#.", "#.", 12345.6789, "12346"},
    20  		{"#,#", "#,#", 12345.6789, "12345,7"},
    21  		{"#,##", "#,##", 12345.6789, "12345,68"},
    22  		{"#,###", "#,###", 12345.6789, "12345,679"},
    23  		{"#,###.", "#,###.", 12345.6789, "12,346"},
    24  		{"#,###.##", "#,###.##", 12345.6789, "12,345.68"},
    25  		{"#,###.###", "#,###.###", 12345.6789, "12,345.679"},
    26  		{"#,###.####", "#,###.####", 12345.6789, "12,345.6789"},
    27  		{"#.###,######", "#.###,######", 12345.6789, "12.345,678900"},
    28  		{"bug46", "#,###.##", 52746220055.92342, "52,746,220,055.92"},
    29  		{"#\u202f###,##", "#\u202f###,##", 12345.6789, "12 345,68"},
    30  
    31  		// special cases
    32  		{"NaN", "#", math.NaN(), "NaN"},
    33  		{"+Inf", "#", math.Inf(1), "Infinity"},
    34  		{"-Inf", "#", math.Inf(-1), "-Infinity"},
    35  		{"signStr <= -0.000000001", "", -0.000000002, "-0.00"},
    36  		{"signStr = 0", "", 0, "0.00"},
    37  		{"Format directive must start with +", "+000", 12345.6789, "+12345.678900000"},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		got := FormatFloat(test.format, test.num)
    42  		if got != test.formatted {
    43  			t.Errorf("On %v (%v, %v), got %v, wanted %v",
    44  				test.name, test.format, test.num, got, test.formatted)
    45  		}
    46  	}
    47  	// Test a single integer
    48  	got := FormatInteger("#", 12345)
    49  	if got != "12345.000000000" {
    50  		t.Errorf("On %v (%v, %v), got %v, wanted %v",
    51  			"integerTest", "#", 12345, got, "12345.000000000")
    52  	}
    53  	// Test the things that could panic
    54  	panictests := []TestStruct{
    55  		{"RenderFloat(): invalid positive sign directive", "-", 12345.6789, "12,345.68"},
    56  		{"RenderFloat(): thousands separator directive must be followed by 3 digit-specifiers", "0.01", 12345.6789, "12,345.68"},
    57  	}
    58  	for _, test := range panictests {
    59  		didPanic := false
    60  		var message interface{}
    61  		func() {
    62  			defer func() {
    63  				if message = recover(); message != nil {
    64  					didPanic = true
    65  				}
    66  			}()
    67  
    68  			// call the target function
    69  			_ = FormatFloat(test.format, test.num)
    70  		}()
    71  		if didPanic != true {
    72  			t.Errorf("On %v, should have panic and did not.",
    73  				test.name)
    74  		}
    75  	}
    76  }