Ver Fonte

Add FileSystem tests in BansheeUtility

Marc Legendre há 9 anos atrás
pai
commit
e5a7669e17

+ 3 - 0
Source/BansheeUtility/CMakeLists.txt

@@ -17,6 +17,9 @@ include_directories(${BansheeUtility_INC})
 # Target
 add_library(BansheeUtility SHARED ${BS_BANSHEEUTILITY_SRC})
 
+add_executable(BansheeUtilityTest Source/BansheeUtilityTest.cpp)
+target_link_libraries(BansheeUtilityTest BansheeUtility)
+
 # Defines
 target_compile_definitions(BansheeUtility PRIVATE -DBS_UTILITY_EXPORTS)
 

+ 2 - 0
Source/BansheeUtility/CMakeSources.cmake

@@ -166,12 +166,14 @@ set(BS_BANSHEEUTILITY_SRC_MATH
 )
 
 set(BS_BANSHEEUTILITY_INC_TESTING
+	"Include/BsFileSystemTestSuite.h"
 	"Include/BsTestSuite.h"
 	"Include/BsTestOutput.h"
 	"Include/BsConsoleTestOutput.h"
 )
 
 set(BS_BANSHEEUTILITY_SRC_TESTING
+	"Source/BsFileSystemTestSuite.cpp"
 	"Source/BsTestSuite.cpp"
 	"Source/BsTestOutput.cpp"
 	"Source/BsConsoleTestOutput.cpp"

+ 40 - 0
Source/BansheeUtility/Include/BsFileSystemTestSuite.h

@@ -0,0 +1,40 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsTestSuite.h"
+
+namespace BansheeEngine
+{
+	class FileSystemTestSuite : public TestSuite
+	{
+	public:
+		FileSystemTestSuite();
+		virtual void startUp();
+		virtual void shutDown();
+
+	private:
+		Path testDirectory;
+		void testExists_yes_file();
+		void testExists_yes_dir();
+		void testExists_no();
+		void testGetFileSize_zero();
+		void testGetFileSize_not_zero();
+		void testIsFile_yes();
+		void testIsFile_no();
+		void testIsDirectory_yes();
+		void testIsDirectory_no();
+		void testRemove_file();
+		void testRemove_directory();
+		void testMove();
+		void testMove_overwrite_existing();
+		void testMove_no_overwrite_existing();
+		void testCopy();
+		void testCopy_recursive();
+		void testCopy_overwrite_existing();
+		void testCopy_no_overwrite_existing();
+		void testGetChildren();
+		void testGetLastModifiedTime();
+		void testGetTempDirectoryPath();
+	};
+}

+ 16 - 0
Source/BansheeUtility/Source/BansheeUtilityTest.cpp

@@ -0,0 +1,16 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsFileSystemTestSuite.h"
+#include "BsConsoleTestOutput.h"
+
+using namespace BansheeEngine;
+
+int main()
+{
+
+	SPtr<TestSuite> tests = FileSystemTestSuite::create<FileSystemTestSuite>();
+	ConsoleTestOutput testOutput;
+	tests->run(testOutput);
+
+	return 0;
+}

+ 302 - 0
Source/BansheeUtility/Source/BsFileSystemTestSuite.cpp

@@ -0,0 +1,302 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsFileSystemTestSuite.h"
+
+#include "BsDebug.h"
+#include "BsException.h"
+#include "BsFileSystem.h"
+
+#include <algorithm>
+#include <fstream>
+
+namespace BansheeEngine
+{
+	const String testDirectoryName = "FileSystemTestDirectory/";
+
+	void createFile(Path path, String content)
+	{
+		std::ofstream fs;
+		fs.open(path.toPlatformString().c_str());
+		fs << content;
+		fs.close();
+	}
+
+	void createEmptyFile(Path path)
+	{
+		createFile(path, "");
+	}
+
+	String readFile(Path path)
+	{
+		String content;
+		std::ifstream fs;
+		fs.open(path.toPlatformString().c_str());
+		fs >> content;
+		fs.close();
+		return content;
+	}
+
+	void FileSystemTestSuite::startUp()
+	{
+		testDirectory = FileSystem::getWorkingDirectoryPath() + testDirectoryName;
+		if (FileSystem::exists(testDirectory))
+		{
+			BS_EXCEPT(InternalErrorException,
+			          String("Directory '") + testDirectoryName
+			          + "' should not already exist; you should remove it manually.");
+		}
+		else
+		{
+			FileSystem::createDir(testDirectory);
+			BS_TEST_ASSERT_MSG(FileSystem::exists(testDirectory), "FileSystemTestSuite::startUp(): test directory creation failed");
+		}
+	}
+
+	void FileSystemTestSuite::shutDown()
+	{
+		FileSystem::remove(testDirectory, true);
+		if (FileSystem::exists(testDirectory))
+		{
+			LOGERR("FileSystemTestSuite failed to delete '" + testDirectory.toString()
+				   + "', you should remove it manually.");
+		}
+	}
+
+	FileSystemTestSuite::FileSystemTestSuite()
+	{
+		BS_ADD_TEST(FileSystemTestSuite::testExists_yes_file);
+		BS_ADD_TEST(FileSystemTestSuite::testExists_yes_dir);
+		BS_ADD_TEST(FileSystemTestSuite::testExists_no);
+		BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_zero);
+		BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_not_zero);
+		BS_ADD_TEST(FileSystemTestSuite::testIsFile_yes);
+		BS_ADD_TEST(FileSystemTestSuite::testIsFile_no);
+		BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_yes);
+		BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_no);
+		BS_ADD_TEST(FileSystemTestSuite::testRemove_file);
+		BS_ADD_TEST(FileSystemTestSuite::testRemove_directory);
+		BS_ADD_TEST(FileSystemTestSuite::testMove);
+		BS_ADD_TEST(FileSystemTestSuite::testMove_overwrite_existing);
+		BS_ADD_TEST(FileSystemTestSuite::testMove_no_overwrite_existing);
+		BS_ADD_TEST(FileSystemTestSuite::testCopy);
+		BS_ADD_TEST(FileSystemTestSuite::testCopy_overwrite_existing);
+		BS_ADD_TEST(FileSystemTestSuite::testCopy_no_overwrite_existing);
+		BS_ADD_TEST(FileSystemTestSuite::testGetChildren);
+		BS_ADD_TEST(FileSystemTestSuite::testGetLastModifiedTime);
+		BS_ADD_TEST(FileSystemTestSuite::testGetTempDirectoryPath);
+	}
+
+	void FileSystemTestSuite::testExists_yes_file()
+	{
+		Path path = testDirectory + "plop";
+		createEmptyFile(path);
+		BS_TEST_ASSERT(FileSystem::exists(path));
+		FileSystem::remove(path);
+	}
+
+	void FileSystemTestSuite::testExists_yes_dir()
+	{
+		Path path = testDirectory + "plop/";
+		FileSystem::createDir(path);
+		BS_TEST_ASSERT(FileSystem::exists(path));
+		FileSystem::remove(path);
+	}
+
+	void FileSystemTestSuite::testExists_no()
+	{
+		BS_TEST_ASSERT(!FileSystem::exists(Path("this-file-does-not-exist")));
+	}
+
+	void FileSystemTestSuite::testGetFileSize_zero()
+	{
+		Path path = testDirectory + "file-size-test-1";
+		createEmptyFile(path);
+		BS_TEST_ASSERT(FileSystem::getFileSize(path) == 0);
+		FileSystem::remove(path);
+	}
+
+	void FileSystemTestSuite::testGetFileSize_not_zero()
+	{
+		Path path = testDirectory + "file-size-test-2";
+		createFile(path, "0123456789");
+		BS_TEST_ASSERT(FileSystem::getFileSize(path) == 10);
+		FileSystem::remove(path);
+	}
+
+	void FileSystemTestSuite::testIsFile_yes()
+	{
+		Path path = testDirectory + "some-file-1";
+		createEmptyFile(path);
+		BS_TEST_ASSERT(FileSystem::isFile(path));
+	}
+
+	void FileSystemTestSuite::testIsFile_no()
+	{
+		Path path = testDirectory + "some-directory-1/";
+		FileSystem::createDir(path);
+		BS_TEST_ASSERT(!FileSystem::isFile(path));
+	}
+
+	void FileSystemTestSuite::testIsDirectory_yes()
+	{
+		Path path = testDirectory + "some-directory-2/";
+		FileSystem::createDir(path);
+		BS_TEST_ASSERT(FileSystem::isDirectory(path));
+	}
+
+	void FileSystemTestSuite::testIsDirectory_no()
+	{
+		Path path = testDirectory + "some-file-2";
+		createEmptyFile(path);
+		BS_TEST_ASSERT(!FileSystem::isDirectory(path));
+	}
+
+	void FileSystemTestSuite::testRemove_file()
+	{
+		Path path = testDirectory + "file-to-remove";
+		createEmptyFile(path);
+		BS_TEST_ASSERT(FileSystem::exists(path));
+		FileSystem::remove(path);
+		BS_TEST_ASSERT(!FileSystem::exists(path));
+	}
+
+	void FileSystemTestSuite::testRemove_directory()
+	{
+		Path path = testDirectory + "directory-to-remove/";
+		FileSystem::createDir(path);
+		BS_TEST_ASSERT(FileSystem::exists(path));
+		FileSystem::remove(path, true);
+		BS_TEST_ASSERT(!FileSystem::exists(path));
+	}
+
+	void FileSystemTestSuite::testMove()
+	{
+		Path source = testDirectory + "move-source-1";
+		Path destination = testDirectory + "move-destination-1";
+		createFile(source, "move-data-source-1");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(!FileSystem::exists(destination));
+		FileSystem::move(source, destination);
+		BS_TEST_ASSERT(!FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(destination) == "move-data-source-1");
+	}
+
+	void FileSystemTestSuite::testMove_overwrite_existing()
+	{
+		Path source = testDirectory + "move-source-2";
+		Path destination = testDirectory + "move-destination-2";
+		createFile(source, "move-data-source-2");
+		createFile(destination, "move-data-destination-2");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		FileSystem::move(source, destination, true);
+		BS_TEST_ASSERT(!FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(destination) == "move-data-source-2");
+	}
+
+	void FileSystemTestSuite::testMove_no_overwrite_existing()
+	{
+		Path source = testDirectory + "move-source-3";
+		Path destination = testDirectory + "move-destination-3";
+		createFile(source, "move-data-source-3");
+		createFile(destination, "move-data-destination-3");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		FileSystem::move(source, destination, false);
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(destination) == "move-data-destination-3");
+	}
+
+	void FileSystemTestSuite::testCopy()
+	{
+		Path source = testDirectory + "copy-source-1";
+		Path destination = testDirectory + "copy-destination-1";
+		createFile(source, "copy-data-source-1");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(!FileSystem::exists(destination));
+		FileSystem::copy(source, destination);
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(source) == "copy-data-source-1");
+		BS_TEST_ASSERT(readFile(destination) == "copy-data-source-1");
+	}
+
+	void FileSystemTestSuite::testCopy_overwrite_existing()
+	{
+		Path source = testDirectory + "copy-source-2";
+		Path destination = testDirectory + "copy-destination-2";
+		createFile(source, "copy-data-source-2");
+		createFile(destination, "copy-data-destination-2");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		FileSystem::copy(source, destination, true);
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(source) == "copy-data-source-2");
+		BS_TEST_ASSERT(readFile(destination) == "copy-data-source-2");
+	}
+
+	void FileSystemTestSuite::testCopy_no_overwrite_existing()
+	{
+		Path source = testDirectory + "copy-source-3";
+		Path destination = testDirectory + "copy-destination-3";
+		createFile(source, "copy-data-source-3");
+		createFile(destination, "copy-data-destination-3");
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		FileSystem::copy(source, destination, false);
+		BS_TEST_ASSERT(FileSystem::exists(source));
+		BS_TEST_ASSERT(FileSystem::exists(destination));
+		BS_TEST_ASSERT(readFile(source) == "copy-data-source-3");
+		BS_TEST_ASSERT(readFile(destination) == "copy-data-destination-3");
+	}
+
+
+#define CONTAINS(v, e) (std::find(v.begin(), v.end(), e) != v.end())
+
+	void FileSystemTestSuite::testGetChildren()
+	{
+		Path path = testDirectory + "get-children-test/";
+		FileSystem::createDir(path);
+		FileSystem::createDir(path + "foo/");
+		FileSystem::createDir(path + "bar/");
+		FileSystem::createDir(path + "baz/");
+		createEmptyFile(path + "ga");
+		createEmptyFile(path + "bu");
+		createEmptyFile(path + "zo");
+		createEmptyFile(path + "meu");
+		Vector<Path> files, directories;
+		FileSystem::getChildren(path, files, directories);
+		BS_TEST_ASSERT(files.size() == 4);
+		BS_TEST_ASSERT(CONTAINS(files, path + "ga"));
+		BS_TEST_ASSERT(CONTAINS(files, path + "bu"));
+		BS_TEST_ASSERT(CONTAINS(files, path + "zo"));
+		BS_TEST_ASSERT(CONTAINS(files, path + "meu"));
+		BS_TEST_ASSERT(directories.size() == 3);
+		BS_TEST_ASSERT(CONTAINS(directories, path + "foo"));
+		BS_TEST_ASSERT(CONTAINS(directories, path + "bar"));
+		BS_TEST_ASSERT(CONTAINS(directories, path + "baz"));
+	}
+
+	void FileSystemTestSuite::testGetLastModifiedTime()
+	{
+		std::time_t beforeTime;
+		time(&beforeTime);
+
+		Path path = testDirectory + "blah1234";
+		createFile(path, "blah");
+		std::time_t mtime = FileSystem::getLastModifiedTime(path);
+		BS_TEST_ASSERT(mtime >= beforeTime);
+		BS_TEST_ASSERT(mtime <= beforeTime + 10);
+	}
+
+	void FileSystemTestSuite::testGetTempDirectoryPath()
+	{
+		Path path = FileSystem::getTempDirectoryPath();
+		/* No judging. */
+		BS_TEST_ASSERT(!path.toString().empty());
+	}
+}

+ 1 - 1
Source/BansheeUtility/Source/BsTestOutput.cpp

@@ -9,4 +9,4 @@ namespace BansheeEngine
 	{
 		BS_EXCEPT(UnitTestException, desc);
 	}
-}
+}

+ 1 - 1
Source/BansheeUtility/Source/BsTestSuite.cpp

@@ -49,4 +49,4 @@ namespace BansheeEngine
 		if (!success)
 			mOutput->outputFail(desc, mActiveTestName, file, line);
 	}
-}
+}