From b887f7267b2a402f75e0f09aac2abc073034d890 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 4 Jun 2009 00:37:23 -0400 Subject: [PATCH] Unit Tests - Record, T2 - Added Record::get, ::set, ::get_int tests. - Added T2 tests. - Fixed Record::composeF4 bug --- src/Record.cpp | 7 ++++- src/T2.cpp | 33 ++++++++++++++++++++ src/T2.h | 8 +++-- src/tests/.gitignore | 1 + src/tests/Makefile | 5 ++- src/tests/record.t.cpp | 21 ++++++++++++- src/tests/t2.t.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 src/tests/t2.t.cpp diff --git a/src/Record.cpp b/src/Record.cpp index 27ca8fd94..80be6488a 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -59,7 +59,12 @@ std::string Record::composeF4 () bool first = true; foreach (r, (*this)) - ff4 += (first ? "" : " ") + r->second.composeF4 (); + { + if (r->second.value () != "") + ff4 += (first ? "" : " ") + r->second.composeF4 (); + + first = false; + } ff4 += "]"; return ff4; diff --git a/src/T2.cpp b/src/T2.cpp index 15451e704..7ccbccce6 100644 --- a/src/T2.cpp +++ b/src/T2.cpp @@ -29,6 +29,7 @@ #include #include "Nibbler.h" #include "T2.h" +#include "text.h" #include "util.h" //////////////////////////////////////////////////////////////////////////////// @@ -291,6 +292,38 @@ void T2::addAnnotation (const std::string& description) (*this)[s.str ()] = Att (s.str (), description); } +//////////////////////////////////////////////////////////////////////////////// +void T2::addTag (const std::string& tag) +{ + std::vector tags; + split (tags, get ("tags"), ','); + + if (std::find (tags.begin (), tags.end (), tag) == tags.end ()) + { + tags.push_back (tag); + std::string combined; + join (combined, ",", tags); + set ("tags", combined); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void T2::removeTag (const std::string& tag) +{ + std::vector tags; + split (tags, get ("tags"), ','); + + std::vector ::iterator i; + i = std::find (tags.begin (), tags.end (), tag); + if (i != tags.end ()) + { + tags.erase (i); + std::string combined; + join (combined, ",", tags); + set ("tags", combined); + } +} + //////////////////////////////////////////////////////////////////////////////// bool T2::validate () const { diff --git a/src/T2.h b/src/T2.h index 296afa272..476bb3911 100644 --- a/src/T2.h +++ b/src/T2.h @@ -40,11 +40,10 @@ public: T2& operator= (const T2&); // Assignment operator ~T2 (); // Destructor - void legacyParse (const std::string&); std::string composeCSV (); // Status values. - enum status {pending, completed, deleted, recurring}; + enum status {pending, completed, deleted, recurring /* , retired, deferred */}; // Public data. Sequence sequence; @@ -63,9 +62,13 @@ public: void addRemoveTag (const std::string&); // SPECIAL int getTagCount () const; void getTags (std::vector&) const; +*/ void addTag (const std::string&); +/* void addTags (const std::vector &); +*/ void removeTag (const std::string&); +/* void removeTags (); */ @@ -77,6 +80,7 @@ public: private: int determineVersion (const std::string&); + void legacyParse (const std::string&); private: /* diff --git a/src/tests/.gitignore b/src/tests/.gitignore index 972e0f93c..1d02ea303 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -1,4 +1,5 @@ t.t +t2.t t.benchmark.t tdb.t date.t diff --git a/src/tests/Makefile b/src/tests/Makefile index e5ccc1914..098e8055b 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,4 +1,4 @@ -PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \ +PROJECT = t.t t2.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \ parse.t seq.t att.t stringtable.t record.t nibbler.t subst.t CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti LFLAGS = -L/usr/local/lib @@ -24,6 +24,9 @@ clean: t.t: t.t.o $(OBJECTS) test.o g++ t.t.o $(OBJECTS) test.o $(LFLAGS) -o t.t +t2.t: t2.t.o $(OBJECTS) test.o + g++ t2.t.o $(OBJECTS) test.o $(LFLAGS) -o t2.t + tdb.t: tdb.t.o $(OBJECTS) test.o g++ tdb.t.o $(OBJECTS) test.o $(LFLAGS) -o tdb.t diff --git a/src/tests/record.t.cpp b/src/tests/record.t.cpp index 598967ffb..7080116d4 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 (11); + UnitTest t (17); // (blank) bool good = true; @@ -80,6 +80,25 @@ int main (int argc, char** argv) t.is (record.get ("one"), "two", "one=two"); t.is (record.get ("three"), "four", "three=four"); + // Record::set + record.clear (); + record.set ("name", "value"); + t.is (record.composeF4 (), "[name:\"value\"]", "Record::set"); + + // Record::get_int + record.set ("one", 1); + t.is (record.composeF4 (), "[name:\"value\" one:\"1\"]", "Record::set"); + t.is (record.get_int ("one"), 1, "Record::get_int"); + + // Record::remove + record.remove ("one"); + t.is (record.composeF4 (), "[name:\"value\"]", "Record::remove"); + + // Record::all + std::vector all = record.all (); + t.is (all.size (), (size_t)1, "Record::all size"); + t.is (all[0].name (), "name", "Record::all[0].name ()"); + return 0; } diff --git a/src/tests/t2.t.cpp b/src/tests/t2.t.cpp new file mode 100644 index 000000000..68bfb6aac --- /dev/null +++ b/src/tests/t2.t.cpp @@ -0,0 +1,70 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 "task.h" +#include "test.h" + +Context context; + +//////////////////////////////////////////////////////////////////////////////// +int main (int argc, char** argv) +{ + UnitTest test (3); + + T2 t; + t.addTag ("tag1"); + t.addTag ("tag2"); + test.is (t.composeF4 (), "[tag:\"tag1&commatag2\" uuid:\"...\"]", "T2::addTag"); + + T2 t2 (t.composeF4 ()); + test.is (t2.composeF4 (), "[tag:\"tag1&commatag2\" uuid:\"...\"]", "T2::composeF4 -> parse round trip"); + + // Round-trip testing. + T2 t3; + std::string before = t3.composeF4 (); +/* + t3 (t3.composeF4 ()); + t3 (t3.composeF4 ()); + t3 (t3.composeF4 ()); +*/ + std::string after = t3.composeF4 (); + test.is (before, after, "T2::composeF4 -> parse round trip 4 iterations"); + +/* + +T2::composeCSV +T2::id +T2::*Status +T2::*Tag* +T2::*Annotation* + +*/ + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +