Browse Source

refactor test suite to make room for tests of all bindings

python
Jørgen Lien Sellæg 7 years ago committed by Jørgen Sverre Lien Sellæg
parent
commit
91ba2234fe
  1. 3
      src/CMakeLists.txt
  2. 30
      src/plugins.cc
  3. 5
      src/plugins.h
  4. 27
      src/python_module.cc
  5. 7
      src/python_module.h
  6. 1
      src/terminal.hpp
  7. 8
      tests/CMakeLists.txt
  8. 13
      tests/python_bindings/CMakeLists.txt
  9. 25
      tests/python_bindings/PythonInterpreter_tests/interpreter_test.cc
  10. 0
      tests/python_bindings/PythonInterpreter_tests/interpreter_test.py
  11. 0
      tests/python_bindings/Terminal_tests/ls/hello_world.txt
  12. 40
      tests/python_bindings/Terminal_tests/terminal_test.cc
  13. 0
      tests/python_bindings/Terminal_tests/terminal_test.py
  14. 12
      tests/python_bindings/test_suite.cc
  15. 16
      tests/python_bindings/test_suite.h
  16. 76
      tests/python_interpreter_test.cc
  17. 6
      tests/stubs/plugins.cc

3
src/CMakeLists.txt

@ -26,6 +26,8 @@ set(JUCI_SHARED_FILES
tooltips.cpp tooltips.cpp
usages_clang.cpp usages_clang.cpp
utility.cpp utility.cpp
python_interpreter.cc
python_module.cc
) )
if(LIBLLDB_FOUND) if(LIBLLDB_FOUND)
list(APPEND JUCI_SHARED_FILES debug_lldb.cpp) list(APPEND JUCI_SHARED_FILES debug_lldb.cpp)
@ -56,6 +58,7 @@ set(JUCI_SOURCES
selection_dialog.cpp selection_dialog.cpp
tooltips.cpp tooltips.cpp
window.cpp window.cpp
plugins.cc
) )
if(APPLE) if(APPLE)

30
src/plugins.cc

@ -1,30 +1,7 @@
#include "plugins.h" #include "plugins.h"
#include "cmake.h" #include "python_module.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 "terminal.h"
#include "git.h" #include "config.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();
};
Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) { Plugins::Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) {
auto &config = Config::get(); auto &config = Config::get();
@ -50,7 +27,8 @@ void Plugins::load() {
if(module) { if(module) {
Terminal::get().print("Reloading plugin ´" + module_name + "´\n"); Terminal::get().print("Reloading plugin ´" + module_name + "´\n");
interpreter.reload_module(module); interpreter.reload_module(module);
} else { }
else {
Terminal::get().print("Loading plugin ´" + module_name + "´\n"); Terminal::get().print("Loading plugin ´" + module_name + "´\n");
py::module::import(module_name.c_str()); py::module::import(module_name.c_str());
} }

5
src/plugins.h

@ -9,11 +9,6 @@ public:
void load(); void load();
private: private:
class Module {
public:
static PyObject *init_jucipp_module();
};
py::detail::embedded_module jucipp_module; py::detail::embedded_module jucipp_module;
Python::Interpreter interpreter; Python::Interpreter interpreter;
}; };

27
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();
}

7
src/python_module.h

@ -0,0 +1,7 @@
#pragma once
#include "python_bind.h"
class Module {
public:
static PyObject *init_jucipp_module();
};

1
src/terminal.hpp

@ -3,6 +3,7 @@
#include "mutex.hpp" #include "mutex.hpp"
#include "process.hpp" #include "process.hpp"
#include "python_bind.h" #include "python_bind.h"
#include "python_type_casters.h"
#include "source_base.hpp" #include "source_base.hpp"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>

8
tests/CMakeLists.txt

@ -19,9 +19,10 @@ add_library(test_stubs OBJECT
stubs/notebook.cpp stubs/notebook.cpp
stubs/project.cpp stubs/project.cpp
stubs/selection_dialog.cpp stubs/selection_dialog.cpp
stubs/tooltips.cpp
stubs/plugins.cc
) )
<<<<<<< HEAD
if(BUILD_TESTING) if(BUILD_TESTING)
add_executable(process_test process_test.cpp $<TARGET_OBJECTS:test_stubs>) add_executable(process_test process_test.cpp $<TARGET_OBJECTS:test_stubs>)
target_link_libraries(process_test juci_shared) target_link_libraries(process_test juci_shared)
@ -121,7 +122,8 @@ if(BUILD_FUZZING)
target_compile_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer) target_compile_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer)
target_link_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer) target_link_options(markdown_fuzzer PRIVATE -fsanitize=address,fuzzer)
target_link_libraries(markdown_fuzzer juci_shared) target_link_libraries(markdown_fuzzer juci_shared)
======= endif()
add_executable(process_test process_test.cc $<TARGET_OBJECTS:test_stubs>) add_executable(process_test process_test.cc $<TARGET_OBJECTS:test_stubs>)
target_link_libraries(process_test juci_shared ${PYTHON_LIBRARIES}) target_link_libraries(process_test juci_shared ${PYTHON_LIBRARIES})
add_test(process_test process_test) add_test(process_test process_test)
@ -171,7 +173,6 @@ if(LIBLLDB_FOUND)
target_link_libraries(lldb_test juci_shared ${PYTHON_LIBRARIES}) target_link_libraries(lldb_test juci_shared ${PYTHON_LIBRARIES})
add_test(lldb_test lldb_test) add_test(lldb_test lldb_test)
add_subdirectory("lldb_test_files") add_subdirectory("lldb_test_files")
>>>>>>> de6858a... fix tests after moving pybind code
endif() endif()
add_executable(git_test git_test.cc $<TARGET_OBJECTS:test_stubs>) add_executable(git_test git_test.cc $<TARGET_OBJECTS:test_stubs>)
@ -182,3 +183,4 @@ add_executable(python_interpreter_test python_interpreter_test.cc $<TARGET_OBJEC
target_link_libraries(python_interpreter_test juci_shared ${PYTHON_LIBRARIES}) target_link_libraries(python_interpreter_test juci_shared ${PYTHON_LIBRARIES})
add_test(python_interpreter_test python_interpreter_test) add_test(python_interpreter_test python_interpreter_test)
add_subdirectory(./python_bindings)

13
tests/python_bindings/CMakeLists.txt

@ -0,0 +1,13 @@
add_library(test_suite test_suite.cc)
target_link_libraries(test_suite juci_shared ${PYTHON_LIBRARIES})
include_directories(${CMAKE_SOURCE_DIR}/tests/python_bindings)
add_executable(pb_python_interpreter_test PythonInterpreter_tests/interpreter_test.cc)
target_link_libraries(pb_python_interpreter_test juci_shared test_suite $<TARGET_OBJECTS:test_stubs>)
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 $<TARGET_OBJECTS:test_stubs>)
add_test(pb_terminal_test pb_terminal_test)

25
tests/python_bindings/PythonInterpreter_tests/interpreter_test.cc

@ -0,0 +1,25 @@
#include "python_type_casters.h"
#include <test_suite.h>
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;
}

0
tests/python_interpreter_test_files/basic_test.py → tests/python_bindings/PythonInterpreter_tests/interpreter_test.py

0
tests/python_interpreter_test_files/ls/hello_world.txt → tests/python_bindings/Terminal_tests/ls/hello_world.txt

40
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<int>();
g_assert_cmpint(res, ==, 0);
});
test_suite.app->run();
thread.join();
connection.disconnect();
}
}
}

0
tests/python_interpreter_test_files/terminal_test.py → tests/python_bindings/Terminal_tests/terminal_test.py

12
tests/python_bindings/test_suite.cc

@ -0,0 +1,12 @@
#include "test_suite.h"
#include "python_module.h"
#include <iostream>
suite::suite(const boost::filesystem::path &path) {
auto sys = py::module::import("sys");
sys.attr("path").cast<py::list>().append((test_file_path / path).string());
config.terminal.history_size = 100;
}
suite::~suite() {
g_assert_true(has_assertion);
}

16
tests/python_bindings/test_suite.h

@ -0,0 +1,16 @@
#pragma once
#include "config.h"
#include "plugins.h"
#include <gtkmm.h>
class __attribute__((visibility("default")))
suite {
public:
suite(const boost::filesystem::path &path);
Glib::RefPtr<Gtk::Application> 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();
};

76
tests/python_interpreter_test.cc

@ -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<Gtk::Application> 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<py::list>().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<int>();
g_assert_cmpint(res, ==, 0);
});
test_suite.app->run();
thread.join();
connection.disconnect();
}
}
return 0;
}

6
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() {}
Loading…
Cancel
Save