diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fcce2b4..226a78e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,6 +26,8 @@ set(JUCI_SHARED_FILES tooltips.cpp usages_clang.cpp utility.cpp + python_interpreter.cc + python_module.cc ) if(LIBLLDB_FOUND) list(APPEND JUCI_SHARED_FILES debug_lldb.cpp) @@ -56,6 +58,7 @@ set(JUCI_SOURCES selection_dialog.cpp tooltips.cpp window.cpp + plugins.cc ) if(APPLE) diff --git a/src/plugins.cc b/src/plugins.cc index 350b2d0..6539060 100644 --- a/src/plugins.cc +++ b/src/plugins.cc @@ -1,30 +1,7 @@ #include "plugins.h" -#include "cmake.h" -#include "compile_commands.h" -#include "config.h" -#include "ctags.h" -#ifdef JUCI_ENABLE_DEBUG -#include "debug_lldb.h" -#endif -#include "dialogs.h" +#include "python_module.h" #include "terminal.h" -#include "git.h" - -PyObject *Plugins::Module::init_jucipp_module() { - auto api = py::module("Jucipp", "API"); - CMake::init_module(api); - CompileCommands::init_module(api); - Config::init_module(api); - Ctags::init_module(api); -#ifdef JUCI_ENABLE_DEBUG - Debug::LLDB::init_module(api); -#endif - Dialog::init_module(api); - Dispatcher::init_module(api); - Git::init_module(api); - Terminal::init_module(api); - return api.ptr(); -}; +#include "config.h" Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) { auto &config = Config::get(); @@ -47,10 +24,11 @@ void Plugins::load() { if((is_directory && !is_pycache) || has_py_extension) { try { auto module = interpreter.add_module(module_name); - if (module) { + if(module) { Terminal::get().print("Reloading plugin ´" + module_name + "´\n"); interpreter.reload_module(module); - } else { + } + else { Terminal::get().print("Loading plugin ´" + module_name + "´\n"); py::module::import(module_name.c_str()); } diff --git a/src/plugins.h b/src/plugins.h index 39f38a0..056be25 100644 --- a/src/plugins.h +++ b/src/plugins.h @@ -9,11 +9,6 @@ public: void load(); private: - class Module { - public: - static PyObject *init_jucipp_module(); - }; - py::detail::embedded_module jucipp_module; Python::Interpreter interpreter; }; diff --git a/src/python_module.cc b/src/python_module.cc new file mode 100644 index 0000000..69eedca --- /dev/null +++ b/src/python_module.cc @@ -0,0 +1,27 @@ +#include "python_module.h" +#include "cmake.h" +#include "compile_commands.h" +#include "config.h" +#include "ctags.h" +#ifdef JUCI_ENABLE_DEBUG +#include "debug_lldb.h" +#endif +#include "dialogs.h" +#include "terminal.h" +#include "git.h" + +PyObject *Module::init_jucipp_module() { + auto api = py::module("Jucipp", "API"); + CMake::init_module(api); + CompileCommands::init_module(api); + Config::init_module(api); + Ctags::init_module(api); +#ifdef JUCI_ENABLE_DEBUG + Debug::LLDB::init_module(api); +#endif + Dialog::init_module(api); + Dispatcher::init_module(api); + Git::init_module(api); + Terminal::init_module(api); + return api.ptr(); +} diff --git a/src/python_module.h b/src/python_module.h new file mode 100644 index 0000000..001655e --- /dev/null +++ b/src/python_module.h @@ -0,0 +1,7 @@ +#pragma once +#include "python_bind.h" + +class Module { +public: + static PyObject *init_jucipp_module(); +}; diff --git a/src/terminal.hpp b/src/terminal.hpp index 31cf1f6..c95cb4e 100644 --- a/src/terminal.hpp +++ b/src/terminal.hpp @@ -3,6 +3,7 @@ #include "mutex.hpp" #include "process.hpp" #include "python_bind.h" +#include "python_type_casters.h" #include "source_base.hpp" #include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 44ac878..258d557 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,9 +19,10 @@ add_library(test_stubs OBJECT stubs/notebook.cpp stubs/project.cpp stubs/selection_dialog.cpp + stubs/tooltips.cpp + stubs/plugins.cc ) -<<<<<<< HEAD if(BUILD_TESTING) add_executable(process_test process_test.cpp $) target_link_libraries(process_test juci_shared) @@ -121,7 +122,8 @@ if(BUILD_FUZZING) target_compile_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer) target_link_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer) target_link_libraries(markdown_fuzzer juci_shared) -======= +endif() + add_executable(process_test process_test.cc $) target_link_libraries(process_test juci_shared ${PYTHON_LIBRARIES}) add_test(process_test process_test) @@ -171,7 +173,6 @@ if(LIBLLDB_FOUND) target_link_libraries(lldb_test juci_shared ${PYTHON_LIBRARIES}) add_test(lldb_test lldb_test) add_subdirectory("lldb_test_files") ->>>>>>> de6858a... fix tests after moving pybind code endif() add_executable(git_test git_test.cc $) @@ -182,3 +183,4 @@ add_executable(python_interpreter_test python_interpreter_test.cc $) +add_test(pb_python_interpreter_test pb_python_interpreter_test) + +add_executable(pb_terminal_test Terminal_tests/terminal_test.cc) +target_link_libraries(pb_terminal_test juci_shared test_suite $) +add_test(pb_terminal_test pb_terminal_test) diff --git a/tests/python_bindings/PythonInterpreter_tests/interpreter_test.cc b/tests/python_bindings/PythonInterpreter_tests/interpreter_test.cc new file mode 100644 index 0000000..2ef7681 --- /dev/null +++ b/tests/python_bindings/PythonInterpreter_tests/interpreter_test.cc @@ -0,0 +1,25 @@ +#include "python_type_casters.h" +#include + +int main() { + { + suite test_suite("PythonInterpreter_tests"); + { + py::module::import("interpreter_test"); + test_suite.has_assertion = true; + } + } + { + suite test_suite("PythonInterpreter_tests"); + { + try { + py::module::import("exception_test"); + } + catch(const py::error_already_set &error) { + test_suite.has_assertion = true; + } + } + } + + return 0; +} diff --git a/tests/python_interpreter_test_files/basic_test.py b/tests/python_bindings/PythonInterpreter_tests/interpreter_test.py similarity index 100% rename from tests/python_interpreter_test_files/basic_test.py rename to tests/python_bindings/PythonInterpreter_tests/interpreter_test.py diff --git a/tests/python_interpreter_test_files/ls/hello_world.txt b/tests/python_bindings/Terminal_tests/ls/hello_world.txt similarity index 100% rename from tests/python_interpreter_test_files/ls/hello_world.txt rename to tests/python_bindings/Terminal_tests/ls/hello_world.txt diff --git a/tests/python_bindings/Terminal_tests/terminal_test.cc b/tests/python_bindings/Terminal_tests/terminal_test.cc new file mode 100644 index 0000000..89972b2 --- /dev/null +++ b/tests/python_bindings/Terminal_tests/terminal_test.cc @@ -0,0 +1,40 @@ +#include "test_suite.h" +#include "terminal.h" + +int main() { + const auto test_directory = "Terminal_tests"; + { + suite test_suite(test_directory); + { + auto &terminal = Terminal::get(); + auto connection = terminal.get_buffer()->signal_insert().connect([&](const Gtk::TextBuffer::iterator &, const Glib::ustring &msg, int) { + g_assert_cmpstr(msg.c_str(), ==, "Hello, World!\n"); + test_suite.has_assertion = true; + }); + auto module = py::module::import("terminal_test"); + module.attr("hello_world")(); + connection.disconnect(); + } + } + { + suite test_suite(test_directory); + { + auto &terminal = Terminal::get(); + auto connection = terminal.get_buffer()->signal_insert().connect([&](const Gtk::TextBuffer::iterator &, const Glib::ustring &msg, int) { + g_assert_cmpstr(msg.c_str(), ==, "hello_world.txt\n"); + test_suite.has_assertion = true; + test_suite.app->release(); + }); + test_suite.app->hold(); + std::thread thread([&] { + const auto ls_dir = test_suite.test_file_path / test_directory / "ls"; + auto module = py::module::import("terminal_test"); + auto res = module.attr("process")(ls_dir).cast(); + g_assert_cmpint(res, ==, 0); + }); + test_suite.app->run(); + thread.join(); + connection.disconnect(); + } + } +} \ No newline at end of file diff --git a/tests/python_interpreter_test_files/terminal_test.py b/tests/python_bindings/Terminal_tests/terminal_test.py similarity index 100% rename from tests/python_interpreter_test_files/terminal_test.py rename to tests/python_bindings/Terminal_tests/terminal_test.py diff --git a/tests/python_bindings/test_suite.cc b/tests/python_bindings/test_suite.cc new file mode 100644 index 0000000..ced4fcd --- /dev/null +++ b/tests/python_bindings/test_suite.cc @@ -0,0 +1,12 @@ +#include "test_suite.h" +#include "python_module.h" +#include + +suite::suite(const boost::filesystem::path &path) { + auto sys = py::module::import("sys"); + sys.attr("path").cast().append((test_file_path / path).string()); + config.terminal.history_size = 100; +} +suite::~suite() { + g_assert_true(has_assertion); +} diff --git a/tests/python_bindings/test_suite.h b/tests/python_bindings/test_suite.h new file mode 100644 index 0000000..95370cd --- /dev/null +++ b/tests/python_bindings/test_suite.h @@ -0,0 +1,16 @@ +#pragma once +#include "config.h" +#include "plugins.h" +#include + +class __attribute__((visibility("default"))) +suite { +public: + suite(const boost::filesystem::path &path); + Glib::RefPtr app = Gtk::Application::create(); + Config &config = Config::get(); + boost::filesystem::path test_file_path = boost::filesystem::canonical(std::string(JUCI_TESTS_PATH) + "/python_bindings"); + bool has_assertion = false; + Plugins plugins; + ~suite(); +}; diff --git a/tests/python_interpreter_test.cc b/tests/python_interpreter_test.cc deleted file mode 100644 index 7376a51..0000000 --- a/tests/python_interpreter_test.cc +++ /dev/null @@ -1,76 +0,0 @@ -#include "config.h" -#include "plugins.h" -#include "python_interpreter.h" -#include "terminal.h" -#include "config.h" -#include "python_type_casters.h" - - -class __attribute__((visibility("default"))) -suite { -public: - Glib::RefPtr app = Gtk::Application::create(); - Plugins plugins; - Terminal &terminal = Terminal::get(); - Config &config = Config::get(); - boost::filesystem::path test_file_path = boost::filesystem::canonical(std::string(JUCI_TESTS_PATH) + "/python_interpreter_test_files"); - bool has_assertion = false; - suite() { - auto sys = plugins.interpreter.add_module("sys"); - sys.attr("path").cast().append(test_file_path.string()); - config.terminal.history_size = 100; - } - ~suite() { - g_assert_true(has_assertion); - } -}; - -int main() { - { - suite test_suite; - { - py::module::import("basic_test"); - try { - py::module::import("exception_test"); - } - catch(const py::error_already_set &error) { - test_suite.has_assertion = true; - } - } - } - - { - suite test_suite; - { - auto connection = test_suite.terminal.get_buffer()->signal_insert().connect([&](const Gtk::TextBuffer::iterator &, const Glib::ustring &msg, int) { - g_assert_cmpstr(msg.c_str(), ==, "Hello, World!\n"); - test_suite.has_assertion = true; - }); - auto module = py::module::import("terminal_test"); - module.attr("hello_world")(); - connection.disconnect(); - } - } - { - suite test_suite; - { - auto connection = test_suite.terminal.get_buffer()->signal_insert().connect([&](const Gtk::TextBuffer::iterator &, const Glib::ustring &msg, int) { - g_assert_cmpstr(msg.c_str(), ==, "hello_world.txt\n"); - test_suite.has_assertion = true; - test_suite.app->release(); - }); - test_suite.app->hold(); - std::thread thread([&] { - const auto ls_dir = test_suite.test_file_path / "ls"; - auto module = py::module::import("terminal_test"); - auto res = module.attr("process")(ls_dir).cast(); - g_assert_cmpint(res, ==, 0); - }); - test_suite.app->run(); - thread.join(); - connection.disconnect(); - } - } - - return 0; -} diff --git a/tests/stubs/plugins.cc b/tests/stubs/plugins.cc new file mode 100644 index 0000000..c375e43 --- /dev/null +++ b/tests/stubs/plugins.cc @@ -0,0 +1,6 @@ +#include "plugins.h" +#include "python_module.h" + +Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {} + +void Plugins::load() {}