diff options
author | Yuri Gorshenin <y@maps.me> | 2015-07-20 11:38:18 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:56:35 +0300 |
commit | c1eba8ce837e56813bd624285a5f53a88713039d (patch) | |
tree | ede6c28ec1b9ba862935bbe9a7a6f54a6f04842e /platform | |
parent | 97de910d8519cffd2c09980945fe5e897a252d97 (diff) |
[platform] Common testing code is extracted into a separate library.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/platform_tests/file_utils.hpp | 148 | ||||
-rw-r--r-- | platform/platform_tests/local_country_file_tests.cpp | 59 | ||||
-rw-r--r-- | platform/platform_tests/platform_tests.pro | 2 | ||||
-rw-r--r-- | platform/platform_tests_support/platform_tests_support.pro | 15 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_dir.cpp | 64 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_dir.hpp | 39 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_file.cpp | 57 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_file.hpp | 41 |
8 files changed, 246 insertions, 179 deletions
diff --git a/platform/platform_tests/file_utils.hpp b/platform/platform_tests/file_utils.hpp deleted file mode 100644 index 655b911fbb..0000000000 --- a/platform/platform_tests/file_utils.hpp +++ /dev/null @@ -1,148 +0,0 @@ -#pragma once -#include "testing/testing.hpp" - -#include "platform/country_file.hpp" -#include "platform/local_country_file.hpp" -#include "platform/local_country_file_utils.hpp" -#include "platform/platform.hpp" - -#include "coding/file_name_utils.hpp" -#include "coding/file_writer.hpp" -#include "coding/internal/file_data.hpp" - -//TODO(ldragunov, gorshenin) If there will be one more usage of this header, we must extract -// it to the standalone test support library. - -namespace platform -{ -namespace tests -{ -// Scoped test directory in a writable dir. -class ScopedTestDir -{ -public: - /// Creates test dir in a writable directory. - /// @param path Path for a testing directory, should be relative to writable-dir. - ScopedTestDir(string const & relativePath) - : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), - m_relativePath(relativePath), - m_reset(false) - { - Platform & platform = GetPlatform(); - Platform::EError ret = platform.MkDir(GetFullPath()); - switch (ret) - { - case Platform::ERR_OK: - break; - case Platform::ERR_FILE_ALREADY_EXISTS: - Platform::EFileType type; - TEST_EQUAL(Platform::ERR_OK, Platform::GetFileType(GetFullPath(), type), ()); - TEST_EQUAL(Platform::FILE_TYPE_DIRECTORY, type, ()); - break; - default: - CHECK(false, ("Can't create directory:", GetFullPath(), "error:", ret)); - break; - } - } - - ~ScopedTestDir() - { - if (m_reset) - return; - - string const fullPath = GetFullPath(); - Platform::EError ret = Platform::RmDir(fullPath); - switch (ret) - { - case Platform::ERR_OK: - break; - case Platform::ERR_FILE_DOES_NOT_EXIST: - LOG(LWARNING, (fullPath, "was deleted before destruction of ScopedTestDir.")); - break; - case Platform::ERR_DIRECTORY_NOT_EMPTY: - LOG(LWARNING, ("There are files in", fullPath)); - break; - default: - LOG(LWARNING, ("Platform::RmDir() error for", fullPath, ":", ret)); - break; - } - } - - inline void Reset() { m_reset = true; } - - inline string const & GetFullPath() const { return m_fullPath; } - - inline string const & GetRelativePath() const { return m_relativePath; } - - bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } - -private: - string const m_fullPath; - string const m_relativePath; - bool m_reset; - - DISALLOW_COPY_AND_MOVE(ScopedTestDir); -}; - -class ScopedTestFile -{ -public: - ScopedTestFile(string const & relativePath, string const & contents) - : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), m_reset(false) - { - { - FileWriter writer(GetFullPath()); - writer.Write(contents.data(), contents.size()); - } - TEST(Exists(), ("Can't create test file", GetFullPath())); - } - - ScopedTestFile(ScopedTestDir const & dir, CountryFile const & countryFile, TMapOptions file, - string const & contents) - : ScopedTestFile( - my::JoinFoldersToPath(dir.GetRelativePath(), countryFile.GetNameWithExt(file)), - contents) - { - } - - ~ScopedTestFile() - { - if (m_reset) - return; - if (!Exists()) - { - LOG(LWARNING, ("File", GetFullPath(), "was deleted before dtor of ScopedTestFile.")); - return; - } - if (!my::DeleteFileX(GetFullPath())) - LOG(LWARNING, ("Can't remove test file:", GetFullPath())); - } - - inline string const & GetFullPath() const { return m_fullPath; } - - inline void Reset() { m_reset = true; } - - bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } - -private: - string const m_fullPath; - bool m_reset; - - DISALLOW_COPY_AND_MOVE(ScopedTestFile); -}; - -string DebugPrint(ScopedTestDir const & dir) -{ - ostringstream os; - os << "ScopedTestDir [" << dir.GetFullPath() << "]"; - return os.str(); -} - -string DebugPrint(ScopedTestFile const & file) -{ - ostringstream os; - os << "ScopedTestFile [" << file.GetFullPath() << "]"; - return os.str(); -} -} // namespace tests -} // namespace platform diff --git a/platform/platform_tests/local_country_file_tests.cpp b/platform/platform_tests/local_country_file_tests.cpp index 1244376747..90a90da3a4 100644 --- a/platform/platform_tests/local_country_file_tests.cpp +++ b/platform/platform_tests/local_country_file_tests.cpp @@ -1,11 +1,11 @@ #include "testing/testing.hpp" -#include "file_utils.hpp" - #include "platform/country_file.hpp" #include "platform/local_country_file.hpp" #include "platform/local_country_file_utils.hpp" #include "platform/platform.hpp" +#include "platform/platform_tests_support/scoped_dir.hpp" +#include "platform/platform_tests_support/scoped_file.hpp" #include "coding/file_name_utils.hpp" #include "coding/file_writer.hpp" @@ -20,7 +20,7 @@ #include "std/bind.hpp" #include "std/set.hpp" -using namespace platform::tests; +using namespace platform::tests_support; namespace platform { @@ -106,7 +106,7 @@ UNIT_TEST(LocalCountryFile_DiskFiles) TEST(!localFile.OnDisk(TMapOptions::ECarRouting), ()); TEST(!localFile.OnDisk(TMapOptions::EMapWithCarRouting), ()); - ScopedTestFile testMapFile(countryFile.GetNameWithExt(TMapOptions::EMap), "map"); + ScopedFile testMapFile(countryFile.GetNameWithExt(TMapOptions::EMap), "map"); localFile.SyncWithDisk(); TEST(localFile.OnDisk(TMapOptions::EMap), ()); @@ -114,7 +114,7 @@ UNIT_TEST(LocalCountryFile_DiskFiles) TEST(!localFile.OnDisk(TMapOptions::EMapWithCarRouting), ()); TEST_EQUAL(3, localFile.GetSize(TMapOptions::EMap), ()); - ScopedTestFile testRoutingFile(countryFile.GetNameWithExt(TMapOptions::ECarRouting), "routing"); + ScopedFile testRoutingFile(countryFile.GetNameWithExt(TMapOptions::ECarRouting), "routing"); localFile.SyncWithDisk(); TEST(localFile.OnDisk(TMapOptions::EMap), ()); @@ -141,17 +141,17 @@ UNIT_TEST(LocalCountryFile_CleanupMapFiles) CountryFile brazilFile("Brazil"); CountryFile irelandFile("Ireland"); - ScopedTestDir testDir1("1"); + ScopedDir testDir1("1"); LocalCountryFile japanLocalFile(testDir1.GetFullPath(), japanFile, 1 /* version */); - ScopedTestFile japanMapFile(testDir1, japanFile, TMapOptions::EMap, "Japan"); + ScopedFile japanMapFile(testDir1, japanFile, TMapOptions::EMap, "Japan"); - ScopedTestDir testDir2("2"); + ScopedDir testDir2("2"); LocalCountryFile brazilLocalFile(testDir2.GetFullPath(), brazilFile, 2 /* version */); - ScopedTestFile brazilMapFile(testDir2, brazilFile, TMapOptions::EMap, "Brazil"); + ScopedFile brazilMapFile(testDir2, brazilFile, TMapOptions::EMap, "Brazil"); LocalCountryFile irelandLocalFile(testDir2.GetFullPath(), irelandFile, 2 /* version */); - ScopedTestFile irelandMapFile(testDir2, irelandFile, TMapOptions::EMap, "Ireland"); + ScopedFile irelandMapFile(testDir2, irelandFile, TMapOptions::EMap, "Ireland"); - ScopedTestDir testDir3("3"); + ScopedDir testDir3("3"); // Check that FindAllLocalMaps() vector<LocalCountryFile> localFiles; @@ -186,22 +186,22 @@ UNIT_TEST(LocalCountryFile_CleanupMapFiles) UNIT_TEST(LocalCountryFile_CleanupPartiallyDownloadedFiles) { - ScopedTestFile toBeDeleted[] = {{"Ireland.mwm.ready", "Ireland"}, - {"Netherlands.mwm.routing.downloading2", "Netherlands"}, - {"Germany.mwm.ready3", "Germany"}, - {"UK_England.mwm.resume4", "UK"}}; - ScopedTestFile toBeKept[] = { + ScopedFile toBeDeleted[] = {{"Ireland.mwm.ready", "Ireland"}, + {"Netherlands.mwm.routing.downloading2", "Netherlands"}, + {"Germany.mwm.ready3", "Germany"}, + {"UK_England.mwm.resume4", "UK"}}; + ScopedFile toBeKept[] = { {"Italy.mwm", "Italy"}, {"Spain.mwm", "Spain map"}, {"Spain.mwm.routing", "Spain routing"}}; CleanupMapsDirectory(); - for (ScopedTestFile & file : toBeDeleted) + for (ScopedFile & file : toBeDeleted) { TEST(!file.Exists(), (file)); file.Reset(); } - for (ScopedTestFile & file : toBeKept) + for (ScopedFile & file : toBeKept) TEST(file.Exists(), (file)); } @@ -217,13 +217,12 @@ UNIT_TEST(LocalCountryFile_DirectoryLookup) CountryFile const irelandFile("Ireland"); CountryFile const netherlandsFile("Netherlands"); - ScopedTestDir testDir("test-dir"); + ScopedDir testDir("test-dir"); - ScopedTestFile testIrelandMapFile(testDir, irelandFile, TMapOptions::EMap, "Ireland-map"); - ScopedTestFile testNetherlandsMapFile(testDir, netherlandsFile, TMapOptions::EMap, - "Netherlands-map"); - ScopedTestFile testNetherlandsRoutingFile(testDir, netherlandsFile, TMapOptions::ECarRouting, - "Netherlands-routing"); + ScopedFile testIrelandMapFile(testDir, irelandFile, TMapOptions::EMap, "Ireland-map"); + ScopedFile testNetherlandsMapFile(testDir, netherlandsFile, TMapOptions::EMap, "Netherlands-map"); + ScopedFile testNetherlandsRoutingFile(testDir, netherlandsFile, TMapOptions::ECarRouting, + "Netherlands-routing"); vector<LocalCountryFile> localFiles; FindAllLocalMapsInDirectory(testDir.GetFullPath(), 150309, localFiles); @@ -252,8 +251,8 @@ UNIT_TEST(LocalCountryFile_AllLocalFilesLookup) Platform & platform = GetPlatform(); - ScopedTestDir testDir("010101"); - ScopedTestFile testItalyMapFile(testDir, italyFile, TMapOptions::EMap, "Italy-map"); + ScopedDir testDir("010101"); + ScopedFile testItalyMapFile(testDir, italyFile, TMapOptions::EMap, "Italy-map"); vector<LocalCountryFile> localFiles; FindAllLocalMaps(localFiles); @@ -282,7 +281,7 @@ UNIT_TEST(LocalCountryFile_PreparePlaceForCountryFiles) TEST(italyLocalFile.get(), ()); TEST_EQUAL(expectedItalyFile, *italyLocalFile, ()); - ScopedTestDir directoryForV1("1"); + ScopedDir directoryForV1("1"); CountryFile germanyFile("Germany"); LocalCountryFile expectedGermanyFile(directoryForV1.GetFullPath(), germanyFile, 1 /* version */); @@ -301,7 +300,7 @@ UNIT_TEST(LocalCountryFile_PreparePlaceForCountryFiles) UNIT_TEST(LocalCountryFile_CountryIndexes) { - ScopedTestDir testDir("101010"); + ScopedDir testDir("101010"); CountryFile germanyFile("Germany"); LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */); @@ -331,11 +330,11 @@ UNIT_TEST(LocalCountryFile_DoNotDeleteUserFiles) my::LogLevel oldLogLevel = my::g_LogLevel; my::g_LogLevel = LCRITICAL; MY_SCOPE_GUARD(restoreLogLevel, [&oldLogLevel]() - { + { my::g_LogLevel = oldLogLevel; }); - ScopedTestDir testDir("101010"); + ScopedDir testDir("101010"); CountryFile germanyFile("Germany"); LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */); diff --git a/platform/platform_tests/platform_tests.pro b/platform/platform_tests/platform_tests.pro index 35662320f4..d76df36f2e 100644 --- a/platform/platform_tests/platform_tests.pro +++ b/platform/platform_tests/platform_tests.pro @@ -4,7 +4,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = platform coding base zlib tomcrypt jansson +DEPENDENCIES = platform_tests_support platform coding base zlib tomcrypt jansson include($$ROOT_DIR/common.pri) diff --git a/platform/platform_tests_support/platform_tests_support.pro b/platform/platform_tests_support/platform_tests_support.pro new file mode 100644 index 0000000000..db4208ea99 --- /dev/null +++ b/platform/platform_tests_support/platform_tests_support.pro @@ -0,0 +1,15 @@ +TARGET = platform_tests_support +TEMPLATE = lib +CONFIG += staticlib warn_on + +ROOT_DIR = ../.. + +include($$ROOT_DIR/common.pri) + +SOURCES += \ + scoped_dir.cpp \ + scoped_file.cpp \ + +HEADERS += \ + scoped_dir.hpp \ + scoped_file.hpp \ diff --git a/platform/platform_tests_support/scoped_dir.cpp b/platform/platform_tests_support/scoped_dir.cpp new file mode 100644 index 0000000000..8e5c510193 --- /dev/null +++ b/platform/platform_tests_support/scoped_dir.cpp @@ -0,0 +1,64 @@ +#include "platform/platform_tests_support/scoped_dir.hpp" + +#include "testing/testing.hpp" +#include "coding/file_name_utils.hpp" +#include "base/logging.hpp" +#include "std/sstream.hpp" + +namespace platform +{ +namespace tests_support +{ +ScopedDir::ScopedDir(string const & relativePath) + : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), + m_relativePath(relativePath), + m_reset(false) +{ + Platform & platform = GetPlatform(); + Platform::EError ret = platform.MkDir(GetFullPath()); + switch (ret) + { + case Platform::ERR_OK: + break; + case Platform::ERR_FILE_ALREADY_EXISTS: + Platform::EFileType type; + TEST_EQUAL(Platform::ERR_OK, Platform::GetFileType(GetFullPath(), type), ()); + TEST_EQUAL(Platform::FILE_TYPE_DIRECTORY, type, ()); + break; + default: + TEST(false, ("Can't create directory:", GetFullPath(), "error:", ret)); + break; + } +} + +ScopedDir::~ScopedDir() +{ + if (m_reset) + return; + + string const fullPath = GetFullPath(); + Platform::EError ret = Platform::RmDir(fullPath); + switch (ret) + { + case Platform::ERR_OK: + break; + case Platform::ERR_FILE_DOES_NOT_EXIST: + LOG(LWARNING, (fullPath, "was deleted before destruction of ScopedDir.")); + break; + case Platform::ERR_DIRECTORY_NOT_EMPTY: + LOG(LWARNING, ("There are files in", fullPath)); + break; + default: + LOG(LWARNING, ("Platform::RmDir() error for", fullPath, ":", ret)); + break; + } +} + +string DebugPrint(ScopedDir const & dir) +{ + ostringstream os; + os << "ScopedDir [" << dir.GetFullPath() << "]"; + return os.str(); +} +} // namespace tests_support +} // namespace platform diff --git a/platform/platform_tests_support/scoped_dir.hpp b/platform/platform_tests_support/scoped_dir.hpp new file mode 100644 index 0000000000..854d354b6e --- /dev/null +++ b/platform/platform_tests_support/scoped_dir.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "platform/platform.hpp" +#include "base/macros.hpp" +#include "std/string.hpp" + +namespace platform +{ +namespace tests_support +{ +// Scoped test directory in a writable dir. +class ScopedDir +{ +public: + /// Creates test dir in a writable directory. + /// @param path Path for a testing directory, should be relative to writable-dir. + ScopedDir(string const & relativePath); + + ~ScopedDir(); + + inline void Reset() { m_reset = true; } + + inline string const & GetFullPath() const { return m_fullPath; } + + inline string const & GetRelativePath() const { return m_relativePath; } + + bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } + +private: + string const m_fullPath; + string const m_relativePath; + bool m_reset; + + DISALLOW_COPY_AND_MOVE(ScopedDir); +}; + +string DebugPrint(ScopedDir const & dir); +} // namespace tests_support +} // namespace platform diff --git a/platform/platform_tests_support/scoped_file.cpp b/platform/platform_tests_support/scoped_file.cpp new file mode 100644 index 0000000000..420b972b4f --- /dev/null +++ b/platform/platform_tests_support/scoped_file.cpp @@ -0,0 +1,57 @@ +#include "platform/platform_tests_support/scoped_file.hpp" + +#include "testing/testing.hpp" + +#include "platform/country_file.hpp" +#include "platform/platform_tests_support/scoped_dir.hpp" + +#include "coding/file_name_utils.hpp" +#include "coding/file_writer.hpp" +#include "coding/internal/file_data.hpp" + +#include "base/logging.hpp" + +#include "std/sstream.hpp" + +namespace platform +{ +namespace tests_support +{ +ScopedFile::ScopedFile(string const & relativePath, string const & contents) + : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), m_reset(false) +{ + { + FileWriter writer(GetFullPath()); + writer.Write(contents.data(), contents.size()); + } + TEST(Exists(), ("Can't create test file", GetFullPath())); +} + +ScopedFile::ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, TMapOptions file, + string const & contents) + : ScopedFile(my::JoinFoldersToPath(dir.GetRelativePath(), countryFile.GetNameWithExt(file)), + contents) +{ +} + +ScopedFile::~ScopedFile() +{ + if (m_reset) + return; + if (!Exists()) + { + LOG(LWARNING, ("File", GetFullPath(), "was deleted before dtor of ScopedFile.")); + return; + } + if (!my::DeleteFileX(GetFullPath())) + LOG(LWARNING, ("Can't remove test file:", GetFullPath())); +} + +string DebugPrint(ScopedFile const & file) +{ + ostringstream os; + os << "ScopedFile [" << file.GetFullPath() << "]"; + return os.str(); +} +} // namespace tests_support +} // namespace platform diff --git a/platform/platform_tests_support/scoped_file.hpp b/platform/platform_tests_support/scoped_file.hpp new file mode 100644 index 0000000000..448614d850 --- /dev/null +++ b/platform/platform_tests_support/scoped_file.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "platform/country_defines.hpp" +#include "platform/platform.hpp" +#include "base/macros.hpp" +#include "std/string.hpp" + +namespace platform +{ +class CountryFile; + +namespace tests_support +{ +class ScopedDir; + +class ScopedFile +{ +public: + ScopedFile(string const & relativePath, string const & contents); + + ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, TMapOptions file, + string const & contents); + + ~ScopedFile(); + + inline string const & GetFullPath() const { return m_fullPath; } + + inline void Reset() { m_reset = true; } + + inline bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } + +private: + string const m_fullPath; + bool m_reset; + + DISALLOW_COPY_AND_MOVE(ScopedFile); +}; + +string DebugPrint(ScopedFile const & file); +} // namespace tests_support +} // namespace platform |