changeset 222:5a99711d52f9

Directory: add unit tests
author David Demelier <markand@malikania.fr>
date Thu, 08 May 2014 23:31:47 +0200
parents 6a664378c5c4
children c6513d9c696b
files C++/Tests/Directory/CMakeLists.txt C++/Tests/Directory/TestDirectory.cpp C++/Tests/Directory/TestDirectory.h CMakeLists.txt
diffstat 4 files changed, 191 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Directory/CMakeLists.txt	Thu May 08 23:31:47 2014 +0200
@@ -0,0 +1,27 @@
+#
+# CMakeLists.txt -- tests for Directory
+#
+# Copyright (c) 2014 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+set(
+	SOURCES
+	${code_SOURCE_DIR}/C++/Directory.cpp
+	${code_SOURCE_DIR}/C++/Directory.h
+	TestDirectory.cpp
+	TestDirectory.h
+)
+
+define_test(directory "${SOURCES}")
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Directory/TestDirectory.cpp	Thu May 08 23:31:47 2014 +0200
@@ -0,0 +1,119 @@
+/*
+ * TestDirectory.h -- test directory class
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cppunit/TextTestRunner.h>
+
+#include <Directory.h>
+
+#include "TestDirectory.h"
+
+void TestDirectory::withDot()
+{
+	try {
+		auto flags = Directory::NotDotDot;
+		Directory directory(".", flags);
+		bool dot(false);
+		bool dotdot(false);
+
+		for (const auto &entry : directory) {
+			if (entry.name == ".")
+				dot = true;
+			if (entry.name == "..")
+				dotdot = true;
+		}
+
+		CPPUNIT_ASSERT_EQUAL(dot, true);
+		CPPUNIT_ASSERT_EQUAL(dotdot, false);
+	} catch (const std::runtime_error &error) {
+		std::cout << "skipping test because: " << error.what() << std::endl;
+	}
+}
+
+void TestDirectory::withDotDot()
+{
+	try {
+		auto flags = Directory::NotDot;
+		Directory directory(".", flags);
+		bool dot(false);
+		bool dotdot(false);
+
+		for (const auto &entry : directory) {
+			if (entry.name == ".")
+				dot = true;
+			if (entry.name == "..")
+				dotdot = true;
+		}
+
+		CPPUNIT_ASSERT_EQUAL(dot, false);
+		CPPUNIT_ASSERT_EQUAL(dotdot, true);
+	} catch (const std::runtime_error &error) {
+		std::cout << "skipping test because: " << error.what() << std::endl;
+	}
+}
+
+void TestDirectory::withBothDots()
+{
+	try {
+		Directory directory(".");
+		bool dot(false);
+		bool dotdot(false);
+
+		for (const auto &entry : directory) {
+			if (entry.name == ".")
+				dot = true;
+			if (entry.name == "..")
+				dotdot = true;
+		}
+
+		CPPUNIT_ASSERT_EQUAL(dot, true);
+		CPPUNIT_ASSERT_EQUAL(dotdot, true);
+	} catch (const std::runtime_error &error) {
+		std::cout << "skipping test because: " << error.what() << std::endl;
+	}
+}
+
+void TestDirectory::withoutDots()
+{
+	try {
+		auto flags = Directory::NotDot | Directory::NotDotDot;
+		Directory directory(".", flags);
+		bool dot(false);
+		bool dotdot(false);
+
+		for (const auto &entry : directory) {
+			if (entry.name == ".")
+				dot = true;
+			if (entry.name == "..")
+				dotdot = true;
+		}
+
+		CPPUNIT_ASSERT_EQUAL(dot, false);
+		CPPUNIT_ASSERT_EQUAL(dotdot, false);
+	} catch (const std::runtime_error &error) {
+		std::cout << "skipping test because: " << error.what() << std::endl;
+	}
+}
+
+int main()
+{
+	CppUnit::TextTestRunner runnerText;
+
+	runnerText.addTest(TestDirectory::suite());
+
+	return runnerText.run("", false) == 1 ? 0 : 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Directory/TestDirectory.h	Thu May 08 23:31:47 2014 +0200
@@ -0,0 +1,41 @@
+/*
+ * TestDirectory.h -- test directory class
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _TEST_DIRECTORY_H_
+#define _TEST_DIRECTORY_H_
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestFixture.h>
+
+class TestDirectory : public CppUnit::TestFixture {
+private:
+	CPPUNIT_TEST_SUITE(TestDirectory);
+	CPPUNIT_TEST(withDot);
+	CPPUNIT_TEST(withDotDot);
+	CPPUNIT_TEST(withBothDots);
+	CPPUNIT_TEST(withoutDots);
+	CPPUNIT_TEST_SUITE_END();
+
+public:
+	void withDot();
+	void withDotDot();
+	void withBothDots();
+	void withoutDots();
+};
+
+#endif // !_TEST_DIRECTORY_H_
\ No newline at end of file
--- a/CMakeLists.txt	Thu May 08 23:15:21 2014 +0200
+++ b/CMakeLists.txt	Thu May 08 23:31:47 2014 +0200
@@ -58,6 +58,10 @@
 option(WITH_UTF8 "Enable Utf8 functions tests" On)
 option(WITH_XMLPARSER "Enable XML tests" On)
 
+if (WITH_DIRECTORY)
+	add_subdirectory(C++/Tests/Directory)
+endif ()
+
 if (WITH_DYNLIB)
 	add_subdirectory(C++/Tests/DynLib)
 endif ()