From 1957e3c80e2bf0a4e7e47414c31b8fadfd3419f0 Mon Sep 17 00:00:00 2001 From: Ben Ridley Date: Tue, 17 Nov 2020 11:26:46 +1100 Subject: [PATCH] Add some more complete test cases, test for broken clamping Signed-off-by: Ben Ridley --- timeinterval/timeinterval_test.go | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/timeinterval/timeinterval_test.go b/timeinterval/timeinterval_test.go index 7c4652d1..5b0e299c 100644 --- a/timeinterval/timeinterval_test.go +++ b/timeinterval/timeinterval_test.go @@ -506,3 +506,71 @@ func TestYamlMarshal(t *testing.T) { } } } + +var completeTestCases = []struct { + in string + contains []string + excludes []string +}{ + { + in: ` +--- +weekdays: ['monday:wednesday', 'saturday', 'sunday'] +times: + - start_time: '13:00' + end_time: '15:00' +days_of_month: ['1', '10', '20:-1'] +years: ['2020:2023'] +months: ['january:march'] +`, + contains: []string{ + "10 Jan 21 13:00 GMT", + "30 Jan 21 14:24 GMT", + }, + excludes: []string{ + "09 Jan 21 13:00 GMT", + "20 Jan 21 12:59 GMT", + "02 Feb 21 13:00 GMT", + }, + }, + { + // Check for broken clamping (clamping begin date after end of month to the end of the month) + in: ` +--- +days_of_month: ['30:31'] +years: ['2020:2023'] +months: ['february'] +`, + excludes: []string{ + "28 Feb 21 13:00 GMT", + }, + }, +} + +// Tests the entire flow from unmarshalling to containing a time +func TestTimeIntervalComplete(t *testing.T) { + for _, tc := range completeTestCases { + var ti TimeInterval + if err := yaml.Unmarshal([]byte(tc.in), &ti); err != nil { + t.Error(err) + } + for _, ts := range tc.contains { + tt, err := time.Parse(time.RFC822, ts) + if err != nil { + t.Error(err) + } + if !ti.ContainsTime(tt) { + t.Errorf("Expected %s to contain %s", tc.in, ts) + } + } + for _, ts := range tc.excludes { + tt, err := time.Parse(time.RFC822, ts) + if err != nil { + t.Error(err) + } + if ti.ContainsTime(tt) { + t.Errorf("Expected %s to exclude %s", tc.in, ts) + } + } + } +}