chore: add more tests (#3203)

This commit is contained in:
Kevin Wan 2023-05-04 23:43:34 +08:00 committed by GitHub
parent fe97fab274
commit 42300a7d83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -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)
}
}

View File

@ -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())
}