1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Accept days, months and years in time ranges. (#483)

This commit is contained in:
Sascha L. Teichmann 2023-10-19 13:13:11 +02:00 committed by GitHub
parent 81edb6ccbe
commit 455010dc64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 8 deletions

View file

@ -9,6 +9,7 @@
package models
import (
"strings"
"testing"
"time"
)
@ -25,6 +26,36 @@ func TestNewTimeInterval(t *testing.T) {
}
}
func TestParseDuration(t *testing.T) {
now := time.Now()
for _, x := range []struct {
in string
expected time.Duration
reference time.Time
fail bool
}{
{"1h", time.Hour, now, false},
{"2y", now.Sub(now.AddDate(-2, 0, 0)), now, false},
{"13M", now.Sub(now.AddDate(0, -13, 0)), now, false},
{"31d", now.Sub(now.AddDate(0, 0, -31)), now, false},
{"1h2d3m", now.Sub(now.AddDate(0, 0, -2)) + time.Hour + 3*time.Minute, now, false},
{strings.Repeat("1", 70) + "y1d", 0, now, true},
} {
got, err := parseDuration(x.in, x.reference)
if err != nil {
if !x.fail {
t.Errorf("%q should not fail: %v", x.in, err)
}
continue
}
if got != x.expected {
t.Errorf("%q got %v expected %v", x.in, got, x.expected)
}
}
}
// TestGuessDate tests whether a sample of strings are correctly parsed into Dates by guessDate()
func TestGuessDate(t *testing.T) {
if _, guess := guessDate("2006-01-02T15:04:05"); !guess {