From e6df1b38e26c6421664d9dfedc69bde2f8845cfb Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 4 Jun 2014 18:57:36 -0400 Subject: [PATCH] DOM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Standardized DOM interface to return success/failure, with the possibility of blank values. This matches the Eval::source callback interface‥ - Added more unit tests. --- src/DOM.cpp | 135 ++++++++++++++++++++++++++++----------- src/DOM.h | 4 +- src/Filter.cpp | 6 +- src/commands/CmdCalc.cpp | 15 +---- src/commands/CmdGet.cpp | 10 +-- test/dom.t.cpp | 41 +++++++++--- 6 files changed, 140 insertions(+), 71 deletions(-) diff --git a/src/DOM.cpp b/src/DOM.cpp index be9b989fd..1e787bf01 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -73,7 +73,7 @@ const std::vector DOM::get_references () const // system.version // system.os // -const std::string DOM::get (const std::string& name) +bool DOM::get (const std::string& name, std::string& value) { int len = name.length (); Nibbler n (name); @@ -82,31 +82,56 @@ const std::string DOM::get (const std::string& name) if (len > 3 && name.substr (0, 3) == "rc.") { - return context.config.get (name.substr (3)); + std::string key = name.substr (3); + std::map ::iterator c = context.config.find (key); + if (c != context.config.end ()) + { + value = c->second; + return true; + } + + return false; } // context.* if (len > 8 && name.substr (0, 8) == "context.") { - if (name == "context.program") return context.parser.tree ()->_branches[0]->attribute ("raw"); + if (name == "context.program") + { + value = context.program; + return true; + } else if (name == "context.args") { - std::string combined; + value = ""; std::vector ::iterator i; for (i = context.parser.tree ()->_branches.begin (); i != context.parser.tree ()->_branches.end (); ++i) { - if (combined != "") - combined += " "; + if (value != "") + value += " "; - combined += (*i)->attribute ("raw"); + value += (*i)->attribute ("raw"); } - return combined; + return true; } - else if (name == "context.width") return format (context.terminal_width ? context.terminal_width : context.getWidth ()); - else if (name == "context.height") return format (context.terminal_height ? context.terminal_height : context.getHeight ()); - else throw format (STRING_DOM_UNREC, name); + else if (name == "context.width") + { + value = format (context.terminal_width + ? context.terminal_width + : context.getWidth ()); + return true; + } + else if (name == "context.height") + { + value = format (context.terminal_height + ? context.terminal_height + : context.getHeight ()); + return true; + } + else + throw format (STRING_DOM_UNREC, name); } // TODO stats. @@ -117,40 +142,45 @@ const std::string DOM::get (const std::string& name) { // Taskwarrior version number. if (name == "system.version") - return VERSION; + { + value = VERSION; + return true; + } // OS type. else if (name == "system.os") + { #if defined (DARWIN) - return "Darwin"; + value = "Darwin"; #elif defined (SOLARIS) - return "Solaris"; + value = "Solaris"; #elif defined (CYGWIN) - return "Cygwin"; + value = "Cygwin"; #elif defined (HAIKU) - return "Haiku"; + value = "Haiku"; #elif defined (OPENBSD) - return "OpenBSD"; + value = "OpenBSD"; #elif defined (FREEBSD) - return "FreeBSD"; + value = "FreeBSD"; #elif defined (NETBSD) - return "NetBSD"; + value = "NetBSD"; #elif defined (LINUX) - return "Linux"; + value = "Linux"; #elif defined (KFREEBSD) - return "GNU/kFreeBSD"; + value = "GNU/kFreeBSD"; #elif defined (GNUHURD) - return "GNU/Hurd"; + value = "GNU/Hurd"; #else - return STRING_DOM_UNKNOWN; + value = STRING_DOM_UNKNOWN; #endif - + return true; + } else throw format (STRING_DOM_UNREC, name); } - // Pass-through. - return name; + // Empty string if nothing is found. + return false; } //////////////////////////////////////////////////////////////////////////////// @@ -160,18 +190,27 @@ const std::string DOM::get (const std::string& name) // . // . // -const std::string DOM::get (const std::string& name, const Task& task) +bool DOM::get (const std::string& name, const Task& task, std::string& value) { // if (task.size () && name == "id") - return format (task.id); + { + value = format (task.id); + return true; + } if (task.size () && name == "urgency") - return format (task.urgency_c ()); + { + value = format (task.urgency_c ()); + return true; + } std::string canonical; if (task.size () && context.parser.canonicalize (canonical, "attribute", name)) - return task.get (canonical); + { + value = task.get (canonical); + return true; + } // . Nibbler n (name); @@ -190,10 +229,21 @@ const std::string DOM::get (const std::string& name, const Task& task) std::string attr; n.getUntilEOS (attr); - if (attr == "id") return format (ref.id); - else if (attr == "urgency") return format (ref.urgency_c ()); + if (attr == "id") + { + value = format (ref.id); + return true; + } + else if (attr == "urgency") + { + value = format (ref.urgency_c ()); + return true; + } else if (context.parser.canonicalize (canonical, "attribute", attr)) - return ref.get (canonical); + { + value = ref.get (canonical); + return true; + } } n.restore (); @@ -214,17 +264,28 @@ const std::string DOM::get (const std::string& name, const Task& task) std::string attr; n.getUntilEOS (attr); - if (attr == "id") return format (ref.id); - else if (attr == "urgency") return format (ref.urgency_c (), 4, 3); + if (attr == "id") + { + value = format (ref.id); + return true; + } + else if (attr == "urgency") + { + value = format (ref.urgency_c (), 4, 3); + return true; + } else if (context.parser.canonicalize (canonical, "attribute", attr)) - return ref.get (canonical); + { + value = ref.get (canonical); + return true; + } } n.restore (); } // Delegate to the context-free version of DOM::get. - return this->get (name); + return this->get (name, value); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/DOM.h b/src/DOM.h index dbc1fe2d0..bbad94189 100644 --- a/src/DOM.h +++ b/src/DOM.h @@ -38,8 +38,8 @@ public: ~DOM (); const std::vector get_references () const; - const std::string get (const std::string&); - const std::string get (const std::string&, const Task&); + bool get (const std::string&, std::string&); + bool get (const std::string&, const Task&, std::string&); void set (const std::string&, const std::string&); private: diff --git a/src/Filter.cpp b/src/Filter.cpp index 9535e77de..1275a8848 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -44,10 +44,10 @@ Task& contextTask = dummy; //////////////////////////////////////////////////////////////////////////////// bool domSource (const std::string& identifier, Variant& value) { - std::string stringValue = context.dom.get (identifier, contextTask); - if (stringValue != identifier) + std::string result; + if (context.dom.get (identifier, contextTask, result)) { - value = Variant (stringValue); + value = Variant (result); value.source (identifier); return true; } diff --git a/src/commands/CmdCalc.cpp b/src/commands/CmdCalc.cpp index 9789663e2..94ab4c353 100644 --- a/src/commands/CmdCalc.cpp +++ b/src/commands/CmdCalc.cpp @@ -25,6 +25,7 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -33,20 +34,6 @@ extern Context context; -//////////////////////////////////////////////////////////////////////////////// -static bool domSource (const std::string& name, Variant& value) -{ - Task t; - std::string resolved = context.dom.get (name, t); - if (resolved != name) - { - value = Variant (resolved); - return true; - } - - return false; -} - //////////////////////////////////////////////////////////////////////////////// CmdCalc::CmdCalc () { diff --git a/src/commands/CmdGet.cpp b/src/commands/CmdGet.cpp index e481b47ec..645468565 100644 --- a/src/commands/CmdGet.cpp +++ b/src/commands/CmdGet.cpp @@ -58,12 +58,12 @@ int CmdGet::execute (std::string& output) for (word = words.begin (); word != words.end (); ++word) { Task t; - std::string result = context.dom.get (*word, t); - results.push_back (result); - - if (result != "" && - result != *word) + std::string result; + if (context.dom.get (*word, t, result)) + { + results.push_back (result); found = true; + } } join (output, " ", results); diff --git a/test/dom.t.cpp b/test/dom.t.cpp index 361b93d75..2e4e06dbf 100644 --- a/test/dom.t.cpp +++ b/test/dom.t.cpp @@ -36,7 +36,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (6); + UnitTest t (17); // Ensure environment has no influence. unsetenv ("TASKDATA"); @@ -47,19 +47,40 @@ int main (int argc, char** argv) // Prime the pump. const char* fake_argv[] = {"task"}; context.parser.initialize (1, fake_argv); + context.program = "task"; + context.config.set ("name", "value"); DOM dom; - t.is (dom.get ("system.version"), VERSION, "DOM system.version -> VERSION"); - t.ok (dom.get ("system.os") != "", "DOM system.os -> != Unknown"); - t.is (dom.get ("context.program"), "task", "DOM context.program -> 'task'"); - t.is (dom.get ("context.args"), "task", "DOM context.args -> 'task'"); - t.ok (dom.get ("context.width") != "0", "DOM context.width -> '0'"); - t.ok (dom.get ("context.height") != "0", "DOM context.height -> '0'"); + std::string result; + t.ok (dom.get ("system.version", result), "DOM system.version -> true"); + t.is (result, VERSION, "DOM system.version -> VERSION"); - // TODO dom.get rc.name -// t.is (dom.get ("rc.verbose"), "yes", "DOM rc.verbose -> 'yes'"); + t.ok (dom.get ("system.os", result), "DOM system.os -> true"); + t.ok (result != "", "DOM system.os -> != Unknown"); - // TODO dom.set rc.name + t.ok (dom.get ("context.program", result), "DOM context.program -> true"); + t.is (result, "task", "DOM context.program -> 'task'"); + + t.ok (dom.get ("context.args", result), "DOM context.args -> true"); + t.is (result, "task", "DOM context.args -> 'task'"); + + t.ok (dom.get ("context.width", result), "DOM context.width -> true"); + t.ok (result != "0", "DOM context.width -> '0'"); + + t.ok (dom.get ("context.height", result), "DOM context.height -> true"); + t.ok (result != "0", "DOM context.height -> '0'"); + + // dom.get rc.name + t.ok (dom.get ("rc.name", result), "DOM rc.name -> true"); + t.is (result, "value", "DOM rc.name -> value"); + + // dom.get rc.missing + t.notok (dom.get ("rc.missing", result), "DOM rc.missing -> false"); + + // dom.set rc.name + dom.set ("rc.new", "value"); + t.ok (dom.get ("rc.new", result), "DOM rc.new -> true"); + t.is (result, "value", "DOM rc.new -> value"); } catch (const std::string& error)