From 0e636ad5d51cc475567f9af823358dea49313f44 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 23 May 2016 13:53:36 +0200 Subject: [PATCH] Added source_test testing Source::View::cleanup_whitespace_characters. Removed the now reduntant example_test, and moved comment to source_test. --- src/source.cc | 52 ++++++++++---------- src/source.h | 1 + tests/CMakeLists.txt | 26 +++++----- tests/{cmake_test.cc => cmake_build_test.cc} | 2 +- tests/example_test.cc | 16 ------ tests/source_test.cc | 43 ++++++++++++++++ tests/stubs/selectiondialog.cc | 20 ++++++++ tests/stubs/terminal.cc | 4 ++ tests/stubs/tooltips.cc | 7 +++ 9 files changed, 117 insertions(+), 54 deletions(-) rename tests/{cmake_test.cc => cmake_build_test.cc} (93%) delete mode 100644 tests/example_test.cc create mode 100644 tests/source_test.cc create mode 100644 tests/stubs/selectiondialog.cc create mode 100644 tests/stubs/tooltips.cc diff --git a/src/source.cc b/src/source.cc index 69e90f8..dd05d36 100644 --- a/src/source.cc +++ b/src/source.cc @@ -371,6 +371,31 @@ void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) { tab+=tab_char; } +void Source::View::cleanup_whitespace_characters() { + auto buffer=get_buffer(); + buffer->begin_user_action(); + for(int line=0;lineget_line_count();line++) { + auto iter=buffer->get_iter_at_line(line); + auto end_iter=iter; + while(!end_iter.ends_line()) + end_iter.forward_char(); + if(iter==end_iter) + continue; + iter=end_iter; + while(!iter.starts_line() && (*iter==' ' || *iter=='\t' || iter.ends_line())) + iter.backward_char(); + if(*iter!=' ' && *iter!='\t') + iter.forward_char(); + if(iter==end_iter) + continue; + buffer->erase(iter, end_iter); + } + auto iter=buffer->end(); + if(!iter.starts_line()) + buffer->insert(buffer->end(), "\n"); + buffer->end_user_action(); +} + Gsv::DrawSpacesFlags Source::View::parse_show_whitespace_characters(const std::string &text) { namespace qi = boost::spirit::qi; @@ -398,31 +423,8 @@ Gsv::DrawSpacesFlags Source::View::parse_show_whitespace_characters(const std::s bool Source::View::save(const std::vector &views) { if(file_path.empty() || !get_buffer()->get_modified()) return false; - //Remove trailing whitespace characters on save, and add trailing newline if missing - if(Config::get().source.cleanup_whitespace_characters) { - auto buffer=get_buffer(); - buffer->begin_user_action(); - for(int line=0;lineget_line_count();line++) { - auto iter=buffer->get_iter_at_line(line); - auto end_iter=iter; - while(!end_iter.ends_line()) - end_iter.forward_char(); - if(iter==end_iter) - continue; - iter=end_iter; - while(!iter.starts_line() && (*iter==' ' || *iter=='\t' || iter.ends_line())) - iter.backward_char(); - if(*iter!=' ' && *iter!='\t') - iter.forward_char(); - if(iter==end_iter) - continue; - buffer->erase(iter, end_iter); - } - auto iter=buffer->end(); - if(!iter.starts_line()) - buffer->insert(buffer->end(), "\n"); - buffer->end_user_action(); - } + if(Config::get().source.cleanup_whitespace_characters) + cleanup_whitespace_characters(); if(filesystem::write(file_path, get_buffer())) { last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); diff --git a/src/source.h b/src/source.h index f7ad28d..ffdade5 100644 --- a/src/source.h +++ b/src/source.h @@ -161,6 +161,7 @@ namespace Source { guint previous_non_modifier_keyval=0; guint last_keyval=0; private: + void cleanup_whitespace_characters(); Gsv::DrawSpacesFlags parse_show_whitespace_characters(const std::string &text); GtkSourceSearchContext *search_context; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f9fc5d5..fee7b5b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,5 @@ add_definitions(-DBOOST_LOG_DYN_LINK) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wall -Wextra -Wno-unused-parameter -Wno-reorder") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-access-control -std=c++11 -pthread -Wall -Wextra -Wno-unused-parameter -Wno-reorder") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJUCI_TESTS_PATH=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\"") @@ -12,6 +12,7 @@ if(APPLE) endif() find_package(Boost 1.54 COMPONENTS regex system filesystem REQUIRED) +find_package(ASPELL REQUIRED) set(LIBCLANGMM_INCLUDE_DIR ../libclangmm/src) include(FindPkgConfig) @@ -21,6 +22,7 @@ set(global_includes ${Boost_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS} ${GTKSVMM_INCLUDE_DIRS} + ${ASPELL_INCLUDE_DIR} ../src ../tiny-process-library ) @@ -29,6 +31,7 @@ set(global_libraries ${GTKMM_LIBRARIES} ${GTKSVMM_LIBRARIES} ${Boost_LIBRARIES} + ${ASPELL_LIBRARIES} ) set(stub_sources @@ -36,22 +39,21 @@ set(stub_sources stubs/dialogs.cc stubs/dispatcher.cc stubs/info.cc + stubs/selectiondialog.cc stubs/terminal.cc + stubs/tooltips.cc ) include_directories(${global_includes}) add_library(stubs_library ${stub_sources}) -add_executable(cmake_test cmake_test.cc +add_executable(cmake_build_test cmake_build_test.cc ../src/filesystem.cc ../src/cmake.cc ../src/project_build.cc) -target_link_libraries(cmake_test ${global_libraries} stubs_library) -add_test(cmake_test cmake_test) - -#Added for example only, and requires display server to work -#However, it is possible to use the Broadway backend if the test is run in a pure terminal environment: -#broadwayd& -#make test -add_executable(example_test example_test.cc) -target_link_libraries(example_test ${global_libraries} stubs_library) -add_test(example_test example_test) +target_link_libraries(cmake_build_test ${global_libraries} stubs_library) +add_test(cmake_build_test cmake_build_test) + +add_executable(source_test source_test.cc + ../src/source.cc) +target_link_libraries(source_test ${global_libraries} stubs_library) +add_test(source_test source_test) diff --git a/tests/cmake_test.cc b/tests/cmake_build_test.cc similarity index 93% rename from tests/cmake_test.cc rename to tests/cmake_build_test.cc index b6786b5..a9a3ab8 100644 --- a/tests/cmake_test.cc +++ b/tests/cmake_build_test.cc @@ -15,7 +15,7 @@ int main() { auto functions_parameters=cmake.get_functions_parameters("project"); g_assert(functions_parameters.at(0).second.at(0)=="juci"); - g_assert(cmake.get_executable(tests_path/"cmake_test.cc").filename()=="cmake_test"); + g_assert(cmake.get_executable(tests_path/"cmake_build_test.cc").filename()=="cmake_build_test"); auto build=Project::Build::create(tests_path); g_assert(dynamic_cast(build.get())); diff --git a/tests/example_test.cc b/tests/example_test.cc deleted file mode 100644 index 2f50b4c..0000000 --- a/tests/example_test.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include "terminal.h" -#include "info.h" -#include - -//In case one has to test functions that include Terminal::print or Info::print -//Requires display server to work -//However, it is possible to use the Broadway backend if the test is run in a pure terminal environment: -//broadwayd& -//make test - -int main() { - auto app=Gtk::Application::create(); - Terminal::get().print("some message"); - Info::get().print("some message"); - g_assert(true); -} diff --git a/tests/source_test.cc b/tests/source_test.cc new file mode 100644 index 0000000..237b2d5 --- /dev/null +++ b/tests/source_test.cc @@ -0,0 +1,43 @@ +#include +#include "source.h" +#include "filesystem.h" + +int filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { + return 0; +} + +int filesystem::read_non_utf8(const std::string &path, Glib::RefPtr text_buffer) { + return 0; +} + +bool filesystem::write(const std::string &path, Glib::RefPtr text_buffer) { + return false; +} + +std::string hello_world=R"(#include + +int main() { + std::cout << "hello world\n"; +})"; + +std::string hello_world_cleaned=R"(#include + +int main() { + std::cout << "hello world\n"; +} +)"; + +//Requires display server to work +//However, it is possible to use the Broadway backend if the test is run in a pure terminal environment: +//broadwayd& +//make test + +int main() { + auto app=Gtk::Application::create(); + Gsv::init(); + + Source::View source_view("", Glib::RefPtr()); + source_view.get_buffer()->set_text(hello_world); + source_view.cleanup_whitespace_characters(); + g_assert(source_view.get_buffer()->get_text()==hello_world_cleaned); +} diff --git a/tests/stubs/selectiondialog.cc b/tests/stubs/selectiondialog.cc new file mode 100644 index 0000000..2b14137 --- /dev/null +++ b/tests/stubs/selectiondialog.cc @@ -0,0 +1,20 @@ +#include "selectiondialog.h" + +ListViewText::ListViewText(bool use_markup) {} + +SelectionDialogBase::SelectionDialogBase(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup): + text_view(text_view), list_view_text(use_markup) {} + +void SelectionDialogBase::show() {} + +void SelectionDialogBase::hide() {} + +void SelectionDialogBase::add_row(const std::string& row) {} + +SelectionDialog::SelectionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup) : + SelectionDialogBase(text_view, start_mark, show_search_entry, use_markup) {} + +SelectionDialogBase::~SelectionDialogBase() {} + +bool SelectionDialog::on_key_press(GdkEventKey* key) { return true; } +bool CompletionDialog::on_key_press(GdkEventKey* key) { return true;} diff --git a/tests/stubs/terminal.cc b/tests/stubs/terminal.cc index f96f60a..0eb09ef 100644 --- a/tests/stubs/terminal.cc +++ b/tests/stubs/terminal.cc @@ -10,6 +10,10 @@ int Terminal::process(const std::string &command, const boost::filesystem::path return 0; } +int Terminal::process(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) { + return 0; +} + size_t Terminal::print(const std::string &message, bool bold) { return 0; } diff --git a/tests/stubs/tooltips.cc b/tests/stubs/tooltips.cc new file mode 100644 index 0000000..a7d73d9 --- /dev/null +++ b/tests/stubs/tooltips.cc @@ -0,0 +1,7 @@ +#include "tooltips.h" + +Tooltip::~Tooltip() {} + +Gdk::Rectangle Tooltips::drawn_tooltips_rectangle=Gdk::Rectangle(); + +void Tooltips::hide() {}