- Standardized DOM interface to return success/failure, with the possibility
  of blank values. This matches the Eval::source callback interface‥
- Added more unit tests.
This commit is contained in:
Paul Beckingham 2014-06-04 18:57:36 -04:00
parent 850dbb36f0
commit e6df1b38e2
6 changed files with 140 additions and 71 deletions

View file

@ -73,7 +73,7 @@ const std::vector <std::string> DOM::get_references () const
// system.version // system.version
// system.os // 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 (); int len = name.length ();
Nibbler n (name); Nibbler n (name);
@ -82,31 +82,56 @@ const std::string DOM::get (const std::string& name)
if (len > 3 && if (len > 3 &&
name.substr (0, 3) == "rc.") name.substr (0, 3) == "rc.")
{ {
return context.config.get (name.substr (3)); std::string key = name.substr (3);
std::map <std::string, std::string>::iterator c = context.config.find (key);
if (c != context.config.end ())
{
value = c->second;
return true;
}
return false;
} }
// context.* // context.*
if (len > 8 && if (len > 8 &&
name.substr (0, 8) == "context.") 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") else if (name == "context.args")
{ {
std::string combined; value = "";
std::vector <Tree*>::iterator i; std::vector <Tree*>::iterator i;
for (i = context.parser.tree ()->_branches.begin (); i != context.parser.tree ()->_branches.end (); ++i) for (i = context.parser.tree ()->_branches.begin (); i != context.parser.tree ()->_branches.end (); ++i)
{ {
if (combined != "") if (value != "")
combined += " "; 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.width")
else if (name == "context.height") return format (context.terminal_height ? context.terminal_height : context.getHeight ()); {
else throw format (STRING_DOM_UNREC, name); 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.<name> // TODO stats.<name>
@ -117,40 +142,45 @@ const std::string DOM::get (const std::string& name)
{ {
// Taskwarrior version number. // Taskwarrior version number.
if (name == "system.version") if (name == "system.version")
return VERSION; {
value = VERSION;
return true;
}
// OS type. // OS type.
else if (name == "system.os") else if (name == "system.os")
{
#if defined (DARWIN) #if defined (DARWIN)
return "Darwin"; value = "Darwin";
#elif defined (SOLARIS) #elif defined (SOLARIS)
return "Solaris"; value = "Solaris";
#elif defined (CYGWIN) #elif defined (CYGWIN)
return "Cygwin"; value = "Cygwin";
#elif defined (HAIKU) #elif defined (HAIKU)
return "Haiku"; value = "Haiku";
#elif defined (OPENBSD) #elif defined (OPENBSD)
return "OpenBSD"; value = "OpenBSD";
#elif defined (FREEBSD) #elif defined (FREEBSD)
return "FreeBSD"; value = "FreeBSD";
#elif defined (NETBSD) #elif defined (NETBSD)
return "NetBSD"; value = "NetBSD";
#elif defined (LINUX) #elif defined (LINUX)
return "Linux"; value = "Linux";
#elif defined (KFREEBSD) #elif defined (KFREEBSD)
return "GNU/kFreeBSD"; value = "GNU/kFreeBSD";
#elif defined (GNUHURD) #elif defined (GNUHURD)
return "GNU/Hurd"; value = "GNU/Hurd";
#else #else
return STRING_DOM_UNKNOWN; value = STRING_DOM_UNKNOWN;
#endif #endif
return true;
}
else else
throw format (STRING_DOM_UNREC, name); throw format (STRING_DOM_UNREC, name);
} }
// Pass-through. // Empty string if nothing is found.
return name; return false;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -160,18 +190,27 @@ const std::string DOM::get (const std::string& name)
// <id>.<attribute> // <id>.<attribute>
// <uuid>.<attribute> // <uuid>.<attribute>
// //
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)
{ {
// <attr> // <attr>
if (task.size () && name == "id") if (task.size () && name == "id")
return format (task.id); {
value = format (task.id);
return true;
}
if (task.size () && name == "urgency") if (task.size () && name == "urgency")
return format (task.urgency_c ()); {
value = format (task.urgency_c ());
return true;
}
std::string canonical; std::string canonical;
if (task.size () && context.parser.canonicalize (canonical, "attribute", name)) if (task.size () && context.parser.canonicalize (canonical, "attribute", name))
return task.get (canonical); {
value = task.get (canonical);
return true;
}
// <id>.<name> // <id>.<name>
Nibbler n (name); Nibbler n (name);
@ -190,10 +229,21 @@ const std::string DOM::get (const std::string& name, const Task& task)
std::string attr; std::string attr;
n.getUntilEOS (attr); n.getUntilEOS (attr);
if (attr == "id") return format (ref.id); if (attr == "id")
else if (attr == "urgency") return format (ref.urgency_c ()); {
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)) else if (context.parser.canonicalize (canonical, "attribute", attr))
return ref.get (canonical); {
value = ref.get (canonical);
return true;
}
} }
n.restore (); n.restore ();
@ -214,17 +264,28 @@ const std::string DOM::get (const std::string& name, const Task& task)
std::string attr; std::string attr;
n.getUntilEOS (attr); n.getUntilEOS (attr);
if (attr == "id") return format (ref.id); if (attr == "id")
else if (attr == "urgency") return format (ref.urgency_c (), 4, 3); {
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)) else if (context.parser.canonicalize (canonical, "attribute", attr))
return ref.get (canonical); {
value = ref.get (canonical);
return true;
}
} }
n.restore (); n.restore ();
} }
// Delegate to the context-free version of DOM::get. // Delegate to the context-free version of DOM::get.
return this->get (name); return this->get (name, value);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -38,8 +38,8 @@ public:
~DOM (); ~DOM ();
const std::vector <std::string> get_references () const; const std::vector <std::string> get_references () const;
const std::string get (const std::string&); bool get (const std::string&, std::string&);
const std::string get (const std::string&, const Task&); bool get (const std::string&, const Task&, std::string&);
void set (const std::string&, const std::string&); void set (const std::string&, const std::string&);
private: private:

View file

@ -44,10 +44,10 @@ Task& contextTask = dummy;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool domSource (const std::string& identifier, Variant& value) bool domSource (const std::string& identifier, Variant& value)
{ {
std::string stringValue = context.dom.get (identifier, contextTask); std::string result;
if (stringValue != identifier) if (context.dom.get (identifier, contextTask, result))
{ {
value = Variant (stringValue); value = Variant (result);
value.source (identifier); value.source (identifier);
return true; return true;
} }

View file

@ -25,6 +25,7 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <Context.h> #include <Context.h>
#include <Filter.h>
#include <Eval.h> #include <Eval.h>
#include <Dates.h> #include <Dates.h>
#include <main.h> #include <main.h>
@ -33,20 +34,6 @@
extern Context context; 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 () CmdCalc::CmdCalc ()
{ {

View file

@ -58,12 +58,12 @@ int CmdGet::execute (std::string& output)
for (word = words.begin (); word != words.end (); ++word) for (word = words.begin (); word != words.end (); ++word)
{ {
Task t; Task t;
std::string result = context.dom.get (*word, t); std::string result;
results.push_back (result); if (context.dom.get (*word, t, result))
{
if (result != "" && results.push_back (result);
result != *word)
found = true; found = true;
}
} }
join (output, " ", results); join (output, " ", results);

View file

@ -36,7 +36,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (6); UnitTest t (17);
// Ensure environment has no influence. // Ensure environment has no influence.
unsetenv ("TASKDATA"); unsetenv ("TASKDATA");
@ -47,19 +47,40 @@ int main (int argc, char** argv)
// Prime the pump. // Prime the pump.
const char* fake_argv[] = {"task"}; const char* fake_argv[] = {"task"};
context.parser.initialize (1, fake_argv); context.parser.initialize (1, fake_argv);
context.program = "task";
context.config.set ("name", "value");
DOM dom; DOM dom;
t.is (dom.get ("system.version"), VERSION, "DOM system.version -> VERSION"); std::string result;
t.ok (dom.get ("system.os") != "<unknown>", "DOM system.os -> != Unknown"); t.ok (dom.get ("system.version", result), "DOM system.version -> true");
t.is (dom.get ("context.program"), "task", "DOM context.program -> 'task'"); t.is (result, VERSION, "DOM system.version -> VERSION");
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'");
// TODO dom.get rc.name t.ok (dom.get ("system.os", result), "DOM system.os -> true");
// t.is (dom.get ("rc.verbose"), "yes", "DOM rc.verbose -> 'yes'"); t.ok (result != "<unknown>", "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) catch (const std::string& error)