2024-02-12 12:49:17 -06:00

243 lines
7.0 KiB
Go

package config
import (
"math"
"strconv"
"testing"
)
type mock_config struct {
Notags string
Ignored string `ignored:"true"`
Info string `info:"This is an info string."`
Secret string `secret:"true"`
Env string `env:"TEST_ENV"`
Default_string string `default:"This is a default string."`
Default_bool bool `default:"true"`
Default_int int `default:"100"`
Default_int64 int64 `default:"100"`
Default_float64 float64 `default:"100.001"`
}
func TestGetEnv(t *testing.T) {
var (
expected_string string = "This is a test string."
expected_bool bool = true
expected_int int = 100
expected_int64 int64 = 100
expected_float64 float64 = 100.001
expected_unset_default string = "This is a default value."
)
// string
t.Setenv("TEST_STRING", expected_string)
test_string, err := getEnv("TEST_STRING", "This is a default String")
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_string != expected_string {
t.Fatalf("Expected %s, got %s", expected_string, test_string)
}
// bool
t.Setenv("TEST_BOOL", strconv.FormatBool(expected_bool))
if _, err := getEnv("TEST_STRING", expected_bool); err == nil {
t.Fatal("getEnv should have failed with invalid type")
}
test_bool, err := getEnv("TEST_BOOL", false)
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_bool != expected_bool {
t.Fatalf("Expected %t, got %t", expected_bool, test_bool)
}
// int
if _, err := getEnv("TEST_STRING", expected_int); err == nil {
t.Fatal("getEnv should have failed with invalid type")
}
t.Setenv("TEST_INT", strconv.FormatInt(int64(expected_int), 10))
test_int, err := getEnv("TEST_INT", 999)
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_int != expected_int {
t.Fatalf("Expected %d, got %d", expected_int, test_int)
}
// int64
t.Setenv("TEST_INT64", strconv.FormatInt(expected_int64, 10))
if _, err := getEnv("TEST_STRING", expected_int64); err == nil {
t.Fatal("getEnv should have failed with invalid type")
}
test_int64, err := getEnv("TEST_INT64", int64(999))
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_int64 != expected_int64 {
t.Fatalf("Expected %d, got %d", expected_int64, test_int64)
}
// float64
t.Setenv("TEST_FLOAT64", strconv.FormatFloat(expected_float64, 'f', 3, 32))
if _, err := getEnv("TEST_STRING", expected_float64); err == nil {
t.Fatal("getEnv should have failed with invalid type")
}
test_float64, err := getEnv("TEST_FLOAT64", 999.99)
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_float64 != expected_float64 {
t.Fatalf("Expected %f, got %f", expected_float64, test_float64)
}
// unset or missing environment variable
test_unset, err := getEnv("TEST_DEFAULT", expected_unset_default)
if err != nil {
t.Fatalf("Error calling getEnv: %v", err)
}
if test_unset != expected_unset_default {
t.Fatalf("Expected %s, got %s", expected_unset_default, test_unset)
}
}
func TestGetStructInfo(t *testing.T) {
test_config := mock_config{
Notags: "notags",
Ignored: "ignored",
Secret: "secret",
}
cfgInfo, err := getStructInfo(&test_config)
if err != nil {
t.Fatalf("Error calling getStructInfo: %v", err)
}
for _, v := range cfgInfo {
switch v.Name {
case "Info":
if v.Info != "() This is an info string." {
t.Fatalf("Expected: %s, got: %s", "() This is an info string.", v.Info)
}
case "Secret":
if v.Secret != true {
t.Fatalf("Expected: %t, got: %t", true, false)
}
case "Env":
if v.Alt != "TEST_ENV" {
t.Fatalf("Expected: %s, got: %s", "TEST_ENV", v.Alt)
}
case "Default_value":
if v.DefaultValue != "This is a default string." {
t.Fatalf("Expected: %s, got: %s", "This is a default string.", v.DefaultValue)
}
}
}
}
func TestTypeConversion(t *testing.T) {
var (
expected_string string = "This is a string."
expected_int int = 100
expected_int8 int8 = 100
expected_int16 int16 = 100
expected_int32 int32 = 100
expected_int64 int64 = 100
expected_uint uint = 100
expected_uint16 uint16 = 100
expected_uint32 uint32 = 100
expected_uint64 uint64 = 100
expected_float32 float32 = 100.001
expected_float64 float64 = 100.001
expected_bool bool = true
)
// string
output_string, _ := typeConversion("string", expected_string)
if output_string != expected_string {
t.Fatalf("Expected: %s, got: %s", expected_string, output_string)
}
// int
output_int, _ := typeConversion("int", "100")
if int(output_int.(int64)) != expected_int {
t.Fatalf("Expected: %d, got: %d", expected_int, output_int)
}
// int8
output_int8, _ := typeConversion("int8", "100")
if int8(output_int8.(int64)) != expected_int8 {
t.Fatalf("Expected: %d, got: %d", expected_int8, output_int8)
}
// int16
output_int16, _ := typeConversion("int16", "100")
if int16(output_int16.(int64)) != expected_int16 {
t.Fatalf("Expected: %d, got: %d", expected_int16, output_int16)
}
// int32
output_int32, _ := typeConversion("int32", "100")
if int32(output_int32.(int64)) != expected_int32 {
t.Fatalf("Expected: %d, got: %d", expected_int32, output_int32)
}
// int64
output_int64, _ := typeConversion("int64", "100")
if output_int64.(int64) != expected_int64 {
t.Fatalf("Expected: %d, got: %d", expected_int64, output_int64)
}
// uint
output_uint, _ := typeConversion("uint", "100")
if uint(output_uint.(uint64)) != expected_uint {
t.Fatalf("Expected: %d, got: %d", expected_uint, output_uint)
}
// uint16
output_uint16, _ := typeConversion("uint16", "100")
if uint16(output_uint16.(uint64)) != expected_uint16 {
t.Fatalf("Expected: %d, got: %d", expected_uint16, output_uint16)
}
// uint32
output_uint32, _ := typeConversion("uint32", "100")
if uint32(output_uint32.(uint64)) != expected_uint32 {
t.Fatalf("Expected: %d, got: %d", expected_uint32, output_uint32)
}
// uint64
output_uint64, _ := typeConversion("uint64", "100")
if output_uint64.(uint64) != expected_uint64 {
t.Fatalf("Expected: %d, got: %d", expected_uint64, output_uint64)
}
// float32
output_float32, _ := typeConversion("float32", "100.001")
if float32(math.Round(output_float32.(float64)*1000)/1000) != expected_float32 {
t.Fatalf("Expected: %f, got: %f", expected_float32, float32(math.Round(output_float32.(float64)*1000)/1000))
}
// float64
output_float64, _ := typeConversion("float64", "100.001")
if math.Round(output_float64.(float64)*1000)/1000 != expected_float64 {
t.Fatalf("Expected: %f, got: %f", expected_float64, math.Round(output_float64.(float64)*1000)/1000)
}
// bool
output_bool, _ := typeConversion("bool", "true")
if output_bool.(bool) != expected_bool {
t.Fatalf("Expected: %t, got: %t", expected_bool, output_bool.(bool))
}
}
func TestParseFlags(t *testing.T) {
test_config := Config{}
cfgInfo, _ := getStructInfo(&test_config)
if err := test_config.parseFlags(cfgInfo); err != nil {
t.Fatalf("Expected error, no return data to validate: %v", err)
}
}