Adds tests.

This commit is contained in:
Hyatt 2025-02-10 10:08:56 -06:00
parent 2ac8e37e17
commit fb420314da
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA
2 changed files with 67 additions and 9 deletions

View File

@ -89,7 +89,7 @@ func TestGetStructInfo(t *testing.T) {
case "Info":
assert.Equal(t, "() This is an info string.", v.Info)
case "Secret":
assert.Equal(t, false, v.Secret)
assert.Equal(t, true, v.Secret)
case "Env":
assert.Equal(t, "TEST_ENV", v.Alt)
case "Default_value":
@ -123,22 +123,22 @@ func TestTypeConversion(t *testing.T) {
// int
output_int, err := typeConversion("int", strconv.FormatInt(int64(expected_int), 10))
assert.NoError(t, err)
assert.Equal(t, expected_string, output_int)
assert.Equal(t, expected_int, int(output_int.(int64)))
// int8
output_int8, err := typeConversion("int8", strconv.FormatInt(int64(expected_int8), 10))
assert.NoError(t, err)
assert.Equal(t, expected_int8, output_int8)
assert.Equal(t, expected_int8, int8(output_int8.(int64))) // nolint: gosec
// int16
output_int16, err := typeConversion("int16", strconv.FormatInt(int64(expected_int16), 10))
assert.NoError(t, err)
assert.Equal(t, expected_int16, output_int16)
assert.Equal(t, expected_int16, int16(output_int16.(int64))) // nolint: gosec
// int32
output_int32, err := typeConversion("int32", strconv.FormatInt(int64(expected_int32), 10))
assert.NoError(t, err)
assert.Equal(t, expected_int32, output_int32)
assert.Equal(t, expected_int32, int32(output_int32.(int64))) // nolint: gosec
// int64
output_int64, err := typeConversion("int64", strconv.FormatInt(expected_int64, 10))
@ -148,17 +148,17 @@ func TestTypeConversion(t *testing.T) {
// uint
output_uint, err := typeConversion("uint", strconv.FormatInt(int64(expected_uint), 10))
assert.NoError(t, err)
assert.Equal(t, expected_uint, output_uint)
assert.Equal(t, expected_uint, uint(output_uint.(uint64))) // nolint: gosec
// uint16
output_uint16, err := typeConversion("uint16", strconv.FormatInt(int64(expected_uint16), 10))
assert.NoError(t, err)
assert.Equal(t, expected_uint16, output_uint16)
assert.Equal(t, expected_uint16, uint16(output_uint16.(uint64))) // nolint: gosec
// uint32
output_uint32, err := typeConversion("uint32", strconv.FormatInt(int64(expected_uint32), 10))
assert.NoError(t, err)
assert.Equal(t, expected_uint32, output_uint32)
assert.Equal(t, expected_uint32, uint32(output_uint32.(uint64))) // nolint: gosec
// uint64
output_uint64, err := typeConversion("uint64", strconv.FormatInt(int64(expected_uint64), 10))
@ -168,7 +168,7 @@ func TestTypeConversion(t *testing.T) {
// float32
output_float32, err := typeConversion("float32", strconv.FormatFloat(float64(expected_float32), 'f', 3, 64))
assert.NoError(t, err)
assert.Equal(t, expected_float32, output_float32)
assert.Equal(t, expected_float32, float32(output_float32.(float64))) // nolint: gosec
// float64
output_float64, err := typeConversion("float64", strconv.FormatFloat(expected_float64, 'f', 3, 64))

View File

@ -0,0 +1,58 @@
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")
}