Skip to content

Commit f33ae90

Browse files
authored
Add test cases for xutils, xmagics_manager, xpreamble_manager and xbuffer (#36)
1 parent f80ca79 commit f33ae90

File tree

2 files changed

+237
-6
lines changed

2 files changed

+237
-6
lines changed

include/xeus-cpp/xholder.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
#include <nlohmann/json.hpp>
1717

1818
#include "xpreamble.hpp"
19+
#include "xeus_cpp_config.hpp"
1920

2021
namespace nl = nlohmann;
2122

2223
namespace xcpp
2324
{
24-
class xholder_preamble
25+
class XEUS_CPP_API xholder_preamble
2526
{
2627
public:
2728

test/test_interpreter.cpp

Lines changed: 235 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@
88

99
#include "doctest/doctest.h"
1010
#include "xeus-cpp/xinterpreter.hpp"
11+
#include "xeus-cpp/xholder.hpp"
12+
#include "xeus-cpp/xmanager.hpp"
1113
#include "xeus-cpp/xutils.hpp"
1214

1315
TEST_SUITE("execute_request")
1416
{
1517
TEST_CASE("fetch_documentation")
1618
{
17-
1819
xcpp::interpreter interpreter(0, nullptr);
1920

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

2425
nl::json result = interpreter.execute_request(
25-
code,
26-
false,
27-
false,
28-
user_expressions,
26+
code,
27+
false,
28+
false,
29+
user_expressions,
2930
false
3031
);
3132

@@ -48,3 +49,232 @@ TEST_SUITE("extract_filename")
4849
REQUIRE(argc == 2);
4950
}
5051
}
52+
53+
TEST_SUITE("should_print_version")
54+
{
55+
// This test case checks if the function `should_print_version` correctly identifies
56+
// when the "--version" argument is passed. It sets up a scenario where "--version"
57+
// is one of the command line arguments and checks if the function returns true.
58+
TEST_CASE("should_print_version_with_version_arg")
59+
{
60+
char arg1[] = "program_name";
61+
char arg2[] = "--version";
62+
char* argv[] = {arg1, arg2};
63+
int argc = 2;
64+
65+
bool result = xcpp::should_print_version(argc, argv);
66+
67+
REQUIRE(result == true);
68+
}
69+
70+
// This test case checks if the function `should_print_version` correctly identifies
71+
// when the "--version" argument is not passed. It sets up a scenario where "--version"
72+
// is not one of the command line arguments and checks if the function returns false.
73+
TEST_CASE("should_print_version_without_version_arg")
74+
{
75+
char arg1[] = "program_name";
76+
char arg2[] = "-f";
77+
char arg3[] = "filename";
78+
char* argv[] = {arg1, arg2, arg3};
79+
int argc = 3;
80+
81+
bool result = xcpp::should_print_version(argc, argv);
82+
83+
REQUIRE(result == false);
84+
}
85+
}
86+
87+
TEST_SUITE("build_interpreter")
88+
{
89+
// This test case checks if the function `build_interpreter` returns a non-null pointer
90+
// when valid arguments are passed. It sets up a scenario with valid command line arguments
91+
// and checks if the function returns a non-null pointer.
92+
TEST_CASE("build_interpreter_pointer_not_null")
93+
{
94+
char arg1[] = "program_name";
95+
char arg2[] = "-option1";
96+
char* argv[] = {arg1, arg2};
97+
int argc = 2;
98+
99+
interpreter_ptr interp_ptr = xcpp::build_interpreter(argc, argv);
100+
101+
REQUIRE(interp_ptr != nullptr);
102+
}
103+
}
104+
105+
TEST_SUITE("is_match_magics_manager")
106+
{
107+
// This test case checks if the function `is_match` correctly identifies strings that match
108+
// the regex pattern used in `xmagics_manager`. It sets up a scenario where strings that should
109+
// match the pattern are passed and checks if the function returns true.
110+
TEST_CASE("is_match_true")
111+
{
112+
xcpp::xmagics_manager manager;
113+
114+
bool result1 = manager.is_match("%%magic");
115+
bool result2 = manager.is_match("%magic");
116+
117+
REQUIRE(result1 == true);
118+
REQUIRE(result2 == true);
119+
}
120+
121+
// This test case checks if the function `is_match` correctly identifies strings that do not match
122+
// the regex pattern used in `xmagics_manager`. It sets up a scenario where strings that should not
123+
// match the pattern are passed and checks if the function returns false.
124+
TEST_CASE("is_match_false")
125+
{
126+
xcpp::xmagics_manager manager;
127+
128+
bool result1 = manager.is_match("not a magic");
129+
bool result2 = manager.is_match("%%");
130+
bool result3 = manager.is_match("%");
131+
132+
REQUIRE(result1 == false);
133+
REQUIRE(result2 == false);
134+
REQUIRE(result3 == false);
135+
}
136+
}
137+
138+
TEST_SUITE("clone_magics_manager")
139+
{
140+
// This test case checks if the function `clone_magics_manager` returns a non-null pointer
141+
// when called. It doesn't require any specific setup as it's testing the default behavior
142+
// of the function, and checks if the function returns a non-null pointer.
143+
TEST_CASE("clone_magics_manager_not_null")
144+
{
145+
xcpp::xmagics_manager manager;
146+
147+
xcpp::xpreamble* clone = manager.clone();
148+
149+
REQUIRE(clone != nullptr);
150+
}
151+
152+
// This test case checks if the function `clone_magics_manager` returns a cloned object
153+
// of the same type as the original. It calls the function and checks if the type of the
154+
// returned object matches the type of the original `magics_manager`.
155+
TEST_CASE("clone_magics_manager_same_type")
156+
{
157+
xcpp::xmagics_manager manager;
158+
159+
xcpp::xpreamble* clone = manager.clone();
160+
161+
REQUIRE(dynamic_cast<xcpp::xmagics_manager*>(clone) != nullptr);
162+
163+
delete clone;
164+
}
165+
}
166+
167+
TEST_SUITE("xpreamble_manager_operator")
168+
{
169+
// This test case checks if the `xpreamble_manager` correctly registers and accesses
170+
// a `xpreamble` object. It sets up a scenario where a `xpreamble` object is registered
171+
// with a name and checks if the object can be accessed using the same name.
172+
TEST_CASE("register_and_access")
173+
{
174+
std::string name = "test";
175+
xcpp::xpreamble_manager manager;
176+
xcpp::xmagics_manager* magics = new xcpp::xmagics_manager();
177+
manager.register_preamble(name, magics);
178+
179+
xcpp::xholder_preamble& result = manager.operator[](name);
180+
181+
REQUIRE(&(result.get_cast<xcpp::xmagics_manager>()) == magics);
182+
}
183+
}
184+
185+
TEST_SUITE("xbuffer")
186+
{
187+
// This test case checks if the `xoutput_buffer` correctly calls the callback function
188+
// when the buffer is flushed. It sets up a scenario where a `xoutput_buffer` object is
189+
// created with a callback function, and checks if the callback function is called when
190+
// the buffer is flushed.
191+
TEST_CASE("xoutput_buffer_calls_callback_on_sync")
192+
{
193+
std::string callback_output;
194+
auto callback = [&callback_output](const std::string& value)
195+
{
196+
callback_output = value;
197+
};
198+
xcpp::xoutput_buffer buffer(callback);
199+
std::ostream stream(&buffer);
200+
201+
stream << "Hello, world!";
202+
stream.flush();
203+
204+
REQUIRE(callback_output == "Hello, world!");
205+
}
206+
207+
// This test case checks if the `xinput_buffer` correctly calls the callback function
208+
// when the buffer is flushed. It sets up a scenario where a `xinput_buffer` object is
209+
// created with a callback function, and checks if the callback function is called when
210+
// the buffer is flushed.
211+
TEST_CASE("xinput_buffer_calls_callback_on_underflow")
212+
{
213+
std::string callback_input;
214+
auto callback = [&callback_input](std::string& value)
215+
{
216+
value = callback_input;
217+
};
218+
xcpp::xinput_buffer buffer(callback);
219+
std::istream stream(&buffer);
220+
221+
callback_input = "Hello, world!";
222+
std::string output;
223+
std::getline(stream, output);
224+
225+
REQUIRE(output == "Hello, world!");
226+
}
227+
228+
// This test case checks if the `xoutput_buffer` correctly handles an empty output.
229+
// It sets up a scenario where the `xoutput_buffer` is given an empty output, then checks
230+
// if the buffer correctly identifies and handles this situation without errors or exceptions.
231+
TEST_CASE("xoutput_buffer_handles_empty_output")
232+
{
233+
std::string callback_output;
234+
auto callback = [&callback_output](const std::string& value)
235+
{
236+
callback_output = value;
237+
};
238+
239+
xcpp::xoutput_buffer buffer(callback);
240+
std::ostream stream(&buffer);
241+
242+
stream << "";
243+
stream.flush();
244+
245+
REQUIRE(callback_output == "");
246+
}
247+
248+
// This test case checks if the `xinput_buffer` correctly handles an empty input.
249+
// It sets up a scenario where the `xinput_buffer` is given an empty input, then checks
250+
// if the buffer correctly identifies and handles this situation without errors or exceptions.
251+
TEST_CASE("xinput_buffer_handles_empty_input")
252+
{
253+
std::string callback_input = "";
254+
auto callback = [&callback_input](std::string& value)
255+
{
256+
value = callback_input;
257+
};
258+
259+
xcpp::xinput_buffer buffer(callback);
260+
std::istream stream(&buffer);
261+
262+
std::string output;
263+
std::getline(stream, output);
264+
265+
REQUIRE(output == "");
266+
}
267+
268+
// This test case checks if the `xnull` correctly discards the output.
269+
// It sets up a scenario where the `xnull` is given some output, then checks
270+
// if the output is correctly discarded and not stored or returned.
271+
TEST_CASE("xnull_discards_output")
272+
{
273+
xcpp::xnull null_buf;
274+
std::ostream null_stream(&null_buf);
275+
276+
null_stream << "Hello, world!";
277+
278+
REQUIRE(null_stream.good() == true);
279+
}
280+
}

0 commit comments

Comments
 (0)