From f3280966e0f21a6a219f71f361bd1e290e85ef79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20R=C3=B6sch?= Date: Fri, 2 Aug 2019 20:44:40 +0200 Subject: [PATCH] Allow continue by tag. --- src/commands/CmdContinue.cpp | 29 ++++++++++++++++++++ test/continue.t | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/commands/CmdContinue.cpp b/src/commands/CmdContinue.cpp index 1672c9b4..229fd6b0 100644 --- a/src/commands/CmdContinue.cpp +++ b/src/commands/CmdContinue.cpp @@ -41,6 +41,7 @@ int CmdContinue ( // Gather IDs and TAGs. std::set ids = cli.getIds(); + auto tags = cli.getTags(); if (ids.size() > 1) { @@ -65,6 +66,34 @@ int CmdContinue ( assert (intervals.size () == 1); to_copy = intervals.front (); } + else if (!tags.empty()) + { + // Load the data. + // Note: There is no filter. + Interval filter; + auto tracked = getTracked (database, rules, filter); + + for (int i = tracked.size() -1; i >= 0; --i) + { + bool allTagsFound = true; + for (unsigned int t = 0; t < tags.size(); ++t) + { + if (!tracked[i].hasTag(tags[t])) + { + allTagsFound = false; + break; + } + } + if (allTagsFound) + { + to_copy = tracked[i]; + break; + } + } + + if (to_copy.empty()) + throw format ("Tags '{1}' do not correspond to any tracking.", joinQuotedIfNeeded (", ", tags)); + } else { Interval latest = getLatestInterval (database); diff --git a/test/continue.t b/test/continue.t index a6e00be1..a6d299dc 100755 --- a/test/continue.t +++ b/test/continue.t @@ -109,6 +109,58 @@ class TestContinue(TestCase): self.assertIn("Recorded BAR\n", out) self.assertIn("Tracking FOO\n", out) + def test_continue_with_multiple_tags(self): + """Verify that 'continue' with multiple tags works""" + code, out, err = self.t("start FOO BAR 2h ago") + self.assertIn("Tracking BAR FOO\n", out) + + code, out, err = self.t("start BAR 1h ago") + self.assertIn("Tracking BAR\n", out) + + code, out, err = self.t("start FOO 30min ago") + self.assertIn("Tracking FOO\n", out) + + code, out, err = self.t("continue FOO BAR") + self.assertIn("Recorded FOO\n", out) + self.assertIn("Tracking BAR FOO\n", out) + + def test_continue_with_invalid_tag(self): + """Verify that 'continue' with invalid tag is an error""" + code, out, err = self.t("start FOO 1h ago") + self.assertIn("Tracking FOO\n", out) + + code, out, err = self.t("stop 30min ago") + self.assertIn("Recorded FOO\n", out) + + code, out, err = self.t.runError("continue BAR") + self.assertIn("Tags 'BAR' do not correspond to any tracking.\n", err) + + def test_continue_with_tag_without_active_tracking(self): + """Verify that continuing a specified interval works""" + code, out, err = self.t("start FOO 1h ago") + self.assertIn("Tracking FOO\n", out) + + code, out, err = self.t("start BAR 30min ago") + self.assertIn("Tracking BAR\n", out) + + code, out, err = self.t("stop 15min ago") + self.assertIn("Recorded BAR\n", out) + + code, out, err = self.t("continue FOO") + self.assertIn("Tracking FOO\n", out) + + def test_continue_with_tag_with_active_tracking(self): + """Verify that continuing a specified interval stops active tracking""" + code, out, err = self.t("start FOO 1h ago") + self.assertIn("Tracking FOO\n", out) + + code, out, err = self.t("start BAR 30min ago") + self.assertIn("Tracking BAR\n", out) + + code, out, err = self.t("continue FOO") + self.assertIn("Recorded BAR\n", out) + self.assertIn("Tracking FOO\n", out) + def test_continue_with_id_and_date(self): """Verify that continuing a specified interval with date continues at given date""" now_utc = datetime.now().utcnow()