From 123d18e872f28676b2d775e86827e7bb794884d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Neboj=C5=A1a=20Stri=C4=8Devi=C4=87?= Date: Wed, 2 Nov 2022 06:16:34 +0000 Subject: [PATCH] Don't treat tasks scheduled in future as missed --- src/commands/schedule_tasks.janet | 22 ++++++++++++++-------- test/commands/schedule_tasks_test.janet | 22 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/commands/schedule_tasks.janet b/src/commands/schedule_tasks.janet index f7df0e8..1ea9506 100644 --- a/src/commands/schedule_tasks.janet +++ b/src/commands/schedule_tasks.janet @@ -26,19 +26,25 @@ (string "every year on " (remove-year formatted-date)) true (string "on " formatted-date) true)) -(defn- missed-on-day [plan task] +(defn- missed-on-day [plan task date] (find (fn [day] (and (scheduled-for? task (day :date)) (not (day/has-task? day task)))) - (plan/all-days plan))) + (plan/all-days-before plan date))) # Public -(defn missed? [plan task] - (def day (missed-on-day plan task)) +(defn missed? + ``` + Checks if the task was missed in the plan up to the passed date. + + Returns true or false. + ``` + [plan task date] + (def day (missed-on-day plan task date)) (and day (not (plan/has-task-after? plan task (day :date))))) -(defn- mark-tasks-as-missed [plan tasks] +(defn- mark-tasks-as-missed [plan tasks date] (map (fn [task] - (let [day (missed-on-day plan task)] + (let [day (missed-on-day plan task date)] (task/mark-as-missed task (day :date)))) tasks)) @@ -52,8 +58,8 @@ (let [tasks (filter (fn [task] (scheduled-for? task (day :date))) scheduled-tasks)] (day/add-tasks day tasks))) (loop [day :in future-days] - (let [tasks (filter (fn [task] (missed? plan task)) scheduled-tasks) - missed-tasks (mark-tasks-as-missed plan tasks)] + (let [tasks (filter (fn [task] (missed? plan task date)) scheduled-tasks) + missed-tasks (mark-tasks-as-missed plan tasks date)] (day/add-tasks day missed-tasks))) plan) diff --git a/test/commands/schedule_tasks_test.janet b/test/commands/schedule_tasks_test.janet index 5116f3c..2768bbb 100644 --- a/test/commands/schedule_tasks_test.janet +++ b/test/commands/schedule_tasks_test.janet @@ -75,13 +75,13 @@ (def plan (plan/build-plan :days @[(day/build-day (d/date 2022 8 2)) (day/build-day (d/date 2022 8 1))])) - (is (missed? plan scheduled-task))) + (is (missed? plan scheduled-task (d/date 2022 8 3)))) (deftest missed-returns-true-when-day-does-not-exist (def plan (plan/build-plan :days @[(day/build-day (d/date 2022 8 2)) (day/build-day (d/date 2022 7 15))])) - (is (missed? plan scheduled-task))) + (is (missed? plan scheduled-task (d/date 2022 8 3)))) (deftest missed-returns-false-when-task-was-scheduled-for-another-day (def plan (plan/build-plan @@ -89,7 +89,13 @@ @[] @[(task/build-task "Weekly meeting" true)]) (day/build-day (d/date 2022 8 1))])) - (is (not (missed? plan scheduled-task)))) + (is (not (missed? plan scheduled-task (d/date 2022 8 3))))) + +(deftest missed-returns-false-when-task-will-be-scheduled-in-future + (def plan (plan/build-plan + :days @[(day/build-day (d/date 2022 8 2)) + (day/build-day (d/date 2022 7 31))])) + (is (not (missed? plan scheduled-task (d/date 2022 7 31))))) ## ————————————————————————————————————————————————————————————————————————————————————————————————— ## Test schedule-tasks @@ -161,6 +167,16 @@ (is (= false (task :done))) (is (= "every Monday" (task :schedule))))))) +(deftest does-not-schedule-tasks-today-that-are-not-scheduled-for-future-day-that-is-not-in-plan + (def plan (plan/build-plan + :days @[(day/build-day (d/date 2022 1 19)) + (day/build-day (d/date 2022 1 18)) + (day/build-day (d/date 2022 1 16))])) + (schedule-tasks plan scheduled-tasks (d/date 2022 1 16)) + (is (= 1 (length (((plan :days) 0) :tasks)))) + (is (empty? (((plan :days) 1) :tasks))) + (is (empty? (((plan :days) 2) :tasks)))) + ## ————————————————————————————————————————————————————————————————————————————————————————————————— ## Test build-command