File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99(import ../file_repository )
1010(import ../schedule_parser )
1111
12+ (def command " --schedule-tasks" )
1213(def weekdays [" Monday" " Tuesday" " Wednesday" " Thursday" " Friday" ])
1314
1415(defn- remove-year [formatted-date ]
4849 (task/mark-as-missed task (day :date ))))
4950 tasks ))
5051
52+ (defn- format-parse-errors [errors ]
53+ (map (fn [error ] (string command " " (string/ascii-lower error ) " ." )) errors ))
54+
5155# # —————————————————————————————————————————————————————————————————————————————————————————————————
5256# # Public Interface
5357
7074 (let [load-file-result (file_repository/load argument )
7175 error (load-file-result :error )]
7276 (if error
73- {:errors [(string " --schedule-tasks " (string/ascii-lower error ))]}
74- (let [parse-result (schedule_parser/parse (load-file-result :text ))]
75- (if parse-result
76- (let [schedule (first parse-result )]
77- (if (empty? schedule )
78- {:errors [" --schedule-tasks schedule is empty." ]}
79- {:command [schedule-tasks (first parse-result ) (date/today )]}))
80- {:errors [" --schedule-tasks schedule could not be parsed." ]}))))
77+ {:errors [(string command " " (string/ascii-lower error ))]}
78+ (let [parse-result (schedule_parser/parse (load-file-result :text ))
79+ errors (parse-result :errors )]
80+ (if errors
81+ {:errors (format-parse-errors errors )}
82+ {:command [schedule-tasks (parse-result :tasks ) (date/today )]}))))
8183 {}))
Original file line number Diff line number Diff line change 1717
1818(defn parse
1919 ```
20- Parses schedule string and returns an array of task entities.
20+ Parses schedule-string and returns a tuple:
21+
22+ - {:tasks tasks} Where tasks is an array of task entities, when parsing was successfull.
23+ - {:errors errors} Where errors is an array of strings.
2124 ```
2225 [schedule-string ]
23- (peg/match schedule-grammar schedule-string ))
26+ (let [parse-result (peg/match schedule-grammar schedule-string )]
27+ (if parse-result
28+ (let [tasks (first parse-result )]
29+ (if (empty? tasks )
30+ {:errors [" Schedule is empty" ]}
31+ {:tasks tasks }))
32+ {:errors [" Schedule can not be parsed" ]})))
Original file line number Diff line number Diff line change 199199 (def arguments {" schedule-tasks" " test/examples/unparsable-schedule.md" })
200200 (def result (build-command arguments ))
201201 (is (nil? (result :command )))
202- (is (= " --schedule-tasks schedule could not be parsed." (first (result :errors )))))
202+ (is (= " --schedule-tasks schedule can not be parsed." (first (result :errors )))))
203203
204204(deftest build-command-when-schedule-is-empty
205205 (def arguments {" schedule-tasks" " test/examples/empty-schedule.md" })
Original file line number Diff line number Diff line change 22(import ../src/date :as d )
33(import ../src/schedule_parser )
44
5+ # # —————————————————————————————————————————————————————————————————————————————————————————————————
6+ # # Test parse-schedule
7+
58(deftest parse-schedule
69 (def schedule-string
710 ```
1114 - Puzzle Storm on Lichess (every day)
1215 - Deploy the web app (every weekday)
1316 - Pay football practice (every month)
14- - Martha's birthsday (every 05-24)
17+ - Martha's birthday (every 05-24)
1518 - Meeting with Jack (on 2022-05-03)
1619 ``` )
17- (def scheduled-tasks (first (schedule_parser/parse schedule-string )))
20+ (def result (schedule_parser/parse schedule-string ))
21+ (def scheduled-tasks (result :tasks ))
1822 (is (= 6 (length scheduled-tasks )))
1923 (let [task (scheduled-tasks 0 )]
2024 (is (= " Weekly Meeting" (task :title )))
2933 (is (= false (task :done )))
3034 (is (= " on 2022-05-03" (task :schedule )))))
3135
36+ (deftest parse-schedule-when-schedule-can-not-be-parsed
37+ (def schedule-string
38+ ```
39+ ## Schedule
40+
41+ * One (always)
42+ ``` )
43+ (def result (schedule_parser/parse schedule-string ))
44+ (is (= " Schedule can not be parsed" (first (result :errors )))))
45+
46+ (deftest parse-schedule-when-schedule-is-empty
47+ (def schedule-string
48+ ```
49+ # Scheduled Tasks
50+ ``` )
51+ (def result (schedule_parser/parse schedule-string ))
52+ (is (= " Schedule is empty" (first (result :errors )))))
53+
3254(run-tests! )
You can’t perform that action at this time.
0 commit comments