From d230e4031345d551c852e20b43b5d2e9c2d51161 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Tue, 24 Jul 2018 18:40:11 +0200 Subject: [PATCH] #9 TI-1: Make undo work with config actions --- src/commands/CmdUndo.cpp | 23 +++++++++++++++++- test/undo.t | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/commands/CmdUndo.cpp b/src/commands/CmdUndo.cpp index 27d8aea4..6f2cd6ab 100644 --- a/src/commands/CmdUndo.cpp +++ b/src/commands/CmdUndo.cpp @@ -37,6 +37,27 @@ static void undoIntervalAction(UndoAction& action, Database& database) database.modifyInterval (after, before); } +static void undoConfigAction (UndoAction& action, Rules &rules, Database& database) +{ + const std::string& before = action.getBefore (); + + if (before.empty ()) { + const std::string& after = action.getAfter (); + auto pos = after.find (' '); + std::string name = after.substr (0, pos); + + Rules::unsetConfigVariable (database, rules, name, false); + } + else + { + auto pos = before.find ('='); + std::string name = before.substr (0, pos-1); + std::string value = before.substr (pos+1, before.length ()); + + Rules::setConfigVariable (database, rules, name, value, false); + } +} + //////////////////////////////////////////////////////////////////////////////// int CmdUndo (Rules& rules, Database& database) { @@ -66,7 +87,7 @@ int CmdUndo (Rules& rules, Database& database) } else if (type == "config") { - throw "Undo of config actions not yet implemented!"; + undoConfigAction (action, rules, database); } else { diff --git a/test/undo.t b/test/undo.t index 705e1acb..dd6880c2 100755 --- a/test/undo.t +++ b/test/undo.t @@ -61,6 +61,58 @@ class TestUndo(TestCase): expectedStart=one_hour_before_utc, expectedTags=["foo"]) + def test_undo_config_add_name(self): + """Test undo of command 'config' (add name)""" + self.t("config foo bar :yes") + + before = [x for x in self.t.timewrc_content if x != '\n'] + + self.t("undo") + + after = [x for x in self.t.timewrc_content if x != '\n'] + + self.assertNotEqual(before, after) + self.assertEqual([], after) + + def test_undo_config_remove_name(self): + """Test undo of command 'config' (remove name)""" + self.t("config foo bar :yes") + self.t("config foo :yes") + + before = self.t.timewrc_content[:] + + self.t("undo") + + after = self.t.timewrc_content[:] + + self.assertNotEqual(before, after) + + def test_undo_config_set_value(self): + """Test undo of command 'config' (set value)""" + self.t("config foo bar :yes") + self.t("config foo baz :yes") + + before = self.t.timewrc_content[:] + + self.t("undo") + + after = self.t.timewrc_content[:] + + self.assertNotEqual(before, after) + + def test_undo_config_remove_value(self): + """Test undo of command 'config' (remove value)""" + self.t("config foo bar :yes") + self.t("config foo '' :yes") + + before = self.t.timewrc_content[:] + + self.t("undo") + + after = self.t.timewrc_content[:] + + self.assertNotEqual(before, after) + def test_undo_continue(self): """Test undo of command 'continue'""" now_utc = datetime.now().utcnow()