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

View File

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

View File

@@ -1,6 +1,6 @@
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 \
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
LFLAGS = -L/usr/local/lib -lncurses
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 \
../Context.o ../Keymap.o ../command.o ../interactive.o ../report.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)
@@ -81,3 +82,12 @@ color.t: color.t.o $(OBJECTS) test.o
list.t: list.t.o $(OBJECTS) test.o
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;
}
////////////////////////////////////////////////////////////////////////////////