Browse Source

test: encoder of logger module

dev
Dnomd343 9 months ago
parent
commit
11acb494be
  1. 24
      go.mod
  2. 62
      go.sum
  3. 122
      logger/encoder_test.go
  4. 114
      mocks/PrimitiveArrayEncoder.go

24
go.mod

@ -4,22 +4,26 @@ go 1.21.0
require (
github.com/BurntSushi/toml v1.3.2
github.com/andybalholm/brotli v1.0.5
github.com/andybalholm/brotli v1.1.0
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/klauspost/compress v1.16.7
github.com/gookit/color v1.5.4
github.com/klauspost/compress v1.17.6
github.com/magiconair/properties v1.8.7
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.9.3
go.uber.org/zap v1.25.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/fatih/color v1.15.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/sys v0.16.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

62
go.sum

@ -1,36 +1,64 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a h1:v6zMvHuY9yue4+QkG/HQ/W67wvtQmWJ4SDo9aK/GIno=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a/go.mod h1:I79BieaU4fxrw4LMXby6q5OS9XnoR9UIKLOzDFjUmuw=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 h1:jik8PHtAIsPlCRJjJzl4udgEf7hawInF9texMeO2jrU=
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

122
logger/encoder_test.go

@ -0,0 +1,122 @@
package logger
import (
"XProxy/mocks"
"fmt"
"github.com/magiconair/properties/assert"
"github.com/petermattis/goid"
"github.com/stretchr/testify/mock"
"go.uber.org/zap/zapcore"
"runtime"
"testing"
"time"
)
var testTime = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
// zapCaller build fake caller with the absolute path of current code.
func zapCaller() zapcore.EntryCaller {
_, srcPath, _, _ := runtime.Caller(0)
return zapcore.EntryCaller{
File: srcPath,
Line: 0,
}
}
func Test_getCaller(t *testing.T) {
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"
assert.Equal(t, getCaller(caller, true), "unknown")
assert.Equal(t, getCaller(caller, false), "unknown")
}
func Test_timeEncoder(t *testing.T) {
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
timeEncoder(testTime, encoder)
encoder.AssertCalled(t, "AppendString", "2000-01-01 00:00:00.000")
}
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
timeColoredEncoder(testTime, encoder)
exceptPrefix := "\x1b[36m" + logger.prefix + "\x1b[0m"
encoder.AssertCalled(t, "AppendString", exceptPrefix+" \x1b[90m2000-01-01 00:00:00.000\x1b[0m")
}
}
func Test_callerEncoder(t *testing.T) {
logger.verbose = false
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerEncoder(zapCaller(), encoder)
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 = true
{
encoder := mocks.NewPrimitiveArrayEncoder(t)
encoder.On("AppendString", mock.Anything).Once()
callerEncoder(zapCaller(), encoder)
expectPrefix := fmt.Sprintf("[%d]", goid.Get())
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) {
encoder := mocks.NewPrimitiveArrayEncoder(t)
enroll := func(values []string, call *mock.Call) *mock.Call {
for _, value := range values {
if call == nil {
call = encoder.On("AppendString", value).Once()
} else {
call = encoder.On("AppendString", value).Once().NotBefore(call)
}
}
return call
}
caller := enroll([]string{
"[DEBUG]",
"[INFO]",
"[WARN]",
"[ERROR]",
"[PANIC]",
}, nil)
levelEncoder(zapcore.DebugLevel, encoder)
levelEncoder(zapcore.InfoLevel, encoder)
levelEncoder(zapcore.WarnLevel, encoder)
levelEncoder(zapcore.ErrorLevel, encoder)
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)
}

114
mocks/PrimitiveArrayEncoder.go

@ -0,0 +1,114 @@
// Code generated by mockery v2.40.1. DO NOT EDIT.
package mocks
import mock "github.com/stretchr/testify/mock"
// PrimitiveArrayEncoder is an autogenerated mock type for the PrimitiveArrayEncoder type
type PrimitiveArrayEncoder struct {
mock.Mock
}
// AppendBool provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendBool(_a0 bool) {
_m.Called(_a0)
}
// AppendByteString provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendByteString(_a0 []byte) {
_m.Called(_a0)
}
// AppendComplex128 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendComplex128(_a0 complex128) {
_m.Called(_a0)
}
// AppendComplex64 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendComplex64(_a0 complex64) {
_m.Called(_a0)
}
// AppendFloat32 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendFloat32(_a0 float32) {
_m.Called(_a0)
}
// AppendFloat64 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendFloat64(_a0 float64) {
_m.Called(_a0)
}
// AppendInt provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendInt(_a0 int) {
_m.Called(_a0)
}
// AppendInt16 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendInt16(_a0 int16) {
_m.Called(_a0)
}
// AppendInt32 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendInt32(_a0 int32) {
_m.Called(_a0)
}
// AppendInt64 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendInt64(_a0 int64) {
_m.Called(_a0)
}
// AppendInt8 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendInt8(_a0 int8) {
_m.Called(_a0)
}
// AppendString provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendString(_a0 string) {
_m.Called(_a0)
}
// AppendUint provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUint(_a0 uint) {
_m.Called(_a0)
}
// AppendUint16 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUint16(_a0 uint16) {
_m.Called(_a0)
}
// AppendUint32 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUint32(_a0 uint32) {
_m.Called(_a0)
}
// AppendUint64 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUint64(_a0 uint64) {
_m.Called(_a0)
}
// AppendUint8 provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUint8(_a0 uint8) {
_m.Called(_a0)
}
// AppendUintptr provides a mock function with given fields: _a0
func (_m *PrimitiveArrayEncoder) AppendUintptr(_a0 uintptr) {
_m.Called(_a0)
}
// NewPrimitiveArrayEncoder creates a new instance of PrimitiveArrayEncoder. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewPrimitiveArrayEncoder(t interface {
mock.TestingT
Cleanup(func())
}) *PrimitiveArrayEncoder {
mock := &PrimitiveArrayEncoder{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
Loading…
Cancel
Save