UnitTest build and initial Path class

#631
presentation
Andreas Butti 7 years ago
parent 0dd455a15f
commit 535c2d68e7
  1. 2
      src/CMakeLists.txt
  2. 27
      src/control/RecentManager.cpp
  3. 85
      src/util/Path.cpp
  4. 55
      src/util/Path.h
  5. 2
      src/util/Range.cpp
  6. 10
      src/util/StringUtils.cpp
  7. 1
      src/util/StringUtils.h
  8. 1
      src/util/XournalTypeList.h
  9. 37
      test/CMakeLists.txt
  10. 9
      test/TestMain.cpp
  11. 54
      test/util/PathTest.cpp
  12. 9
      test/util/StringUtilTest.cpp

@ -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

@ -6,6 +6,8 @@
#include <gtk/gtk.h>
#include <Path.h>
#include <boost/filesystem.hpp>
using boost::filesystem::path;
#include <boost/algorithm/string.hpp>
@ -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);
}

@ -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;
}

@ -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;
};

@ -1,7 +1,5 @@
#include "Range.h"
#include <glib.h>
Range::Range(double x, double y)
{
XOJ_INIT_TYPE(Range);

@ -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;
}

@ -25,4 +25,5 @@ public:
static void replaceAllChars(string& input, const std::vector<replace_pair> replaces);
static vector<string> split(string input, char delimiter);
static bool startsWith(string str, string start);
static bool endsWith(string str, string end);
};

@ -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);

@ -29,30 +29,17 @@ add_library (xournalpp-test-base OBJECT
## ------------------------
# I18nTest
add_executable (test-i18nTest $<TARGET_OBJECTS:xournalpp-core> $<TARGET_OBJECTS:xournalpp-test-base>
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 $<TARGET_OBJECTS:xournalpp-core> $<TARGET_OBJECTS:xournalpp-test-base>
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 $<TARGET_OBJECTS:xournalpp-core> $<TARGET_OBJECTS:xournalpp-test-base>
util/StringUtil.cpp
# Util Test
add_executable (test-util $<TARGET_OBJECTS:xournalpp-core> $<TARGET_OBJECTS:xournalpp-test-base>
${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)

@ -12,15 +12,12 @@
#include <config-dev.h>
#include <config-test.h>
#include <CrashHandler.h>
#include <Stacktrace.h>
#include <XournalType.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <gtk/gtk.h>
#include <iostream>
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();

@ -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 <config-test.h>
#include <Path.h>
#include <cppunit/extensions/HelperMacros.h>
#include <ctime>
#include <stdlib.h>
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);

@ -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'
Loading…
Cancel
Save