77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
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))
|
|
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
|
|
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))
|
|
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))
|
|
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)
|
|
}
|
|
}
|