From 73c6d6012a3cff79f72c6fb31a48b84f49c8cd09 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 29 Aug 2013 18:58:01 -0400 Subject: [PATCH] fleshing out --- README.md | 2 +- cronexpression.go | 2 +- cronexpression_test.go | 109 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 cronexpression_test.go diff --git a/README.md b/README.md index cc8b77a..2d67405 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Go language (golang) cron expression parser. Given a cron expression and a time The reference documentation for this implementation is found at https://en.wikipedia.org/wiki/Cron#CRON_expression, with the following -difference: +differences: * Supports the second field (before minute field) * If five fields are present, a wildcard year field is appended diff --git a/cronexpression.go b/cronexpression.go index 6b8fa6b..2cb6574 100644 --- a/cronexpression.go +++ b/cronexpression.go @@ -1,7 +1,7 @@ /*! * Copyright 2013 Raymond Hill * - * Project: github.com/gorhill/gocronexpression + * Project: github.com/gorhill/cronexpression * File: cronexpression.go * Version: 1.0 * License: GPL v3 see diff --git a/cronexpression_test.go b/cronexpression_test.go new file mode 100644 index 0000000..002e0db --- /dev/null +++ b/cronexpression_test.go @@ -0,0 +1,109 @@ +/*! + * Copyright 2013 Raymond Hill + * + * Project: github.com/gorhill/cronexpression + * File: cronexpression_test.go + * Version: 1.0 + * License: GPL v3 see + * + */ + +package cronexpression_test + +/******************************************************************************/ + +import ( + "cronexpression" + "testing" + "time" +) + +/******************************************************************************/ + +type crontimes struct { + from string + next string +} + +type crontest struct { + expr string + layout string + times []crontimes +} + +var crontests = []crontest{ + // Seconds + { + "* * * * * * *", + "2006-01-02 15:04:05", + []crontimes{ + {"2013-01-01 00:00:00", "2013-01-01 00:00:01"}, + {"2013-01-01 00:00:59", "2013-01-01 00:01:00"}, + {"2013-01-01 23:59:59", "2013-01-02 00:00:00"}, + {"2013-02-28 23:59:59", "2013-03-01 00:00:00"}, + {"2016-02-28 23:59:59", "2016-02-29 00:00:00"}, + {"2012-12-31 23:59:59", "2013-01-01 00:00:00"}, + }, + }, + + // Minutes + { + "* * * * *", + "2006-01-02 15:04:05", + []crontimes{ + {"2013-01-01 00:00:00", "2013-01-01 00:01:00"}, + {"2013-01-01 00:00:59", "2013-01-01 00:01:00"}, + {"2013-01-01 00:59:00", "2013-01-01 01:00:00"}, + {"2013-01-01 23:59:00", "2013-01-02 00:00:00"}, + {"2013-02-28 23:59:00", "2013-03-01 00:00:00"}, + {"2016-02-28 23:59:00", "2016-02-29 00:00:00"}, + {"2012-12-31 23:59:00", "2013-01-01 00:00:00"}, + }, + }, + + // Minutes with intervals + { + "17-43/5 * * * *", + "2006-01-02 15:04:05", + []crontimes{ + {"2013-01-01 00:00:00", "2013-01-01 00:17:00"}, + {"2013-01-01 00:16:59", "2013-01-01 00:17:00"}, + {"2013-01-01 00:30:00", "2013-01-01 00:32:00"}, + {"2013-01-01 00:50:00", "2013-01-01 01:17:00"}, + {"2013-01-01 23:50:00", "2013-01-02 00:17:00"}, + {"2013-02-28 23:50:00", "2013-03-01 00:17:00"}, + {"2016-02-28 23:50:00", "2016-02-29 00:17:00"}, + {"2012-12-31 23:50:00", "2013-01-01 00:17:00"}, + }, + }, + + // Minutes interval, list + { + "15-30/4,55 * * * *", + "2006-01-02 15:04:05", + []crontimes{ + {"2013-01-01 00:00:00", "2013-01-01 00:15:00"}, + {"2013-01-01 00:16:00", "2013-01-01 00:19:00"}, + {"2013-01-01 00:30:00", "2013-01-01 00:55:00"}, + {"2013-01-01 00:55:00", "2013-01-01 01:15:00"}, + {"2013-01-01 23:55:00", "2013-01-02 00:15:00"}, + {"2013-02-28 23:55:00", "2013-03-01 00:15:00"}, + {"2016-02-28 23:55:00", "2016-02-29 00:15:00"}, + {"2012-12-31 23:55:00", "2013-01-01 00:15:00"}, + }, + }, + + // TODO: more tests +} + +func TestCronExpressions(t *testing.T) { + for _, test := range crontests { + for _, times := range test.times { + from, _ := time.Parse("2006-01-02 15:04:05", times.from) + next := cronexpression.NextTimeFromCronString(test.expr, from) + if next.Format(test.layout) != times.next { + t.Errorf("(\"%s\").NextTime(\"%s\") = \"%s\", got \"%s\"", test.expr, times.from, times.next, next.Format(test.layout)) + } + } + } +}