|
@ -8,6 +8,7 @@ import ( |
|
|
"github.com/stretchr/testify/mock" |
|
|
"github.com/stretchr/testify/mock" |
|
|
"go.uber.org/zap/zapcore" |
|
|
"go.uber.org/zap/zapcore" |
|
|
"runtime" |
|
|
"runtime" |
|
|
|
|
|
"strings" |
|
|
"testing" |
|
|
"testing" |
|
|
"time" |
|
|
"time" |
|
|
) |
|
|
) |
|
@ -23,100 +24,76 @@ func zapCaller() zapcore.EntryCaller { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// encoderTest is a helper function to test buffer output of mock encoder.
|
|
|
|
|
|
func encoderTest(t *testing.T, exec func(*mocks.PrimitiveArrayEncoder), expect string) { |
|
|
|
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
|
|
|
encoder.On("AppendInt64", mock.Anything).Maybe() // only enroll used method
|
|
|
|
|
|
encoder.On("AppendString", mock.Anything).Maybe() |
|
|
|
|
|
|
|
|
|
|
|
exec(encoder) |
|
|
|
|
|
var values []string |
|
|
|
|
|
for _, call := range encoder.Calls { |
|
|
|
|
|
values = append(values, fmt.Sprintf("%v", call.Arguments.Get(0))) |
|
|
|
|
|
} |
|
|
|
|
|
assert.Equal(t, strings.Join(values, " "), expect) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func Test_getCaller(t *testing.T) { |
|
|
func Test_getCaller(t *testing.T) { |
|
|
caller := zapCaller() |
|
|
caller := zapCaller() |
|
|
assert.Equal(t, getCaller(caller, false), "logger/encoder_test") |
|
|
|
|
|
assert.Equal(t, getCaller(caller, true), "logger/encoder_test.go:0") |
|
|
|
|
|
|
|
|
|
|
|
caller.File = "Invalid Path" |
|
|
caller.File = "Invalid Path" |
|
|
assert.Equal(t, getCaller(caller, true), "unknown") |
|
|
assert.Equal(t, getCaller(caller, true), "unknown") |
|
|
assert.Equal(t, getCaller(caller, false), "unknown") |
|
|
assert.Equal(t, getCaller(caller, false), "unknown") |
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, getCaller(zapCaller(), false), "logger/encoder_test") |
|
|
|
|
|
assert.Equal(t, getCaller(zapCaller(), true), "logger/encoder_test.go:0") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_timeEncoder(t *testing.T) { |
|
|
func Test_timeEncoder(t *testing.T) { |
|
|
{ |
|
|
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) { |
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
|
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
|
|
|
timeEncoder(testTime, encoder) |
|
|
timeEncoder(testTime, encoder) |
|
|
encoder.AssertCalled(t, "AppendString", "2000-01-01 00:00:00.000") |
|
|
}, "2000-01-01 00:00:00.000") |
|
|
} |
|
|
|
|
|
{ |
|
|
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) { |
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
|
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
|
|
|
timeColoredEncoder(testTime, encoder) |
|
|
timeColoredEncoder(testTime, encoder) |
|
|
exceptPrefix := "\x1b[36m" + logger.prefix + "\x1b[0m" |
|
|
}, "\x1b[36m"+logger.prefix+"\x1b[0m \x1b[90m2000-01-01 00:00:00.000\x1b[0m") |
|
|
encoder.AssertCalled(t, "AppendString", exceptPrefix+" \x1b[90m2000-01-01 00:00:00.000\x1b[0m") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_callerEncoder(t *testing.T) { |
|
|
func Test_callerEncoder(t *testing.T) { |
|
|
logger.verbose = false |
|
|
verboseVal := logger.verbose |
|
|
{ |
|
|
callerTest := func(entry func(zapcore.EntryCaller, zapcore.PrimitiveArrayEncoder), expect string) { |
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) { |
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
entry(zapCaller(), encoder) |
|
|
callerEncoder(zapCaller(), encoder) |
|
|
}, expect) |
|
|
encoder.AssertCalled(t, "AppendString", "[logger/encoder_test]") |
|
|
|
|
|
} |
|
|
|
|
|
{ |
|
|
|
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
|
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
|
|
|
callerColoredEncoder(zapCaller(), encoder) |
|
|
|
|
|
encoder.AssertCalled(t, "AppendString", "\x1b[35m[logger/encoder_test]\x1b[0m") |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.verbose = false |
|
|
|
|
|
callerTest(callerEncoder, "[logger/encoder_test]") |
|
|
|
|
|
callerTest(callerColoredEncoder, "\x1b[35m[logger/encoder_test]\x1b[0m") |
|
|
|
|
|
|
|
|
logger.verbose = true |
|
|
logger.verbose = true |
|
|
{ |
|
|
gid := fmt.Sprintf("[%d]", goid.Get()) |
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
callerTest(callerEncoder, gid+" [logger/encoder_test.go:0]") |
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
callerTest(callerColoredEncoder, "\x1b[34m"+gid+"\x1b[0m \x1b[35m[logger/encoder_test.go:0]\x1b[0m") |
|
|
callerEncoder(zapCaller(), encoder) |
|
|
|
|
|
expectPrefix := fmt.Sprintf("[%d]", goid.Get()) |
|
|
logger.verbose = verboseVal |
|
|
encoder.AssertCalled(t, "AppendString", expectPrefix+" [logger/encoder_test.go:0]") |
|
|
|
|
|
} |
|
|
|
|
|
{ |
|
|
|
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
|
|
|
encoder.On("AppendString", mock.Anything).Once() |
|
|
|
|
|
callerColoredEncoder(zapCaller(), encoder) |
|
|
|
|
|
expectPrefix := fmt.Sprintf("\x1b[34m[%d]\x1b[0m", goid.Get()) |
|
|
|
|
|
encoder.AssertCalled(t, "AppendString", expectPrefix+" \x1b[35m[logger/encoder_test.go:0]\x1b[0m") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_levelEncoder(t *testing.T) { |
|
|
func Test_levelEncoder(t *testing.T) { |
|
|
encoder := mocks.NewPrimitiveArrayEncoder(t) |
|
|
levelTest := func(entry func(zapcore.Level, zapcore.PrimitiveArrayEncoder), level zapcore.Level, expect string) { |
|
|
enroll := func(values []string, call *mock.Call) *mock.Call { |
|
|
encoderTest(t, func(encoder *mocks.PrimitiveArrayEncoder) { |
|
|
for _, value := range values { |
|
|
entry(level, encoder) |
|
|
if call == nil { |
|
|
}, expect) |
|
|
call = encoder.On("AppendString", value).Once() |
|
|
|
|
|
} else { |
|
|
|
|
|
call = encoder.On("AppendString", value).Once().NotBefore(call) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return call |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
caller := enroll([]string{ |
|
|
levelTest(levelEncoder, zapcore.DebugLevel, "[DEBUG]") |
|
|
"[DEBUG]", |
|
|
levelTest(levelEncoder, zapcore.InfoLevel, "[INFO]") |
|
|
"[INFO]", |
|
|
levelTest(levelEncoder, zapcore.WarnLevel, "[WARN]") |
|
|
"[WARN]", |
|
|
levelTest(levelEncoder, zapcore.ErrorLevel, "[ERROR]") |
|
|
"[ERROR]", |
|
|
levelTest(levelEncoder, zapcore.PanicLevel, "[PANIC]") |
|
|
"[PANIC]", |
|
|
|
|
|
}, nil) |
|
|
levelTest(levelColoredEncoder, zapcore.DebugLevel, "\x1b[39m[DEBUG]\x1b[0m") |
|
|
levelEncoder(zapcore.DebugLevel, encoder) |
|
|
levelTest(levelColoredEncoder, zapcore.InfoLevel, "\x1b[32m[INFO]\x1b[0m") |
|
|
levelEncoder(zapcore.InfoLevel, encoder) |
|
|
levelTest(levelColoredEncoder, zapcore.WarnLevel, "\x1b[33m[WARN]\x1b[0m") |
|
|
levelEncoder(zapcore.WarnLevel, encoder) |
|
|
levelTest(levelColoredEncoder, zapcore.ErrorLevel, "\x1b[31m[ERROR]\x1b[0m") |
|
|
levelEncoder(zapcore.ErrorLevel, encoder) |
|
|
levelTest(levelColoredEncoder, zapcore.PanicLevel, "\x1b[91m[PANIC]\x1b[0m") |
|
|
levelEncoder(zapcore.PanicLevel, encoder) |
|
|
|
|
|
|
|
|
|
|
|
enroll([]string{ |
|
|
|
|
|
"\x1b[39m[DEBUG]\x1b[0m", |
|
|
|
|
|
"\x1b[32m[INFO]\x1b[0m", |
|
|
|
|
|
"\x1b[33m[WARN]\x1b[0m", |
|
|
|
|
|
"\x1b[31m[ERROR]\x1b[0m", |
|
|
|
|
|
"\x1b[91m[PANIC]\x1b[0m", |
|
|
|
|
|
}, caller) |
|
|
|
|
|
levelColoredEncoder(zapcore.DebugLevel, encoder) |
|
|
|
|
|
levelColoredEncoder(zapcore.InfoLevel, encoder) |
|
|
|
|
|
levelColoredEncoder(zapcore.WarnLevel, encoder) |
|
|
|
|
|
levelColoredEncoder(zapcore.ErrorLevel, encoder) |
|
|
|
|
|
levelColoredEncoder(zapcore.PanicLevel, encoder) |
|
|
|
|
|
} |
|
|
} |
|
|