From fdd94971fc164397bdf18cf7558b91283afc9769 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 29 Mar 2016 19:26:56 +0300 Subject: Add more tests for files utils. --- data/test/CMakeLists.txt | 1 + data/test/Makefile.am | 3 +- data/test/simplefile.txt | 2 ++ src/utils/files_unittest.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 data/test/simplefile.txt diff --git a/data/test/CMakeLists.txt b/data/test/CMakeLists.txt index 83bbb8d5d..3336dcf8d 100644 --- a/data/test/CMakeLists.txt +++ b/data/test/CMakeLists.txt @@ -1,5 +1,6 @@ SET(FILES palette.gpl + simplefile.txt ) INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/test) diff --git a/data/test/Makefile.am b/data/test/Makefile.am index 904b30852..81e19612b 100644 --- a/data/test/Makefile.am +++ b/data/test/Makefile.am @@ -1,7 +1,8 @@ testdir = $(pkgdatadir)/data/test test_DATA = \ - palette.gpl + palette.gpl \ + simplefile.txt EXTRA_DIST = \ $(test_DATA) \ diff --git a/data/test/simplefile.txt b/data/test/simplefile.txt new file mode 100644 index 000000000..5ba62e4c1 --- /dev/null +++ b/data/test/simplefile.txt @@ -0,0 +1,2 @@ +this is test +file. \ No newline at end of file diff --git a/src/utils/files_unittest.cc b/src/utils/files_unittest.cc index 79de6259d..c876e2b72 100644 --- a/src/utils/files_unittest.cc +++ b/src/utils/files_unittest.cc @@ -29,7 +29,7 @@ #include "debug.h" -TEST_CASE("Files renameFile", "files") +TEST_CASE("Files renameFile") { PHYSFS_init("manaplus"); dirSeparator = "/"; @@ -64,3 +64,89 @@ TEST_CASE("Files renameFile", "files") delete [] buf; delete [] buf2; } + +TEST_CASE("Files existsLocal") +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + resourceManager->addToSearchPath("data", Append_false); + resourceManager->addToSearchPath("../data", Append_false); + REQUIRE(Files::existsLocal(Files::getPath("help/about.txt")) == true); + REQUIRE_FALSE(Files::existsLocal(Files::getPath("help/about1.txt"))); + REQUIRE_FALSE(Files::existsLocal(Files::getPath("help1/about.txt"))); +} + +TEST_CASE("Files loadTextFileString") +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + resourceManager->addToSearchPath("data", Append_false); + resourceManager->addToSearchPath("../data", Append_false); + REQUIRE(Files::loadTextFileString("test/simplefile.txt") == + "this is test \nfile."); +} + +TEST_CASE("Files loadTextFile") +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + resourceManager->addToSearchPath("data", Append_false); + resourceManager->addToSearchPath("../data", Append_false); + + StringVect lines; + Files::loadTextFile("test/simplefile.txt", lines); + REQUIRE(lines.size() == 2); + REQUIRE(lines[0] == "this is test "); + REQUIRE(lines[1] == "file."); +} + +TEST_CASE("Files saveTextFile") +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + resourceManager->addToSearchPath("data", Append_false); + resourceManager->addToSearchPath("../data", Append_false); + + const std::string dir = Files::getPath("test"); + REQUIRE(dir.size() > 0); + Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2"); + std::string data = Files::loadTextFileString("test/tempfile.txt"); + ::remove((dir + "/tempfile.txt").c_str()); + REQUIRE(data == "test line\ntext line2\n"); +} + +TEST_CASE("Files getFilesInDir") +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + resourceManager->addToSearchPath("data", Append_false); + resourceManager->addToSearchPath("../data", Append_false); + + StringVect list; + Files::getFilesInDir("test", + list, + ".gpl"); + REQUIRE(list.size() == 1); + REQUIRE(list[0] == "test/palette.gpl"); + + list.clear(); + Files::getFilesInDir("perserver/default", + list, + ".xml"); + REQUIRE(list.size() == 5); + REQUIRE(list[0] == "perserver/default/charcreation.xml"); + REQUIRE(list[1] == "perserver/default/deadmessages.xml"); + REQUIRE(list[2] == "perserver/default/defaultcommands.xml"); + REQUIRE(list[3] == "perserver/default/features.xml"); + REQUIRE(list[4] == "perserver/default/weapons.xml"); +} -- cgit v1.2.3-70-g09d2