From 36a8f8266edfe85988b2cff495572093164072f4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 7 Feb 2024 00:06:14 +0800 Subject: [PATCH] test: add logger module test suite --- logger/logger_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 logger/logger_test.go diff --git a/logger/logger_test.go b/logger/logger_test.go new file mode 100644 index 0000000..08ef07e --- /dev/null +++ b/logger/logger_test.go @@ -0,0 +1,47 @@ +package logger + +import ( + "github.com/stretchr/testify/assert" + "go.uber.org/zap/zapcore" + "os" + "path" + "testing" +) + +func Test_init(t *testing.T) { + pwd := path.Dir(zapCaller().File) + assert.Equal(t, path.Join(pwd, "../"), project) + + assert.NotNil(t, logger) + assert.NotNil(t, logger.entry) + assert.NotNil(t, logger.level) + + assert.Equal(t, logger.verbose, false) + assert.Equal(t, logger.prefix, "[XProxy]") + assert.Equal(t, logger.level.Level(), InfoLevel) + + assert.Equal(t, len(logger.plain.writers), 0) + assert.Equal(t, len(logger.colored.writers), 1) + + for _, level := range []zapcore.Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, PanicLevel} { + logger.level.SetLevel(level) + assert.Equal(t, logger.entry.Level(), level) + } + logger.level.SetLevel(InfoLevel) // revert to INFO level +} + +func Test_addWrites(t *testing.T) { + core := new(logCore) + assert.Equal(t, len(core.plain.writers), 0) + assert.Equal(t, len(core.colored.writers), 0) + + core.entry = nil + core.addPlainWrites(os.Stdout, os.Stdout, os.Stdout) + assert.NotNil(t, core.entry) + assert.Equal(t, len(core.plain.writers), 3) + + core.entry = nil + core.addColoredWrites(os.Stderr, os.Stderr, os.Stderr) + assert.NotNil(t, core.entry) + assert.Equal(t, len(core.plain.writers), 3) +}