diff C++/Tests/Directory/TestDirectory.cpp @ 222:5a99711d52f9

Directory: add unit tests
author David Demelier <markand@malikania.fr>
date Thu, 08 May 2014 23:31:47 +0200
parents
children
line wrap: on
line diff
--- /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