awl/logawl/logging_test.go
Sam Therapy 1b5d5a3fed
Some checks failed
continuous-integration/drone/push Build is failing
Do "a few things"
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-07-20 23:14:15 +02:00

91 lines
1.7 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package logawl_test
import (
"bytes"
"testing"
"time"
"git.froth.zone/sam/awl/logawl"
"github.com/stretchr/testify/assert"
)
var logger = logawl.New()
func TestLogawl(t *testing.T) {
t.Parallel()
assert.Equal(t, logawl.Level(2), logger.Level) //cast 2 (int) to 2 (level)
//Validate setting and getting levels from memory works
for i := range logawl.AllLevels {
logger.SetLevel(logawl.Level(i))
assert.Equal(t, logawl.Level(i), logger.GetLevel())
}
}
func TestUnmarshalLevels(t *testing.T) {
t.Parallel()
m := make(map[int]string)
var err error
//Fill map with unmarshalled level info
for i := range logawl.AllLevels {
m[i], err = logger.UnMarshalLevel(logawl.Level(i))
assert.Nil(t, err)
}
//iterate over map and assert equal
for i := range logawl.AllLevels {
lv, err := logger.UnMarshalLevel(logawl.Level(i))
assert.Nil(t, err)
assert.Equal(t, m[i], lv)
}
lv, err := logger.UnMarshalLevel(logawl.Level(9001))
assert.NotNil(t, err)
assert.Equal(t, "", lv)
assert.ErrorContains(t, err, "invalid log level choice")
}
func TestLogger(t *testing.T) {
t.Parallel()
for i := range logawl.AllLevels {
// only test non-exiting log levels
switch i {
case 1:
fn := func() {
logger.Info("")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 2:
fn := func() {
logger.Info("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 3:
fn := func() {
logger.Debug("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
}
}
}
func TestFmt(t *testing.T) {
t.Parallel()
ti := time.Now()
test := []byte("test")
assert.NotNil(t, logger.FormatHeader(&test, ti, 0, logawl.Level(9001))) //make sure error is error
}