From 7a219c999ec6eb718fd8cb6142bb2df56b75cee9 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 7 Jun 2009 16:31:10 -0400 Subject: [PATCH] Unit Tests - Config - Added unit tests for Config. Why didn't I do this a year ago? - Shut off old 1.7.0 code (gulp). - Task now thoroughly broken, and only 1.8.0 can help. --- src/Config.cpp | 2 +- src/Config.h | 2 +- src/Context.cpp | 43 ---------------- src/Context.h | 2 - src/task.cpp | 4 +- src/tests/.gitignore | 1 + src/tests/Makefile | 5 +- src/tests/config.t.cpp | 111 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 src/tests/config.t.cpp diff --git a/src/Config.cpp b/src/Config.cpp index 36531f40c..2614890fd 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -270,7 +270,7 @@ const std::string Config::get ( } //////////////////////////////////////////////////////////////////////////////// -bool Config::get (const std::string& key, bool default_value) +bool Config::get (const std::string& key, const bool default_value) { if ((*this).find (key) != (*this).end ()) { diff --git a/src/Config.h b/src/Config.h index 0286ea950..a50d16150 100644 --- a/src/Config.h +++ b/src/Config.h @@ -44,7 +44,7 @@ public: const std::string get (const char*, const char*); const std::string get (const std::string&); const std::string get (const std::string&, const std::string&); - bool get (const std::string&, bool); + bool get (const std::string&, const bool); int get (const std::string&, const int); double get (const std::string&, const double); void set (const std::string&, const int); diff --git a/src/Context.cpp b/src/Context.cpp index b2c511b22..daf4a4b0f 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -57,49 +57,6 @@ Context::Context () #endif } -//////////////////////////////////////////////////////////////////////////////// -Context::Context (const Context& other) -{ - throw std::string ("unimplemented Context::Context"); - config = other.config; - filter = other.filter; - keymap = other.keymap; - sequence = other.sequence; - subst = other.subst; - task = other.task; - tdb = other.tdb; - stringtable = other.stringtable; - program = other.program; - args = other.args; - cmd = other.cmd; - messages = other.messages; - footnotes = other.footnotes; -} - -//////////////////////////////////////////////////////////////////////////////// -Context& Context::operator= (const Context& other) -{ - throw std::string ("unimplemented Context::operator="); - if (this != &other) - { - config = other.config; - filter = other.filter; - keymap = other.keymap; - sequence = other.sequence; - subst = other.subst; - task = other.task; - tdb = other.tdb; - stringtable = other.stringtable; - program = other.program; - args = other.args; - cmd = other.cmd; - messages = other.messages; - footnotes = other.footnotes; - } - - return *this; -} - //////////////////////////////////////////////////////////////////////////////// Context::~Context () { diff --git a/src/Context.h b/src/Context.h index 70ce60d23..5083d0633 100644 --- a/src/Context.h +++ b/src/Context.h @@ -41,8 +41,6 @@ class Context { public: Context (); // Default constructor - Context (const Context&); // Copy constructor - Context& operator= (const Context&); // Assignment operator ~Context (); // Destructor void initialize (int, char**); // all startup diff --git a/src/task.cpp b/src/task.cpp index ee57af755..dc1a56072 100644 --- a/src/task.cpp +++ b/src/task.cpp @@ -310,6 +310,7 @@ int main (int argc, char** argv) status = context.run (); // start OBSOLETE +/* TDB tdb; std::string dataLocation = expandPath (context.config.get ("data.location")); tdb.dataDirectory (dataLocation); @@ -332,6 +333,8 @@ int main (int argc, char** argv) } std::cout << runTaskCommand (context.args, tdb); +*/ +// end OBSOLETE } catch (std::string& error) @@ -345,7 +348,6 @@ int main (int argc, char** argv) std::cerr << context.stringtable.get (100, "Unknown error.") << std::endl; return -2; } -// end OBSOLETE return status; } diff --git a/src/tests/.gitignore b/src/tests/.gitignore index 630fdb8ad..09b9555f7 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -15,3 +15,4 @@ nibbler.t subst.t filt.t cmd.t +config.t diff --git a/src/tests/Makefile b/src/tests/Makefile index 2e1d5f2a3..dfc1f6f72 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,6 +1,6 @@ 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 filt.t \ - cmd.t + cmd.t config.t CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti LFLAGS = -L/usr/local/lib OBJECTS = ../TDB.o ../TDB2.o ../T.o ../T2.o ../parse.o ../text.o ../Date.o \ @@ -73,3 +73,6 @@ filt.t: filt.t.o $(OBJECTS) test.o cmd.t: cmd.t.o $(OBJECTS) test.o g++ cmd.t.o $(OBJECTS) test.o $(LFLAGS) -o cmd.t +config.t: config.t.o $(OBJECTS) test.o + g++ config.t.o $(OBJECTS) test.o $(LFLAGS) -o config.t + diff --git a/src/tests/config.t.cpp b/src/tests/config.t.cpp new file mode 100644 index 000000000..7202cdc82 --- /dev/null +++ b/src/tests/config.t.cpp @@ -0,0 +1,111 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 +#include +#include + +Context context; + +//////////////////////////////////////////////////////////////////////////////// +int main (int argc, char** argv) +{ + UnitTest t (18); + + // void set (const std::string&, const int); + // int get (const std::string&, const int); + Config c; + c.set ("int1", 0); + t.is (c.get ("int1", 9), 0, "Config::set/get int"); + + c.set ("int2", 3); + t.is (c.get ("int2", 9), 3, "Config::set/get int"); + + c.set ("int3", -9); + t.is (c.get ("int3", 9), -9, "Config::set/get int"); + + // void set (const std::string&, const double); + // double get (const std::string&, const double); + c.set ("double1", 0.0); + t.is (c.get ("double1", 9.0), 0.0, "Config::set/get double"); + + c.set ("double2", 3.0); + t.is (c.get ("double2", 9.0), 3.0, "Config::set/get double"); + + c.set ("double3", -9.0); + t.is (c.get ("double3", 9.0), -9.0, "Config::set/get double"); + + // void set (const std::string&, const std::string&); + c.set ("str1", "one"); + t.is (c.get ("str1", ""), "one", "Config::set/get std::string"); + + c.set ("str1", ""); + t.is (c.get ("str1", "no"), "", "Config::set/get std::string"); + + // const std::string get (const char*); + c.set ("str1", "one"); + t.is (c.get ((char*) "str1"), (char*)"one", "Config::set/get char*"); + + // const std::string get (const char*, const char*); + c.set ("str1", "one"); + t.is (c.get ((char*)"str1", (char*)""), "one", "Config::set/get char*"); + + c.set ("str1", ""); + t.is (c.get ((char*)"str1", (char*)"no"), "", "Config::set/get char*"); + + // const std::string get (const std::string&); + c.set ("str1", "one"); + t.is (c.get (std::string ("str1")), "one", "Config::set/get std::string"); + + c.set ("str1", ""); + t.is (c.get (std::string ("str1")), "", "Config::set/get std::string"); + + // const std::string get (const std::string&, const std::string&); + c.set ("str1", "one"); + t.is (c.get (std::string ("str1"), std::string ("no")), "one", "Config::set/get std::string"); + + c.set ("str1", ""); + t.is (c.get (std::string ("str1"), std::string ("no")), "", "Config::set/get std::string"); + + // bool get (const std::string&, const bool); + c.set ("bool1", false); + t.is (c.get (std::string ("bool1"), (bool)true), false, "Config::set/get bool"); + + c.set ("bool1", true); + t.is (c.get (std::string ("bool1"), (bool)false), true, "Config::set/get bool"); + + // void all (std::vector &); + std::vector all; + c.all (all); + + // 8 created in this test program. + // 22 default report setting created in Config::Config. + t.is (all.size (), (size_t) 8 + 22, "Config::all"); + + return 0; +} + +////////////////////////////////////////////////////////////////////////////////