Browse Source

Add process_test, and fixits test in source_view_test

merge-requests/365/head
eidheim 10 years ago
parent
commit
bb711f70d0
  1. 9
      src/CMakeLists.txt
  2. 2
      src/source.h
  3. 31
      src/source_clang.cc
  4. 34
      src/window.cc
  5. 5
      tests/CMakeLists.txt
  6. 50
      tests/process_test.cc
  7. 1
      tests/source_clang_test.cc

9
src/CMakeLists.txt

@ -36,6 +36,7 @@ set(global_libraries
set(project_files
config.cc
dialogs.cc
dialogs_unix.cc
directories.cc
entrybox.cc
info.cc
@ -47,8 +48,6 @@ set(project_files
terminal.cc
tooltips.cc
window.cc
../tiny-process-library/process.cpp
)
#Files used both in ../src and ../tests
@ -74,6 +73,8 @@ set(project_shared_files
../libclangmm/src/Tokens.cc
../libclangmm/src/TranslationUnit.cc
../libclangmm/src/Utility.cc
../tiny-process-library/process.cpp
)
if(LIBLLDB_FOUND)
@ -81,9 +82,9 @@ if(LIBLLDB_FOUND)
endif()
if(MSYS)
list(APPEND project_files dialogs_unix.cc ../tiny-process-library/process_win.cpp)
list(APPEND project_shared_files ../tiny-process-library/process_win.cpp)
else()
list(APPEND project_files dialogs_unix.cc ../tiny-process-library/process_unix.cpp)
list(APPEND project_shared_files ../tiny-process-library/process_unix.cpp)
endif()
include_directories(${global_includes})

2
src/source.h

@ -81,7 +81,7 @@ namespace Source {
std::function<std::string()> get_token_spelling;
std::function<std::vector<std::pair<boost::filesystem::path, size_t> >(const std::vector<Source::View*> &views, const std::string &text)> rename_similar_tokens;
std::function<void()> goto_next_diagnostic;
std::function<void()> apply_fix_its;
std::function<std::vector<FixIt>()> get_fix_its;
std::unique_ptr<CompletionDialog> autocomplete_dialog;
std::unique_ptr<SelectionDialog> selection_dialog;

31
src/source_clang.cc

@ -1275,37 +1275,12 @@ Source::ClangViewAutocomplete(file_path, language) {
}
};
apply_fix_its=[this]() {
get_fix_its=[this]() {
if(!parsed) {
Info::get().print("Buffer is parsing");
return;
}
std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > fix_it_marks;
for(auto &fix_it: fix_its) {
auto start_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.first.line-1, fix_it.offsets.first.index-1);
auto end_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.second.line-1, fix_it.offsets.second.index-1);
fix_it_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter));
}
size_t c=0;
get_buffer()->begin_user_action();
for(auto &fix_it: fix_its) {
if(fix_it.type==FixIt::Type::INSERT) {
get_buffer()->insert(fix_it_marks[c].first->get_iter(), fix_it.source);
}
if(fix_it.type==FixIt::Type::REPLACE) {
get_buffer()->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter());
get_buffer()->insert(fix_it_marks[c].first->get_iter(), fix_it.source);
}
if(fix_it.type==FixIt::Type::ERASE) {
get_buffer()->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter());
}
c++;
return std::vector<FixIt>();
}
for(auto &mark_pair: fix_it_marks) {
get_buffer()->delete_mark(mark_pair.first);
get_buffer()->delete_mark(mark_pair.second);
}
get_buffer()->end_user_action();
return fix_its;
};
}

34
src/window.cc

@ -552,8 +552,36 @@ void Window::set_menu_actions() {
});
menu.add_action("source_apply_fix_its", [this]() {
if(notebook.get_current_page()!=-1) {
if(notebook.get_current_view()->apply_fix_its) {
notebook.get_current_view()->apply_fix_its();
auto view=notebook.get_current_view();
if(view->get_fix_its) {
auto buffer=view->get_buffer();
auto fix_its=view->get_fix_its();
std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > fix_it_marks;
for(auto &fix_it: fix_its) {
auto start_iter=buffer->get_iter_at_line_index(fix_it.offsets.first.line-1, fix_it.offsets.first.index-1);
auto end_iter=buffer->get_iter_at_line_index(fix_it.offsets.second.line-1, fix_it.offsets.second.index-1);
fix_it_marks.emplace_back(buffer->create_mark(start_iter), buffer->create_mark(end_iter));
}
size_t c=0;
buffer->begin_user_action();
for(auto &fix_it: fix_its) {
if(fix_it.type==Source::FixIt::Type::INSERT) {
buffer->insert(fix_it_marks[c].first->get_iter(), fix_it.source);
}
if(fix_it.type==Source::FixIt::Type::REPLACE) {
buffer->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter());
buffer->insert(fix_it_marks[c].first->get_iter(), fix_it.source);
}
if(fix_it.type==Source::FixIt::Type::ERASE) {
buffer->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter());
}
c++;
}
for(auto &mark_pair: fix_it_marks) {
buffer->delete_mark(mark_pair.first);
buffer->delete_mark(mark_pair.second);
}
buffer->end_user_action();
}
}
});
@ -810,7 +838,7 @@ void Window::activate_menu_items(bool activate) {
menu.actions["source_goto_method"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_methods) : false);
menu.actions["source_rename"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->rename_similar_tokens) : false);
menu.actions["source_goto_next_diagnostic"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->goto_next_diagnostic) : false);
menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->apply_fix_its) : false);
menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_fix_its) : false);
#ifdef JUCI_ENABLE_DEBUG
if(notebook.get_current_page()!=-1) {
auto view=notebook.get_current_view();

5
tests/CMakeLists.txt

@ -33,6 +33,11 @@ add_library(stubs OBJECT ${stub_files})
include_directories(${global_includes})
add_executable(process_test process_test.cc
$<TARGET_OBJECTS:project_shared> $<TARGET_OBJECTS:stubs>)
target_link_libraries(process_test ${global_libraries})
add_test(process_test process_test)
add_executable(cmake_build_test cmake_build_test.cc
$<TARGET_OBJECTS:project_shared> $<TARGET_OBJECTS:stubs>)
target_link_libraries(cmake_build_test ${global_libraries})

50
tests/process_test.cc

@ -0,0 +1,50 @@
#include <glib.h>
#include "process.hpp"
int main() {
auto output=std::make_shared<std::string>();
auto error=std::make_shared<std::string>();
{
Process process("echo Test", "", [output](const char *bytes, size_t n) {
*output+=std::string(bytes, n);
});
g_assert(process.get_exit_status()==0);
g_assert(output->substr(0, 4)=="Test");
output->clear();
}
{
Process process("echo Test && ls an_incorrect_path", "", [output](const char *bytes, size_t n) {
*output+=std::string(bytes, n);
}, [error](const char *bytes, size_t n) {
*error+=std::string(bytes, n);
});
g_assert(process.get_exit_status()>0);
g_assert(output->substr(0, 4)=="Test");
g_assert(!error->empty());
output->clear();
error->clear();
}
{
Process process("bash", "", [output](const char *bytes, size_t n) {
*output+=std::string(bytes, n);
}, nullptr, true);
process.write("echo Test\n");
process.write("exit\n");
g_assert(process.get_exit_status()==0);
g_assert(output->substr(0, 4)=="Test");
output->clear();
}
{
Process process("cat", "", [output](const char *bytes, size_t n) {
*output+=std::string(bytes, n);
}, nullptr, true);
process.write("Test\n");
process.close_stdin();
g_assert(process.get_exit_status()==0);
g_assert(output->substr(0, 4)=="Test");
output->clear();
}
}

1
tests/source_clang_test.cc

@ -73,6 +73,7 @@ int main() {
while(!clang_view->parsed)
flush_events();
g_assert_cmpuint(clang_view->diagnostics.size(), >, 0);
g_assert_cmpuint(clang_view->get_fix_its().size(), >, 0);
clang_view->async_delete();
clang_view->delete_thread.join();

Loading…
Cancel
Save