From e5f7e18d56460e8d14eef025e795f27ff0cf99c1 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 1 Mar 2010 20:53:44 -0500 Subject: [PATCH] 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. --- src/API.cpp | 128 +++++++++++++++++++++------- src/Record.cpp | 10 +++ src/Record.h | 1 + src/tests/hook.api.task_get_due.t | 80 +++++++++++++++++ src/tests/hook.api.task_get_end.t | 81 ++++++++++++++++++ src/tests/hook.api.task_get_entry.t | 80 +++++++++++++++++ src/tests/hook.api.task_get_start.t | 81 ++++++++++++++++++ src/tests/hook.api.task_get_until.t | 80 +++++++++++++++++ src/tests/hook.api.task_get_wait.t | 80 +++++++++++++++++ src/tests/record.t.cpp | 8 +- 10 files changed, 599 insertions(+), 30 deletions(-) create mode 100755 src/tests/hook.api.task_get_due.t create mode 100755 src/tests/hook.api.task_get_end.t create mode 100755 src/tests/hook.api.task_get_entry.t create mode 100755 src/tests/hook.api.task_get_start.t create mode 100755 src/tests/hook.api.task_get_until.t create mode 100755 src/tests/hook.api.task_get_wait.t diff --git a/src/API.cpp b/src/API.cpp index 504af8574..26bf558c0 100644 --- a/src/API.cpp +++ b/src/API.cpp @@ -321,49 +321,117 @@ static int api_task_get_status (lua_State* L) 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) { 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 -- 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_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_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_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_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_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_project); lua_setglobal (L, "task_set_project"); diff --git a/src/Record.cpp b/src/Record.cpp index 5605e594d..21dc8299f 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -157,6 +157,16 @@ int Record::get_int (const std::string& name) const 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) { diff --git a/src/Record.h b/src/Record.h index a450baa61..8d740c06c 100644 --- a/src/Record.h +++ b/src/Record.h @@ -47,6 +47,7 @@ public: std::vector all (); const std::string get (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&, int); void remove (const std::string&); diff --git a/src/tests/hook.api.task_get_due.t b/src/tests/hook.api.task_get_due.t new file mode 100755 index 000000000..b71fdbb33 --- /dev/null +++ b/src/tests/hook.api.task_get_due.t @@ -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; + diff --git a/src/tests/hook.api.task_get_end.t b/src/tests/hook.api.task_get_end.t new file mode 100755 index 000000000..10e47d80a --- /dev/null +++ b/src/tests/hook.api.task_get_end.t @@ -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; + diff --git a/src/tests/hook.api.task_get_entry.t b/src/tests/hook.api.task_get_entry.t new file mode 100755 index 000000000..6d0dccace --- /dev/null +++ b/src/tests/hook.api.task_get_entry.t @@ -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; + diff --git a/src/tests/hook.api.task_get_start.t b/src/tests/hook.api.task_get_start.t new file mode 100755 index 000000000..10d2fb95b --- /dev/null +++ b/src/tests/hook.api.task_get_start.t @@ -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; + diff --git a/src/tests/hook.api.task_get_until.t b/src/tests/hook.api.task_get_until.t new file mode 100755 index 000000000..e75975577 --- /dev/null +++ b/src/tests/hook.api.task_get_until.t @@ -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; + diff --git a/src/tests/hook.api.task_get_wait.t b/src/tests/hook.api.task_get_wait.t new file mode 100755 index 000000000..5b99a5ab6 --- /dev/null +++ b/src/tests/hook.api.task_get_wait.t @@ -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; + diff --git a/src/tests/record.t.cpp b/src/tests/record.t.cpp index 8d296ede8..4898e1b81 100644 --- a/src/tests/record.t.cpp +++ b/src/tests/record.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (19); + UnitTest t (21); // (blank) 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.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 ("one"); + record.remove ("two"); t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::remove"); // Record::all