mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
DOM
- 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:
parent
850dbb36f0
commit
e6df1b38e2
6 changed files with 140 additions and 71 deletions
135
src/DOM.cpp
135
src/DOM.cpp
|
@ -73,7 +73,7 @@ const std::vector <std::string> 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 <std::string, std::string>::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 <Tree*>::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.<name>
|
||||
|
@ -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)
|
|||
// <id>.<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>
|
||||
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;
|
||||
}
|
||||
|
||||
// <id>.<name>
|
||||
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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -38,8 +38,8 @@ public:
|
|||
~DOM ();
|
||||
|
||||
const std::vector <std::string> 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:
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <Context.h>
|
||||
#include <Filter.h>
|
||||
#include <Eval.h>
|
||||
#include <Dates.h>
|
||||
#include <main.h>
|
||||
|
@ -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 ()
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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") != "<unknown>", "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 != "<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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue