config_test.go 856 字节
package utils

import (
	"github.com/stretchr/testify/assert"
	"os"
	"testing"
)

func TestEnvConfigurator(t *testing.T) {
	os.Setenv("bool", "true")
	os.Setenv("hello", "world")
	os.Setenv("number", "10")

	var config Configurator = EnvConfigurator{}
	assert.Equal(t, "world", config.DefaultString("hello", "w"))
	assert.Equal(t, "w", config.DefaultString("unknown", "w"))

	assert.Equal(t, true, config.DefaultBool("bool", false))
	assert.Equal(t, true, config.DefaultBool("unknown", true))

	assert.Equal(t, 10, config.DefaultInt("number", 5))
	assert.Equal(t, 5, config.DefaultInt("unknown", 5))

	assert.Equal(t, int64(10), config.DefaultInt64("number", 5))
	assert.Equal(t, int64(5), config.DefaultInt64("unknown", 5))

	assert.Equal(t, float64(10), config.DefaultFloat("number", 5))
	assert.Equal(t, float64(5), config.DefaultFloat("unknown", 5))
}