Enhancement - Hooks

- Implemented API calls: task_get_due, task_get_end, task_get_entry,
  task_get_start, task_get_until and task_get_wait.
- Implemented unit tests for API calls.
- Implemented new Record::get_ulong method.
- Implemented unit tests for get_ulong.
This commit is contained in:
Paul Beckingham 2010-03-01 20:53:44 -05:00
parent 98ebe8b7cc
commit e5f7e18d56
10 changed files with 599 additions and 30 deletions

View file

@ -321,49 +321,117 @@ static int api_task_get_status (lua_State* L)
return 1; return 1;
} }
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_due (lua_State* L)
{
if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("due");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_entry (lua_State* L)
{
if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("entry");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_start (lua_State* L)
{
if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("start");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_end (lua_State* L)
{
if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("end");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
}
/* /*
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_due (id)
{
return task.due_date
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_entry (id)
{
return task.entry_date
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_start (id)
{
return task.start_date
}
////////////////////////////////////////////////////////////////////////////////
static int api_task_get_end (id)
{
return task.end_date
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static int api_task_get_recur (id) static int api_task_get_recur (id)
{ {
return task.recur return task.recur
} }
*/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static int api_task_get_until (id) static int api_task_get_until (lua_State* L)
{ {
return task.until_date if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("until");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static int api_task_get_wait (id) static int api_task_get_wait (lua_State* L)
{ {
return task.wait_date if (the_task != NULL)
{
unsigned int value = (unsigned int) the_task->get_ulong ("wait");
if (value)
{
lua_pushinteger (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
} }
/*
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
-- 'id' is the task id passed to the hook function. Date attributes are -- 'id' is the task id passed to the hook function. Date attributes are
-- expected as numeric epoch offsets. Tags and annotations are expected -- expected as numeric epoch offsets. Tags and annotations are expected
@ -496,14 +564,16 @@ void API::initialize ()
lua_pushcfunction (L, api_task_get_tags); lua_setglobal (L, "task_get_tags"); lua_pushcfunction (L, api_task_get_tags); lua_setglobal (L, "task_get_tags");
*/ */
lua_pushcfunction (L, api_task_get_status); lua_setglobal (L, "task_get_status"); lua_pushcfunction (L, api_task_get_status); lua_setglobal (L, "task_get_status");
/*
lua_pushcfunction (L, api_task_get_due); lua_setglobal (L, "task_get_due"); lua_pushcfunction (L, api_task_get_due); lua_setglobal (L, "task_get_due");
lua_pushcfunction (L, api_task_get_entry); lua_setglobal (L, "task_get_entry"); lua_pushcfunction (L, api_task_get_entry); lua_setglobal (L, "task_get_entry");
lua_pushcfunction (L, api_task_get_start); lua_setglobal (L, "task_get_start"); lua_pushcfunction (L, api_task_get_start); lua_setglobal (L, "task_get_start");
lua_pushcfunction (L, api_task_get_end); lua_setglobal (L, "task_get_end"); lua_pushcfunction (L, api_task_get_end); lua_setglobal (L, "task_get_end");
/*
lua_pushcfunction (L, api_task_get_recur); lua_setglobal (L, "task_get_recur"); lua_pushcfunction (L, api_task_get_recur); lua_setglobal (L, "task_get_recur");
*/
lua_pushcfunction (L, api_task_get_until); lua_setglobal (L, "task_get_until"); lua_pushcfunction (L, api_task_get_until); lua_setglobal (L, "task_get_until");
lua_pushcfunction (L, api_task_get_wait); lua_setglobal (L, "task_get_wait"); lua_pushcfunction (L, api_task_get_wait); lua_setglobal (L, "task_get_wait");
/*
lua_pushcfunction (L, api_task_set_description); lua_setglobal (L, "task_set_description"); lua_pushcfunction (L, api_task_set_description); lua_setglobal (L, "task_set_description");
lua_pushcfunction (L, api_task_set_annotations); lua_setglobal (L, "task_set_annotations"); lua_pushcfunction (L, api_task_set_annotations); lua_setglobal (L, "task_set_annotations");
lua_pushcfunction (L, api_task_set_project); lua_setglobal (L, "task_set_project"); lua_pushcfunction (L, api_task_set_project); lua_setglobal (L, "task_set_project");

View file

@ -157,6 +157,16 @@ int Record::get_int (const std::string& name) const
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////////////////
unsigned long Record::get_ulong (const std::string& name) const
{
Record::const_iterator i = this->find (name);
if (i != this->end ())
return strtoul (i->second.value ().c_str (), NULL, 10);
return 0;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Record::set (const std::string& name, const std::string& value) void Record::set (const std::string& name, const std::string& value)
{ {

View file

@ -47,6 +47,7 @@ public:
std::vector <Att> all (); std::vector <Att> all ();
const std::string get (const std::string&) const; const std::string get (const std::string&) const;
int get_int (const std::string&) const; int get_int (const std::string&) const;
unsigned long get_ulong (const std::string&) const;
void set (const std::string&, const std::string&); void set (const std::string&, const std::string&);
void set (const std::string&, int); void set (const std::string&, int);
void remove (const std::string&); void remove (const std::string&);

View file

@ -0,0 +1,80 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_due () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo due:eom};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_due');
}
else
{
pass ('Hook called task_get_due - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -0,0 +1,81 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_end () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo};
qx{../task rc:hook.rc do 1};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_end');
}
else
{
pass ('Hook called task_get_end - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -0,0 +1,80 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_entry () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_entry');
}
else
{
pass ('Hook called task_get_entry - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -0,0 +1,81 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_start () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo};
qx{../task rc:hook.rc start 1};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_start');
}
else
{
pass ('Hook called task_get_start - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -0,0 +1,80 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_until () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo due:tomorrow recur:weekly until:eoy};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_until');
}
else
{
pass ('Hook called task_get_until - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -0,0 +1,80 @@
#! /usr/bin/perl
################################################################################
## task - a command line task list manager.
##
## Copyright 2006 - 2010, Paul Beckingham.
## All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the
##
## Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor,
## Boston, MA
## 02110-1301
## USA
##
################################################################################
use strict;
use warnings;
use Test::More tests => 7;
# Create the rc file.
if (open my $fh, '>', 'hook.rc')
{
print $fh "data.location=.\n",
"hooks=on\n",
"hook.pre-display=" . $ENV{'PWD'} . "/hook:test\n";
close $fh;
ok (-r 'hook.rc', 'Created hook.rc');
}
if (open my $fh, '>', 'hook')
{
print $fh "function test ()\n",
" print ('<<' .. task_get_wait () .. '>>')\n",
" return 0, nil\n",
"end\n";
close $fh;
ok (-r 'hook', 'Created hook');
}
my $output = qx{../task rc:hook.rc version};
if ($output =~ /PUC-Rio/)
{
# Test the hook.
qx{../task rc:hook.rc add foo wait:tomorrow};
$output = qx{../task rc:hook.rc info 1};
like ($output, qr/^<<\d+>>$/ms, 'Hook called task_get_wait');
}
else
{
pass ('Hook called task_get_wait - skip: no Lua support');
}
# Cleanup.
unlink 'pending.data';
ok (!-r 'pending.data', 'Removed pending.data');
unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data');
unlink 'hook';
ok (!-r 'hook', 'Removed hook');
unlink 'hook.rc';
ok (!-r 'hook.rc', 'Removed hook.rc');
exit 0;

View file

@ -35,7 +35,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (19); UnitTest t (21);
// (blank) // (blank)
bool good = true; bool good = true;
@ -94,8 +94,14 @@ int main (int argc, char** argv)
t.is (record.composeF4 (), "[name:\"value\" one:\"1\"]\n", "Record::set"); t.is (record.composeF4 (), "[name:\"value\" one:\"1\"]\n", "Record::set");
t.is (record.get_int ("one"), 1, "Record::get_int"); t.is (record.get_int ("one"), 1, "Record::get_int");
// Record::get_ulong
record.set ("two", "4294967295");
t.is (record.composeF4 (), "[name:\"value\" one:\"1\" two:\"4294967295\"]\n", "Record::set");
t.is (record.get_int ("two"), 4294967295, "Record::get_ulong");
// Record::remove // Record::remove
record.remove ("one"); record.remove ("one");
record.remove ("two");
t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::remove"); t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::remove");
// Record::all // Record::all