From 8e96ad52292efd764eb5dc7bf32eeb2e9279dc05 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Mon, 17 Dec 2018 20:15:46 +0100 Subject: [PATCH] Add functions to add/remove a set of tags to/from an interval Signed-off-by: Thomas Lauf --- src/CLI.h | 2 +- src/Interval.cpp | 23 +++++++++++++++++++++-- src/Interval.h | 2 ++ src/commands/CmdTag.cpp | 5 +---- src/commands/CmdUntag.cpp | 5 +---- test/tag.t | 18 ++++++++++++++++++ 6 files changed, 44 insertions(+), 11 deletions(-) mode change 100755 => 100644 test/tag.t diff --git a/src/CLI.h b/src/CLI.h index 0b69addd..6cf7bf25 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -70,7 +70,7 @@ public: bool getComplementaryHint (const std::string&, bool) const; bool getHint(const std::string&, bool) const; std::set getIds () const; - std::set getTags () const; + std::set getTags () const; std::string getAnnotation() const; Duration getDuration() const; std::vector getDomReferences () const; diff --git a/src/Interval.cpp b/src/Interval.cpp index 57cad61d..2d55429b 100644 --- a/src/Interval.cpp +++ b/src/Interval.cpp @@ -75,8 +75,13 @@ const std::set & Interval::tags () const //////////////////////////////////////////////////////////////////////////////// void Interval::tag (const std::string& tag) { - if (_tags.find (tag) == _tags.end ()) - _tags.insert (tag); + _tags.insert (tag); +} + +//////////////////////////////////////////////////////////////////////////////// +void Interval::tag (const std::set& tags) +{ + _tags.insert (tags.begin (), tags.end ()); } //////////////////////////////////////////////////////////////////////////////// @@ -85,6 +90,20 @@ void Interval::untag (const std::string& tag) _tags.erase (tag); } +//////////////////////////////////////////////////////////////////////////////// +void Interval::untag (const std::set & tags) +{ + std::set updated; + + std::set_difference ( + _tags.begin (), _tags.end (), + tags.begin (), tags.end (), + std::inserter (updated, updated.end ()) + ); + + _tags = updated; +} + //////////////////////////////////////////////////////////////////////////////// void Interval::clearTags () { diff --git a/src/Interval.h b/src/Interval.h index 93dbe311..d569ffe0 100644 --- a/src/Interval.h +++ b/src/Interval.h @@ -46,7 +46,9 @@ public: bool hasTag (const std::string&) const; const std::set & tags () const; void tag (const std::string&); + void tag (const std::set&); void untag (const std::string&); + void untag (const std::set&); void clearTags (); void setRange (const Range& range); diff --git a/src/commands/CmdTag.cpp b/src/commands/CmdTag.cpp index d03e473e..f79743c4 100644 --- a/src/commands/CmdTag.cpp +++ b/src/commands/CmdTag.cpp @@ -103,10 +103,7 @@ int CmdTag ( { Interval modified {interval}; - for (auto& tag : tags) - { - modified.tag (tag); - } + modified.tag (tags); database.modifyInterval (interval, modified, verbose); diff --git a/src/commands/CmdUntag.cpp b/src/commands/CmdUntag.cpp index d8fb6cbe..def1ced1 100644 --- a/src/commands/CmdUntag.cpp +++ b/src/commands/CmdUntag.cpp @@ -103,10 +103,7 @@ int CmdUntag ( { Interval modified {interval}; - for (auto& tag : tags) - { - modified.untag (tag); - } + modified.untag (tags); database.modifyInterval (interval, modified, verbose); diff --git a/test/tag.t b/test/tag.t old mode 100755 new mode 100644 index 9b5ae503..f8e26180 --- a/test/tag.t +++ b/test/tag.t @@ -206,6 +206,15 @@ class TestTag(TestCase): expectedTags=["foo"], description="unmodified interval") + def test_tag_with_identical_tags(self): + self.t("track 2016-01-01T00:00:00 - 2016-01-01T01:00:00") + self.t("tag @1 foo foo") + + j = self.t.export() + + self.assertEquals(len(j), 1) + self.assertEqual(j[0]['tags'], ['foo']) + def test_tag_with_identical_ids(self): """Call 'tag' with identical ids""" now_utc = datetime.now().utcnow() @@ -271,6 +280,15 @@ class TestTag(TestCase): code, out, err = self.t.runError("tag @2 foo") self.assertIn("ID '@2' does not correspond to any tracking.", err) + def test_untag_with_identical_tags(self): + self.t("track 2016-01-01T00:00:00 - 2016-01-01T01:00:00 foo bar") + self.t("untag @1 foo foo") + + j = self.t.export() + + self.assertEquals(len(j), 1) + self.assertEqual(j[0]['tags'], ['bar']) + if __name__ == "__main__": from simpletap import TAPTestRunner