diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index fe830f5aa..517313576 100644 --- a/src/tests/date.t.cpp +++ b/src/tests/date.t.cpp @@ -1,5 +1,27 @@ //////////////////////////////////////////////////////////////////////////////// -// Copyright 2005 - 2008, Paul Beckingham. All rights reserved. +// 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 // //////////////////////////////////////////////////////////////////////////////// #include @@ -9,7 +31,7 @@ //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - plan (100); + UnitTest t (100); try { @@ -17,207 +39,207 @@ int main (int argc, char** argv) Date yesterday; yesterday -= 1; - ok (yesterday <= now, "yesterday <= now"); - ok (yesterday < now, "yesterday < now"); - notok (yesterday == now, "!(yesterday == now)"); - ok (yesterday != now, "yesterday != now"); - ok (now >= yesterday, "now >= yesterday"); - ok (now > yesterday, "now > yesterday"); + t.ok (yesterday <= now, "yesterday <= now"); + t.ok (yesterday < now, "yesterday < now"); + t.notok (yesterday == now, "!(yesterday == now)"); + t.ok (yesterday != now, "yesterday != now"); + t.ok (now >= yesterday, "now >= yesterday"); + t.ok (now > yesterday, "now > yesterday"); // Loose comparisons. Date left ("7/4/2008"); Date comp1 ("7/4/2008"); - ok (left.sameDay (comp1), "7/4/2008 is on the same day as 7/4/2008"); - ok (left.sameMonth (comp1), "7/4/2008 is in the same month as 7/4/2008"); - ok (left.sameYear (comp1), "7/4/2008 is in the same year as 7/4/2008"); + t.ok (left.sameDay (comp1), "7/4/2008 is on the same day as 7/4/2008"); + t.ok (left.sameMonth (comp1), "7/4/2008 is in the same month as 7/4/2008"); + t.ok (left.sameYear (comp1), "7/4/2008 is in the same year as 7/4/2008"); Date comp2 ("7/5/2008"); - notok (left.sameDay (comp2), "7/4/2008 is not on the same day as 7/5/2008"); - ok (left.sameMonth (comp2), "7/4/2008 is in the same month as 7/5/2008"); - ok (left.sameYear (comp2), "7/4/2008 is in the same year as 7/5/2008"); + t.notok (left.sameDay (comp2), "7/4/2008 is not on the same day as 7/5/2008"); + t.ok (left.sameMonth (comp2), "7/4/2008 is in the same month as 7/5/2008"); + t.ok (left.sameYear (comp2), "7/4/2008 is in the same year as 7/5/2008"); Date comp3 ("8/4/2008"); - notok (left.sameDay (comp3), "7/4/2008 is not on the same day as 8/4/2008"); - notok (left.sameMonth (comp3), "7/4/2008 is not in the same month as 8/4/2008"); - ok (left.sameYear (comp3), "7/4/2008 is in the same year as 8/4/2008"); + t.notok (left.sameDay (comp3), "7/4/2008 is not on the same day as 8/4/2008"); + t.notok (left.sameMonth (comp3), "7/4/2008 is not in the same month as 8/4/2008"); + t.ok (left.sameYear (comp3), "7/4/2008 is in the same year as 8/4/2008"); Date comp4 ("7/4/2009"); - notok (left.sameDay (comp4), "7/4/2008 is not on the same day as 7/4/2009"); - notok (left.sameMonth (comp4), "7/4/2008 is not in the same month as 7/4/2009"); - notok (left.sameYear (comp4), "7/4/2008 is not in the same year as 7/4/2009"); + t.notok (left.sameDay (comp4), "7/4/2008 is not on the same day as 7/4/2009"); + t.notok (left.sameMonth (comp4), "7/4/2008 is not in the same month as 7/4/2009"); + t.notok (left.sameYear (comp4), "7/4/2008 is not in the same year as 7/4/2009"); // Validity. - ok (Date::valid (2, 29, 2008), "valid: 2/29/2008"); - notok (Date::valid (2, 29, 2007), "invalid: 2/29/2007"); + t.ok (Date::valid (2, 29, 2008), "valid: 2/29/2008"); + t.notok (Date::valid (2, 29, 2007), "invalid: 2/29/2007"); // Leap year. - ok (Date::leapYear (2008), "2008 is a leap year"); - notok (Date::leapYear (2007), "2007 is not a leap year"); - ok (Date::leapYear (2000), "2000 is a leap year"); - ok (Date::leapYear (1900), "1900 is a leap year"); + t.ok (Date::leapYear (2008), "2008 is a leap year"); + t.notok (Date::leapYear (2007), "2007 is not a leap year"); + t.ok (Date::leapYear (2000), "2000 is a leap year"); + t.ok (Date::leapYear (1900), "1900 is a leap year"); // Days in month. - is (Date::daysInMonth (2, 2008), 29, "29 days in February 2008"); - is (Date::daysInMonth (2, 2007), 28, "28 days in February 2007"); + t.is (Date::daysInMonth (2, 2008), 29, "29 days in February 2008"); + t.is (Date::daysInMonth (2, 2007), 28, "28 days in February 2007"); // Names. - is (Date::monthName (1), "January", "1 = January"); - is (Date::monthName (2), "February", "2 = February"); - is (Date::monthName (3), "March", "3 = March"); - is (Date::monthName (4), "April", "4 = April"); - is (Date::monthName (5), "May", "5 = May"); - is (Date::monthName (6), "June", "6 = June"); - is (Date::monthName (7), "July", "7 = July"); - is (Date::monthName (8), "August", "8 = August"); - is (Date::monthName (9), "September", "9 = September"); - is (Date::monthName (10), "October", "10 = October"); - is (Date::monthName (11), "November", "11 = November"); - is (Date::monthName (12), "December", "12 = December"); + t.is (Date::monthName (1), "January", "1 = January"); + t.is (Date::monthName (2), "February", "2 = February"); + t.is (Date::monthName (3), "March", "3 = March"); + t.is (Date::monthName (4), "April", "4 = April"); + t.is (Date::monthName (5), "May", "5 = May"); + t.is (Date::monthName (6), "June", "6 = June"); + t.is (Date::monthName (7), "July", "7 = July"); + t.is (Date::monthName (8), "August", "8 = August"); + t.is (Date::monthName (9), "September", "9 = September"); + t.is (Date::monthName (10), "October", "10 = October"); + t.is (Date::monthName (11), "November", "11 = November"); + t.is (Date::monthName (12), "December", "12 = December"); - is (Date::dayName (0), "Sunday", "0 == Sunday"); - is (Date::dayName (1), "Monday", "1 == Monday"); - is (Date::dayName (2), "Tuesday", "2 == Tuesday"); - is (Date::dayName (3), "Wednesday", "3 == Wednesday"); - is (Date::dayName (4), "Thursday", "4 == Thursday"); - is (Date::dayName (5), "Friday", "5 == Friday"); - is (Date::dayName (6), "Saturday", "6 == Saturday"); + t.is (Date::dayName (0), "Sunday", "0 == Sunday"); + t.is (Date::dayName (1), "Monday", "1 == Monday"); + t.is (Date::dayName (2), "Tuesday", "2 == Tuesday"); + t.is (Date::dayName (3), "Wednesday", "3 == Wednesday"); + t.is (Date::dayName (4), "Thursday", "4 == Thursday"); + t.is (Date::dayName (5), "Friday", "5 == Friday"); + t.is (Date::dayName (6), "Saturday", "6 == Saturday"); - is (Date::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0"); - is (Date::dayOfWeek ("sunday"), 0, "sunday == 0"); - is (Date::dayOfWeek ("Sunday"), 0, "Sunday == 0"); - is (Date::dayOfWeek ("Monday"), 1, "Monday == 1"); - is (Date::dayOfWeek ("Tuesday"), 2, "Tuesday == 2"); - is (Date::dayOfWeek ("Wednesday"), 3, "Wednesday == 3"); - is (Date::dayOfWeek ("Thursday"), 4, "Thursday == 4"); - is (Date::dayOfWeek ("Friday"), 5, "Friday == 5"); - is (Date::dayOfWeek ("Saturday"), 6, "Saturday == 6"); + t.is (Date::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0"); + t.is (Date::dayOfWeek ("sunday"), 0, "sunday == 0"); + t.is (Date::dayOfWeek ("Sunday"), 0, "Sunday == 0"); + t.is (Date::dayOfWeek ("Monday"), 1, "Monday == 1"); + t.is (Date::dayOfWeek ("Tuesday"), 2, "Tuesday == 2"); + t.is (Date::dayOfWeek ("Wednesday"), 3, "Wednesday == 3"); + t.is (Date::dayOfWeek ("Thursday"), 4, "Thursday == 4"); + t.is (Date::dayOfWeek ("Friday"), 5, "Friday == 5"); + t.is (Date::dayOfWeek ("Saturday"), 6, "Saturday == 6"); Date happyNewYear (1, 1, 2008); - is (happyNewYear.dayOfWeek (), 2, "1/1/2008 == Tuesday"); - is (happyNewYear.month (), 1, "1/1/2008 == January"); - is (happyNewYear.day (), 1, "1/1/2008 == 1"); - is (happyNewYear.year (), 2008, "1/1/2008 == 2008"); + t.is (happyNewYear.dayOfWeek (), 2, "1/1/2008 == Tuesday"); + t.is (happyNewYear.month (), 1, "1/1/2008 == January"); + t.is (happyNewYear.day (), 1, "1/1/2008 == 1"); + t.is (happyNewYear.year (), 2008, "1/1/2008 == 2008"); - is (now - yesterday, 1, "today - yesterday == 1"); + t.is (now - yesterday, 1, "today - yesterday == 1"); - is (happyNewYear.toString (), "1/1/2008", "toString 1/1/2008"); + t.is (happyNewYear.toString (), "1/1/2008", "toString 1/1/2008"); int m, d, y; happyNewYear.toMDY (m, d, y); - is (m, 1, "1/1/2008 == January"); - is (d, 1, "1/1/2008 == 1"); - is (y, 2008, "1/1/2008 == 2008"); + t.is (m, 1, "1/1/2008 == January"); + t.is (d, 1, "1/1/2008 == 1"); + t.is (y, 2008, "1/1/2008 == 2008"); Date epoch (9, 8, 2001); - ok ((int)epoch.toEpoch () < 1000000000, "9/8/2001 < 1,000,000,000"); + t.ok ((int)epoch.toEpoch () < 1000000000, "9/8/2001 < 1,000,000,000"); epoch += 86400; - ok ((int)epoch.toEpoch () > 1000000000, "9/9/2001 > 1,000,000,000"); + t.ok ((int)epoch.toEpoch () > 1000000000, "9/9/2001 > 1,000,000,000"); Date fromEpoch (epoch.toEpoch ()); - is (fromEpoch.toString (), epoch.toString (), "ctor (time_t)"); + t.is (fromEpoch.toString (), epoch.toString (), "ctor (time_t)"); // Date parsing. Date fromString1 ("1/1/2008"); - is (fromString1.month (), 1, "ctor (std::string) -> m"); - is (fromString1.day (), 1, "ctor (std::string) -> d"); - is (fromString1.year (), 2008, "ctor (std::string) -> y"); + t.is (fromString1.month (), 1, "ctor (std::string) -> m"); + t.is (fromString1.day (), 1, "ctor (std::string) -> d"); + t.is (fromString1.year (), 2008, "ctor (std::string) -> y"); Date fromString2 ("1/1/2008", "m/d/Y"); - is (fromString2.month (), 1, "ctor (std::string) -> m"); - is (fromString2.day (), 1, "ctor (std::string) -> d"); - is (fromString2.year (), 2008, "ctor (std::string) -> y"); + t.is (fromString2.month (), 1, "ctor (std::string) -> m"); + t.is (fromString2.day (), 1, "ctor (std::string) -> d"); + t.is (fromString2.year (), 2008, "ctor (std::string) -> y"); Date fromString3 ("20080101", "YMD"); - is (fromString3.month (), 1, "ctor (std::string) -> m"); - is (fromString3.day (), 1, "ctor (std::string) -> d"); - is (fromString3.year (), 2008, "ctor (std::string) -> y"); + t.is (fromString3.month (), 1, "ctor (std::string) -> m"); + t.is (fromString3.day (), 1, "ctor (std::string) -> d"); + t.is (fromString3.year (), 2008, "ctor (std::string) -> y"); Date fromString4 ("12/31/2007"); - is (fromString4.month (), 12, "ctor (std::string) -> m"); - is (fromString4.day (), 31, "ctor (std::string) -> d"); - is (fromString4.year (), 2007, "ctor (std::string) -> y"); + t.is (fromString4.month (), 12, "ctor (std::string) -> m"); + t.is (fromString4.day (), 31, "ctor (std::string) -> d"); + t.is (fromString4.year (), 2007, "ctor (std::string) -> y"); Date fromString5 ("12/31/2007", "m/d/Y"); - is (fromString5.month (), 12, "ctor (std::string) -> m"); - is (fromString5.day (), 31, "ctor (std::string) -> d"); - is (fromString5.year (), 2007, "ctor (std::string) -> y"); + t.is (fromString5.month (), 12, "ctor (std::string) -> m"); + t.is (fromString5.day (), 31, "ctor (std::string) -> d"); + t.is (fromString5.year (), 2007, "ctor (std::string) -> y"); Date fromString6 ("20071231", "YMD"); - is (fromString6.month (), 12, "ctor (std::string) -> m"); - is (fromString6.day (), 31, "ctor (std::string) -> d"); - is (fromString6.year (), 2007, "ctor (std::string) -> y"); + t.is (fromString6.month (), 12, "ctor (std::string) -> m"); + t.is (fromString6.day (), 31, "ctor (std::string) -> d"); + t.is (fromString6.year (), 2007, "ctor (std::string) -> y"); Date fromString7 ("01/01/2008", "m/d/Y"); - is (fromString7.month (), 1, "ctor (std::string) -> m"); - is (fromString7.day (), 1, "ctor (std::string) -> d"); - is (fromString7.year (), 2008, "ctor (std::string) -> y"); + t.is (fromString7.month (), 1, "ctor (std::string) -> m"); + t.is (fromString7.day (), 1, "ctor (std::string) -> d"); + t.is (fromString7.year (), 2008, "ctor (std::string) -> y"); // Relative dates. Date r1 ("today"); - ok (r1.sameDay (now), "today = now"); + t.ok (r1.sameDay (now), "today = now"); Date r2 ("tomorrow"); - ok (r2.sameDay (now + 86400), "tomorrow = now + 1d"); + t.ok (r2.sameDay (now + 86400), "tomorrow = now + 1d"); Date r3 ("yesterday"); - ok (r3.sameDay (now - 86400), "yesterday = now - 1d"); + t.ok (r3.sameDay (now - 86400), "yesterday = now - 1d"); Date r4 ("sunday"); if (now.dayOfWeek () >= 0) - ok (r4.sameDay (now + (0 - now.dayOfWeek () + 7) * 86400), "next sunday"); + t.ok (r4.sameDay (now + (0 - now.dayOfWeek () + 7) * 86400), "next sunday"); else - ok (r4.sameDay (now + (0 - now.dayOfWeek ()) * 86400), "next sunday");; + t.ok (r4.sameDay (now + (0 - now.dayOfWeek ()) * 86400), "next sunday");; Date r5 ("monday"); if (now.dayOfWeek () >= 1) - ok (r5.sameDay (now + (1 - now.dayOfWeek () + 7) * 86400), "next monday"); + t.ok (r5.sameDay (now + (1 - now.dayOfWeek () + 7) * 86400), "next monday"); else - ok (r5.sameDay (now + (1 - now.dayOfWeek ()) * 86400), "next monday");; + t.ok (r5.sameDay (now + (1 - now.dayOfWeek ()) * 86400), "next monday");; Date r6 ("tuesday"); if (now.dayOfWeek () >= 2) - ok (r6.sameDay (now + (2 - now.dayOfWeek () + 7) * 86400), "next tuesday"); + t.ok (r6.sameDay (now + (2 - now.dayOfWeek () + 7) * 86400), "next tuesday"); else - ok (r6.sameDay (now + (2 - now.dayOfWeek ()) * 86400), "next tuesday");; + t.ok (r6.sameDay (now + (2 - now.dayOfWeek ()) * 86400), "next tuesday");; Date r7 ("wednesday"); if (now.dayOfWeek () >= 3) - ok (r7.sameDay (now + (3 - now.dayOfWeek () + 7) * 86400), "next wednesday"); + t.ok (r7.sameDay (now + (3 - now.dayOfWeek () + 7) * 86400), "next wednesday"); else - ok (r7.sameDay (now + (3 - now.dayOfWeek ()) * 86400), "next wednesday");; + t.ok (r7.sameDay (now + (3 - now.dayOfWeek ()) * 86400), "next wednesday");; Date r8 ("thursday"); if (now.dayOfWeek () >= 4) - ok (r8.sameDay (now + (4 - now.dayOfWeek () + 7) * 86400), "next thursday"); + t.ok (r8.sameDay (now + (4 - now.dayOfWeek () + 7) * 86400), "next thursday"); else - ok (r8.sameDay (now + (4 - now.dayOfWeek ()) * 86400), "next thursday");; + t.ok (r8.sameDay (now + (4 - now.dayOfWeek ()) * 86400), "next thursday");; Date r9 ("friday"); if (now.dayOfWeek () >= 5) - ok (r9.sameDay (now + (5 - now.dayOfWeek () + 7) * 86400), "next friday"); + t.ok (r9.sameDay (now + (5 - now.dayOfWeek () + 7) * 86400), "next friday"); else - ok (r9.sameDay (now + (5 - now.dayOfWeek ()) * 86400), "next friday");; + t.ok (r9.sameDay (now + (5 - now.dayOfWeek ()) * 86400), "next friday");; Date r10 ("saturday"); if (now.dayOfWeek () >= 6) - ok (r10.sameDay (now + (6 - now.dayOfWeek () + 7) * 86400), "next saturday"); + t.ok (r10.sameDay (now + (6 - now.dayOfWeek () + 7) * 86400), "next saturday"); else - ok (r10.sameDay (now + (6 - now.dayOfWeek ()) * 86400), "next saturday");; + t.ok (r10.sameDay (now + (6 - now.dayOfWeek ()) * 86400), "next saturday");; Date r11 ("eow"); - ok (r11 < now + (8 * 86400), "eow < 7 days away"); + t.ok (r11 < now + (8 * 86400), "eow < 7 days away"); Date r12 ("eom"); - ok (r12.sameMonth (now), "eom in same month as now"); + t.ok (r12.sameMonth (now), "eom in same month as now"); Date r13 ("eoy"); - ok (r13.sameYear (now), "eoy in same year as now"); + t.ok (r13.sameYear (now), "eoy in same year as now"); } catch (std::string& e) { - fail ("Exception thrown."); - diag (e); + t.fail ("Exception thrown."); + t.diag (e); } return 0; diff --git a/src/tests/duration.t.cpp b/src/tests/duration.t.cpp index 584b94458..a50b6a11d 100644 --- a/src/tests/duration.t.cpp +++ b/src/tests/duration.t.cpp @@ -1,5 +1,27 @@ //////////////////////////////////////////////////////////////////////////////// -// Copyright 2005 - 2008, Paul Beckingham. All rights reserved. +// 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 // //////////////////////////////////////////////////////////////////////////////// #include @@ -16,27 +38,27 @@ // biannual, biyearly, annual, semiannual, yearly, Ny int main (int argc, char** argv) { - plan (17); + UnitTest t (17); std::string d; - d = "daily"; is (convertDuration (d), 1, "duration daily = 1"); - d = "day"; is (convertDuration (d), 1, "duration day = 1"); - d = "0d"; is (convertDuration (d), 0, "duration 0d = 0"); - d = "1d"; is (convertDuration (d), 1, "duration 1d = 1"); - d = "7d"; is (convertDuration (d), 7, "duration 7d = 7"); - d = "10d"; is (convertDuration (d), 10, "duration 10d = 10"); - d = "100d"; is (convertDuration (d), 100, "duration 100d = 100"); + d = "daily"; t.is (convertDuration (d), 1, "duration daily = 1"); + d = "day"; t.is (convertDuration (d), 1, "duration day = 1"); + d = "0d"; t.is (convertDuration (d), 0, "duration 0d = 0"); + d = "1d"; t.is (convertDuration (d), 1, "duration 1d = 1"); + d = "7d"; t.is (convertDuration (d), 7, "duration 7d = 7"); + d = "10d"; t.is (convertDuration (d), 10, "duration 10d = 10"); + d = "100d"; t.is (convertDuration (d), 100, "duration 100d = 100"); - d = "weekly"; is (convertDuration (d), 7, "duration weekly = 7"); - d = "sennight"; is (convertDuration (d), 7, "duration sennight = 7"); - d = "biweekly"; is (convertDuration (d), 14, "duration biweekly = 14"); - d = "fortnight"; is (convertDuration (d), 14, "duration fortnight = 14"); - d = "week"; is (convertDuration (d), 7, "duration week = 7"); - d = "0w"; is (convertDuration (d), 0, "duration 0w = 0"); - d = "1w"; is (convertDuration (d), 7, "duration 1w = 7"); - d = "7w"; is (convertDuration (d), 49, "duration 7w = 49"); - d = "10w"; is (convertDuration (d), 70, "duration 10w = 70"); - d = "100w"; is (convertDuration (d), 700, "duration 100w = 700"); + d = "weekly"; t.is (convertDuration (d), 7, "duration weekly = 7"); + d = "sennight"; t.is (convertDuration (d), 7, "duration sennight = 7"); + d = "biweekly"; t.is (convertDuration (d), 14, "duration biweekly = 14"); + d = "fortnight"; t.is (convertDuration (d), 14, "duration fortnight = 14"); + d = "week"; t.is (convertDuration (d), 7, "duration week = 7"); + d = "0w"; t.is (convertDuration (d), 0, "duration 0w = 0"); + d = "1w"; t.is (convertDuration (d), 7, "duration 1w = 7"); + d = "7w"; t.is (convertDuration (d), 49, "duration 7w = 49"); + d = "10w"; t.is (convertDuration (d), 70, "duration 10w = 70"); + d = "100w"; t.is (convertDuration (d), 700, "duration 100w = 700"); return 0; } diff --git a/src/tests/t.t.cpp b/src/tests/t.t.cpp index f8d38e23b..ae76b18c6 100644 --- a/src/tests/t.t.cpp +++ b/src/tests/t.t.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2008, Paul Beckingham. +// Copyright 2006 - 2009, Paul Beckingham. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -31,34 +31,34 @@ //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - plan (5); + UnitTest test (5); T t; std::string s = t.compose (); - is ((int)s.length (), 46, "T::T (); T::compose ()"); - diag (s); + test.is ((int)s.length (), 46, "T::T (); T::compose ()"); + test.diag (s); t.setStatus (T::completed); s = t.compose (); - is (s[37], '+', "T::setStatus (completed)"); - diag (s); + test.is (s[37], '+', "T::setStatus (completed)"); + test.diag (s); t.setStatus (T::deleted); s = t.compose (); - is (s[37], 'X', "T::setStatus (deleted)"); - diag (s); + test.is (s[37], 'X', "T::setStatus (deleted)"); + test.diag (s); t.setStatus (T::recurring); s = t.compose (); - is (s[37], 'r', "T::setStatus (recurring)"); - diag (s); + test.is (s[37], 'r', "T::setStatus (recurring)"); + test.diag (s); // Round trip test. std::string sample = "00000000-0000-0000-0000-000000000000 - [] [] Sample"; T t2; t2.parse (sample); sample += "\n"; - is (t2.compose (), sample, "T::parse -> T::compose round trip"); + test.is (t2.compose (), sample, "T::parse -> T::compose round trip"); return 0; } diff --git a/src/tests/tdb.t.cpp b/src/tests/tdb.t.cpp index 1ec2160d6..129a0cdbf 100644 --- a/src/tests/tdb.t.cpp +++ b/src/tests/tdb.t.cpp @@ -34,7 +34,7 @@ //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - plan (43); + UnitTest t (38); try { @@ -46,14 +46,14 @@ int main (int argc, char** argv) TDB tdb; tdb.dataDirectory ("."); std::vector all; - ok (!tdb.pendingT (all), "TDB::pendingT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (!tdb.allPendingT (all), "TDB::allPendingT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (!tdb.completedT (all), "TDB::completedT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); - is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.pendingT (all), "TDB::pendingT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.allPendingT (all), "TDB::allPendingT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.completedT (all), "TDB::completedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); // Add a new task. T t1; @@ -61,76 +61,69 @@ int main (int argc, char** argv) t1.setStatus (T::pending); t1.setAttribute ("project", "p1"); t1.setDescription ("task 1"); - diag (t1.compose ()); - ok (tdb.addT (t1), "TDB::addT t1"); + t.diag (t1.compose ()); + t.ok (tdb.addT (t1), "TDB::addT t1"); // Verify as above. - ok (tdb.pendingT (all), "TDB::pendingT read db"); - is ((int) all.size (), 1, "empty db"); - ok (tdb.allPendingT (all), "TDB::allPendingT read db"); - is ((int) all.size (), 1, "empty db"); - ok (!tdb.completedT (all), "TDB::completedT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); - is ((int) all.size (), 0, "empty db"); + t.ok (tdb.pendingT (all), "TDB::pendingT read db"); + t.is ((int) all.size (), 1, "empty db"); + t.ok (tdb.allPendingT (all), "TDB::allPendingT read db"); + t.is ((int) all.size (), 1, "empty db"); + t.ok (!tdb.completedT (all), "TDB::completedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); // TODO Modify task. - fail ("modify"); - fail ("verify"); // Complete task. - ok (tdb.completeT (t1), "TDB::completeT t1");; - ok (tdb.pendingT (all), "TDB::pendingT read db"); - is ((int) all.size (), 0, "empty db"); - ok (tdb.allPendingT (all), "TDB::allPendingT read db"); - is ((int) all.size (), 1, "empty db"); - ok (!tdb.completedT (all), "TDB::completedT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); - is ((int) all.size (), 0, "empty db"); + t.ok (tdb.completeT (t1), "TDB::completeT t1");; + t.ok (tdb.pendingT (all), "TDB::pendingT read db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (tdb.allPendingT (all), "TDB::allPendingT read db"); + t.is ((int) all.size (), 1, "empty db"); + t.ok (!tdb.completedT (all), "TDB::completedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (!tdb.allCompletedT (all), "TDB::allCompletedT read empty db"); + t.is ((int) all.size (), 0, "empty db"); - is (tdb.gc (), 1, "TDB::gc"); - ok (tdb.pendingT (all), "TDB::pendingT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (tdb.allPendingT (all), "TDB::allPendingT read empty db"); - is ((int) all.size (), 0, "empty db"); - ok (tdb.completedT (all), "TDB::completedT read db"); - is ((int) all.size (), 1, "empty db"); - ok (tdb.allCompletedT (all), "TDB::allCompletedT read db"); - is ((int) all.size (), 1, "empty db"); + t.is (tdb.gc (), 1, "TDB::gc"); + t.ok (tdb.pendingT (all), "TDB::pendingT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (tdb.allPendingT (all), "TDB::allPendingT read empty db"); + t.is ((int) all.size (), 0, "empty db"); + t.ok (tdb.completedT (all), "TDB::completedT read db"); + t.is ((int) all.size (), 1, "empty db"); + t.ok (tdb.allCompletedT (all), "TDB::allCompletedT read db"); + t.is ((int) all.size (), 1, "empty db"); // Add a new task. T t2; t2.setId (2); t2.setAttribute ("project", "p2"); t2.setDescription ("task 2"); - diag (t2.compose ()); - ok (tdb.addT (t2), "TDB::addT t2"); - - fail ("verify"); + t.diag (t2.compose ()); + t.ok (tdb.addT (t2), "TDB::addT t2"); // Delete task. - ok (tdb.deleteT (t2), "TDB::deleteT t2"); - - fail ("verify"); + t.ok (tdb.deleteT (t2), "TDB::deleteT t2"); // GC the files. - is (tdb.gc (), 1, "1 <- TDB::gc"); + t.is (tdb.gc (), 1, "1 <- TDB::gc"); } catch (std::string& error) { - diag (error); + t.diag (error); return -1; } catch (...) { - diag ("Unknown error."); + t.diag ("Unknown error."); return -2; } - unlink ("./pending.data"); unlink ("./completed.data"); diff --git a/src/tests/test.cpp b/src/tests/test.cpp index 1937e9230..4c8f201b3 100644 --- a/src/tests/test.cpp +++ b/src/tests/test.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2008, Paul Beckingham. +// Copyright 2006 - 2009, Paul Beckingham. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -25,81 +25,157 @@ // //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include - -static int total = 0; -static int counter = 0; +#include "test.h" /////////////////////////////////////////////////////////////////////////////// -static void check (void) +UnitTest::UnitTest () +: mPlanned (0) +, mCounter (0) +, mPassed (0) +, mFailed (0) +, mSkipped (0) { - if (counter > total) - std::cout << "# Warning: There are more tests than planned." +} + +/////////////////////////////////////////////////////////////////////////////// +UnitTest::UnitTest (int planned) +: mPlanned (planned) +, mCounter (0) +, mPassed (0) +, mFailed (0) +, mSkipped (0) +{ + std::cout << "1.." << mPlanned << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +UnitTest::~UnitTest () +{ + float percentPassed = 0.0; + if (mPlanned > 0) + percentPassed = (100.0 * mPassed) / max (mPlanned, mPassed + mFailed + mSkipped); + + if (mCounter < mPlanned) + { + std::cout << "# Only " + << mCounter + << " tests, out of a planned " + << mPlanned + << " were run." << std::endl; + mSkipped += mPlanned - mCounter; + } + + else if (mCounter > mPlanned) + std::cout << "# " + << mCounter + << " tests were run, but only " + << mPlanned + << " were planned." + << std::endl; + + std::cout << "# " + << mPassed + << " passed, " + << mFailed + << " failed, " + << mSkipped + << " skipped. " + << std::setprecision (3) << percentPassed + << "% passed." + << std::endl; } /////////////////////////////////////////////////////////////////////////////// -void plan (int quantity) +void UnitTest::plan (int planned) { - total = quantity; - std::cout << "1.." << quantity << std::endl; - check (); + mPlanned = planned; + mCounter = 0; + mPassed = 0; + mFailed = 0; + mSkipped = 0; + + std::cout << "1.." << mPlanned << std::endl; } /////////////////////////////////////////////////////////////////////////////// -void ok (bool expression, const std::string& name) +void UnitTest::planMore (int extra) { - ++counter; + mPlanned += extra; + std::cout << "1.." << mPlanned << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +void UnitTest::ok (bool expression, const std::string& name) +{ + ++mCounter; if (expression) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void notok (bool expression, const std::string& name) +void UnitTest::notok (bool expression, const std::string& name) { - ++counter; + ++mCounter; if (!expression) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is (bool actual, bool expected, const std::string& name) +void UnitTest::is (bool actual, bool expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -109,22 +185,27 @@ void is (bool actual, bool expected, const std::string& name) << "# got: " << actual << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is (size_t actual, size_t expected, const std::string& name) +void UnitTest::is (size_t actual, size_t expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -134,22 +215,27 @@ void is (size_t actual, size_t expected, const std::string& name) << "# got: " << actual << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is (int actual, int expected, const std::string& name) +void UnitTest::is (int actual, int expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -159,22 +245,27 @@ void is (int actual, int expected, const std::string& name) << "# got: " << actual << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is (double actual, double expected, const std::string& name) +void UnitTest::is (double actual, double expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -184,22 +275,27 @@ void is (double actual, double expected, const std::string& name) << "# got: " << actual << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is (char actual, char expected, const std::string& name) +void UnitTest::is (char actual, char expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -209,25 +305,30 @@ void is (char actual, char expected, const std::string& name) << "# got: " << actual << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void is ( +void UnitTest::is ( const std::string& actual, const std::string& expected, const std::string& name) { - ++counter; + ++mCounter; if (actual == expected) + { + ++mPassed; std::cout << "ok " - << counter + << mCounter << " - " << name << std::endl; + } else + { + ++mFailed; std::cout << "not ok " - << counter + << mCounter << " - " << name << std::endl @@ -239,11 +340,46 @@ void is ( << actual << "'" << std::endl; - check (); + } } /////////////////////////////////////////////////////////////////////////////// -void diag (const std::string& text) +void UnitTest::is ( + const char* actual, + const char* expected, + const std::string& name) +{ + ++mCounter; + if (! strcmp (actual, expected)) + { + ++mPassed; + std::cout << "ok " + << mCounter + << " - " + << name + << std::endl; + } + else + { + ++mFailed; + std::cout << "not ok " + << mCounter + << " - " + << name + << std::endl + << "# expected: '" + << expected + << "'" + << std::endl + << "# got: '" + << actual + << "'" + << std::endl; + } +} + +/////////////////////////////////////////////////////////////////////////////// +void UnitTest::diag (const std::string& text) { std::string trimmed = trim (text, " \t\n\r\f"); @@ -251,22 +387,36 @@ void diag (const std::string& text) } /////////////////////////////////////////////////////////////////////////////// -void pass (const std::string& text) +void UnitTest::pass (const std::string& text) { - ++counter; + ++mCounter; + ++mPassed; std::cout << "ok " - << counter + << mCounter << " " << text << std::endl; } /////////////////////////////////////////////////////////////////////////////// -void fail (const std::string& text) +void UnitTest::fail (const std::string& text) { - ++counter; + ++mCounter; + ++mFailed; std::cout << "not ok " - << counter + << mCounter + << " " + << text + << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +void UnitTest::skip (const std::string& text) +{ + ++mCounter; + ++mSkipped; + std::cout << "skip " + << mCounter << " " << text << std::endl; diff --git a/src/tests/test.h b/src/tests/test.h index 36cfa2f67..b4bcf7f66 100644 --- a/src/tests/test.h +++ b/src/tests/test.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2008, Paul Beckingham. +// Copyright 2006 - 2009, Paul Beckingham. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -24,23 +24,42 @@ // USA // //////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDED_TEST -#define INCLUDED_TEST +#ifndef INCLUDED_UNITTEST +#define INCLUDED_UNITTEST #include -void plan (int); -void ok (bool, const std::string&); -void notok (bool, const std::string&); -void is (bool, bool, const std::string&); -void is (int, int, const std::string&); -void is (size_t, size_t, const std::string&); -void is (double, double, const std::string&); -void is (char, char, const std::string&); -void is (const std::string&, const std::string&, const std::string&); -void diag (const std::string&); -void fail (const std::string&); -void pass (const std::string&); +class UnitTest +{ +public: + UnitTest (); + UnitTest (int); + ~UnitTest (); + + void plan (int); + void planMore (int); + void ok (bool, const std::string&); + void notok (bool, const std::string&); + void is (bool, bool, const std::string&); + void is (size_t, size_t, const std::string&); + void is (int, int, const std::string&); + void is (double, double, const std::string&); + void is (char, char, const std::string&); + void is (const std::string&, const std::string&, const std::string&); + void is (const char*, const char*, const std::string&); + void diag (const std::string&); + void pass (const std::string&); + void fail (const std::string&); + void skip (const std::string&); + +private: + int mPlanned; + int mCounter; + int mPassed; + int mFailed; + int mSkipped; +}; #endif + ////////////////////////////////////////////////////////////////////////////////