2023-09-07 13:19:39 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormatLogMessage(t *testing.T) {
|
2024-07-09 22:14:15 +00:00
|
|
|
f := func(format string, args []any, maxArgLen int, expectedResult string) {
|
2023-09-07 13:19:39 +00:00
|
|
|
t.Helper()
|
|
|
|
result := formatLogMessage(maxArgLen, format, args)
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected result; got\n%q\nwant\n%q", result, expectedResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zero format args
|
|
|
|
f("foobar", nil, 1, "foobar")
|
|
|
|
|
|
|
|
// Format args not exceeding the maxArgLen
|
2024-07-09 22:14:15 +00:00
|
|
|
f("foo: %d, %s, %s, %s", []any{123, "bar", []byte("baz"), fmt.Errorf("abc")}, 3, "foo: 123, bar, baz, abc")
|
2023-09-07 13:19:39 +00:00
|
|
|
|
|
|
|
// Format args exceeding the maxArgLen
|
2024-07-09 22:14:15 +00:00
|
|
|
f("foo: %s, %q, %s", []any{"abcde", fmt.Errorf("foo bar baz"), "xx"}, 4, `foo: a..e, "f..z", xx`)
|
2023-09-07 13:19:39 +00:00
|
|
|
}
|