Skip to content

Commit

Permalink
Improved pre parsing validation and doc test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
corka149 committed Apr 27, 2019
1 parent 0f31b0f commit 1a3c407
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ defmodule Timed.Cli do
## Examples
iex> parsed_args = {[date: "2018-11-11"], [], []}
iex> parsed_args = {[date: "2018-11-11", time: "~17:00"], [], []}
iex> Timed.Cli.args_valid? parsed_args
true
iex> invalid_parsed_args = {[time: "25:10"], [], []}
iex> parsed_args = {[time: "07:00~"], [], []}
iex> Timed.Cli.args_valid? parsed_args
true
iex> invalid_parsed_args = {[time: "28:00~29:10"], [], []}
iex> Timed.Cli.args_valid? invalid_parsed_args
false
"""
Expand Down Expand Up @@ -107,14 +110,11 @@ defmodule Timed.Cli do
# It is ok when no time argument is provided
defp is_valid_time?([]) do true end

defp is_valid_time?([time: time]) do
{result, _} = Time.from_iso8601("#{time}:00")
:ok == result
end

defp is_valid_time?(_) do
Logger.error("Time couldn't be validated.")
false
defp is_valid_time?(args) do
[start_t, end_t] = Timed.parse_time(args)
{result_start, _} = Time.from_iso8601("#{start_t}:00")
{result_end, _} = Time.from_iso8601("#{end_t}:00")
(:ok == result_start or start_t == "") and (:ok == result_end or end_t == "")
end

defp date_arg({aliases, strict}) do
Expand Down
2 changes: 1 addition & 1 deletion lib/timed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Timed do
{:error, "Unkown combination provided"}
end

defp parse_time(args) do
def parse_time(args) do
time = Keyword.get(args, :time, "~")
case String.split(time, "~") do
dt when length(dt) == 2 -> dt
Expand Down

0 comments on commit 1a3c407

Please sign in to comment.