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

42
env/duration_test.go vendored Normal file
View File

@@ -0,0 +1,42 @@
package env
import (
"os"
"testing"
"time"
)
func TestDuration(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue time.Duration
expected time.Duration
shouldSet bool
}{
{"valid positive duration", "TEST_DURATION", "100", 0, 100 * time.Nanosecond, true},
{"valid zero", "TEST_DURATION", "0", 10 * time.Second, 0, true},
{"large value", "TEST_DURATION", "1000000000", 0, 1 * time.Second, true},
{"valid negative duration", "TEST_DURATION", "-100", 0, -100 * time.Nanosecond, true},
{"not set", "TEST_DURATION_NOTSET", "", 5 * time.Minute, 5 * time.Minute, false},
{"invalid value", "TEST_DURATION", "not_a_number", 30 * time.Second, 30 * time.Second, true},
{"empty string", "TEST_DURATION", "", 1 * time.Hour, 1 * time.Hour, true},
{"float value", "TEST_DURATION", "10.5", 2 * time.Second, 2 * time.Second, true},
{"very large value", "TEST_DURATION", "9223372036854775807", 0, 9223372036854775807 * time.Nanosecond, 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 := Duration(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Duration() = %v, want %v", result, tt.expected)
}
})
}
}