From 73ddf1b7b5e0bc85ccf12456e119a2e0d7f60155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Fri, 20 Jan 2017 00:16:59 +0000 Subject: [PATCH] Cron Expression Sigil (#23) * EronExpression Sigil * Make Tests Async --- lib/crontab/cron_expression.ex | 94 ++++++++++++++++--- .../crontab/cron_expression/composer_test.exs | 2 +- test/crontab/cron_expression/parser_test.exs | 2 +- test/crontab/cron_expression_test.exs | 7 ++ test/crontab/date_checker_test.exs | 2 +- test/crontab/functional_test.exs | 2 +- test/crontab/scheduler_test.exs | 2 +- 7 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 test/crontab/cron_expression_test.exs diff --git a/lib/crontab/cron_expression.ex b/lib/crontab/cron_expression.ex index 425834c..f382c39 100644 --- a/lib/crontab/cron_expression.ex +++ b/lib/crontab/cron_expression.ex @@ -3,6 +3,21 @@ defmodule Crontab.CronExpression do This is the Crontab.CronExpression module / struct. """ + @type t :: %Crontab.CronExpression{} + @type interval :: :minute | :hour | :day | :month | :weekday | :year + @type min_max :: {:-, time_unit, time_unit} + @type value :: time_unit | :* | :L | {:L, value} | {:/, time_unit | :* + | min_max, pos_integer} | min_max | {:W, time_unit | :L} + @type minute :: 0..59 + @type hour :: 0..23 + @type day :: 0..31 + @type month :: 1..12 + @type weekday :: 0..7 + @type year :: integer + @type time_unit :: minute | hour | day | month | weekday | year + @type condition :: {interval, [value]} + @type condition_list :: [condition] + @doc """ Defines the Cron Interval @@ -20,20 +35,48 @@ defmodule Crontab.CronExpression do """ defstruct extended: false, second: [:*], minute: [:*], hour: [:*], day: [:*], month: [:*], weekday: [:*], year: [:*] - @type t :: %Crontab.CronExpression{} - @type interval :: :minute | :hour | :day | :month | :weekday | :year - @type min_max :: {:-, time_unit, time_unit} - @type value :: time_unit | :* | :L | {:L, value} | {:/, time_unit | :* - | min_max, pos_integer} | min_max | {:W, time_unit | :L} - @type minute :: 0..59 - @type hour :: 0..23 - @type day :: 0..31 - @type month :: 1..12 - @type weekday :: 0..7 - @type year :: integer - @type time_unit :: minute | hour | day | month | weekday | year - @type condition :: {interval, [value]} - @type condition_list :: [condition] + @doc """ + Create a `%Crontab.CronExpression{}` via sigil. + + ### Examples + + iex> ~e[*] + %Crontab.CronExpression{ + extended: false, + second: [:*], + minute: [:*], + hour: [:*], + day: [:*], + month: [:*], + weekday: [:*], + year: [:*]} + + iex> ~e[*]e + %Crontab.CronExpression{ + extended: true, + second: [:*], + minute: [:*], + hour: [:*], + day: [:*], + month: [:*], + weekday: [:*], + year: [:*]} + + iex> ~e[1 2 3 4 5 6 7]e + %Crontab.CronExpression{ + extended: true, + second: [1], + minute: [2], + hour: [3], + day: [4], + month: [5], + weekday: [6], + year: [7]} + """ + @spec sigil_e(binary, charlist) :: t + def sigil_e(cron_expression, options) + def sigil_e(cron_expression, [?e]), do: Crontab.CronExpression.Parser.parse!(cron_expression, true) + def sigil_e(cron_expression, _options), do: Crontab.CronExpression.Parser.parse!(cron_expression, false) @doc """ Convert Crontab.CronExpression struct to Tuple List @@ -71,4 +114,27 @@ defmodule Crontab.CronExpression do def to_condition_list(interval = %Crontab.CronExpression{}) do [{:second, interval.second} | to_condition_list(%{interval | extended: false})] end + + defimpl Inspect do + @doc """ + Pretty Print Cron Expressions + + ### Examples: + + iex> IO.inspect %Crontab.CronExpression{} + ~e[* * * * * *] + + iex> import Crontab.CronExpression + iex> IO.inspect %Crontab.CronExpression{extended: true} + ~e[* * * * * * *]e + + """ + @spec inspect(Crontab.CronExpression.t, any) :: String.t + def inspect(cron_expression = %Crontab.CronExpression{extended: false}, _options) do + "~e[" <> Crontab.CronExpression.Composer.compose(cron_expression) <> "]" + end + def inspect(cron_expression = %Crontab.CronExpression{extended: true}, _options) do + "~e[" <> Crontab.CronExpression.Composer.compose(cron_expression) <> "]e" + end + end end diff --git a/test/crontab/cron_expression/composer_test.exs b/test/crontab/cron_expression/composer_test.exs index 59395fa..2ef4bc3 100644 --- a/test/crontab/cron_expression/composer_test.exs +++ b/test/crontab/cron_expression/composer_test.exs @@ -1,4 +1,4 @@ defmodule Crontab.CronExpression.ComposerTest do - use ExUnit.Case + use ExUnit.Case, async: true doctest Crontab.CronExpression.Composer end diff --git a/test/crontab/cron_expression/parser_test.exs b/test/crontab/cron_expression/parser_test.exs index 83674df..5edb80c 100644 --- a/test/crontab/cron_expression/parser_test.exs +++ b/test/crontab/cron_expression/parser_test.exs @@ -1,5 +1,5 @@ defmodule Crontab.CronExpression.ParserTest do - use ExUnit.Case + use ExUnit.Case, async: true doctest Crontab.CronExpression.Parser import Crontab.CronExpression.Parser diff --git a/test/crontab/cron_expression_test.exs b/test/crontab/cron_expression_test.exs new file mode 100644 index 0000000..32cb4a8 --- /dev/null +++ b/test/crontab/cron_expression_test.exs @@ -0,0 +1,7 @@ +defmodule Crontab.CronExpressionTest do + use ExUnit.Case, async: true + doctest Crontab.CronExpression, import: true + + # I'd like to doctest that one, but I can't get the sigil working + # doctest Inspect.Crontab.CronExpression, import: true +end diff --git a/test/crontab/date_checker_test.exs b/test/crontab/date_checker_test.exs index 1cccf0c..93267d2 100644 --- a/test/crontab/date_checker_test.exs +++ b/test/crontab/date_checker_test.exs @@ -1,5 +1,5 @@ defmodule Crontab.DateCheckerTest do - use ExUnit.Case + use ExUnit.Case, async: true doctest Crontab.DateChecker import Crontab.DateChecker diff --git a/test/crontab/functional_test.exs b/test/crontab/functional_test.exs index ae50678..4a7a732 100644 --- a/test/crontab/functional_test.exs +++ b/test/crontab/functional_test.exs @@ -1,5 +1,5 @@ defmodule Crontab.FunctionalTest do - use ExUnit.Case + use ExUnit.Case, async: true tests_find_date = [ ########################################################################################################################################### diff --git a/test/crontab/scheduler_test.exs b/test/crontab/scheduler_test.exs index 91061d9..32df13a 100644 --- a/test/crontab/scheduler_test.exs +++ b/test/crontab/scheduler_test.exs @@ -1,5 +1,5 @@ defmodule Crontab.SchedulerTest do - use ExUnit.Case + use ExUnit.Case, async: true doctest Crontab.Scheduler import Crontab.Scheduler