From 6a7c66aa05feca13f8c1e66939bb7f563613fd3d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 7 Mar 2009 00:14:58 -0500 Subject: [PATCH] Unit Tests - color.disable color.pri config.obsolete - Added unit tests to cover automatic colorization by priority. - Added unit tests to cover automatic disabling of color when !isatty. - Added unit tests to cover display of unsupported configuration variable in the 'version' report. - Added support the '_forcecolor' configuration variable to allow the possibility of unit tests that test color support, yet redirect output to a file. This configuration variable will not be documented. --- src/Config.cpp | 7 ++- src/command.cpp | 19 +++++-- src/report.cpp | 54 +++++++++---------- src/task.cpp | 4 +- src/tests/{bug_hang.t => bug.hang.t} | 0 src/tests/{bug_period.t => bug.period.t} | 0 src/tests/{bug_sort.t => bug.sort.t} | 0 src/tests/color.disable.t | 58 +++++++++++++++++++++ src/tests/color.pri.t | 66 ++++++++++++++++++++++++ src/tests/config.obsolete.t | 57 ++++++++++++++++++++ 10 files changed, 231 insertions(+), 34 deletions(-) rename src/tests/{bug_hang.t => bug.hang.t} (100%) rename src/tests/{bug_period.t => bug.period.t} (100%) rename src/tests/{bug_sort.t => bug.sort.t} (100%) create mode 100755 src/tests/color.disable.t create mode 100755 src/tests/color.pri.t create mode 100755 src/tests/config.obsolete.t diff --git a/src/Config.cpp b/src/Config.cpp index fee5cf39a..64e07b04c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -36,9 +36,14 @@ #include "Config.h" //////////////////////////////////////////////////////////////////////////////// +// These are default (but overridable) reports. These entries are necessary +// because these three reports were converted from hard-coded reports to custom +// reports, and therefore need these config file entries. However, users are +// already used to seeing these three reports, but do not have these entries. +// The choice was a) make users edit their .taskrc files, b) write a .taskrc +// upgrade program to make the change, or c) this. Config::Config () { - // These are default (but overridable) reports. (*this)["report.long.columns"] = "id,project,priority,entry,start,due,age,tags,description"; (*this)["report.long.sort"] = "due+,priority-,project+"; (*this)["report.list.columns"] = "id,project,priority,due,active,age,description"; diff --git a/src/command.cpp b/src/command.cpp index 9bd940fb2..03e6e473d 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -113,7 +113,7 @@ std::string handleProjects (TDB& tdb, T& task, Config& conf) table.addColumn ("Project"); table.addColumn ("Tasks"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -316,7 +316,7 @@ std::string handleVersion (Config& conf) table.addColumn ("Config variable"); table.addColumn ("Value"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -345,9 +345,13 @@ std::string handleVersion (Config& conf) out << "Copyright (C) 2006 - 2009, P. Beckingham." << std::endl - << (conf.get ("color", true) ? Text::colorize (Text::bold, Text::nocolor, PACKAGE) : PACKAGE) + << ((conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) + ? Text::colorize (Text::bold, Text::nocolor, PACKAGE) + : PACKAGE) << " " - << (conf.get ("color", true) ? Text::colorize (Text::bold, Text::nocolor, VERSION) : VERSION) + << ((conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) + ? Text::colorize (Text::bold, Text::nocolor, VERSION) + : VERSION) << std::endl << disclaimer.render () << std::endl @@ -364,6 +368,11 @@ std::string handleVersion (Config& conf) "monthsperline nag newest next oldest project shadow.command shadow.file " "shadow.notify"; + // This configuration variable is supported, but not documented. It exists + // so that unit tests can force color to be on even when the output from task + // is redirected to a file, or stdout is not a tty. + recognized += " _forcecolor"; + std::vector unrecognized; foreach (i, all) { @@ -696,7 +705,7 @@ std::string handleColor (Config& conf) { std::stringstream out; - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { out << optionalBlankLine (conf) << "Foreground" << std::endl << " " diff --git a/src/report.cpp b/src/report.cpp index d2bb6e1e2..afa460821 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -172,7 +172,7 @@ std::string handleCompleted (TDB& tdb, T& task, Config& conf) table.addColumn ("Project"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -209,7 +209,7 @@ std::string handleCompleted (TDB& tdb, T& task, Config& conf) table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 2, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -261,7 +261,7 @@ std::string handleInfo (TDB& tdb, T& task, Config& conf) table.addColumn ("Name"); table.addColumn ("Value"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -359,7 +359,7 @@ std::string handleInfo (TDB& tdb, T& task, Config& conf) Date nextweek = now + 7 * 86400; imminent = dt < nextweek ? true : false; - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { if (overdue) table.setCellFg (row, 1, Text::colorCode (conf.get ("color.overdue", "red"))); @@ -513,7 +513,7 @@ std::string handleReportSummary (TDB& tdb, T& task, Config& conf) table.addColumn ("Complete"); table.addColumn ("0% 100%"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -550,7 +550,7 @@ std::string handleReportSummary (TDB& tdb, T& task, Config& conf) int completedBar = (c * barWidth) / (c + p); std::string bar; - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { bar = "\033[42m"; for (int b = 0; b < completedBar; ++b) @@ -654,7 +654,7 @@ std::string handleReportNext (TDB& tdb, T& task, Config& conf) table.addColumn ("Age"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -729,7 +729,7 @@ std::string handleReportNext (TDB& tdb, T& task, Config& conf) table.addCell (row, 5, age); table.addCell (row, 6, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -879,7 +879,7 @@ std::string handleReportHistory (TDB& tdb, T& task, Config& conf) table.addColumn ("Deleted"); table.addColumn ("Net"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -942,7 +942,7 @@ std::string handleReportHistory (TDB& tdb, T& task, Config& conf) } table.addCell (row, 5, net); - if (conf.get ("color", true) && net) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false) && net) table.setCellFg (row, 5, net > 0 ? Text::red: Text::green); } @@ -952,7 +952,7 @@ std::string handleReportHistory (TDB& tdb, T& task, Config& conf) row = table.addRow (); table.addCell (row, 1, "Average"); - if (conf.get ("color", true)) table.setRowFg (row, Text::bold); + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) table.setRowFg (row, Text::bold); table.addCell (row, 2, totalAdded / (table.rowCount () - 2)); table.addCell (row, 3, totalCompleted / (table.rowCount () - 2)); table.addCell (row, 4, totalDeleted / (table.rowCount () - 2)); @@ -1075,7 +1075,7 @@ std::string handleReportGHistory (TDB& tdb, T& task, Config& conf) table.addColumn ("Month"); table.addColumn ("Number Added/Completed/Deleted"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -1131,7 +1131,7 @@ std::string handleReportGHistory (TDB& tdb, T& task, Config& conf) unsigned int deletedBar = (widthOfBar * deletedGroup[i->first]) / maxLine; std::string bar = ""; - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { char number[24]; std::string aBar = ""; @@ -1190,7 +1190,7 @@ std::string handleReportGHistory (TDB& tdb, T& task, Config& conf) << table.render () << std::endl; - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) out << "Legend: " << Text::colorize (Text::black, Text::on_red, "added") << ", " @@ -1232,7 +1232,7 @@ std::string renderMonths ( table.addColumn ("Fr"); table.addColumn ("Sa"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (i + 1); table.setColumnUnderline (i + 2); @@ -1302,7 +1302,7 @@ std::string renderMonths ( table.addCell (row, thisCol, d); - if (conf.get ("color", true) && + if ((conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) && today.day () == d && today.month () == months.at (c) && today.year () == years.at (c)) @@ -1313,7 +1313,7 @@ std::string renderMonths ( { Date due (::atoi (it->getAttribute ("due").c_str ())); - if (conf.get ("color", true) && + if ((conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) && due.day () == d && due.month () == months.at (c) && due.year () == years.at (c)) @@ -1455,7 +1455,7 @@ std::string handleReportActive (TDB& tdb, T& task, Config& conf) table.addColumn ("Due"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -1511,7 +1511,7 @@ std::string handleReportActive (TDB& tdb, T& task, Config& conf) table.addCell (row, 3, due); table.addCell (row, 4, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -1576,7 +1576,7 @@ std::string handleReportOverdue (TDB& tdb, T& task, Config& conf) table.addColumn ("Due"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -1625,7 +1625,7 @@ std::string handleReportOverdue (TDB& tdb, T& task, Config& conf) table.addCell (row, 3, due); table.addCell (row, 4, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -1693,7 +1693,7 @@ std::string handleReportOldest (TDB& tdb, T& task, Config& conf) table.addColumn ("Age"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -1769,7 +1769,7 @@ std::string handleReportOldest (TDB& tdb, T& task, Config& conf) table.addCell (row, 5, age); table.addCell (row, 6, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -1840,7 +1840,7 @@ std::string handleReportNewest (TDB& tdb, T& task, Config& conf) table.addColumn ("Age"); table.addColumn ("Description"); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { table.setColumnUnderline (0); table.setColumnUnderline (1); @@ -1917,7 +1917,7 @@ std::string handleReportNewest (TDB& tdb, T& task, Config& conf) table.addCell (row, 5, age); table.addCell (row, 6, refTask.getDescription ()); - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (refTask.getAttribute ("fg")); Text::color bg = Text::colorCode (refTask.getAttribute ("bg")); @@ -2449,7 +2449,7 @@ std::string handleCustomReport ( // Common to all columns. // Add underline. - if (conf.get (std::string ("color"), true)) + if (conf.get (std::string ("color"), true) || conf.get (std::string ("_forcecolor"), false)) table.setColumnUnderline (columnCount); else table.setTableDashedUnderline (); @@ -2514,7 +2514,7 @@ std::string handleCustomReport ( } } - if (conf.get ("color", true)) + if (conf.get ("color", true) || conf.get (std::string ("_forcecolor"), false)) { Text::color fg = Text::colorCode (tasks[row].getAttribute ("fg")); Text::color bg = Text::colorCode (tasks[row].getAttribute ("bg")); diff --git a/src/task.cpp b/src/task.cpp index 727db1f16..04c73c3e6 100644 --- a/src/task.cpp +++ b/src/task.cpp @@ -290,7 +290,9 @@ int main (int argc, char** argv) if (!isatty (fileno (stdout))) { conf.set ("curses", "off"); - conf.set ("color", "off"); + + if (! conf.get (std::string ("_forcecolor"), false)) + conf.set ("color", "off"); } TDB tdb; diff --git a/src/tests/bug_hang.t b/src/tests/bug.hang.t similarity index 100% rename from src/tests/bug_hang.t rename to src/tests/bug.hang.t diff --git a/src/tests/bug_period.t b/src/tests/bug.period.t similarity index 100% rename from src/tests/bug_period.t rename to src/tests/bug.period.t diff --git a/src/tests/bug_sort.t b/src/tests/bug.sort.t similarity index 100% rename from src/tests/bug_sort.t rename to src/tests/bug.sort.t diff --git a/src/tests/color.disable.t b/src/tests/color.disable.t new file mode 100755 index 000000000..c4a0f4a58 --- /dev/null +++ b/src/tests/color.disable.t @@ -0,0 +1,58 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2009, 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 => 6; + +# Create the rc file. +if (open my $fh, '>', 'color.rc') +{ + print $fh "data.location=.\n", + "color.pri.H=red\n"; + close $fh; + ok (-r 'color.rc', 'Created color.rc'); +} + +# Test the add command. +qx{../task rc:color.rc add priority:H red}; +my $output = qx{../task rc:color.rc list}; + +like ($output, qr/red/, 'color.disable - found red'); +unlike ($output, qr/\033\[31m/, 'color.disable - no color red'); +unlike ($output, qr/\033\[0m/, 'color.disable - no color reset'); + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'color.rc'; +ok (!-r 'color.rc', 'Removed color.rc'); + +exit 0; + diff --git a/src/tests/color.pri.t b/src/tests/color.pri.t new file mode 100755 index 000000000..14a2ee42d --- /dev/null +++ b/src/tests/color.pri.t @@ -0,0 +1,66 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2009, 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, '>', 'color.rc') +{ + print $fh "data.location=.\n", + "color.pri.H=red\n", + "color.pri.M=green\n", + "color.pri.L=blue\n", + "color.pri.none=yellow\n", + "_forcecolor=1\n"; + close $fh; + ok (-r 'color.rc', 'Created color.rc'); +} + +# Test the add command. +qx{../task rc:color.rc add priority:H red}; +qx{../task rc:color.rc add priority:M green}; +qx{../task rc:color.rc add priority:L blue}; +qx{../task rc:color.rc add yellow}; +my $output = qx{../task rc:color.rc list}; + +like ($output, qr/ \033\[31m .* red .* \033\[0m /x, 'color.pri.H'); +like ($output, qr/ \033\[32m .* green .* \033\[0m /x, 'color.pri.M'); +like ($output, qr/ \033\[34m .* blue .* \033\[0m /x, 'color.pri.L'); +like ($output, qr/ \033\[33m .* yellow .* \033\[0m /x, 'color.pri.none'); + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'color.rc'; +ok (!-r 'color.rc', 'Removed color.rc'); + +exit 0; + diff --git a/src/tests/config.obsolete.t b/src/tests/config.obsolete.t new file mode 100755 index 000000000..7a5f34bc1 --- /dev/null +++ b/src/tests/config.obsolete.t @@ -0,0 +1,57 @@ +#! /usr/bin/perl +################################################################################ +## task - a command line task list manager. +## +## Copyright 2006 - 2009, 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 => 5; + +# Create the rc file. +if (open my $fh, '>', 'obsolete.rc') +{ + print $fh "data.location=.\n", + "foo=1\n"; + close $fh; + ok (-r 'obsolete.rc', 'Created obsolete.rc'); +} + +# Test the add command. +my $output = qx{../task rc:obsolete.rc version}; + +like ($output, qr/Your .taskrc file contains these unrecognized variables:\n/, + 'unsupported configuration variable'); +like ($output, qr/ foo\n/, 'unsupported configuration variable'); + +# Cleanup. +unlink 'pending.data'; +ok (!-r 'pending.data', 'Removed pending.data'); + +unlink 'obsolete.rc'; +ok (!-r 'obsolete.rc', 'Removed obsolete.rc'); + +exit 0; +