Skip to content

Commit

Permalink
Fixed inspect request and added a framework for testing different imp…
Browse files Browse the repository at this point in the history
…lementation requests
  • Loading branch information
anutosh491 authored and vgvassilev committed Jan 8, 2024
1 parent ff8c753 commit 396e6a7
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ jobs:
cd bld
make install
- name: Test xeus-cpp C++
shell: bash -l {0}
run: |
cd bld/test
./test_xeus_cpp
timeout-minutes: 4

- name: test
shell: bash -l {0}
run: |
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ OPTION(XEUS_CPP_BUILD_EXECUTABLE "Build the xcpp executable" ON)
OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON)
OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON)


# Test options
OPTION(XEUS_CPP_BUILD_TESTS "xeus-cpp test suite" ON)

if(EMSCRIPTEN)
add_compile_definitions(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
Expand Down Expand Up @@ -305,6 +306,13 @@ if(EMSCRIPTEN)
xeus_wasm_link_options(xcpp "web,worker")
endif()

# Tests
# =====

if(XEUS_CPP_BUILD_TESTS)
add_subdirectory(test)
endif()

# Installation
# ============
include(CMakePackageConfigHelpers)
Expand Down
1 change: 1 addition & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ dependencies:
- jupyter_kernel_test>=0.4.3
- nbval
- pytest-rerunfailures
- doctest
33 changes: 33 additions & 0 deletions src/xinspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace xcpp
tagfile = it->at("tagfile");
std::string filename = tagfiles_dir + "/" + tagfile;
pugi::xml_document doc;
doc.load_file(filename.c_str());
class_member_predicate predicate{typename_, "function", method[2]};
auto node = doc.find_node(predicate);
if (!node.empty())
Expand Down Expand Up @@ -193,6 +194,7 @@ namespace xcpp
tagfile = it->at("tagfile");
std::string filename = tagfiles_dir + "/" + tagfile;
pugi::xml_document doc;
doc.load_file(filename.c_str());
for (auto c : check)
{
node_predicate predicate{c, find_string};
Expand Down Expand Up @@ -262,5 +264,36 @@ namespace xcpp
kernel_res["status"] = "ok";
}
}

class xintrospection : public xpreamble
{
public:

using xpreamble::pattern;
const std::string spattern = R"(^\?)";

xintrospection(clang::Interpreter& p)
: m_interpreter{p}
{
pattern = spattern;
}

void apply(const std::string& code, nl::json& kernel_res) override
{
std::regex re(spattern + R"((.*))");
std::smatch to_inspect;
std::regex_search(code, to_inspect, re);
inspect(to_inspect[1], kernel_res, m_interpreter);
}

virtual xpreamble* clone() const override
{
return new xintrospection(*this);
}

private:

clang::Interpreter& m_interpreter;
};
}
#endif
11 changes: 2 additions & 9 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,8 @@ namespace xcpp
// Attempt normal evaluation

try
{ std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
auto inspect_request = is_inspect_request(code, re);
if (inspect_request.first)
{
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
}

{
compilation_result = process_code(*m_interpreter, code, error_stream);

}
catch (std::exception& e)
{
Expand Down Expand Up @@ -589,6 +581,7 @@ namespace xcpp

void interpreter::init_preamble()
{
preamble_manager.register_preamble("introspection", new xintrospection(*m_interpreter));
preamble_manager.register_preamble("magics", new xmagics_manager());
preamble_manager.register_preamble("shell", new xsystem());
}
Expand Down
69 changes: 69 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#############################################################################
# Copyright (c) 2023, xeus-cpp contributors #
# #
# Distributed under the terms of the BSD 3-Clause License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
#############################################################################


# Unit tests
# ==========

cmake_minimum_required(VERSION 3.1)

if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
project(xeus-cpp-test)

enable_testing()

find_package(xeus-cpp REQUIRED CONFIG)
endif()

include(CheckCXXCompilerFlag)

string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)

if(CMAKE_CXX_COMPILER_ID MATCHES Clang OR CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Intel)
add_compile_options(-Wunused-parameter -Wextra -Wreorder -Wconversion -Wsign-conversion)

CHECK_CXX_COMPILER_FLAG(-march=native HAS_MARCH_NATIVE)
if (HAS_MARCH_NATIVE)
add_compile_options(-march=native)
endif()
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
add_compile_options(/EHsc /MP /bigobj)
add_link_options(/MANIFEST:NO)
endif()

find_package(doctest)
find_package(Threads)

set(XEUS_CPP_TESTS
main.cpp
test_interpreter.cpp
)

add_executable(test_xeus_cpp ${XEUS_CPP_TESTS})

if (APPLE)
set_target_properties(test_xeus_cpp PROPERTIES
MACOSX_RPATH ON
)
else()
set_target_properties(test_xeus_cpp PROPERTIES
BUILD_WITH_INSTALL_RPATH 1
SKIP_BUILD_RPATH FALSE
)
endif()

set_target_properties(test_xeus_cpp PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
)

target_link_libraries(test_xeus_cpp xeus-cpp doctest::doctest ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(test_xeus_cpp PRIVATE ${XEUS_CPP_INCLUDE_DIR})

add_custom_target(xtest COMMAND test_xeus_cpp DEPENDS test_xeus_cpp)
10 changes: 10 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/***************************************************************************
* Copyright (c) 2023, xeus-cpp contributors
*
* Distributed under the terms of the BSD 3-Clause License.
*
* The full license is in the file LICENSE, distributed with this software.
****************************************************************************/

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
36 changes: 36 additions & 0 deletions test/test_interpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/***************************************************************************
* Copyright (c) 2023, xeus-cpp contributors
*
* Distributed under the terms of the BSD 3-Clause License.
*
* The full license is in the file LICENSE, distributed with this software.
****************************************************************************/

#include "doctest/doctest.h"
#include "xeus-cpp/xinterpreter.hpp"

TEST_SUITE("execute_request")
{
TEST_CASE("fetch_documentation")
{

xcpp::interpreter interpreter(0, nullptr);

std::string code = "?std::vector";
std::string inspect_result = "https://en.cppreference.com/w/cpp/container/vector";
nl::json user_expressions = nl::json::object();

nl::json result = interpreter.execute_request(
code,
false,
false,
user_expressions,
false
);

REQUIRE(result["payload"][0]["data"]["text/plain"] == inspect_result);
REQUIRE(result["user_expressions"] == nl::json::object());
REQUIRE(result["found"] == true);
REQUIRE(result["status"] == "ok");
}
}

0 comments on commit 396e6a7

Please sign in to comment.