From 0019086d9859f7b1b16c53ae4b310225a956e132 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Sat, 1 Aug 2020 12:29:08 +0200 Subject: [PATCH] Address comments and add negative tests Signed-off-by: Julien Pivotto --- model/time.go | 3 +-- model/time_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/model/time.go b/model/time.go index 4b4ac84a..66fbe362 100644 --- a/model/time.go +++ b/model/time.go @@ -191,8 +191,7 @@ func ParseDuration(durationStr string) (Duration, error) { return 0, nil } matches := durationRE.FindStringSubmatch(durationStr) - fmt.Printf("%v", len(matches)) - if len(matches) != 15 { + if matches == nil { return 0, fmt.Errorf("not a valid duration string: %q", durationStr) } var dur time.Duration diff --git a/model/time_test.go b/model/time_test.go index 61bad49c..3a104c03 100644 --- a/model/time_test.go +++ b/model/time_test.go @@ -97,6 +97,10 @@ func TestParseDuration(t *testing.T) { in: "0", out: 0, expectedString: "0s", + }, { + in: "0w", + out: 0, + expectedString: "0s", }, { in: "0s", out: 0, @@ -151,6 +155,23 @@ func TestParseDuration(t *testing.T) { } } +func TestParseBadDuration(t *testing.T) { + var cases = []string{ + "1", + "1y1m1d", + "-1w", + "1.5d", + } + + for _, c := range cases { + _, err := ParseDuration(c) + if err == nil { + t.Errorf("Expected error on input %s", c) + } + + } +} + func TestTimeJSON(t *testing.T) { tests := []struct { in Time