Files
utilities/Env/env_test.go

70 lines
1.5 KiB
Go

package Env
import (
"os"
"testing"
)
func TestEnvironmentVariable_GetName(t *testing.T) {
v := EnvironmentVariable("TEST_VAR")
if v.GetName() != "TEST_VAR" {
t.Errorf("expected 'TEST_VAR', got '%s'", v.GetName())
}
}
func TestEnvironmentVariable_GetEnvString(t *testing.T) {
const envName = "TEST_STRING"
_ = os.Setenv(envName, "value")
defer func() {
_ = os.Unsetenv(envName)
}()
v := EnvironmentVariable(envName)
got := v.GetEnvString("fallback")
if got != "value" {
t.Errorf("expected 'value', got '%s'", got)
}
_ = os.Unsetenv(envName)
got = v.GetEnvString("fallback")
if got != "fallback" {
t.Errorf("expected 'fallback', got '%s'", got)
}
}
func TestEnvironmentVariable_GetEnvBool(t *testing.T) {
const envName = "TEST_BOOL"
v := EnvironmentVariable(envName)
_ = os.Setenv(envName, "true")
if !v.GetEnvBool(false) {
t.Errorf("expected true for 'true'")
}
_ = os.Setenv(envName, "1")
if !v.GetEnvBool(false) {
t.Errorf("expected true for '1'")
}
_ = os.Setenv(envName, "false")
if v.GetEnvBool(true) {
t.Errorf("expected false for 'false'")
}
_ = os.Unsetenv(envName)
if v.GetEnvBool(true) != true {
t.Errorf("expected fallback true")
}
}
func TestEnvironmentVariable_GetEnvInt(t *testing.T) {
const envName = "TEST_INT"
v := EnvironmentVariable(envName)
_ = os.Setenv(envName, "42")
if v.GetEnvInt(envName, 10) != 42 {
t.Errorf("expected 42")
}
_ = os.Unsetenv(envName)
if v.GetEnvInt(envName, 10) != 10 {
t.Errorf("expected fallback 10")
}
}