added more int parsing and tests

This commit is contained in:
2026-01-10 18:22:16 +11:00
parent 61d519399f
commit f3312f7aef
7 changed files with 643 additions and 0 deletions

170
env/int_test.go vendored Normal file
View File

@@ -0,0 +1,170 @@
package env
import (
"os"
"testing"
)
func TestInt(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue int
expected int
shouldSet bool
}{
{"valid positive int", "TEST_INT", "42", 0, 42, true},
{"valid negative int", "TEST_INT", "-42", 0, -42, true},
{"valid zero", "TEST_INT", "0", 10, 0, true},
{"not set", "TEST_INT_NOTSET", "", 100, 100, false},
{"invalid value", "TEST_INT", "not_a_number", 50, 50, true},
{"empty string", "TEST_INT", "", 75, 75, 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 := Int(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Int() = %v, want %v", result, tt.expected)
}
})
}
}
func TestInt8(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue int8
expected int8
shouldSet bool
}{
{"valid positive int8", "TEST_INT8", "42", 0, 42, true},
{"valid negative int8", "TEST_INT8", "-42", 0, -42, true},
{"max int8", "TEST_INT8", "127", 0, 127, true},
{"min int8", "TEST_INT8", "-128", 0, -128, true},
{"overflow", "TEST_INT8", "128", 10, 10, true},
{"not set", "TEST_INT8_NOTSET", "", 50, 50, false},
{"invalid value", "TEST_INT8", "not_a_number", 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 := Int8(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Int8() = %v, want %v", result, tt.expected)
}
})
}
}
func TestInt16(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue int16
expected int16
shouldSet bool
}{
{"valid positive int16", "TEST_INT16", "1000", 0, 1000, true},
{"valid negative int16", "TEST_INT16", "-1000", 0, -1000, true},
{"max int16", "TEST_INT16", "32767", 0, 32767, true},
{"min int16", "TEST_INT16", "-32768", 0, -32768, true},
{"overflow", "TEST_INT16", "32768", 100, 100, true},
{"not set", "TEST_INT16_NOTSET", "", 500, 500, false},
{"invalid value", "TEST_INT16", "invalid", 250, 250, 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 := Int16(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Int16() = %v, want %v", result, tt.expected)
}
})
}
}
func TestInt32(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue int32
expected int32
shouldSet bool
}{
{"valid positive int32", "TEST_INT32", "100000", 0, 100000, true},
{"valid negative int32", "TEST_INT32", "-100000", 0, -100000, true},
{"max int32", "TEST_INT32", "2147483647", 0, 2147483647, true},
{"min int32", "TEST_INT32", "-2147483648", 0, -2147483648, true},
{"overflow", "TEST_INT32", "2147483648", 1000, 1000, true},
{"not set", "TEST_INT32_NOTSET", "", 5000, 5000, false},
{"invalid value", "TEST_INT32", "abc123", 2500, 2500, 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 := Int32(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Int32() = %v, want %v", result, tt.expected)
}
})
}
}
func TestInt64(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue int64
expected int64
shouldSet bool
}{
{"valid positive int64", "TEST_INT64", "1000000000", 0, 1000000000, true},
{"valid negative int64", "TEST_INT64", "-1000000000", 0, -1000000000, true},
{"max int64", "TEST_INT64", "9223372036854775807", 0, 9223372036854775807, true},
{"min int64", "TEST_INT64", "-9223372036854775808", 0, -9223372036854775808, true},
{"overflow", "TEST_INT64", "9223372036854775808", 10000, 10000, true},
{"not set", "TEST_INT64_NOTSET", "", 50000, 50000, false},
{"invalid value", "TEST_INT64", "not_valid", 25000, 25000, 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 := Int64(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Int64() = %v, want %v", result, tt.expected)
}
})
}
}