- Extended DOM.
This commit is contained in:
Paul Beckingham 2014-06-05 17:55:24 -04:00
parent 117c31b322
commit 61dab3eeb9
2 changed files with 148 additions and 76 deletions

View file

@ -190,6 +190,25 @@ bool DOM::get (const std::string& name, std::string& value)
// <id>.<attribute>
// <uuid>.<attribute>
//
// For certain attributes:
// <date>.year
// <date>.month
// <date>.day
// <date>.hour
// <date>.minute
// <date>.second
//
// <tag>.<literal> Includes virtual tags
//
// <annotation>.<N>.entry
// <annotation>.<N>.entry.year
// <annotation>.<N>.entry.month
// <annotation>.<N>.entry.day
// <annotation>.<N>.entry.hour
// <annotation>.<N>.entry.minute
// <annotation>.<N>.entry.second
// <annotation>.<N>.description
//
bool DOM::get (const std::string& name, const Task& task, std::string& value)
{
// <attr>
@ -205,83 +224,109 @@ bool DOM::get (const std::string& name, const Task& task, std::string& value)
return true;
}
// split name on '.'
std::vector <std::string> elements;
split (elements, name, '.');
if (elements.size () == 1)
{
std::string canonical;
if (task.size () && context.parser.canonicalize (canonical, "attribute", name))
{
value = task.get (canonical);
return true;
}
}
else if (elements.size () > 1)
{
Task ref;
// <id>.<name>
Nibbler n (name);
Nibbler n (elements[0]);
n.save ();
int id;
std::string uuid;
bool proceed = false;
if (n.getInt (id))
{
if (n.skip ('.'))
{
Task ref;
if (id == task.id)
ref = task;
else
context.tdb2.get (id, ref);
std::string attr;
n.getUntilEOS (attr);
if (attr == "id")
{
value = format (ref.id);
return true;
proceed = true;
}
else if (attr == "urgency")
{
value = format (ref.urgency_c ());
return true;
}
else if (context.parser.canonicalize (canonical, "attribute", attr))
{
value = ref.get (canonical);
return true;
}
}
else
n.restore ();
}
// <uuid>.<name>
std::string uuid;
if (n.getUUID (uuid))
else if (n.getUUID (uuid))
{
if (n.skip ('.'))
{
Task ref;
if (uuid == task.get ("uuid"))
ref = task;
else
context.tdb2.get (uuid, ref);
std::string attr;
n.getUntilEOS (attr);
proceed = true;
}
}
if (attr == "id")
if (proceed)
{
if (elements[1] == "id")
{
value = format (ref.id);
return true;
}
else if (attr == "urgency")
else if (elements[1] == "urgency")
{
value = format (ref.urgency_c (), 4, 3);
value = format (ref.urgency_c ());
return true;
}
else if (context.parser.canonicalize (canonical, "attribute", attr))
std::string canonical;
if (context.parser.canonicalize (canonical, "attribute", elements[1]))
{
if (elements.size () == 2)
{
value = ref.get (canonical);
return true;
}
}
else if (elements.size () == 3)
{
// <date>.year
// <date>.month
// <date>.day
// <date>.hour
// <date>.minute
// <date>.second
n.restore ();
// <tag>.<literal>
if (elements[1] == "tag")
{
value = ref.hasTag (elements[2]) ? elements[2] : "";
return true;
}
}
else if (elements.size () == 3)
{
// <annotation>.<N>.entry
// <annotation>.<N>.description
}
else if (elements.size () == 4)
{
// <annotation>.<N>.entry.year
// <annotation>.<N>.entry.month
// <annotation>.<N>.entry.day
// <annotation>.<N>.entry.hour
// <annotation>.<N>.entry.minute
// <annotation>.<N>.entry.second
}
}
}
}
// Delegate to the context-free version of DOM::get.

View file

@ -27,44 +27,71 @@
use strict;
use warnings;
use Test::More tests => 5;
use Test::More tests => 12;
# Ensure environment has no influence.
delete $ENV{'TASKDATA'};
delete $ENV{'TASKRC'};
use File::Basename;
my $ut = basename ($0);
my $rc = $ut . '.rc';
# Create the rc file.
if (open my $fh, '>', 'dom.rc')
if (open my $fh, '>', $rc)
{
print $fh "data.location=.\n",
"dateformat=YMD\n",
"dateformat.info=YMD\n";
"dateformat.info=YMD\n",
"confirmation=off\n";
close $fh;
}
# DOM reference to other task.
qx{../src/task rc:dom.rc add one due:20110901 2>&1};
qx{../src/task rc:dom.rc add two due:1.due 2>&1};
my $output = qx{../src/task rc:dom.rc 2 info 2>&1};
like ($output, qr/Due\s+20110901/, 'Found due date duplicated via dom');
qx{../src/task rc:$rc add one due:20110901 2>&1};
qx{../src/task rc:$rc add two due:1.due 2>&1};
my $output = qx{../src/task rc:$rc 2 info 2>&1};
like ($output, qr/Due\s+20110901/, "$ut: Found due date duplicated via dom");
# DOM reference to the current task.
qx{../src/task rc:dom.rc add three due:20110901 wait:due 2>&1};
$output = qx{../src/task rc:dom.rc 3 info 2>&1};
like ($output, qr/Waiting until\s+20110901/, 'Found wait date duplicated from due date');
qx{../src/task rc:$rc add three due:20110901 wait:due +tag1 +tag2 2>&1};
$output = qx{../src/task rc:$rc 3 info 2>&1};
like ($output, qr/Waiting until\s+20110901/, "$ut: Found wait date duplicated from due date");
# ID <--> UUID <--> ID round trip via DOM.
$output = qx{../src/task rc:dom.rc _get 1.uuid 2>&1};
like ($output, qr/^.{36}$/, 'DOM id --> uuid');
$output = qx{../src/task rc:$rc _get 1.uuid 2>&1};
like ($output, qr/^.{36}$/, "$ut: DOM id --> uuid");
my $uuid = chomp $output;
$output = qx{../src/task rc:dom.rc _get ${uuid}.id 2>&1};
like ($output, qr/^1$/, 'DOM uuid --> id');
$output = qx{../src/task rc:$rc _get ${uuid}.id 2>&1};
like ($output, qr/^1$/, "$ut: DOM uuid --> id");
# Failed DOM lookup returns blank.
$output = qx{../src/task rc:dom.rc _get 4.description 2>&1};
$output = qx{../src/task rc:$rc _get 4.description 2>&1};
like ($output, qr/^$/, "DOM 4.description --> ''");
# Test extended DOM support (2.4.0)
$output = qx{../src/task rc:$rc _get 3.tags 2>&1};
like ($output, qr/^tag1,tag2$/, "$ut: <id>.<tags>");
$output = qx{../src/task rc:$rc _get 3.tag.tag1 2>&1};
like ($output, qr/^tag1,tag2$/, "$ut: <id>.tag.tag1");
$output = qx{../src/task rc:$rc _get 3.tag.OVERDUE 2>&1};
like ($output, qr/^OVERDUE$/, "$ut: <id>.tag.<tag>");
$output = qx{../src/task rc:$rc _get 3.due.year 2>&1};
like ($output, qr/^\d{4}$/, "$ut: <id>.due.year");
qx{../src/task rc:$rc 3 annotate note 2>&1};
ok ($? == 0, "$ut: add annotation");
$output = qx{../src/task rc:$rc _get 3.annotation.1.entry 2>&1};
like ($output, qr/^\d+$/, "$ut: <id>.annotation.1.entry");
$output = qx{../src/task rc:$rc _get 3.annotation.1.description 2>&1};
like ($output, qr/^note$/, "$ut: <id>.annotation.1.description");
# Cleanup.
unlink qw(pending.data completed.data undo.data backlog.data dom.rc);
unlink qw(pending.data completed.data undo.data backlog.data), $rc;
exit 0;