Code Reorganization

- Merged Path, File and Directory handling objects from metatask.git.
- Merged unit tests.
- These objects will replace a fair amount of code in task 1.9 and
  more in 2.0.
This commit is contained in:
Paul Beckingham 2010-01-09 17:48:32 -05:00
parent bfdeee2cea
commit a5cb041ef2
13 changed files with 1090 additions and 102 deletions

133
src/Directory.cpp Normal file
View file

@ -0,0 +1,133 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "Directory.h"
////////////////////////////////////////////////////////////////////////////////
Directory::Directory ()
{
}
////////////////////////////////////////////////////////////////////////////////
Directory::Directory (const Directory& other)
: File::File (other)
{
}
////////////////////////////////////////////////////////////////////////////////
Directory::Directory (const File& other)
: File::File (other)
{
}
////////////////////////////////////////////////////////////////////////////////
Directory::Directory (const Path& other)
: File::File (other)
{
}
////////////////////////////////////////////////////////////////////////////////
Directory::Directory (const std::string& in)
: File::File (in)
{
}
////////////////////////////////////////////////////////////////////////////////
Directory::~Directory ()
{
}
////////////////////////////////////////////////////////////////////////////////
Directory& Directory::operator= (const Directory& other)
{
if (this != &other)
{
File::operator= (other);
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
bool Directory::create ()
{
return mkdir (data.c_str (), 0755) == 0 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
bool Directory::remove ()
{
return rmdir (data.c_str ()) == 0 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Directory::list ()
{
std::vector <std::string> files;
list (data, files, false);
return files;
}
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Directory::listRecursive ()
{
std::vector <std::string> files;
list (data, files, true);
return files;
}
////////////////////////////////////////////////////////////////////////////////
void Directory::list (
const std::string& base,
std::vector <std::string>& results,
bool recursive)
{
DIR* dp = opendir (base.c_str ());
if (dp != NULL)
{
struct dirent* de;
while ((de = readdir (dp)) != NULL)
{
if (!strcmp (de->d_name, ".") ||
!strcmp (de->d_name, ".."))
continue;
if (recursive && de->d_type == DT_DIR)
list (base + "/" + de->d_name, results, recursive);
else
results.push_back (base + "/" + de->d_name);
}
closedir (dp);
}
}
////////////////////////////////////////////////////////////////////////////////

55
src/Directory.h Normal file
View file

@ -0,0 +1,55 @@
////////////////////////////////////////////////////////////////////////////////
// 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
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_DIRECTORY
#define INCLUDED_DIRECTORY
#include "File.h"
class Directory : public File
{
public:
Directory ();
Directory (const Directory&);
Directory (const File&);
Directory (const Path&);
Directory (const std::string&);
virtual ~Directory ();
Directory& operator= (const Directory&);
virtual bool create ();
virtual bool remove ();
std::vector <std::string> list ();
std::vector <std::string> listRecursive ();
private:
void list (const std::string&, std::vector <std::string>&, bool);
};
#endif
////////////////////////////////////////////////////////////////////////////////

286
src/File.cpp Normal file
View file

@ -0,0 +1,286 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <fstream>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include "File.h"
////////////////////////////////////////////////////////////////////////////////
File::File ()
: Path::Path ()
{
}
////////////////////////////////////////////////////////////////////////////////
File::File (const Path& other)
: Path::Path (other)
{
}
////////////////////////////////////////////////////////////////////////////////
File::File (const File& other)
: Path::Path (other)
{
}
////////////////////////////////////////////////////////////////////////////////
File::File (const std::string& in)
: Path::Path (in)
{
}
////////////////////////////////////////////////////////////////////////////////
File::~File ()
{
}
////////////////////////////////////////////////////////////////////////////////
File& File::operator= (const File& other)
{
if (this != &other)
{
Path::operator= (other);
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
bool File::create ()
{
std::ofstream out (data.c_str ());
if (out.good ())
{
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::remove ()
{
return unlink (data.c_str ()) == 0 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
// S_IFMT 0170000 type of file
// S_IFIFO 0010000 named pipe (fifo)
// S_IFCHR 0020000 character special
// S_IFDIR 0040000 directory
// S_IFBLK 0060000 block special
// S_IFREG 0100000 regular
// S_IFLNK 0120000 symbolic link
// S_IFSOCK 0140000 socket
// S_IFWHT 0160000 whiteout
// S_ISUID 0004000 set user id on execution
// S_ISGID 0002000 set group id on execution
// S_ISVTX 0001000 save swapped text even after use
// S_IRUSR 0000400 read permission, owner
// S_IWUSR 0000200 write permission, owner
// S_IXUSR 0000100 execute/search permission, owner
mode_t File::mode ()
{
struct stat s;
if (!stat (data.c_str (), &s))
return s.st_mode;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
size_t File::size () const
{
struct stat s;
if (!stat (data.c_str (), &s))
return s.st_size;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
bool File::create (const std::string& name)
{
std::ofstream out (expand (name).c_str ());
if (out.good ())
{
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
std::string File::read (const std::string& name)
{
std::string contents = "";
std::ifstream in (name.c_str ());
if (in.good ())
{
std::string line;
while (getline (in, line))
contents += line + "\n";
in.close ();
}
return contents;
}
////////////////////////////////////////////////////////////////////////////////
bool File::read (const std::string& name, std::string& contents)
{
contents = "";
std::ifstream in (name.c_str ());
if (in.good ())
{
std::string line;
while (getline (in, line))
contents += line + "\n";
in.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::read (const std::string& name, std::vector <std::string>& contents)
{
contents.clear ();
std::ifstream in (name.c_str ());
if (in.good ())
{
std::string line;
while (getline (in, line))
contents.push_back (line);
in.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::write (const std::string& name, const std::string& contents)
{
std::ofstream out (expand (name).c_str (),
std::ios_base::out | std::ios_base::trunc);
if (out.good ())
{
out << contents;
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::write (
const std::string& name,
const std::vector <std::string>& lines,
bool addNewlines /* = true */)
{
std::ofstream out (expand (name).c_str (),
std::ios_base::out | std::ios_base::trunc);
if (out.good ())
{
std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it)
{
out << *it;
if (addNewlines)
out << "\n";
}
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::append (const std::string& name, const std::string& contents)
{
std::ofstream out (expand (name).c_str (),
std::ios_base::out | std::ios_base::app);
if (out.good ())
{
out << contents;
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::append (
const std::string& name,
const std::vector <std::string>& lines,
bool addNewlines /* = true */)
{
std::ofstream out (expand (name).c_str (),
std::ios_base::out | std::ios_base::app);
if (out.good ())
{
std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it)
{
out << *it;
if (addNewlines)
out << "\n";
}
out.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool File::remove (const std::string& name)
{
return unlink (expand (name).c_str ()) == 0 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////

81
src/File.h Normal file
View file

@ -0,0 +1,81 @@
////////////////////////////////////////////////////////////////////////////////
// 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
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_FILE
#define INCLUDED_FILE
#include <string>
#include <vector>
#include <sys/stat.h>
#include "Path.h"
class File : public Path
{
public:
File ();
File (const Path&);
File (const File&);
File (const std::string&);
virtual ~File ();
File& operator= (const File&);
virtual bool create ();
virtual bool remove ();
// bool open ();
// bool openAndLock ();
// void close ();
// bool lock ();
// bool lockNoWait ();
// void unlock ();
// void read (std::string&);
// void read (std::vector <std::string>&);
// void write (const std::string&);
// void write (const std::vector <std::string>&);
virtual mode_t mode ();
virtual size_t size () const;
static bool create (const std::string&);
static std::string read (const std::string&);
static bool read (const std::string&, std::string&);
static bool read (const std::string&, std::vector <std::string>&);
static bool write (const std::string&, const std::string&);
static bool write (const std::string&, const std::vector <std::string>&, bool addNewlines = true);
static bool append (const std::string&, const std::string&);
static bool append (const std::string&, const std::vector <std::string>&, bool addNewlines = true);
static bool remove (const std::string&);
private:
// int handle;
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -1,11 +1,14 @@
bin_PROGRAMS = task bin_PROGRAMS = task
task_SOURCES = Att.cpp Cmd.cpp Config.cpp Context.cpp Date.cpp Duration.cpp \ task_SOURCES = Att.cpp Cmd.cpp Color.cpp Config.cpp Context.cpp Date.cpp \
Filter.cpp Grid.cpp Keymap.cpp Location.cpp Nibbler.cpp \ Directory.cpp Duration.cpp File.cpp Filter.cpp Grid.cpp \
Record.cpp Sequence.cpp StringTable.cpp Subst.cpp Task.cpp \ Keymap.cpp Location.cpp Nibbler.cpp Path.cpp Permission.cpp \
TDB.cpp Table.cpp Timer.cpp Permission.cpp Color.cpp edit.cpp \ Record.cpp Sequence.cpp StringTable.cpp Subst.cpp TDB.cpp \
command.cpp import.cpp interactive.cpp recur.cpp report.cpp \ Table.cpp Task.cpp Timer.cpp command.cpp custom.cpp edit.cpp \
custom.cpp rules.cpp main.cpp text.cpp util.cpp \ import.cpp interactive.cpp main.cpp recur.cpp report.cpp \
Att.h Cmd.h Config.h Context.h Date.h Duration.h Filter.h \ rules.cpp text.cpp util.cpp \
Grid.h Keymap.h Location.h Nibbler.h Record.h Sequence.h \ Att.h Cmd.h Color.h Config.h Context.h Date.h Directory.h \
StringTable.h Subst.h Task.h TDB.h Table.h Timer.h \ Duration.h File.h Filter.h Grid.h Keymap.h Location.h \
Permission.h Color.h i18n.h main.h text.h util.h Nibbler.h Path.h Permission.h Record.h Sequence.h \
StringTable.h Subst.h TDB.h Table.h Task.h Timer.h i18n.h \
main.h text.h util.h

197
src/Path.cpp Normal file
View file

@ -0,0 +1,197 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <fstream>
#include <glob.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include "Path.h"
////////////////////////////////////////////////////////////////////////////////
Path::Path ()
{
}
////////////////////////////////////////////////////////////////////////////////
Path::Path (const Path& other)
{
if (this != &other)
data = other.data;
}
////////////////////////////////////////////////////////////////////////////////
Path::Path (const std::string& in)
{
data = expand (in);
}
////////////////////////////////////////////////////////////////////////////////
Path::~Path ()
{
}
////////////////////////////////////////////////////////////////////////////////
Path& Path::operator= (const Path& other)
{
if (this != &other)
this->data = other.data;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
std::string Path::name () const
{
if (data.length ())
{
std::string::size_type slash = data.rfind ('/');
if (slash != std::string::npos)
return data.substr (slash + 1, std::string::npos);
}
return data;
}
////////////////////////////////////////////////////////////////////////////////
std::string Path::parent () const
{
if (data.length ())
{
std::string::size_type slash = data.rfind ('/');
if (slash != std::string::npos)
return data.substr (0, slash);
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
std::string Path::extension () const
{
if (data.length ())
{
std::string::size_type dot = data.rfind ('.');
if (dot != std::string::npos)
return data.substr (dot + 1, std::string::npos);
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
bool Path::exists () const
{
return access (data.c_str (), F_OK) ? false : true;
}
////////////////////////////////////////////////////////////////////////////////
bool Path::is_directory () const
{
struct stat s = {0};
if (! stat (data.c_str (), &s) &&
s.st_mode & S_IFDIR)
return true;
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Path::readable () const
{
return access (data.c_str (), R_OK) ? false : true;
}
////////////////////////////////////////////////////////////////////////////////
bool Path::writable () const
{
return access (data.c_str (), W_OK) ? false : true;
}
////////////////////////////////////////////////////////////////////////////////
bool Path::executable () const
{
return access (data.c_str (), X_OK) ? false : true;
}
////////////////////////////////////////////////////////////////////////////////
// ~ --> /home/user
// ~foo/x --> /home/foo/s
// ~/x --> /home/foo/x
std::string Path::expand (const std::string& in)
{
std::string copy = in;
std::string::size_type tilde = copy.find ("~");
std::string::size_type slash;
if (tilde != std::string::npos)
{
struct passwd* pw = getpwuid (getuid ());
// Convert: ~ --> /home/user
if (copy.length () == 1)
{
copy = pw->pw_dir;
}
// Convert: ~/x --> /home/user/x
else if (copy.length () > tilde + 1 &&
copy[tilde + 1] == '/')
{
copy.replace (tilde, 1, pw->pw_dir);
}
// Convert: ~foo/x --> /home/foo/x
else if ((slash = copy.find ("/", tilde)) != std::string::npos)
{
std::string name = copy.substr (tilde + 1, slash - tilde - 1);
struct passwd* pw = getpwnam (name.c_str ());
if (pw)
copy.replace (tilde, slash - tilde, pw->pw_dir);
}
}
return copy;
}
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Path::glob (const std::string& pattern)
{
std::vector <std::string> results;
glob_t g;
if (!::glob (pattern.c_str (), GLOB_ERR | GLOB_BRACE | GLOB_TILDE, NULL, &g))
for (int i = 0; i < g.gl_matchc; ++i)
results.push_back (g.gl_pathv[i]);
globfree (&g);
return results;
}
////////////////////////////////////////////////////////////////////////////////

60
src/Path.h Normal file
View file

@ -0,0 +1,60 @@
////////////////////////////////////////////////////////////////////////////////
// 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
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_PATH
#define INCLUDED_PATH
#include <vector>
#include <string>
class Path
{
public:
Path ();
Path (const Path&);
Path (const std::string&);
virtual ~Path ();
Path& operator= (const Path&);
std::string name () const;
std::string parent () const;
std::string extension () const;
bool exists () const;
bool is_directory () const;
bool readable () const;
bool writable () const;
bool executable () const;
// Statics
static std::string expand (const std::string&);
static std::vector<std::string> glob (const std::string&);
public:
std::string data;
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -17,4 +17,7 @@ config.t
util.t util.t
color.t color.t
list.t list.t
path.t
file.t
directory.t
*.log *.log

View file

@ -1,6 +1,6 @@
PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \ PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \
config.t seq.t att.t stringtable.t record.t nibbler.t subst.t filt.t \ config.t seq.t att.t stringtable.t record.t nibbler.t subst.t filt.t \
cmd.t util.t color.t list.t cmd.t util.t color.t list.t path.t file.t directory.t
CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti
LFLAGS = -L/usr/local/lib -lncurses LFLAGS = -L/usr/local/lib -lncurses
OBJECTS = ../TDB.o ../Task.o ../text.o ../Date.o ../Table.o ../Duration.o \ OBJECTS = ../TDB.o ../Task.o ../text.o ../Date.o ../Table.o ../Duration.o \
@ -8,7 +8,8 @@ OBJECTS = ../TDB.o ../Task.o ../text.o ../Date.o ../Table.o ../Duration.o \
../StringTable.o ../Subst.o ../Nibbler.o ../Location.o ../Filter.o \ ../StringTable.o ../Subst.o ../Nibbler.o ../Location.o ../Filter.o \
../Context.o ../Keymap.o ../command.o ../interactive.o ../report.o \ ../Context.o ../Keymap.o ../command.o ../interactive.o ../report.o \
../Grid.o ../Color.o ../rules.o ../recur.o ../custom.o ../import.o \ ../Grid.o ../Color.o ../rules.o ../recur.o ../custom.o ../import.o \
../edit.o ../Timer.o ../Permission.o ../edit.o ../Timer.o ../Permission.o ../Path.o ../File.o \
../Directory.o
all: $(PROJECT) all: $(PROJECT)
@ -81,3 +82,12 @@ color.t: color.t.o $(OBJECTS) test.o
list.t: list.t.o $(OBJECTS) test.o list.t: list.t.o $(OBJECTS) test.o
g++ list.t.o $(OBJECTS) test.o $(LFLAGS) -o list.t g++ list.t.o $(OBJECTS) test.o $(LFLAGS) -o list.t
path.t: path.t.o $(OBJECTS) test.o
g++ path.t.o $(OBJECTS) test.o $(LFLAGS) -o path.t
file.t: file.t.o $(OBJECTS) test.o
g++ file.t.o $(OBJECTS) test.o $(LFLAGS) -o file.t
directory.t: directory.t.o $(OBJECTS) test.o
g++ directory.t.o $(OBJECTS) test.o $(LFLAGS) -o directory.t

93
src/tests/directory.t.cpp Normal file
View file

@ -0,0 +1,93 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <Context.h>
#include <Directory.h>
#include <test.h>
Context context;
int main (int argc, char** argv)
{
UnitTest t (20);
// Directory (const File&);
// Directory (const Path&);
Directory d0 (Path ("/tmp"));
Directory d1 (File ("/tmp"));
Directory d2 (File (Path ("/tmp")));
t.is (d0.data, d1.data, "Directory(std::string) == Directory (File&)");
t.is (d0.data, d2.data, "Directory(std::string) == Directory (File (Path &))");
t.is (d1.data, d2.data, "Directory(File&)) == Directory (File (Path &))");
// Directory (const Directory&);
Directory d3 (d2);
t.is (d3.data, "/tmp", "Directory (Directory&)");
// Directory (const std::string&);
Directory d4 ("/tmp/test_directory");
// Directory& operator= (const Directory&);
Directory d5 = d4;
t.is (d5.data, "/tmp/test_directory", "Directory::operator=");
// virtual bool create ();
t.ok (d5.create (), "Directory::create /tmp/test_directory");
t.ok (d5.exists (), "Directory::exists /tmp/test_directory");
Directory d6 (d5.data + "/dir");
t.ok (d6.create (), "Directory::create /tmp/test_directory/dir");
File::create (d5.data + "/f0");
File::create (d6.data + "/f1");
// std::vector <std::string> list ();
std::vector <std::string> files = d5.list ();
t.is ((int)files.size (), 2, "Directory::list 1 file");
t.is (files[0], "/tmp/test_directory/dir", "file[0] is /tmp/test_directory/dir");
t.is (files[1], "/tmp/test_directory/f0", "file[1] is /tmp/test_directory/f0");
// std::vector <std::string> listRecursive ();
files = d5.listRecursive ();
t.is ((int)files.size (), 2, "Directory::list 1 file");
t.is (files[0], "/tmp/test_directory/dir/f1", "file is /tmp/test_directory/dir/f1");
t.is (files[1], "/tmp/test_directory/f0", "file is /tmp/test_directory/f0");
// virtual bool remove ();
t.ok (File::remove (d5.data + "/f0"), "File::remove /tmp/test_directory/f0");
t.ok (File::remove (d6.data + "/f1"), "File::remove /tmp/test_directory/dir/f1");
t.ok (d6.remove (), "Directory::remove /tmp/test_directory/dir");
t.notok (d6.exists (), "Directory::exists /tmp/test_directory/dir - no");
t.ok (d5.remove (), "Directory::remove /tmp/test_directory");
t.notok (d5.exists (), "Directory::exists /tmp/test_directory - no");
return 0;
}
////////////////////////////////////////////////////////////////////////////////

50
src/tests/file.t.cpp Normal file
View file

@ -0,0 +1,50 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <Context.h>
#include <File.h>
#include <test.h>
Context context;
int main (int argc, char** argv)
{
UnitTest t (5);
File::write ("/tmp/file.t.txt", "This is a test\n");
File f6 ("/tmp/file.t.txt");
t.ok (f6.size () == 15, "File::size /tmp/file.t.txt good");
t.ok (f6.mode () & S_IRUSR, "File::mode /tmp/file.t.txt good");
t.ok (File::remove ("/tmp/file.t.txt"), "File::remove /tmp/file.t.txt good");
t.ok (File::create ("/tmp/file.t.create"), "File::create /tmp/file.t.create good");
t.ok (File::remove ("/tmp/file.t.create"), "File::remove /tmp/file.t.create good");
return 0;
}
////////////////////////////////////////////////////////////////////////////////

107
src/tests/path.t.cpp Normal file
View file

@ -0,0 +1,107 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <Context.h>
#include <Path.h>
#include <test.h>
Context context;
int main (int argc, char** argv)
{
UnitTest t (26);
// Path ();
Path p0;
t.ok (p0.data == "", "Path::Path");
// Path (const Path&);
Path p1 = Path ("foo");
t.ok (p1.data == "foo", "Path::operator=");
// Path (const std::string&);
Path p2 ("~");
t.ok (p2.data != "~", "~ expanded to " + p2.data);
Path p3 ("/tmp");
t.ok (p3.data == "/tmp", "/tmp -> /tmp");
// Path& operator= (const Path&);
Path p3_copy (p3);
t.is (p3.data, p3_copy.data, "Path::Path (Path&)");
// std::string name () const;
Path p4 ("/a/b/c/file.ext");
t.is (p4.name (), "file.ext", "/a/b/c/file.ext name is file.ext");
// std::string parent () const;
t.is (p4.parent (), "/a/b/c", "/a/b/c/file.ext parent is /a/b/c");
// std::string extension () const;
t.is (p4.extension (), "ext", "/a/b/c/file.ext extension is ext");
// bool exists () const;
t.ok (p2.exists (), "~ exists");
t.ok (p3.exists (), "/tmp exists");
// bool is_directory () const;
t.ok (p2.is_directory (), "~ is_directory");
t.ok (p3.is_directory (), "/tmp is_directory");
// bool readable () const;
t.ok (p2.readable (), "~ readable");
t.ok (p3.readable (), "/tmp readable");
// bool writable () const;
t.ok (p2.writable (), "~ writable");
t.ok (p3.writable (), "/tmp writable");
// bool executable () const;
t.ok (p2.executable (), "~ executable");
t.ok (p3.executable (), "/tmp executable");
// static std::string expand (const std::string&);
t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~");
t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/");
// static std::vector <std::string> glob (const std::string&);
std::vector <std::string> out = Path::glob ("/tmp");
t.ok (out.size () == 1, "/tmp -> 1 result");
t.is (out[0], "/tmp", "/tmp -> /tmp");
out = Path::glob ("/t?p");
t.ok (out.size () == 1, "/t?p -> 1 result");
t.is (out[0], "/tmp", "/t?p -> /tmp");
out = Path::glob ("/[s-u]mp");
t.ok (out.size () == 1, "/[s-u]mp -> 1 result");
t.is (out[0], "/tmp", "/[s-u]mp -> /tmp");
return 0;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -1,90 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2008, Paul Beckingham. All rights reserved.
//
//
////////////////////////////////////////////////////////////////////////////////
#include <library.h>
#include <UnitTest.h>
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (66);
t.notok (validUnsignedInteger (""), "!validUnsignedInteger ''");
t.notok (validUnsignedInteger ("F"), "!validUnsignedInteger 'F'");
t.notok (validUnsignedInteger (" "), "!validUnsignedInteger ' '");
t.ok (validUnsignedInteger ("0"), "validUnsignedInteger '0'");
t.notok (validUnsignedInteger ("-1"), "!validUnsignedInteger '-1'");
t.ok (validUnsignedInteger ("+1"), "validUnsignedInteger '+1'");
t.ok (validUnsignedInteger ("123"), "validUnsignedInteger '123'");
t.notok (validInteger (""), "!validInteger ''");
t.notok (validInteger ("F"), "!validInteger 'F'");
t.notok (validInteger (" "), "!validInteger ' '");
t.ok (validInteger ("0"), "validInteger '0'");
t.ok (validInteger ("-1"), "validInteger '-1'");
t.ok (validInteger ("+1"), "validInteger '+1'");
t.ok (validInteger ("123"), "validInteger '123'");
t.notok (validReal (""), "!validReal ''");
t.notok (validReal ("F"), "!validReal 'F'");
t.notok (validReal (" "), "!validReal ' '");
t.ok (validReal ("0"), "validReal '0'");
t.ok (validReal ("1"), "validReal '1'");
t.ok (validReal ("-1"), "validReal '-1'");
t.ok (validReal ("1.23"), "validReal '1.23'");
t.ok (validReal ("-1.23"), "validReal '-1.23'");
// 0/1, T/F, t/f, true/false, True/False, TRUE/FALSE, -/+, yes/no
t.notok (validBoolean (""), "!validBoolean ''");
t.notok (validBoolean (" "), "!validBoolean ' '");
t.notok (validBoolean ("x"), "!validBoolean 'x'");
t.notok (validBoolean ("2"), "!validBoolean '1'");
t.ok (validBoolean ("0"), "validBoolean '0'");
t.ok (validBoolean ("1"), "validBoolean '1'");
t.ok (validBoolean ("T"), "validBoolean 'T'");
t.ok (validBoolean ("F"), "validBoolean 'F'");
t.ok (validBoolean ("t"), "validBoolean 't'");
t.ok (validBoolean ("f"), "validBoolean 'f'");
t.ok (validBoolean ("true"), "validBoolean 'true'");
t.ok (validBoolean ("false"), "validBoolean 'false'");
t.ok (validBoolean ("True"), "validBoolean 'True'");
t.ok (validBoolean ("False"), "validBoolean 'False'");
t.ok (validBoolean ("TRUE"), "validBoolean 'TRUE'");
t.ok (validBoolean ("FALSE"), "validBoolean 'FALSE'");
t.ok (validBoolean ("-"), "validBoolean '-'");
t.ok (validBoolean ("+"), "validBoolean '+'");
t.ok (validBoolean ("YES"), "validBoolean 'YES'");
t.ok (validBoolean ("NO"), "validBoolean 'NO'");
t.ok (validBoolean ("Yes"), "validBoolean 'Yes'");
t.ok (validBoolean ("No"), "validBoolean 'No'");
t.ok (validBoolean ("yes"), "validBoolean 'yes'");
t.ok (validBoolean ("no"), "validBoolean 'no'");
t.ok (validBoolean ("Y"), "validBoolean 'Y'");
t.ok (validBoolean ("N"), "validBoolean 'N'");
t.ok (validBoolean ("y"), "validBoolean 'y'");
t.ok (validBoolean ("n"), "validBoolean 'n'");
t.ok (validBoolean ("on"), "validBoolean 'on'");
t.ok (validBoolean ("off"), "validBoolean 'off'");
t.ok (validBoolean ("On"), "validBoolean 'On'");
t.ok (validBoolean ("Off"), "validBoolean 'Off'");
t.ok (validBoolean ("ON"), "validBoolean 'ON'");
t.ok (validBoolean ("OFF"), "validBoolean 'OFF'");
t.ok (validColor ("black"), "validColor black");
t.ok (validColor ("red"), "validColor red");
t.ok (validColor ("green"), "validColor green");
t.ok (validColor ("yellow"), "validColor yellow");
t.ok (validColor ("blue"), "validColor blue");
t.ok (validColor ("magenta"), "validColor magenta");
t.ok (validColor ("cyan"), "validColor cyan");
t.ok (validColor ("white"), "validColor white");
t.ok (validColor (""), "validColor nocolor");
t.notok (validColor ("donkey"), "fail <- validColor donkey");
return 0;
}
////////////////////////////////////////////////////////////////////////////////