8
8
9
9
#include " doctest/doctest.h"
10
10
#include " xeus-cpp/xinterpreter.hpp"
11
+ #include " xeus-cpp/xholder.hpp"
12
+ #include " xeus-cpp/xmanager.hpp"
11
13
#include " xeus-cpp/xutils.hpp"
12
14
15
+ #include " ../src/xparser.hpp"
16
+
13
17
TEST_SUITE (" execute_request" )
14
18
{
15
19
TEST_CASE (" stl" )
@@ -38,10 +42,10 @@ TEST_SUITE("execute_request")
38
42
nl::json user_expressions = nl::json::object ();
39
43
40
44
nl::json result = interpreter.execute_request (
41
- code,
42
- false ,
43
- false ,
44
- user_expressions,
45
+ code,
46
+ false ,
47
+ false ,
48
+ user_expressions,
45
49
false
46
50
);
47
51
@@ -64,3 +68,263 @@ TEST_SUITE("extract_filename")
64
68
REQUIRE (argc == 2 );
65
69
}
66
70
}
71
+
72
+ TEST_SUITE (" trim" ){
73
+
74
+ TEST_CASE (" trim_basic_test" ){
75
+ std::string argument = " argument" ;
76
+
77
+ std::string result = xcpp::trim (argument);
78
+
79
+ REQUIRE (result == " argument" );
80
+ }
81
+
82
+ /* Checks if it trims the string which
83
+ has an empty space at the start and in the end*/
84
+ TEST_CASE (" trim_start_and_end" ){
85
+ std::string argument = " argument " ;
86
+
87
+ std::string result = xcpp::trim (argument);
88
+
89
+ REQUIRE (result == " argument" );
90
+ }
91
+
92
+ /* Checks if it trims the string which has no characters*/
93
+ TEST_CASE (" trim_empty" ){
94
+ std::string argument = " " ;
95
+
96
+ std::string result = xcpp::trim (argument);
97
+
98
+ REQUIRE (result == " " );
99
+ }
100
+
101
+ }
102
+
103
+ TEST_SUITE (" should_print_version" )
104
+ {
105
+ // This test case checks if the function `should_print_version` correctly identifies
106
+ // when the "--version" argument is passed. It sets up a scenario where "--version"
107
+ // is one of the command line arguments and checks if the function returns true.
108
+ TEST_CASE (" should_print_version_with_version_arg" )
109
+ {
110
+ char arg1[] = " program_name" ;
111
+ char arg2[] = " --version" ;
112
+ char * argv[] = {arg1, arg2};
113
+ int argc = 2 ;
114
+
115
+ bool result = xcpp::should_print_version (argc, argv);
116
+
117
+ REQUIRE (result == true );
118
+ }
119
+
120
+ // This test case checks if the function `should_print_version` correctly identifies
121
+ // when the "--version" argument is not passed. It sets up a scenario where "--version"
122
+ // is not one of the command line arguments and checks if the function returns false.
123
+ TEST_CASE (" should_print_version_without_version_arg" )
124
+ {
125
+ char arg1[] = " program_name" ;
126
+ char arg2[] = " -f" ;
127
+ char arg3[] = " filename" ;
128
+ char * argv[] = {arg1, arg2, arg3};
129
+ int argc = 3 ;
130
+
131
+ bool result = xcpp::should_print_version (argc, argv);
132
+
133
+ REQUIRE (result == false );
134
+ }
135
+ }
136
+
137
+ TEST_SUITE (" build_interpreter" )
138
+ {
139
+ // This test case checks if the function `build_interpreter` returns a non-null pointer
140
+ // when valid arguments are passed. It sets up a scenario with valid command line arguments
141
+ // and checks if the function returns a non-null pointer.
142
+ TEST_CASE (" build_interpreter_pointer_not_null" )
143
+ {
144
+ char arg1[] = " program_name" ;
145
+ char arg2[] = " -option1" ;
146
+ char * argv[] = {arg1, arg2};
147
+ int argc = 2 ;
148
+
149
+ interpreter_ptr interp_ptr = xcpp::build_interpreter (argc, argv);
150
+
151
+ REQUIRE (interp_ptr != nullptr );
152
+ }
153
+ }
154
+
155
+ TEST_SUITE (" is_match_magics_manager" )
156
+ {
157
+ // This test case checks if the function `is_match` correctly identifies strings that match
158
+ // the regex pattern used in `xmagics_manager`. It sets up a scenario where strings that should
159
+ // match the pattern are passed and checks if the function returns true.
160
+ TEST_CASE (" is_match_true" )
161
+ {
162
+ xcpp::xmagics_manager manager;
163
+
164
+ bool result1 = manager.is_match (" %%magic" );
165
+ bool result2 = manager.is_match (" %magic" );
166
+
167
+ REQUIRE (result1 == true );
168
+ REQUIRE (result2 == true );
169
+ }
170
+
171
+ // This test case checks if the function `is_match` correctly identifies strings that do not match
172
+ // the regex pattern used in `xmagics_manager`. It sets up a scenario where strings that should not
173
+ // match the pattern are passed and checks if the function returns false.
174
+ TEST_CASE (" is_match_false" )
175
+ {
176
+ xcpp::xmagics_manager manager;
177
+
178
+ bool result1 = manager.is_match (" not a magic" );
179
+ bool result2 = manager.is_match (" %%" );
180
+ bool result3 = manager.is_match (" %" );
181
+
182
+ REQUIRE (result1 == false );
183
+ REQUIRE (result2 == false );
184
+ REQUIRE (result3 == false );
185
+ }
186
+ }
187
+
188
+ TEST_SUITE (" clone_magics_manager" )
189
+ {
190
+ // This test case checks if the function `clone_magics_manager` returns a non-null pointer
191
+ // when called. It doesn't require any specific setup as it's testing the default behavior
192
+ // of the function, and checks if the function returns a non-null pointer.
193
+ TEST_CASE (" clone_magics_manager_not_null" )
194
+ {
195
+ xcpp::xmagics_manager manager;
196
+
197
+ xcpp::xpreamble* clone = manager.clone ();
198
+
199
+ REQUIRE (clone != nullptr );
200
+ }
201
+
202
+ // This test case checks if the function `clone_magics_manager` returns a cloned object
203
+ // of the same type as the original. It calls the function and checks if the type of the
204
+ // returned object matches the type of the original `magics_manager`.
205
+ TEST_CASE (" clone_magics_manager_same_type" )
206
+ {
207
+ xcpp::xmagics_manager manager;
208
+
209
+ xcpp::xpreamble* clone = manager.clone ();
210
+
211
+ REQUIRE (dynamic_cast <xcpp::xmagics_manager*>(clone) != nullptr );
212
+
213
+ delete clone;
214
+ }
215
+ }
216
+
217
+ TEST_SUITE (" xpreamble_manager_operator" )
218
+ {
219
+ // This test case checks if the `xpreamble_manager` correctly registers and accesses
220
+ // a `xpreamble` object. It sets up a scenario where a `xpreamble` object is registered
221
+ // with a name and checks if the object can be accessed using the same name.
222
+ TEST_CASE (" register_and_access" )
223
+ {
224
+ std::string name = " test" ;
225
+ xcpp::xpreamble_manager manager;
226
+ xcpp::xmagics_manager* magics = new xcpp::xmagics_manager ();
227
+ manager.register_preamble (name, magics);
228
+
229
+ xcpp::xholder_preamble& result = manager.operator [](name);
230
+
231
+ REQUIRE (&(result.get_cast <xcpp::xmagics_manager>()) == magics);
232
+ }
233
+ }
234
+
235
+ TEST_SUITE (" xbuffer" )
236
+ {
237
+ // This test case checks if the `xoutput_buffer` correctly calls the callback function
238
+ // when the buffer is flushed. It sets up a scenario where a `xoutput_buffer` object is
239
+ // created with a callback function, and checks if the callback function is called when
240
+ // the buffer is flushed.
241
+ TEST_CASE (" xoutput_buffer_calls_callback_on_sync" )
242
+ {
243
+ std::string callback_output;
244
+ auto callback = [&callback_output](const std::string& value)
245
+ {
246
+ callback_output = value;
247
+ };
248
+ xcpp::xoutput_buffer buffer (callback);
249
+ std::ostream stream (&buffer);
250
+
251
+ stream << " Hello, world!" ;
252
+ stream.flush ();
253
+
254
+ REQUIRE (callback_output == " Hello, world!" );
255
+ }
256
+
257
+ // This test case checks if the `xinput_buffer` correctly calls the callback function
258
+ // when the buffer is flushed. It sets up a scenario where a `xinput_buffer` object is
259
+ // created with a callback function, and checks if the callback function is called when
260
+ // the buffer is flushed.
261
+ TEST_CASE (" xinput_buffer_calls_callback_on_underflow" )
262
+ {
263
+ std::string callback_input;
264
+ auto callback = [&callback_input](std::string& value)
265
+ {
266
+ value = callback_input;
267
+ };
268
+ xcpp::xinput_buffer buffer (callback);
269
+ std::istream stream (&buffer);
270
+
271
+ callback_input = " Hello, world!" ;
272
+ std::string output;
273
+ std::getline (stream, output);
274
+
275
+ REQUIRE (output == " Hello, world!" );
276
+ }
277
+
278
+ // This test case checks if the `xoutput_buffer` correctly handles an empty output.
279
+ // It sets up a scenario where the `xoutput_buffer` is given an empty output, then checks
280
+ // if the buffer correctly identifies and handles this situation without errors or exceptions.
281
+ TEST_CASE (" xoutput_buffer_handles_empty_output" )
282
+ {
283
+ std::string callback_output;
284
+ auto callback = [&callback_output](const std::string& value)
285
+ {
286
+ callback_output = value;
287
+ };
288
+
289
+ xcpp::xoutput_buffer buffer (callback);
290
+ std::ostream stream (&buffer);
291
+
292
+ stream << " " ;
293
+ stream.flush ();
294
+
295
+ REQUIRE (callback_output == " " );
296
+ }
297
+
298
+ // This test case checks if the `xinput_buffer` correctly handles an empty input.
299
+ // It sets up a scenario where the `xinput_buffer` is given an empty input, then checks
300
+ // if the buffer correctly identifies and handles this situation without errors or exceptions.
301
+ TEST_CASE (" xinput_buffer_handles_empty_input" )
302
+ {
303
+ std::string callback_input = " " ;
304
+ auto callback = [&callback_input](std::string& value)
305
+ {
306
+ value = callback_input;
307
+ };
308
+
309
+ xcpp::xinput_buffer buffer (callback);
310
+ std::istream stream (&buffer);
311
+
312
+ std::string output;
313
+ std::getline (stream, output);
314
+
315
+ REQUIRE (output == " " );
316
+ }
317
+
318
+ // This test case checks if the `xnull` correctly discards the output.
319
+ // It sets up a scenario where the `xnull` is given some output, then checks
320
+ // if the output is correctly discarded and not stored or returned.
321
+ TEST_CASE (" xnull_discards_output" )
322
+ {
323
+ xcpp::xnull null_buf;
324
+ std::ostream null_stream (&null_buf);
325
+
326
+ null_stream << " Hello, world!" ;
327
+
328
+ REQUIRE (null_stream.good () == true );
329
+ }
330
+ }
0 commit comments