Skip to content

Commit

Permalink
Added tests for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 22, 2022
1 parent 76b524d commit 9aab242
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const loggerResult = "this is test message"
const loggerFResult = "this is %s message"

func TestGlobalLogPackage(t *testing.T) {
log.EnableOutput()
log.DisableColor()

t.Run("error logging", func(t *testing.T) {
Expand Down
101 changes: 101 additions & 0 deletions internal/log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package log_test

import (
"bytes"
"testing"

"github.com/evg4b/uncors/internal/log"
"github.com/evg4b/uncors/testing/testutils"
"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"
)

const testMessage = "this is test message"
const testFMessage = "this is %s message"
const prefix = " Test "

func TestPrefixedLogger(t *testing.T) {
log.EnableOutput()
log.DisableColor()
log.EnableDebugMessages()

t.Run("prefix printing", func(t *testing.T) {
logger := log.NewLogger(prefix)

t.Run("Error", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Error(testMessage)

assert.Equal(t, " Test ERROR this is test message\n", output.String())
}))

t.Run("Errorf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Errorf(testFMessage, "Error")

assert.Equal(t, " Test ERROR this is Error message\n", output.String())
}))

t.Run("Info", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Info(testMessage)

assert.Equal(t, " Test INFO this is test message\n", output.String())
}))

t.Run("Infof", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Infof(testFMessage, "Info")

assert.Equal(t, " Test INFO this is Info message\n", output.String())
}))

t.Run("Warning", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Warning(testMessage)

assert.Equal(t, " Test WARNING this is test message\n", output.String())
}))

t.Run("Warningf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Warningf(testFMessage, "Warning")

assert.Equal(t, " Test WARNING this is Warning message\n", output.String())
}))

t.Run("Debug", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Debug(testMessage)

assert.Equal(t, " Test DEBUG this is test message\n", output.String())
}))

t.Run("Debugf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
logger.Debugf(testFMessage, "Debug")

assert.Equal(t, " Test DEBUG this is Debug message\n", output.String())
}))
})

t.Run("custom output", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
var buffer = &bytes.Buffer{}

logger := log.NewLogger(prefix, log.WithOutput(buffer))

logger.Info("Test message")

assert.Empty(t, output.String())
assert.Equal(t, " Test INFO Test message\n", buffer.String())
}))

t.Run("custom styles", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.EnableColor()

logger := log.NewLogger(prefix, log.WithStyle(&pterm.Style{
pterm.BgBlue,
pterm.BgLightBlue,
}))

logger.Info("Test message")

expected := "\x1b[44;104m\x1b[44;104m Test \x1b[0m\x1b[0m \x1b[39;49m\x1b[39;49m\x1b[30;46m\x1b[30;46m" +
" INFO \x1b[0m\x1b[39;49m\x1b[0m\x1b[39;49m \x1b[96m\x1b[96mTest message\x1b[0m\x1b[39;49m\x1b[0m" +
"\x1b[39;49m\x1b[0m\x1b[0m\n"

assert.Equal(t, expected, output.String())
}))
}

0 comments on commit 9aab242

Please sign in to comment.