Skip to content

Commit 396e6a7

Browse files
anutosh491vgvassilev
authored andcommitted
Fixed inspect request and added a framework for testing different implementation requests
1 parent ff8c753 commit 396e6a7

File tree

8 files changed

+167
-10
lines changed

8 files changed

+167
-10
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ jobs:
5454
cd bld
5555
make install
5656
57+
- name: Test xeus-cpp C++
58+
shell: bash -l {0}
59+
run: |
60+
cd bld/test
61+
./test_xeus_cpp
62+
timeout-minutes: 4
63+
5764
- name: test
5865
shell: bash -l {0}
5966
run: |

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ OPTION(XEUS_CPP_BUILD_EXECUTABLE "Build the xcpp executable" ON)
5050
OPTION(XEUS_CPP_USE_SHARED_XEUS "Link xcpp with the xeus shared library (instead of the static library)" ON)
5151
OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (instead of the static library)" ON)
5252

53-
53+
# Test options
54+
OPTION(XEUS_CPP_BUILD_TESTS "xeus-cpp test suite" ON)
5455

5556
if(EMSCRIPTEN)
5657
add_compile_definitions(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
@@ -305,6 +306,13 @@ if(EMSCRIPTEN)
305306
xeus_wasm_link_options(xcpp "web,worker")
306307
endif()
307308

309+
# Tests
310+
# =====
311+
312+
if(XEUS_CPP_BUILD_TESTS)
313+
add_subdirectory(test)
314+
endif()
315+
308316
# Installation
309317
# ============
310318
include(CMakePackageConfigHelpers)

environment-dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ dependencies:
2020
- jupyter_kernel_test>=0.4.3
2121
- nbval
2222
- pytest-rerunfailures
23+
- doctest

src/xinspect.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ namespace xcpp
160160
tagfile = it->at("tagfile");
161161
std::string filename = tagfiles_dir + "/" + tagfile;
162162
pugi::xml_document doc;
163+
doc.load_file(filename.c_str());
163164
class_member_predicate predicate{typename_, "function", method[2]};
164165
auto node = doc.find_node(predicate);
165166
if (!node.empty())
@@ -193,6 +194,7 @@ namespace xcpp
193194
tagfile = it->at("tagfile");
194195
std::string filename = tagfiles_dir + "/" + tagfile;
195196
pugi::xml_document doc;
197+
doc.load_file(filename.c_str());
196198
for (auto c : check)
197199
{
198200
node_predicate predicate{c, find_string};
@@ -262,5 +264,36 @@ namespace xcpp
262264
kernel_res["status"] = "ok";
263265
}
264266
}
267+
268+
class xintrospection : public xpreamble
269+
{
270+
public:
271+
272+
using xpreamble::pattern;
273+
const std::string spattern = R"(^\?)";
274+
275+
xintrospection(clang::Interpreter& p)
276+
: m_interpreter{p}
277+
{
278+
pattern = spattern;
279+
}
280+
281+
void apply(const std::string& code, nl::json& kernel_res) override
282+
{
283+
std::regex re(spattern + R"((.*))");
284+
std::smatch to_inspect;
285+
std::regex_search(code, to_inspect, re);
286+
inspect(to_inspect[1], kernel_res, m_interpreter);
287+
}
288+
289+
virtual xpreamble* clone() const override
290+
{
291+
return new xintrospection(*this);
292+
}
293+
294+
private:
295+
296+
clang::Interpreter& m_interpreter;
297+
};
265298
}
266299
#endif

src/xinterpreter.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,8 @@ namespace xcpp
327327
// Attempt normal evaluation
328328

329329
try
330-
{ std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
331-
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
332-
auto inspect_request = is_inspect_request(code, re);
333-
if (inspect_request.first)
334-
{
335-
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
336-
}
337-
330+
{
338331
compilation_result = process_code(*m_interpreter, code, error_stream);
339-
340332
}
341333
catch (std::exception& e)
342334
{
@@ -589,6 +581,7 @@ namespace xcpp
589581

590582
void interpreter::init_preamble()
591583
{
584+
preamble_manager.register_preamble("introspection", new xintrospection(*m_interpreter));
592585
preamble_manager.register_preamble("magics", new xmagics_manager());
593586
preamble_manager.register_preamble("shell", new xsystem());
594587
}

test/CMakeLists.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#############################################################################
2+
# Copyright (c) 2023, xeus-cpp contributors #
3+
# #
4+
# Distributed under the terms of the BSD 3-Clause License. #
5+
# #
6+
# The full license is in the file LICENSE, distributed with this software. #
7+
#############################################################################
8+
9+
10+
# Unit tests
11+
# ==========
12+
13+
cmake_minimum_required(VERSION 3.1)
14+
15+
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
16+
project(xeus-cpp-test)
17+
18+
enable_testing()
19+
20+
find_package(xeus-cpp REQUIRED CONFIG)
21+
endif()
22+
23+
include(CheckCXXCompilerFlag)
24+
25+
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
26+
27+
if(CMAKE_CXX_COMPILER_ID MATCHES Clang OR CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Intel)
28+
add_compile_options(-Wunused-parameter -Wextra -Wreorder -Wconversion -Wsign-conversion)
29+
30+
CHECK_CXX_COMPILER_FLAG(-march=native HAS_MARCH_NATIVE)
31+
if (HAS_MARCH_NATIVE)
32+
add_compile_options(-march=native)
33+
endif()
34+
endif()
35+
36+
if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
37+
add_compile_options(/EHsc /MP /bigobj)
38+
add_link_options(/MANIFEST:NO)
39+
endif()
40+
41+
find_package(doctest)
42+
find_package(Threads)
43+
44+
set(XEUS_CPP_TESTS
45+
main.cpp
46+
test_interpreter.cpp
47+
)
48+
49+
add_executable(test_xeus_cpp ${XEUS_CPP_TESTS})
50+
51+
if (APPLE)
52+
set_target_properties(test_xeus_cpp PROPERTIES
53+
MACOSX_RPATH ON
54+
)
55+
else()
56+
set_target_properties(test_xeus_cpp PROPERTIES
57+
BUILD_WITH_INSTALL_RPATH 1
58+
SKIP_BUILD_RPATH FALSE
59+
)
60+
endif()
61+
62+
set_target_properties(test_xeus_cpp PROPERTIES
63+
INSTALL_RPATH_USE_LINK_PATH TRUE
64+
)
65+
66+
target_link_libraries(test_xeus_cpp xeus-cpp doctest::doctest ${CMAKE_THREAD_LIBS_INIT})
67+
target_include_directories(test_xeus_cpp PRIVATE ${XEUS_CPP_INCLUDE_DIR})
68+
69+
add_custom_target(xtest COMMAND test_xeus_cpp DEPENDS test_xeus_cpp)

test/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/***************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors
3+
*
4+
* Distributed under the terms of the BSD 3-Clause License.
5+
*
6+
* The full license is in the file LICENSE, distributed with this software.
7+
****************************************************************************/
8+
9+
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
10+
#include "doctest/doctest.h"

test/test_interpreter.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/***************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors
3+
*
4+
* Distributed under the terms of the BSD 3-Clause License.
5+
*
6+
* The full license is in the file LICENSE, distributed with this software.
7+
****************************************************************************/
8+
9+
#include "doctest/doctest.h"
10+
#include "xeus-cpp/xinterpreter.hpp"
11+
12+
TEST_SUITE("execute_request")
13+
{
14+
TEST_CASE("fetch_documentation")
15+
{
16+
17+
xcpp::interpreter interpreter(0, nullptr);
18+
19+
std::string code = "?std::vector";
20+
std::string inspect_result = "https://en.cppreference.com/w/cpp/container/vector";
21+
nl::json user_expressions = nl::json::object();
22+
23+
nl::json result = interpreter.execute_request(
24+
code,
25+
false,
26+
false,
27+
user_expressions,
28+
false
29+
);
30+
31+
REQUIRE(result["payload"][0]["data"]["text/plain"] == inspect_result);
32+
REQUIRE(result["user_expressions"] == nl::json::object());
33+
REQUIRE(result["found"] == true);
34+
REQUIRE(result["status"] == "ok");
35+
}
36+
}

0 commit comments

Comments
 (0)