Files
golib/env/uint_test.go

172 lines
4.5 KiB
Go

package env
import (
"os"
"testing"
)
func TestUInt(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue uint
expected uint
shouldSet bool
}{
{"valid uint", "TEST_UINT", "42", 0, 42, true},
{"valid zero", "TEST_UINT", "0", 10, 0, true},
{"large value", "TEST_UINT", "4294967295", 0, 4294967295, true},
{"not set", "TEST_UINT_NOTSET", "", 100, 100, false},
{"invalid value", "TEST_UINT", "not_a_number", 50, 50, true},
{"negative value", "TEST_UINT", "-42", 75, 75, true},
{"empty string", "TEST_UINT", "", 25, 25, 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 := UInt(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("UInt() = %v, want %v", result, tt.expected)
}
})
}
}
func TestUInt8(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue uint8
expected uint8
shouldSet bool
}{
{"valid uint8", "TEST_UINT8", "42", 0, 42, true},
{"valid zero", "TEST_UINT8", "0", 10, 0, true},
{"max uint8", "TEST_UINT8", "255", 0, 255, true},
{"overflow", "TEST_UINT8", "256", 10, 10, true},
{"not set", "TEST_UINT8_NOTSET", "", 50, 50, false},
{"invalid value", "TEST_UINT8", "abc", 25, 25, true},
{"negative value", "TEST_UINT8", "-1", 30, 30, 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 := UInt8(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("UInt8() = %v, want %v", result, tt.expected)
}
})
}
}
func TestUInt16(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue uint16
expected uint16
shouldSet bool
}{
{"valid uint16", "TEST_UINT16", "1000", 0, 1000, true},
{"valid zero", "TEST_UINT16", "0", 100, 0, true},
{"max uint16", "TEST_UINT16", "65535", 0, 65535, true},
{"overflow", "TEST_UINT16", "65536", 100, 100, true},
{"not set", "TEST_UINT16_NOTSET", "", 500, 500, false},
{"invalid value", "TEST_UINT16", "invalid", 250, 250, true},
{"negative value", "TEST_UINT16", "-100", 300, 300, 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 := UInt16(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("UInt16() = %v, want %v", result, tt.expected)
}
})
}
}
func TestUInt32(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue uint32
expected uint32
shouldSet bool
}{
{"valid uint32", "TEST_UINT32", "100000", 0, 100000, true},
{"valid zero", "TEST_UINT32", "0", 1000, 0, true},
{"max uint32", "TEST_UINT32", "4294967295", 0, 4294967295, true},
{"overflow", "TEST_UINT32", "4294967296", 1000, 1000, true},
{"not set", "TEST_UINT32_NOTSET", "", 5000, 5000, false},
{"invalid value", "TEST_UINT32", "xyz", 2500, 2500, true},
{"negative value", "TEST_UINT32", "-1000", 3000, 3000, 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 := UInt32(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("UInt32() = %v, want %v", result, tt.expected)
}
})
}
}
func TestUInt64(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue uint64
expected uint64
shouldSet bool
}{
{"valid uint64", "TEST_UINT64", "1000000000", 0, 1000000000, true},
{"valid zero", "TEST_UINT64", "0", 10000, 0, true},
{"max uint64", "TEST_UINT64", "18446744073709551615", 0, 18446744073709551615, true},
{"overflow", "TEST_UINT64", "18446744073709551616", 10000, 10000, true},
{"not set", "TEST_UINT64_NOTSET", "", 50000, 50000, false},
{"invalid value", "TEST_UINT64", "not_valid", 25000, 25000, true},
{"negative value", "TEST_UINT64", "-5000", 30000, 30000, 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 := UInt64(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("UInt64() = %v, want %v", result, tt.expected)
}
})
}
}