@@ -95,144 +95,6 @@ func TestBefore(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestFutureDate(t *testing.T) {
|
||||
future := time.Now().Add(24 * time.Hour)
|
||||
past := time.Now().Add(-24 * time.Hour)
|
||||
|
||||
t.Run("future date", func(t *testing.T) {
|
||||
err := FutureDate()(&future)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("past date", func(t *testing.T) {
|
||||
err := FutureDate()(&past)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if err.Code != ErrorCodeOutOfRange {
|
||||
t.Errorf("expected error code %s, got %s", ErrorCodeOutOfRange, err.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil pointer", func(t *testing.T) {
|
||||
var timeVal *time.Time
|
||||
err := FutureDate()(timeVal)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for nil, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPastDate(t *testing.T) {
|
||||
future := time.Now().Add(24 * time.Hour)
|
||||
past := time.Now().Add(-24 * time.Hour)
|
||||
|
||||
t.Run("past date", func(t *testing.T) {
|
||||
err := PastDate()(&past)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("future date", func(t *testing.T) {
|
||||
err := PastDate()(&future)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if err.Code != ErrorCodeOutOfRange {
|
||||
t.Errorf("expected error code %s, got %s", ErrorCodeOutOfRange, err.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil pointer", func(t *testing.T) {
|
||||
var timeVal *time.Time
|
||||
err := PastDate()(timeVal)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for nil, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMinDuration(t *testing.T) {
|
||||
minDuration := 10 * time.Minute
|
||||
|
||||
t.Run("duration above minimum", func(t *testing.T) {
|
||||
duration := 20 * time.Minute
|
||||
err := MinDuration(minDuration)(&duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duration equal to minimum", func(t *testing.T) {
|
||||
duration := 10 * time.Minute
|
||||
err := MinDuration(minDuration)(&duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duration below minimum", func(t *testing.T) {
|
||||
duration := 5 * time.Minute
|
||||
err := MinDuration(minDuration)(&duration)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if err.Code != ErrorCodeOutOfRange {
|
||||
t.Errorf("expected error code %s, got %s", ErrorCodeOutOfRange, err.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil pointer", func(t *testing.T) {
|
||||
var duration *time.Duration
|
||||
err := MinDuration(minDuration)(duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for nil, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMaxDuration(t *testing.T) {
|
||||
maxDuration := 1 * time.Hour
|
||||
|
||||
t.Run("duration below maximum", func(t *testing.T) {
|
||||
duration := 30 * time.Minute
|
||||
err := MaxDuration(maxDuration)(&duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duration equal to maximum", func(t *testing.T) {
|
||||
duration := 1 * time.Hour
|
||||
err := MaxDuration(maxDuration)(&duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duration above maximum", func(t *testing.T) {
|
||||
duration := 2 * time.Hour
|
||||
err := MaxDuration(maxDuration)(&duration)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if err.Code != ErrorCodeOutOfRange {
|
||||
t.Errorf("expected error code %s, got %s", ErrorCodeOutOfRange, err.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil pointer", func(t *testing.T) {
|
||||
var duration *time.Duration
|
||||
err := MaxDuration(maxDuration)(duration)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for nil, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRangeDuration(t *testing.T) {
|
||||
minDuration := 10 * time.Minute
|
||||
maxDuration := 1 * time.Hour
|
||||
|
||||
Reference in New Issue
Block a user