Skip to content

Commit a0f2e81

Browse files
committed
log: test for logging-output (ethereum#28373)
1 parent 6f2a028 commit a0f2e81

File tree

2 files changed

+8
-139
lines changed

2 files changed

+8
-139
lines changed

log/format.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const (
2424
termCtxMaxPadding = 40
2525
)
2626

27+
// ResetGlobalState resets the fieldPadding, which is useful for producing
28+
// predictable output.
29+
func ResetGlobalState() {
30+
fieldPaddingLock.Lock()
31+
fieldPadding = make(map[string]int)
32+
fieldPaddingLock.Unlock()
33+
}
34+
2735
// locationTrims are trimmed for display to avoid unwieldy log lines.
2836
var locationTrims = []string{
2937
"github.com/XinFinOrg/XDPoSChain/",

log/format_test.go

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,10 @@
11
package log
22

33
import (
4-
"fmt"
5-
"math"
6-
"math/big"
74
"math/rand"
8-
"strings"
95
"testing"
10-
11-
"github.com/holiman/uint256"
126
)
137

14-
func TestPrettyInt64(t *testing.T) {
15-
tests := []struct {
16-
n int64
17-
s string
18-
}{
19-
{0, "0"},
20-
{10, "10"},
21-
{-10, "-10"},
22-
{100, "100"},
23-
{-100, "-100"},
24-
{1000, "1000"},
25-
{-1000, "-1000"},
26-
{10000, "10000"},
27-
{-10000, "-10000"},
28-
{99999, "99999"},
29-
{-99999, "-99999"},
30-
{100000, "100,000"},
31-
{-100000, "-100,000"},
32-
{1000000, "1,000,000"},
33-
{-1000000, "-1,000,000"},
34-
{math.MaxInt64, "9,223,372,036,854,775,807"},
35-
{math.MinInt64, "-9,223,372,036,854,775,808"},
36-
}
37-
for i, tt := range tests {
38-
if have := FormatLogfmtInt64(tt.n); have != tt.s {
39-
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
40-
}
41-
}
42-
}
43-
44-
func TestPrettyUint64(t *testing.T) {
45-
tests := []struct {
46-
n uint64
47-
s string
48-
}{
49-
{0, "0"},
50-
{10, "10"},
51-
{100, "100"},
52-
{1000, "1000"},
53-
{10000, "10000"},
54-
{99999, "99999"},
55-
{100000, "100,000"},
56-
{1000000, "1,000,000"},
57-
{math.MaxUint64, "18,446,744,073,709,551,615"},
58-
}
59-
for i, tt := range tests {
60-
if have := FormatLogfmtUint64(tt.n); have != tt.s {
61-
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
62-
}
63-
}
64-
}
65-
66-
func TestPrettyBigInt(t *testing.T) {
67-
tests := []struct {
68-
int string
69-
s string
70-
}{
71-
{"111222333444555678999", "111,222,333,444,555,678,999"},
72-
{"-111222333444555678999", "-111,222,333,444,555,678,999"},
73-
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
74-
{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},
75-
}
76-
77-
for _, tt := range tests {
78-
v, _ := new(big.Int).SetString(tt.int, 10)
79-
if have := formatLogfmtBigInt(v); have != tt.s {
80-
t.Errorf("invalid output %s, want %s", have, tt.s)
81-
}
82-
}
83-
}
84-
85-
func TestPrettyUint256(t *testing.T) {
86-
tests := []struct {
87-
int string
88-
s string
89-
}{
90-
{"111222333444555678999", "111,222,333,444,555,678,999"},
91-
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
92-
}
93-
94-
for _, tt := range tests {
95-
v := new(uint256.Int)
96-
v.SetFromDecimal(tt.int)
97-
if have := formatLogfmtUint256(v); have != tt.s {
98-
t.Errorf("invalid output %s, want %s", have, tt.s)
99-
}
100-
}
101-
}
102-
1038
var sink string
1049

10510
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
@@ -115,47 +20,3 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) {
11520
sink = FormatLogfmtUint64(rand.Uint64())
11621
}
11722
}
118-
119-
func TestSanitation(t *testing.T) {
120-
msg := "\u001b[1G\u001b[K\u001b[1A"
121-
msg2 := "\u001b \u0000"
122-
msg3 := "NiceMessage"
123-
msg4 := "Space Message"
124-
msg5 := "Enter\nMessage"
125-
126-
for i, tt := range []struct {
127-
msg string
128-
want string
129-
}{
130-
{
131-
msg: msg,
132-
want: fmt.Sprintf("] %q %q=%q\n", msg, msg, msg),
133-
},
134-
{
135-
msg: msg2,
136-
want: fmt.Sprintf("] %q %q=%q\n", msg2, msg2, msg2),
137-
},
138-
{
139-
msg: msg3,
140-
want: fmt.Sprintf("] %s %s=%s\n", msg3, msg3, msg3),
141-
},
142-
{
143-
msg: msg4,
144-
want: fmt.Sprintf("] %s %q=%q\n", msg4, msg4, msg4),
145-
},
146-
{
147-
msg: msg5,
148-
want: fmt.Sprintf("] %s %q=%q\n", msg5, msg5, msg5),
149-
},
150-
} {
151-
var (
152-
logger = New()
153-
out = new(strings.Builder)
154-
)
155-
logger.SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(out, TerminalFormat(false))))
156-
logger.Info(tt.msg, tt.msg, tt.msg)
157-
if have := out.String()[24:]; tt.want != have {
158-
t.Fatalf("test %d: want / have: \n%v\n%v", i, tt.want, have)
159-
}
160-
}
161-
}

0 commit comments

Comments
 (0)