Browse Source

Added source_test testing Source::View::cleanup_whitespace_characters. Removed the now reduntant example_test, and moved comment to source_test.

merge-requests/365/head
eidheim 10 years ago
parent
commit
0e636ad5d5
  1. 52
      src/source.cc
  2. 1
      src/source.h
  3. 26
      tests/CMakeLists.txt
  4. 2
      tests/cmake_build_test.cc
  5. 16
      tests/example_test.cc
  6. 43
      tests/source_test.cc
  7. 20
      tests/stubs/selectiondialog.cc
  8. 4
      tests/stubs/terminal.cc
  9. 7
      tests/stubs/tooltips.cc

52
src/source.cc

@ -371,6 +371,31 @@ void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) {
tab+=tab_char; tab+=tab_char;
} }
void Source::View::cleanup_whitespace_characters() {
auto buffer=get_buffer();
buffer->begin_user_action();
for(int line=0;line<buffer->get_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) { Gsv::DrawSpacesFlags Source::View::parse_show_whitespace_characters(const std::string &text) {
namespace qi = boost::spirit::qi; 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<Source::View*> &views) { bool Source::View::save(const std::vector<Source::View*> &views) {
if(file_path.empty() || !get_buffer()->get_modified()) if(file_path.empty() || !get_buffer()->get_modified())
return false; return false;
//Remove trailing whitespace characters on save, and add trailing newline if missing if(Config::get().source.cleanup_whitespace_characters)
if(Config::get().source.cleanup_whitespace_characters) { cleanup_whitespace_characters();
auto buffer=get_buffer();
buffer->begin_user_action();
for(int line=0;line<buffer->get_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(filesystem::write(file_path, get_buffer())) { if(filesystem::write(file_path, get_buffer())) {
last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

1
src/source.h

@ -161,6 +161,7 @@ namespace Source {
guint previous_non_modifier_keyval=0; guint previous_non_modifier_keyval=0;
guint last_keyval=0; guint last_keyval=0;
private: private:
void cleanup_whitespace_characters();
Gsv::DrawSpacesFlags parse_show_whitespace_characters(const std::string &text); Gsv::DrawSpacesFlags parse_show_whitespace_characters(const std::string &text);
GtkSourceSearchContext *search_context; GtkSourceSearchContext *search_context;

26
tests/CMakeLists.txt

@ -1,5 +1,5 @@
add_definitions(-DBOOST_LOG_DYN_LINK) 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}\\\"") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJUCI_TESTS_PATH=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\"")
@ -12,6 +12,7 @@ if(APPLE)
endif() endif()
find_package(Boost 1.54 COMPONENTS regex system filesystem REQUIRED) find_package(Boost 1.54 COMPONENTS regex system filesystem REQUIRED)
find_package(ASPELL REQUIRED)
set(LIBCLANGMM_INCLUDE_DIR ../libclangmm/src) set(LIBCLANGMM_INCLUDE_DIR ../libclangmm/src)
include(FindPkgConfig) include(FindPkgConfig)
@ -21,6 +22,7 @@ set(global_includes
${Boost_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}
${GTKMM_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS}
${GTKSVMM_INCLUDE_DIRS} ${GTKSVMM_INCLUDE_DIRS}
${ASPELL_INCLUDE_DIR}
../src ../src
../tiny-process-library ../tiny-process-library
) )
@ -29,6 +31,7 @@ set(global_libraries
${GTKMM_LIBRARIES} ${GTKMM_LIBRARIES}
${GTKSVMM_LIBRARIES} ${GTKSVMM_LIBRARIES}
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${ASPELL_LIBRARIES}
) )
set(stub_sources set(stub_sources
@ -36,22 +39,21 @@ set(stub_sources
stubs/dialogs.cc stubs/dialogs.cc
stubs/dispatcher.cc stubs/dispatcher.cc
stubs/info.cc stubs/info.cc
stubs/selectiondialog.cc
stubs/terminal.cc stubs/terminal.cc
stubs/tooltips.cc
) )
include_directories(${global_includes}) include_directories(${global_includes})
add_library(stubs_library ${stub_sources}) 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) ../src/filesystem.cc ../src/cmake.cc ../src/project_build.cc)
target_link_libraries(cmake_test ${global_libraries} stubs_library) target_link_libraries(cmake_build_test ${global_libraries} stubs_library)
add_test(cmake_test cmake_test) add_test(cmake_build_test cmake_build_test)
#Added for example only, and requires display server to work add_executable(source_test source_test.cc
#However, it is possible to use the Broadway backend if the test is run in a pure terminal environment: ../src/source.cc)
#broadwayd& target_link_libraries(source_test ${global_libraries} stubs_library)
#make test add_test(source_test source_test)
add_executable(example_test example_test.cc)
target_link_libraries(example_test ${global_libraries} stubs_library)
add_test(example_test example_test)

2
tests/cmake_test.cc → tests/cmake_build_test.cc

@ -15,7 +15,7 @@ int main() {
auto functions_parameters=cmake.get_functions_parameters("project"); auto functions_parameters=cmake.get_functions_parameters("project");
g_assert(functions_parameters.at(0).second.at(0)=="juci"); 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); auto build=Project::Build::create(tests_path);
g_assert(dynamic_cast<Project::CMakeBuild*>(build.get())); g_assert(dynamic_cast<Project::CMakeBuild*>(build.get()));

16
tests/example_test.cc

@ -1,16 +0,0 @@
#include "terminal.h"
#include "info.h"
#include <gtkmm.h>
//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);
}

43
tests/source_test.cc

@ -0,0 +1,43 @@
#include <glib.h>
#include "source.h"
#include "filesystem.h"
int filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
return 0;
}
int filesystem::read_non_utf8(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
return 0;
}
bool filesystem::write(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
return false;
}
std::string hello_world=R"(#include <iostream>
int main() {
std::cout << "hello world\n";
})";
std::string hello_world_cleaned=R"(#include <iostream>
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<Gsv::Language>());
source_view.get_buffer()->set_text(hello_world);
source_view.cleanup_whitespace_characters();
g_assert(source_view.get_buffer()->get_text()==hello_world_cleaned);
}

20
tests/stubs/selectiondialog.cc

@ -0,0 +1,20 @@
#include "selectiondialog.h"
ListViewText::ListViewText(bool use_markup) {}
SelectionDialogBase::SelectionDialogBase(Gtk::TextView& text_view, Glib::RefPtr<Gtk::TextBuffer::Mark> 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<Gtk::TextBuffer::Mark> 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;}

4
tests/stubs/terminal.cc

@ -10,6 +10,10 @@ int Terminal::process(const std::string &command, const boost::filesystem::path
return 0; 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) { size_t Terminal::print(const std::string &message, bool bold) {
return 0; return 0;
} }

7
tests/stubs/tooltips.cc

@ -0,0 +1,7 @@
#include "tooltips.h"
Tooltip::~Tooltip() {}
Gdk::Rectangle Tooltips::drawn_tooltips_rectangle=Gdk::Rectangle();
void Tooltips::hide() {}
Loading…
Cancel
Save