mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: add more tests (#3203)
This commit is contained in:
parent
fe97fab274
commit
42300a7d83
@ -46,7 +46,6 @@ func (b *Buffer) Bytes() []byte {
|
|||||||
func (b *Buffer) Content() string {
|
func (b *Buffer) Content() string {
|
||||||
var m map[string]interface{}
|
var m map[string]interface{}
|
||||||
if err := json.Unmarshal(b.buf.Bytes(), &m); err != nil {
|
if err := json.Unmarshal(b.buf.Bytes(), &m); err != nil {
|
||||||
b.t.Error(err)
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,12 +58,8 @@ func (b *Buffer) Content() string {
|
|||||||
case string:
|
case string:
|
||||||
return val
|
return val
|
||||||
default:
|
default:
|
||||||
bs, err := json.Marshal(content)
|
// err is impossible to be not nil, unmarshaled from b.buf.Bytes()
|
||||||
if err != nil {
|
bs, _ := json.Marshal(content)
|
||||||
b.t.Error(err)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(bs)
|
return string(bs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,8 @@ func TestCollector(t *testing.T) {
|
|||||||
logx.Info(input)
|
logx.Info(input)
|
||||||
assert.Equal(t, input, c.Content())
|
assert.Equal(t, input, c.Content())
|
||||||
assert.Contains(t, c.String(), input)
|
assert.Contains(t, c.String(), input)
|
||||||
|
c.Reset()
|
||||||
|
assert.Empty(t, c.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPanicOnFatal(t *testing.T) {
|
func TestPanicOnFatal(t *testing.T) {
|
||||||
@ -21,8 +23,22 @@ func TestPanicOnFatal(t *testing.T) {
|
|||||||
Discard(t)
|
Discard(t)
|
||||||
logx.Info(input)
|
logx.Info(input)
|
||||||
|
|
||||||
|
PanicOnFatal(t)
|
||||||
PanicOnFatal(t)
|
PanicOnFatal(t)
|
||||||
assert.Panics(t, func() {
|
assert.Panics(t, func() {
|
||||||
logx.Must(errors.New("foo"))
|
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())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user