From c843f40ff3e6e28e20c3b4c68172d9e69be9885d Mon Sep 17 00:00:00 2001
From: Vincent Richard <vincent@vincent-richard.net>
Date: Sun, 2 Jan 2005 14:10:16 +0000
Subject: [PATCH] Added unit tests for 'utility::path'.

---
 ChangeLog                       |   4 +
 SConstruct                      |   3 +-
 src/messaging/IMAPFolder.cpp    |   4 +-
 src/messaging/POP3Folder.cpp    |   4 +-
 src/messaging/maildirFolder.cpp |   5 +-
 src/utility/path.cpp            |   2 +-
 tests/utility/pathTest.cpp      | 317 ++++++++++++++++++++++++++++++++
 7 files changed, 332 insertions(+), 7 deletions(-)
 create mode 100644 tests/utility/pathTest.cpp

diff --git a/ChangeLog b/ChangeLog
index 01e10c24..34081f21 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,10 @@
 VERSION 0.6.2-cvs
 =================
 
+2005-01-02  Vincent Richard  <vincent@vincent-richard.net>
+
+ * Added unit tests for utility::path.
+
 2005-01-01  Vincent Richard  <vincent@vincent-richard.net>
 
  * Converted all C-style casts to C++-style casts.
diff --git a/SConstruct b/SConstruct
index 2732ec8a..024524c0 100644
--- a/SConstruct
+++ b/SConstruct
@@ -308,7 +308,8 @@ libvmimetest_sources = [
 	[ 'tests/parser/textTest', [ 'tests/parser/textTest.cpp' ] ],
 	[ 'tests/utility/md5Test', [ 'tests/utility/md5Test.cpp' ] ],
 	[ 'tests/utility/stringProxyTest', [ 'tests/utility/stringProxyTest.cpp' ] ],
-	[ 'tests/utility/stringUtilsTest', [ 'tests/utility/stringUtilsTest.cpp' ] ]
+	[ 'tests/utility/stringUtilsTest', [ 'tests/utility/stringUtilsTest.cpp' ] ],
+	[ 'tests/utility/pathTest', [ 'tests/utility/pathTest.cpp' ] ]
 ]
 
 libvmime_autotools = [
diff --git a/src/messaging/IMAPFolder.cpp b/src/messaging/IMAPFolder.cpp
index ce5b5236..f197f360 100644
--- a/src/messaging/IMAPFolder.cpp
+++ b/src/messaging/IMAPFolder.cpp
@@ -40,8 +40,8 @@ namespace messaging {
 
 IMAPFolder::IMAPFolder(const folder::path& path, IMAPStore* store, const int type, const int flags)
 	: m_store(store), m_connection(m_store->connection()), m_path(path),
-	  m_name(path.getLastComponent()), m_mode(-1), m_open(false), m_type(type), m_flags(flags),
-	  m_messageCount(0), m_uidValidity(0)
+	  m_name(path.isEmpty() ? folder::path::component("") : path.getLastComponent()), m_mode(-1),
+	  m_open(false), m_type(type), m_flags(flags), m_messageCount(0), m_uidValidity(0)
 {
 	m_store->registerFolder(this);
 }
diff --git a/src/messaging/POP3Folder.cpp b/src/messaging/POP3Folder.cpp
index 61e3475b..1bccf9ea 100644
--- a/src/messaging/POP3Folder.cpp
+++ b/src/messaging/POP3Folder.cpp
@@ -30,7 +30,9 @@ namespace messaging {
 
 
 POP3Folder::POP3Folder(const folder::path& path, POP3Store* store)
-	: m_store(store), m_path(path), m_name(path.getLastComponent()), m_mode(-1), m_open(false)
+	: m_store(store), m_path(path),
+	  m_name(path.isEmpty() ? folder::path::component("") : path.getLastComponent()),
+	  m_mode(-1), m_open(false)
 {
 	m_store->registerFolder(this);
 }
diff --git a/src/messaging/maildirFolder.cpp b/src/messaging/maildirFolder.cpp
index 19153907..e63f6d61 100644
--- a/src/messaging/maildirFolder.cpp
+++ b/src/messaging/maildirFolder.cpp
@@ -36,8 +36,9 @@ namespace messaging {
 
 
 maildirFolder::maildirFolder(const folder::path& path, maildirStore* store)
-	: m_store(store), m_path(path), m_name(path.getLastComponent()), m_mode(-1), m_open(false),
-	  m_unreadMessageCount(0), m_messageCount(0)
+	: m_store(store), m_path(path),
+	  m_name(path.isEmpty() ? folder::path::component("") : path.getLastComponent()),
+	  m_mode(-1), m_open(false), m_unreadMessageCount(0), m_messageCount(0)
 {
 	m_store->registerFolder(this);
 }
diff --git a/src/utility/path.cpp b/src/utility/path.cpp
index b7651615..03ce6098 100644
--- a/src/utility/path.cpp
+++ b/src/utility/path.cpp
@@ -150,7 +150,7 @@ const bool path::isEmpty() const
 
 const path::component path::getLastComponent() const
 {
-	return (isEmpty() ? component("") : m_list[m_list.size() - 1]);
+	return (m_list[m_list.size() - 1]);
 }
 
 
diff --git a/tests/utility/pathTest.cpp b/tests/utility/pathTest.cpp
new file mode 100644
index 00000000..bf915c55
--- /dev/null
+++ b/tests/utility/pathTest.cpp
@@ -0,0 +1,317 @@
+//
+// VMime library (http://vmime.sourceforge.net)
+// Copyright (C) 2002-2004 Vincent Richard <vincent@vincent-richard.net>
+//
+// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#include "../lib/unit++/unit++.h"
+
+#include <iostream>
+#include <ostream>
+
+#include "vmime/vmime.hpp"
+#include "vmime/platforms/posix/posixHandler.hpp"
+#include "vmime/utility/path.hpp"
+
+using namespace unitpp;
+
+
+namespace
+{
+	class pathTest : public suite
+	{
+		typedef vmime::utility::path path;
+		typedef vmime::utility::path::component comp;
+
+
+		void testConstruct1()
+		{
+			assert_eq("1", true, path().isEmpty());
+			assert_eq("2", 0, path().getSize());
+		}
+
+		void testConstruct2()
+		{
+			path p(comp("foo"));
+
+			assert_eq("1", false, p.isEmpty());
+			assert_eq("2", 1, p.getSize());
+			assert_eq("3", "foo", p.getComponentAt(0).getBuffer());
+		}
+
+		void testAppendComponent()
+		{
+			path p;
+
+			assert_eq("1", 0, p.getSize());
+
+			comp c("foo");
+			p.appendComponent(c);
+
+			assert_eq("2", 1, p.getSize());
+			assert_eq("3", c.getBuffer(), p.getComponentAt(0).getBuffer());
+		}
+
+		void testConstruct3()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			path p2(p1);
+
+			assert_eq("1", 2, p2.getSize());
+			assert_eq("2", "foo", p2.getComponentAt(0).getBuffer());
+			assert_eq("3", "bar", p2.getComponentAt(1).getBuffer());
+		}
+
+		void testConstruct4()
+		{
+			// Same as path::path(const component&)
+			path p("foo");
+
+			assert_eq("1", false, p.isEmpty());
+			assert_eq("2", 1, p.getSize());
+			assert_eq("3", "foo", p.getComponentAt(0).getBuffer());
+		}
+
+		void testOperatorDiv1()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			path p2;
+			p2.appendComponent(comp("baz"));
+
+			path p3 = p1 / p2;
+
+			assert_eq("1", 3, p3.getSize());
+			assert_eq("2", p1.getComponentAt(0).getBuffer(), p3.getComponentAt(0).getBuffer());
+			assert_eq("3", p1.getComponentAt(1).getBuffer(), p3.getComponentAt(1).getBuffer());
+			assert_eq("4", p2.getComponentAt(0).getBuffer(), p3.getComponentAt(2).getBuffer());
+		}
+
+		void testOperatorDiv2()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			comp c("baz");
+
+			path p2 = p1 / c;
+
+			assert_eq("1", 3, p2.getSize());
+			assert_eq("2", p1.getComponentAt(0).getBuffer(), p2.getComponentAt(0).getBuffer());
+			assert_eq("3", p1.getComponentAt(1).getBuffer(), p2.getComponentAt(1).getBuffer());
+			assert_eq("4", c.getBuffer(), p2.getComponentAt(2).getBuffer());
+		}
+
+		void testOperatorDivEqual1()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			path p2;
+			p2.appendComponent(comp("baz"));
+
+			path p3(p1);
+			p3 /= p2;
+
+			assert_eq("1", 3, p3.getSize());
+			assert_eq("2", p1.getComponentAt(0).getBuffer(), p3.getComponentAt(0).getBuffer());
+			assert_eq("3", p1.getComponentAt(1).getBuffer(), p3.getComponentAt(1).getBuffer());
+			assert_eq("4", p2.getComponentAt(0).getBuffer(), p3.getComponentAt(2).getBuffer());
+		}
+
+		void testOperatorDivEqual2()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			comp c("baz");
+
+			path p2(p1);
+			p2 /= c;
+
+			assert_eq("1", 3, p2.getSize());
+			assert_eq("2", p1.getComponentAt(0).getBuffer(), p2.getComponentAt(0).getBuffer());
+			assert_eq("3", p1.getComponentAt(1).getBuffer(), p2.getComponentAt(1).getBuffer());
+			assert_eq("4", c.getBuffer(), p2.getComponentAt(2).getBuffer());
+		}
+
+		void testGetParent()
+		{
+			path p1;
+			path p1p = p1.getParent();
+
+			assert_eq("1", true, p1p.isEmpty());
+
+			path p2;
+			p2.appendComponent(comp("foo"));
+			p2.appendComponent(comp("bar"));
+
+			path p2p = p2.getParent();
+
+			assert_eq("2", 1, p2p.getSize());
+			assert_eq("3", p2.getComponentAt(0).getBuffer(), p2p.getComponentAt(0).getBuffer());
+		}
+
+		void testComparison()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+
+			path p2;
+			p2.appendComponent(comp("foo"));
+			p2.appendComponent(comp("bar"));
+
+			path p3;
+			p3.appendComponent(comp("foo"));
+			p3.appendComponent(comp("bar"));
+			p3.appendComponent(comp("baz"));
+
+			assert_eq("1", true, p1 == p2);
+			assert_eq("2", false, p1 == p3);
+
+			assert_eq("3", false, p1 != p2);
+			assert_eq("4", true, p1 != p3);
+
+			assert_eq("5", true, p3.getParent() == p1);
+		}
+
+		void testGetLastComponent()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+			p1.appendComponent(comp("bar"));
+			p1.appendComponent(comp("baz"));
+
+			assert_eq("1", "baz", p1.getLastComponent().getBuffer());
+			assert_eq("2", "bar", p1.getParent().getLastComponent().getBuffer());
+			assert_eq("3", "foo", p1.getParent().getParent().getLastComponent().getBuffer());
+		}
+
+		void testIsDirectParentOf()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+
+			path p2;
+			p2.appendComponent(comp("foo"));
+			p2.appendComponent(comp("bar"));
+
+			path p3;
+			p3.appendComponent(comp("foo"));
+			p3.appendComponent(comp("bar"));
+			p3.appendComponent(comp("baz"));
+
+			assert_eq("1", true, p1.isDirectParentOf(p2));
+			assert_eq("2", true, p2.isDirectParentOf(p3));
+			assert_eq("3", false, p1.isDirectParentOf(p3));
+			assert_eq("4", false, p2.isDirectParentOf(p1));
+		}
+
+		void testIsParentOf()
+		{
+			path p1;
+			p1.appendComponent(comp("foo"));
+
+			path p2;
+			p2.appendComponent(comp("foo"));
+			p2.appendComponent(comp("bar"));
+
+			path p3;
+			p3.appendComponent(comp("foo"));
+			p3.appendComponent(comp("bar"));
+			p3.appendComponent(comp("baz"));
+
+			assert_eq("1", true, p1.isParentOf(p2));
+			assert_eq("2", true, p2.isParentOf(p3));
+			assert_eq("3", true, p1.isParentOf(p3));
+			assert_eq("4", false, p2.isParentOf(p1));
+		}
+
+		void testRenameParent()
+		{
+			path p1;
+			p1.appendComponent(comp("a"));
+			p1.appendComponent(comp("b"));
+			p1.appendComponent(comp("c"));
+			p1.appendComponent(comp("d"));
+
+			path p2;
+			p2.appendComponent(comp("a"));
+			p2.appendComponent(comp("b"));
+
+			path p3;
+			p3.appendComponent(comp("x"));
+			p3.appendComponent(comp("y"));
+			p3.appendComponent(comp("z"));
+
+			path p(p1);
+			p.renameParent(p2, p3);
+
+			assert_eq("1", 5, p.getSize());
+			assert_eq("2", "x", p.getComponentAt(0).getBuffer());
+			assert_eq("3", "y", p.getComponentAt(1).getBuffer());
+			assert_eq("4", "z", p.getComponentAt(2).getBuffer());
+			assert_eq("5", "c", p.getComponentAt(3).getBuffer());
+			assert_eq("6", "d", p.getComponentAt(4).getBuffer());
+		}
+
+	public:
+
+		pathTest() : suite("vmime::utility::path")
+		{
+			// VMime initialization
+			vmime::platformDependant::setHandler<vmime::platforms::posix::posixHandler>();
+
+			add("Construct1", testcase(this, "Construct1", &pathTest::testConstruct1));
+			add("Construct2", testcase(this, "Construct2", &pathTest::testConstruct2));
+			add("Construct3", testcase(this, "Construct3", &pathTest::testConstruct3));
+			add("Construct4", testcase(this, "Construct4", &pathTest::testConstruct4));
+
+			add("AppendComponent", testcase(this, "AppendComponent", &pathTest::testAppendComponent));
+
+			add("OperatorDiv1", testcase(this, "OperatorDiv1", &pathTest::testOperatorDiv1));
+			add("OperatorDiv2", testcase(this, "OperatorDiv2", &pathTest::testOperatorDiv2));
+
+			add("OperatorDivEqual1", testcase(this, "OperatorDivEqual1", &pathTest::testOperatorDivEqual1));
+			add("OperatorDivEqual2", testcase(this, "OperatorDivEqual2", &pathTest::testOperatorDivEqual2));
+
+			add("GetParent", testcase(this, "GetParent", &pathTest::testGetParent));
+
+			add("Comparison", testcase(this, "Comparison", &pathTest::testComparison));
+
+			add("GetLastComponent", testcase(this, "GetLastComponent", &pathTest::testGetLastComponent));
+
+			add("IsDirectParentOf", testcase(this, "IsDirectParentOf", &pathTest::testIsDirectParentOf));
+			add("IsParentOf", testcase(this, "IsParentOf", &pathTest::testIsParentOf));
+
+			add("RenameParent", testcase(this, "RenameParent", &pathTest::testRenameParent));
+
+			suite::main().add("vmime::utility::path", this);
+		}
+
+	};
+
+	pathTest* theTest = new pathTest();
+}