golang-base/internal/config/struct-config_test.go
2025-02-10 10:08:56 -06:00

59 lines
1.1 KiB
Go

package config
import (
"bytes"
"log/slog"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func slogToBuffer() (*bytes.Buffer, *slog.Logger) {
buf := new(bytes.Buffer)
return buf, slog.New(
slog.NewTextHandler(
buf,
&slog.HandlerOptions{
Level: slog.LevelDebug,
},
),
)
}
func TestNew(t *testing.T) {
c := New()
assert.Equal(t, "config.Config", reflect.TypeOf(c).String())
}
func TestSetLogLevel(t *testing.T) {
c := New()
for _, i := range []int{0, 21, 41, 61} {
c.LogLevel = i
setLogLevel(&c)
switch i {
case 0:
assert.Equal(t, slog.LevelError, c.SLogLevel.Level())
case 21:
assert.Equal(t, slog.LevelWarn, c.SLogLevel.Level())
case 41:
assert.Equal(t, slog.LevelInfo, c.SLogLevel.Level())
case 61:
assert.Equal(t, slog.LevelDebug, c.SLogLevel.Level())
}
}
}
func TestPrintRunningConfig(t *testing.T) {
buf, log := slogToBuffer()
c := New()
c.Log = log
cfgInfo, err := getStructInfo(&c)
assert.NoError(t, err)
printRunningConfig(&c, cfgInfo)
assert.Contains(t, buf.String(), "Running Configuration")
}