[feat] package nazalog: Assert函数可选择填入描述信息

pull/3/head
q191201771 4 years ago
parent c1e02c732a
commit fdde3bd4b4

@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"testing"
"os"
"github.com/q191201771/naza/pkg/assert"
)
@ -22,8 +23,10 @@ type MockTestingT struct {
}
func (mtt MockTestingT) Errorf(format string, args ...interface{}) {
_ = fmt.Errorf(format, args...)
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintf(os.Stderr, "\n")
}
func TestEqual(t *testing.T) {
// 测试Equal
assert.Equal(t, nil, nil)
@ -49,6 +52,8 @@ func TestEqual(t *testing.T) {
assert.Equal(mtt, nil, 1)
assert.Equal(mtt, []byte{}, "aaa")
assert.Equal(mtt, nil, errors.New("mock error"))
// 测试msg参数
assert.Equal(mtt, nil, 1, "aaa", "bbb")
}
func TestIsNotNil(t *testing.T) {

@ -102,18 +102,23 @@ func Panicln(v ...interface{}) {
panic(fmt.Sprint(v...))
}
func Assert(expected interface{}, actual interface{}) {
func Assert(expected interface{}, actual interface{}, extInfo ...string) {
if !nazareflect.Equal(expected, actual) {
err := fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v", expected, actual)
var v string
if len(extInfo) == 0 {
v = fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v", expected, actual)
} else {
v = fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v, extInfo=%s", expected, actual, extInfo)
}
switch global.GetOption().AssertBehavior {
case AssertError:
global.Out(LevelError, 2, err)
global.Out(LevelError, 2, v)
case AssertFatal:
global.Out(LevelFatal, 2, err)
global.Out(LevelFatal, 2, v)
fake.Os_Exit(1)
case AssertPanic:
global.Out(LevelPanic, 2, err)
panic(err)
global.Out(LevelPanic, 2, v)
panic(v)
}
}
}

@ -25,6 +25,8 @@ import "errors"
// * 日志文件目录不存在则自动创建
//
// 目前性能和标准库log相当
//
// TODO(chef): 异步日志
var ErrLog = errors.New("naza.log:fxxk")
@ -47,9 +49,14 @@ type Logger interface {
Out(level Level, calldepth int, s string)
// 断言失败后的行为由配置项Option.AssertBehavior决定
// Assert 断言失败后的行为由配置项Option.AssertBehavior决定
// 注意expected和actual的类型必须相同比如int(1)和int32(1)是不相等的
Assert(expected interface{}, actual interface{})
//
// @param expected 期望值
// @param actual 实际值
// @param extInfo 期望值和实际值不相等时打印的补充信息,如果没有,可以不填
//
Assert(expected interface{}, actual interface{}, extInfo ...string)
// flush to disk, typically
Sync()
@ -75,7 +82,7 @@ type Option struct {
// 文件输出和控制台输出可同时打开
// 控制台输出主要用做开发时调试打开后level字段使用彩色输出
Filename string `json:"filename"` // 输出日志文件名,如果为空,则不写日志文件。可包含路径,路径不存在时,将自动创建
IsToStdout bool `json:"is_to_stdout"` // 是否以stdout输出到控制台
IsToStdout bool `json:"is_to_stdout"` // 是否以stdout输出到控制台 TODO(chef): 再增加一个stderr的配置
IsRotateDaily bool `json:"is_rotate_daily"` // 日志按天翻转

@ -165,18 +165,23 @@ func (l *logger) Panicln(v ...interface{}) {
panic(fmt.Sprint(v...))
}
func (l *logger) Assert(expected interface{}, actual interface{}) {
func (l *logger) Assert(expected interface{}, actual interface{}, extInfo ...string) {
if !nazareflect.Equal(expected, actual) {
err := fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v", expected, actual)
var v string
if len(extInfo) == 0 {
v = fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v", expected, actual)
} else {
v = fmt.Sprintf("assert failed. excepted=%+v, but actual=%+v, extInfo=%s", expected, actual, extInfo)
}
switch l.core.option.AssertBehavior {
case AssertError:
l.Out(LevelError, 2, err)
l.Out(LevelError, 2, v)
case AssertFatal:
l.Out(LevelFatal, 2, err)
l.Out(LevelFatal, 2, v)
fake.Os_Exit(1)
case AssertPanic:
l.Out(LevelPanic, 2, err)
panic(err)
l.Out(LevelPanic, 2, v)
panic(v)
}
}
}

@ -235,6 +235,7 @@ func TestAssert(t *testing.T) {
option.AssertBehavior = nazalog.AssertError
})
nazalog.Assert(nil, 1)
nazalog.Assert(nil, 1, "I guess this could be failed.", "I guess so")
_ = nazalog.Init(func(option *nazalog.Option) {
option.AssertBehavior = nazalog.AssertFatal
@ -254,6 +255,7 @@ func TestAssert(t *testing.T) {
l, _ := nazalog.New()
l.Assert(nil, 1)
l.Assert(nil, 1, "I guess this could be failed.", "I guess so")
l, _ = nazalog.New(func(option *nazalog.Option) {
option.AssertBehavior = nazalog.AssertFatal

Loading…
Cancel
Save