diff --git a/README.md b/README.md index 388c248..8ee4b56 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,17 @@ schedule tasks and help you stay in touch with your contacts. For more information, visit the the main [Alas website](https://www.hackberry.dev/alas/). + +## Development + +Install dependencies with: + +```sh +jpm deps +``` + +Run tests with: + +```sh +jpm test +``` diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 4640904..0000000 --- a/TODO.md +++ /dev/null @@ -1 +0,0 @@ -# TODO diff --git a/src/commands/schedule_tasks.janet b/src/commands/schedule_tasks.janet index 1ea9506..c8f656c 100644 --- a/src/commands/schedule_tasks.janet +++ b/src/commands/schedule_tasks.janet @@ -9,6 +9,7 @@ (import ../file_repository) (import ../schedule_parser) +(def command "--schedule-tasks") (def weekdays ["Monday" "Tuesday" "Wednesday" "Thursday" "Friday"]) (defn- remove-year [formatted-date] @@ -48,6 +49,9 @@ (task/mark-as-missed task (day :date)))) tasks)) +(defn- format-parse-errors [errors] + (map (fn [error] (string command " " (string/ascii-lower error) ".")) errors)) + ## ————————————————————————————————————————————————————————————————————————————————————————————————— ## Public Interface @@ -70,12 +74,10 @@ (let [load-file-result (file_repository/load argument) error (load-file-result :error)] (if error - {:errors [(string "--schedule-tasks " (string/ascii-lower error))]} - (let [parse-result (schedule_parser/parse (load-file-result :text))] - (if parse-result - (let [schedule (first parse-result)] - (if (empty? schedule) - {:errors ["--schedule-tasks schedule is empty."]} - {:command [schedule-tasks (first parse-result) (date/today)]})) - {:errors ["--schedule-tasks schedule could not be parsed."]})))) + {:errors [(string command " " (string/ascii-lower error))]} + (let [parse-result (schedule_parser/parse (load-file-result :text)) + errors (parse-result :errors)] + (if errors + {:errors (format-parse-errors errors)} + {:command [schedule-tasks (parse-result :tasks) (date/today)]})))) {})) diff --git a/src/schedule_parser.janet b/src/schedule_parser.janet index 89880b8..2fe17e1 100644 --- a/src/schedule_parser.janet +++ b/src/schedule_parser.janet @@ -17,7 +17,16 @@ (defn parse ``` - Parses schedule string and returns an array of task entities. + Parses schedule-string and returns a tuple: + + - {:tasks tasks} Where tasks is an array of task entities, when parsing was successfull. + - {:errors errors} Where errors is an array of strings. ``` [schedule-string] - (peg/match schedule-grammar schedule-string)) + (let [parse-result (peg/match schedule-grammar schedule-string)] + (if parse-result + (let [tasks (first parse-result)] + (if (empty? tasks) + {:errors ["Schedule is empty"]} + {:tasks tasks})) + {:errors ["Schedule can not be parsed"]}))) diff --git a/test/commands/schedule_tasks_test.janet b/test/commands/schedule_tasks_test.janet index 2768bbb..d191ad6 100644 --- a/test/commands/schedule_tasks_test.janet +++ b/test/commands/schedule_tasks_test.janet @@ -199,7 +199,7 @@ (def arguments {"schedule-tasks" "test/examples/unparsable-schedule.md"}) (def result (build-command arguments)) (is (nil? (result :command))) - (is (= "--schedule-tasks schedule could not be parsed." (first (result :errors))))) + (is (= "--schedule-tasks schedule can not be parsed." (first (result :errors))))) (deftest build-command-when-schedule-is-empty (def arguments {"schedule-tasks" "test/examples/empty-schedule.md"}) diff --git a/test/examples/schedule-without-date.md b/test/examples/schedule-without-date.md new file mode 100644 index 0000000..92e37aa --- /dev/null +++ b/test/examples/schedule-without-date.md @@ -0,0 +1,7 @@ +# Scheduled Tasks + +- Weekly Meeting (every Tuesday) +- Deploy the web app (every weekday) +- Pay football practice +- Martha's birthsday (every 05-24) +- Meeting with Jack (on 2022-05-03) diff --git a/test/schedule_parser_test.janet b/test/schedule_parser_test.janet index 05dc5c8..0f8419c 100644 --- a/test/schedule_parser_test.janet +++ b/test/schedule_parser_test.janet @@ -2,6 +2,9 @@ (import ../src/date :as d) (import ../src/schedule_parser) +## ————————————————————————————————————————————————————————————————————————————————————————————————— +## Test parse-schedule + (deftest parse-schedule (def schedule-string ``` @@ -11,10 +14,11 @@ - Puzzle Storm on Lichess (every day) - Deploy the web app (every weekday) - Pay football practice (every month) - - Martha's birthsday (every 05-24) + - Martha's birthday (every 05-24) - Meeting with Jack (on 2022-05-03) ```) - (def scheduled-tasks (first (schedule_parser/parse schedule-string))) + (def result (schedule_parser/parse schedule-string)) + (def scheduled-tasks (result :tasks)) (is (= 6 (length scheduled-tasks))) (let [task (scheduled-tasks 0)] (is (= "Weekly Meeting" (task :title))) @@ -29,4 +33,22 @@ (is (= false (task :done))) (is (= "on 2022-05-03" (task :schedule))))) +(deftest parse-schedule-when-schedule-can-not-be-parsed + (def schedule-string + ``` + ## Schedule + + * One (always) + ```) + (def result (schedule_parser/parse schedule-string)) + (is (= "Schedule can not be parsed" (first (result :errors))))) + +(deftest parse-schedule-when-schedule-is-empty + (def schedule-string + ``` + # Scheduled Tasks + ```) + (def result (schedule_parser/parse schedule-string)) + (is (= "Schedule is empty" (first (result :errors))))) + (run-tests!)