github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/log/format_test.go (about) 1 package log 2 3 import ( 4 "fmt" 5 "math" 6 "math/big" 7 "math/rand" 8 "strings" 9 "testing" 10 ) 11 12 func TestPrettyInt64(t *testing.T) { 13 tests := []struct { 14 n int64 15 s string 16 }{ 17 {0, "0"}, 18 {10, "10"}, 19 {-10, "-10"}, 20 {100, "100"}, 21 {-100, "-100"}, 22 {1000, "1000"}, 23 {-1000, "-1000"}, 24 {10000, "10000"}, 25 {-10000, "-10000"}, 26 {99999, "99999"}, 27 {-99999, "-99999"}, 28 {100000, "100,000"}, 29 {-100000, "-100,000"}, 30 {1000000, "1,000,000"}, 31 {-1000000, "-1,000,000"}, 32 {math.MaxInt64, "9,223,372,036,854,775,807"}, 33 {math.MinInt64, "-9,223,372,036,854,775,808"}, 34 } 35 for i, tt := range tests { 36 if have := FormatLogfmtInt64(tt.n); have != tt.s { 37 t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s) 38 } 39 } 40 } 41 42 func TestPrettyUint64(t *testing.T) { 43 tests := []struct { 44 n uint64 45 s string 46 }{ 47 {0, "0"}, 48 {10, "10"}, 49 {100, "100"}, 50 {1000, "1000"}, 51 {10000, "10000"}, 52 {99999, "99999"}, 53 {100000, "100,000"}, 54 {1000000, "1,000,000"}, 55 {math.MaxUint64, "18,446,744,073,709,551,615"}, 56 } 57 for i, tt := range tests { 58 if have := FormatLogfmtUint64(tt.n); have != tt.s { 59 t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s) 60 } 61 } 62 } 63 64 func TestPrettyBigInt(t *testing.T) { 65 tests := []struct { 66 int string 67 s string 68 }{ 69 {"111222333444555678999", "111,222,333,444,555,678,999"}, 70 {"-111222333444555678999", "-111,222,333,444,555,678,999"}, 71 {"11122233344455567899900", "11,122,233,344,455,567,899,900"}, 72 {"-11122233344455567899900", "-11,122,233,344,455,567,899,900"}, 73 } 74 75 for _, tt := range tests { 76 v, _ := new(big.Int).SetString(tt.int, 10) 77 if have := formatLogfmtBigInt(v); have != tt.s { 78 t.Errorf("invalid output %s, want %s", have, tt.s) 79 } 80 } 81 } 82 83 var sink string 84 85 func BenchmarkPrettyInt64Logfmt(b *testing.B) { 86 b.ReportAllocs() 87 for i := 0; i < b.N; i++ { 88 sink = FormatLogfmtInt64(rand.Int63()) 89 } 90 } 91 92 func BenchmarkPrettyUint64Logfmt(b *testing.B) { 93 b.ReportAllocs() 94 for i := 0; i < b.N; i++ { 95 sink = FormatLogfmtUint64(rand.Uint64()) 96 } 97 } 98 99 func TestSanitation(t *testing.T) { 100 msg := "\u001b[1G\u001b[K\u001b[1A" 101 msg2 := "\u001b \u0000" 102 msg3 := "NiceMessage" 103 msg4 := "Space Message" 104 msg5 := "Enter\nMessage" 105 106 for i, tt := range []struct { 107 msg string 108 want string 109 }{ 110 { 111 msg: msg, 112 want: fmt.Sprintf("] %q %q=%q\n", msg, msg, msg), 113 }, 114 { 115 msg: msg2, 116 want: fmt.Sprintf("] %q %q=%q\n", msg2, msg2, msg2), 117 }, 118 { 119 msg: msg3, 120 want: fmt.Sprintf("] %s %s=%s\n", msg3, msg3, msg3), 121 }, 122 { 123 msg: msg4, 124 want: fmt.Sprintf("] %s %q=%q\n", msg4, msg4, msg4), 125 }, 126 { 127 msg: msg5, 128 want: fmt.Sprintf("] %s %q=%q\n", msg5, msg5, msg5), 129 }, 130 } { 131 var ( 132 logger = New() 133 out = new(strings.Builder) 134 ) 135 logger.SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(out, TerminalFormat(false)))) 136 logger.Info(tt.msg, tt.msg, tt.msg) 137 if have := out.String()[24:]; tt.want != have { 138 t.Fatalf("test %d: want / have: \n%v\n%v", i, tt.want, have) 139 } 140 } 141 }