Unit Tests - Record, T2

- Added Record::get, ::set, ::get_int tests.
- Added T2 tests.
- Fixed Record::composeF4 bug
This commit is contained in:
Paul Beckingham 2009-06-04 00:37:23 -04:00
parent 25450b4a7c
commit b887f7267b
7 changed files with 140 additions and 5 deletions

View file

@ -59,8 +59,13 @@ std::string Record::composeF4 ()
bool first = true; bool first = true;
foreach (r, (*this)) foreach (r, (*this))
{
if (r->second.value () != "")
ff4 += (first ? "" : " ") + r->second.composeF4 (); ff4 += (first ? "" : " ") + r->second.composeF4 ();
first = false;
}
ff4 += "]"; ff4 += "]";
return ff4; return ff4;
} }

View file

@ -29,6 +29,7 @@
#include <string> #include <string>
#include "Nibbler.h" #include "Nibbler.h"
#include "T2.h" #include "T2.h"
#include "text.h"
#include "util.h" #include "util.h"
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -291,6 +292,38 @@ void T2::addAnnotation (const std::string& description)
(*this)[s.str ()] = Att (s.str (), description); (*this)[s.str ()] = Att (s.str (), description);
} }
////////////////////////////////////////////////////////////////////////////////
void T2::addTag (const std::string& tag)
{
std::vector <std::string> 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 <std::string> tags;
split (tags, get ("tags"), ',');
std::vector <std::string>::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 bool T2::validate () const
{ {

View file

@ -40,11 +40,10 @@ public:
T2& operator= (const T2&); // Assignment operator T2& operator= (const T2&); // Assignment operator
~T2 (); // Destructor ~T2 (); // Destructor
void legacyParse (const std::string&);
std::string composeCSV (); std::string composeCSV ();
// Status values. // Status values.
enum status {pending, completed, deleted, recurring}; enum status {pending, completed, deleted, recurring /* , retired, deferred */};
// Public data. // Public data.
Sequence sequence; Sequence sequence;
@ -63,9 +62,13 @@ public:
void addRemoveTag (const std::string&); // SPECIAL void addRemoveTag (const std::string&); // SPECIAL
int getTagCount () const; int getTagCount () const;
void getTags (std::vector<std::string>&) const; void getTags (std::vector<std::string>&) const;
*/
void addTag (const std::string&); void addTag (const std::string&);
/*
void addTags (const std::vector <std::string>&); void addTags (const std::vector <std::string>&);
*/
void removeTag (const std::string&); void removeTag (const std::string&);
/*
void removeTags (); void removeTags ();
*/ */
@ -77,6 +80,7 @@ public:
private: private:
int determineVersion (const std::string&); int determineVersion (const std::string&);
void legacyParse (const std::string&);
private: private:
/* /*

View file

@ -1,4 +1,5 @@
t.t t.t
t2.t
t.benchmark.t t.benchmark.t
tdb.t tdb.t
date.t date.t

View file

@ -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 parse.t seq.t att.t stringtable.t record.t nibbler.t subst.t
CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti
LFLAGS = -L/usr/local/lib LFLAGS = -L/usr/local/lib
@ -24,6 +24,9 @@ clean:
t.t: t.t.o $(OBJECTS) test.o t.t: t.t.o $(OBJECTS) test.o
g++ t.t.o $(OBJECTS) test.o $(LFLAGS) -o t.t 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 tdb.t: tdb.t.o $(OBJECTS) test.o
g++ tdb.t.o $(OBJECTS) test.o $(LFLAGS) -o tdb.t g++ tdb.t.o $(OBJECTS) test.o $(LFLAGS) -o tdb.t

View file

@ -35,7 +35,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (11); UnitTest t (17);
// (blank) // (blank)
bool good = true; 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 ("one"), "two", "one=two");
t.is (record.get ("three"), "four", "three=four"); 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 <Att> 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; return 0;
} }

70
src/tests/t2.t.cpp Normal file
View file

@ -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;
}
////////////////////////////////////////////////////////////////////////////////