From 42300a7d837a1701e364e1b6c7ce2a633e525e68 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 4 May 2023 23:43:34 +0800 Subject: [PATCH] chore: add more tests (#3203) --- core/logx/logtest/logtest.go | 9 ++------- core/logx/logtest/logtest_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/logx/logtest/logtest.go b/core/logx/logtest/logtest.go index 6e583840..27f817e6 100644 --- a/core/logx/logtest/logtest.go +++ b/core/logx/logtest/logtest.go @@ -46,7 +46,6 @@ func (b *Buffer) Bytes() []byte { func (b *Buffer) Content() string { var m map[string]interface{} if err := json.Unmarshal(b.buf.Bytes(), &m); err != nil { - b.t.Error(err) return "" } @@ -59,12 +58,8 @@ func (b *Buffer) Content() string { case string: return val default: - bs, err := json.Marshal(content) - if err != nil { - b.t.Error(err) - return "" - } - + // err is impossible to be not nil, unmarshaled from b.buf.Bytes() + bs, _ := json.Marshal(content) return string(bs) } } diff --git a/core/logx/logtest/logtest_test.go b/core/logx/logtest/logtest_test.go index da7c3f79..9b0d55ac 100644 --- a/core/logx/logtest/logtest_test.go +++ b/core/logx/logtest/logtest_test.go @@ -14,6 +14,8 @@ func TestCollector(t *testing.T) { logx.Info(input) assert.Equal(t, input, c.Content()) assert.Contains(t, c.String(), input) + c.Reset() + assert.Empty(t, c.Bytes()) } func TestPanicOnFatal(t *testing.T) { @@ -21,8 +23,22 @@ func TestPanicOnFatal(t *testing.T) { Discard(t) logx.Info(input) + PanicOnFatal(t) PanicOnFatal(t) assert.Panics(t, func() { logx.Must(errors.New("foo")) }) } + +func TestCollectorContent(t *testing.T) { + const input = "hello" + c := NewCollector(t) + c.buf.WriteString(input) + assert.Empty(t, c.Content()) + c.Reset() + c.buf.WriteString(`{}`) + assert.Empty(t, c.Content()) + c.Reset() + c.buf.WriteString(`{"content":1}`) + assert.Equal(t, "1", c.Content()) +}