mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
add testcase
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
8904ae9d26
commit
1fd78c99e2
1 changed files with 61 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"math/rand"
|
||||
"testing"
|
||||
)
|
||||
|
|
@ -22,3 +23,63 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) {
|
|||
sink = appendUint64(buf, rand.Uint64(), false)
|
||||
}
|
||||
}
|
||||
|
||||
// testStringer implements both TerminalStringer and fmt.Stringer
|
||||
type testStringer struct{}
|
||||
|
||||
func (t testStringer) String() string {
|
||||
return "full string representation"
|
||||
}
|
||||
|
||||
func (t testStringer) TerminalString() string {
|
||||
return "terminal string representation"
|
||||
}
|
||||
|
||||
func TestFormatSlogValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
level slog.Level
|
||||
value slog.Value
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "DEBUG level uses String()",
|
||||
level: slog.LevelDebug,
|
||||
value: slog.AnyValue(testStringer{}),
|
||||
expected: `"full string representation"`,
|
||||
},
|
||||
{
|
||||
name: "TRACE level uses String()",
|
||||
level: LevelTrace,
|
||||
value: slog.AnyValue(testStringer{}),
|
||||
expected: `"full string representation"`,
|
||||
},
|
||||
{
|
||||
name: "INFO level uses TerminalString()",
|
||||
level: slog.LevelInfo,
|
||||
value: slog.AnyValue(testStringer{}),
|
||||
expected: `"terminal string representation"`,
|
||||
},
|
||||
{
|
||||
name: "WARN level uses TerminalString()",
|
||||
level: slog.LevelWarn,
|
||||
value: slog.AnyValue(testStringer{}),
|
||||
expected: `"terminal string representation"`,
|
||||
},
|
||||
{
|
||||
name: "ERROR level uses TerminalString()",
|
||||
level: slog.LevelError,
|
||||
value: slog.AnyValue(testStringer{}),
|
||||
expected: `"terminal string representation"`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FormatSlogValue(tt.level, tt.value, nil)
|
||||
if string(result) != tt.expected {
|
||||
t.Errorf("FormatSlogValue() = '%v', want '%v'", string(result), tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue