From 535c2d68e7fcfaeb3465036d012cb2afe4cd739d Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Fri, 28 Dec 2018 11:13:42 +0100 Subject: [PATCH] UnitTest build and initial Path class #631 --- src/CMakeLists.txt | 2 - src/control/RecentManager.cpp | 27 +++--- src/util/Path.cpp | 85 +++++++++++++++++++ src/util/Path.h | 55 ++++++++++++ src/util/Range.cpp | 2 - src/util/StringUtils.cpp | 10 +++ src/util/StringUtils.h | 1 + src/util/XournalTypeList.h | 1 + test/CMakeLists.txt | 37 +++----- test/TestMain.cpp | 9 -- test/util/PathTest.cpp | 54 ++++++++++++ .../{StringUtil.cpp => StringUtilTest.cpp} | 9 ++ 12 files changed, 239 insertions(+), 53 deletions(-) create mode 100644 src/util/Path.cpp create mode 100644 src/util/Path.h create mode 100644 test/util/PathTest.cpp rename test/util/{StringUtil.cpp => StringUtilTest.cpp} (79%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a555e410..c25e7f11 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,8 +73,6 @@ file (GLOB xournalpp_SOURCES_RECURSE set (xournalpp_SOURCES ${xournalpp_SOURCES_RECURSE} ${xournalpp_SOURCES}) unset (xournalpp_SOURCES_RECURSE) -list (REMOVE_ITEM xournalpp_SOURCES ${PROJECT_SOURCE_DIR}/src/model/LayerListener.cpp) # TOCHECK - ## Core library ## # Used for xournalpp and xournalpp-test diff --git a/src/control/RecentManager.cpp b/src/control/RecentManager.cpp index cc8dee3b..8efb5969 100644 --- a/src/control/RecentManager.cpp +++ b/src/control/RecentManager.cpp @@ -6,6 +6,8 @@ #include +#include + #include using boost::filesystem::path; #include @@ -135,7 +137,10 @@ void RecentManager::openRecent(path p) { XOJ_CHECK_TYPE(RecentManager); - if (p.filename().empty()) return; + if (p.filename().empty()) + { + return; + } for (RecentManagerListener* l : this->listener) { @@ -177,30 +182,20 @@ GList* RecentManager::filterRecent(GList* items, bool xoj) for (GList* l = items; l != NULL; l = l->next) { GtkRecentInfo* info = (GtkRecentInfo*) l->data; - string uri(gtk_recent_info_get_uri(info)); + + Path p = Path::fromUri(gtk_recent_info_get_uri(info)); // Skip remote files - if (!ba::starts_with(uri, "file://")) + if (p.isEmpty() || !p.exists()) { continue; } - using namespace boost::filesystem; - try { - // substr is for removing uri's file:// - if (!exists(path(uri.substr(7)))) - { - continue; - } - } catch (boost::filesystem::filesystem_error&) { - continue; - } - - if (xoj && (ba::ends_with(uri, ".xoj") || ba::ends_with(uri, ".xopp"))) + if (xoj && (p.hasExtension(".xoj") || p.hasExtension(".xopp"))) { filteredItems = g_list_prepend(filteredItems, info); } - if (!xoj && ba::ends_with(uri, ".pdf")) + if (!xoj && p.hasExtension(".pdf")) { filteredItems = g_list_prepend(filteredItems, info); } diff --git a/src/util/Path.cpp b/src/util/Path.cpp new file mode 100644 index 00000000..f1a4d7b9 --- /dev/null +++ b/src/util/Path.cpp @@ -0,0 +1,85 @@ +#include "Path.h" +#include "StringUtils.h" + +Path::Path() +{ + XOJ_INIT_TYPE(Path); +} + +Path::Path(const Path& other) + : path(other.path) +{ + XOJ_INIT_TYPE(Path); +} + +Path::Path(string path) + : path(path) +{ + XOJ_INIT_TYPE(Path); +} + +Path::~Path() +{ + XOJ_CHECK_TYPE(Path); + XOJ_RELEASE_TYPE(Path); +} + +/** + * @return true if empty + */ +bool Path::isEmpty() +{ + XOJ_CHECK_TYPE(Path); + + return path.empty(); +} + +/** + * Check if this is a file which exists + */ +bool Path::exists() +{ + XOJ_CHECK_TYPE(Path); + + return g_file_test(path.c_str(), G_FILE_TEST_EXISTS); +} + +/** + * Check if the path ends with this extension + */ +bool Path::hasExtension(string ext) +{ + XOJ_CHECK_TYPE(Path); + + return StringUtils::endsWith(path, ext); +} + +/** + * Return the Path as String + */ +string Path::str() +{ + XOJ_CHECK_TYPE(Path); + + return path; +} + +/** + * Convert an uri to a path, if the uri does not start with file:// an empty Path is returned + */ +Path Path::fromUri(string uri) +{ + if (!StringUtils::startsWith(uri, "file://")) + { + return Path(); + } + + gchar* filename = g_filename_from_uri (uri.c_str(), NULL, NULL); + Path p(filename); + g_free(filename); + + return p; +} + + + diff --git a/src/util/Path.h b/src/util/Path.h new file mode 100644 index 00000000..de333297 --- /dev/null +++ b/src/util/Path.h @@ -0,0 +1,55 @@ +/* + * Xournal++ + * + * Path for file / folder handling + * + * @author Xournal++ Team + * https://github.com/xournalpp/xournalpp + * + * @license GNU GPLv2 or later + */ + +#pragma once + +#include "XournalType.h" + +class Path +{ +public: + Path(); + Path(const Path& other); + Path(string path); + virtual ~Path(); + +public: + /** + * @return true if empty + */ + bool isEmpty(); + + /** + * Check if this file / folder exists + */ + bool exists(); + + /** + * Check if the path ends with this extension + */ + bool hasExtension(string ext); + + /** + * Return the Path as String + */ + string str(); + +public: + /** + * Convert an uri to a path, if the uri does not start with file:// an empty Path is returned + */ + static Path fromUri(string uri); + +private: + XOJ_TYPE_ATTRIB; + + string path; +}; diff --git a/src/util/Range.cpp b/src/util/Range.cpp index 9f3052d7..60825608 100644 --- a/src/util/Range.cpp +++ b/src/util/Range.cpp @@ -1,7 +1,5 @@ #include "Range.h" -#include - Range::Range(double x, double y) { XOJ_INIT_TYPE(Range); diff --git a/src/util/StringUtils.cpp b/src/util/StringUtils.cpp index b6664948..8bce2524 100644 --- a/src/util/StringUtils.cpp +++ b/src/util/StringUtils.cpp @@ -53,4 +53,14 @@ bool StringUtils::startsWith(string str, string start) return str.compare(0, start.length(), start) == 0; } +bool StringUtils::endsWith(string str, string end) +{ + if (end.size() > str.size()) + { + return false; + } + + return str.compare(str.length() - end.length(), end.length(), end) == 0; +} + diff --git a/src/util/StringUtils.h b/src/util/StringUtils.h index 1aaf15fa..5473448b 100644 --- a/src/util/StringUtils.h +++ b/src/util/StringUtils.h @@ -25,4 +25,5 @@ public: static void replaceAllChars(string& input, const std::vector replaces); static vector split(string input, char delimiter); static bool startsWith(string str, string start); + static bool endsWith(string str, string end); }; diff --git a/src/util/XournalTypeList.h b/src/util/XournalTypeList.h index 5304a203..9089b70e 100644 --- a/src/util/XournalTypeList.h +++ b/src/util/XournalTypeList.h @@ -258,6 +258,7 @@ XOJ_DECLARE_TYPE(TouchHelper, 247); XOJ_DECLARE_TYPE(TouchDisableInterface, 248); XOJ_DECLARE_TYPE(TouchDisableCustom, 249); XOJ_DECLARE_TYPE(TouchDisableX11, 250); +XOJ_DECLARE_TYPE(Path, 251); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b05107a3..0a45b31f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -29,30 +29,17 @@ add_library (xournalpp-test-base OBJECT ## ------------------------ -# I18nTest -add_executable (test-i18nTest $ $ - util/I18nTest.cpp +# These dirs are xournalpp only so it's safe to add then recursively +file (GLOB_RECURSE util_sources_SOURCES_RECURSE + util/*.cpp ) -add_dependencies (test-i18nTest xournalpp-core xournalpp-test-base util) -target_link_libraries (test-i18nTest ${xournalpp_LDFLAGS} ${CppUnit_LDFLAGS}) -## ------------------------ - -# XojPreviewExtractor -add_executable (test-xojPreviewExtractor $ $ - util/XojPreviewExtractorTest.cpp -) -add_dependencies (test-xojPreviewExtractor xournalpp-core xournalpp-test-base util) -target_link_libraries (test-xojPreviewExtractor ${xournalpp_LDFLAGS} ${CppUnit_LDFLAGS}) - -## ------------------------ - -# StringUtil -add_executable (test-StringUtil $ $ - util/StringUtil.cpp +# Util Test +add_executable (test-util $ $ + ${util_sources_SOURCES_RECURSE} ) -add_dependencies (test-StringUtil xournalpp-core xournalpp-test-base util) -target_link_libraries (test-StringUtil ${xournalpp_LDFLAGS} ${CppUnit_LDFLAGS}) +add_dependencies (test-util xournalpp-core xournalpp-test-base util) +target_link_libraries (test-util ${xournalpp_LDFLAGS} ${CppUnit_LDFLAGS}) ## ------------------------ @@ -64,7 +51,9 @@ add_dependencies (test-loadHandler xournalpp-core xournalpp-test-base util) target_link_libraries (test-loadHandler ${xournalpp_LDFLAGS} ${CppUnit_LDFLAGS}) ## CTest ## -add_test (I18nTest test-i18nTest) -add_test (StringUtil test-StringUtil) -add_test (XojPreviewExtractor test-xojPreviewExtractor) +add_test (util test-util) add_test (LoadHandler test-loadHandler) + + + + diff --git a/test/TestMain.cpp b/test/TestMain.cpp index 68fe74b3..7b1e6761 100644 --- a/test/TestMain.cpp +++ b/test/TestMain.cpp @@ -12,15 +12,12 @@ #include #include #include -#include #include #include #include #include -#include - #include using std::cout; using std::endl; @@ -30,9 +27,6 @@ using std::endl; */ int main(int argc, char* argv[]) { - - //gtk_init(&argc, &argv); - // init crash handler installCrashHandlers(); @@ -40,9 +34,6 @@ int main(int argc, char* argv[]) Log::initlog(); #endif - // Init GTK Display - //gdk_display_open_default_libgtk_only(); - // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); diff --git a/test/util/PathTest.cpp b/test/util/PathTest.cpp new file mode 100644 index 00000000..61bacc56 --- /dev/null +++ b/test/util/PathTest.cpp @@ -0,0 +1,54 @@ +/* + * Xournal++ + * + * This file is part of the Xournal UnitTests + * + * @author Xournal++ Team + * https://github.com/xournalpp/xournalpp + * + * @license GNU GPLv2 or later + */ + +#include +#include + +#include +#include +#include + +using namespace std; + +class PathTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(PathTest); + + CPPUNIT_TEST(testUnsupportedUri); + CPPUNIT_TEST(testPathFromUri); + + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp() + { + } + + void tearDown() + { + } + + void testUnsupportedUri() + { + Path b = Path::fromUri("http://localhost/test.txt"); + CPPUNIT_ASSERT_EQUAL(true, b.isEmpty()); + } + + void testPathFromUri() + { + Path b = Path::fromUri("file:///tmp/test.txt"); + CPPUNIT_ASSERT_EQUAL(false, b.isEmpty()); + CPPUNIT_ASSERT_EQUAL(string("/tmp/test.txt"), b.str()); + } +}; + +// Registers the fixture into the 'registry' +CPPUNIT_TEST_SUITE_REGISTRATION(PathTest); diff --git a/test/util/StringUtil.cpp b/test/util/StringUtilTest.cpp similarity index 79% rename from test/util/StringUtil.cpp rename to test/util/StringUtilTest.cpp index 04b6f1ad..5d1a23c2 100644 --- a/test/util/StringUtil.cpp +++ b/test/util/StringUtilTest.cpp @@ -26,6 +26,7 @@ class StringUtilTest : public CppUnit::TestFixture CPPUNIT_TEST(testSplit); CPPUNIT_TEST(testSplitEmpty); CPPUNIT_TEST(testSplitOne); + CPPUNIT_TEST(testEndsWith); CPPUNIT_TEST_SUITE_END(); @@ -71,6 +72,14 @@ public: CPPUNIT_ASSERT_EQUAL(std::string("aa"), splitted[0]); } + void testEndsWith() + { + CPPUNIT_ASSERT_EQUAL(true, StringUtils::endsWith("asdfsfdafdasfda.xoj", ".xoj")); + CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("111111111111111", "2222")); + CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("111111111122221", "2222")); + CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("", "asdf")); + CPPUNIT_ASSERT_EQUAL(true, StringUtils::endsWith("aaaaaaa", "")); + } }; // Registers the fixture into the 'registry'