浏览代码

chore: add more tests (#3203)

Kevin Wan 2 年之前
父节点
当前提交
42300a7d83
共有 2 个文件被更改,包括 18 次插入7 次删除
  1. 2 7
      core/logx/logtest/logtest.go
  2. 16 0
      core/logx/logtest/logtest_test.go

+ 2 - 7
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)
 	}
 }

+ 16 - 0
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())
+}