44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package env
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestString(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
value string
|
|
defaultValue string
|
|
expected string
|
|
shouldSet bool
|
|
}{
|
|
{"valid string", "TEST_STRING", "hello", "default", "hello", true},
|
|
{"empty string", "TEST_STRING", "", "default", "", true},
|
|
{"string with spaces", "TEST_STRING", "hello world", "default", "hello world", true},
|
|
{"string with special chars", "TEST_STRING", "test@123!$%", "default", "test@123!$%", true},
|
|
{"multiline string", "TEST_STRING", "line1\nline2\nline3", "default", "line1\nline2\nline3", true},
|
|
{"unicode string", "TEST_STRING", "Hello 世界 🌍", "default", "Hello 世界 🌍", true},
|
|
{"not set", "TEST_STRING_NOTSET", "", "default_value", "default_value", false},
|
|
{"numeric string", "TEST_STRING", "12345", "default", "12345", true},
|
|
{"boolean string", "TEST_STRING", "true", "default", "true", true},
|
|
{"path string", "TEST_STRING", "/usr/local/bin", "default", "/usr/local/bin", true},
|
|
{"url string", "TEST_STRING", "https://example.com", "default", "https://example.com", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.shouldSet {
|
|
os.Setenv(tt.key, tt.value)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
|
|
result := String(tt.key, tt.defaultValue)
|
|
if result != tt.expected {
|
|
t.Errorf("String() = %v, want %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|