conflib/conflib_test.go

55 lines
1.4 KiB
Go
Raw Normal View History

2024-10-24 01:07:34 +00:00
package conflib_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.janky.solutions/finn/conflib"
)
type Config struct {
StringField string
IntField int
BoolField bool `env:"BOOL"`
NestedField nested
StringWithDefault string `default:"default value"`
IntWithDefault int `default:"4"`
BoolWithDefault bool `default:"true"`
}
type nested struct {
Sample string
}
func TestLoadDefaults(t *testing.T) {
loader := conflib.Loader[Config]{AllowNoSources: true}
config, err := loader.Load()
require.NoError(t, err)
assert.Equal(t, "default value", config.StringWithDefault)
assert.Equal(t, 4, config.IntWithDefault)
assert.Equal(t, true, config.BoolWithDefault)
}
func TestLoadJson(t *testing.T) {
loader := conflib.Loader[Config]{}
config, err := loader.Load("testdata/working.json")
require.NoError(t, err)
assert.Equal(t, "default value", config.StringWithDefault)
assert.Equal(t, "valllue", config.StringField)
}
func TestLoadEnv(t *testing.T) {
os.Setenv("TESTAPP_STRING_FIELD", "idklol")
os.Setenv("TESTAPP_INT_FIELD", "4")
os.Setenv("TESTAPP_BOOL", "true")
loader := conflib.Loader[Config]{EnvPrefix: "TESTAPP"}
config, err := loader.Load()
require.NoError(t, err)
assert.Equal(t, "idklol", config.StringField)
assert.Equal(t, 4, config.IntField)
assert.Equal(t, true, config.BoolField)
}