This source file includes following definitions.
- message
- status_msg_init
- status_msg_deinit
- edit_search_update_callback
- edit_dialog_replace_show
- edit_dialog_replace_prompt_show
- edit_load_syntax
- edit_get_syntax_color
- edit_load_macro_cmd
- setup
- teardown
- test_replace_check
- START_TEST
- START_TEST
- START_TEST
- START_TEST
- START_TEST
- START_TEST
- START_TEST
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #define TEST_SUITE_NAME "/src/editor"
27
28 #include "tests/mctest.h"
29
30 #include <ctype.h>
31
32 #include "lib/charsets.h"
33 #include "src/selcodepage.h"
34
35 #include "src/editor/editwidget.h"
36 #include "src/editor/editmacros.h"
37 #include "src/editor/editsearch.h"
38
39 static WGroup owner;
40 static WEdit *test_edit;
41 static const char **replace_regex__from;
42 static const char **replace_regex__to;
43 static gboolean only_in_selection = FALSE;
44
45
46 static const char *test_regex_in = "qwe\n"
47 "qwe\n"
48 "qwe\n"
49 "qwe\n"
50 "qwe\n"
51 "qwe\n"
52 "qwe\n"
53 "qwe\n";
54
55
56
57
58
59 static const char *test_regex_out1 = "Xqwe\n"
60 "Xqwe\n"
61 "Xqwe\n"
62 "Xqwe\n"
63 "Xqwe\n"
64 "Xqwe\n"
65 "Xqwe\n"
66 "Xqwe\n";
67
68
69
70
71 static const char *test_regex_out2 = "Xwe\n"
72 "Xwe\n"
73 "Xwe\n"
74 "Xwe\n"
75 "Xwe\n"
76 "Xwe\n"
77 "Xwe\n"
78 "Xwe\n";
79
80
81 static const char *test_regex_selected_in = "qwe\n"
82 "qwe\n"
83 "qwe\n"
84 "qwe\n"
85 "qwe\n"
86 "qwe\n"
87 "qwe\n"
88 "qwe\n";
89
90
91
92
93 static const char *test1_regex_selected_out = "qwe\n"
94 "qwe\n"
95 "Xqwe\n"
96 "Xqwe\n"
97 "Xqwe\n"
98 "qwe\n"
99 "qwe\n"
100 "qwe\n";
101
102
103
104
105 static const char *test2_regex_selected_out = "qwe\n"
106 "qwe\n"
107 "qwe\n"
108 "Xqwe\n"
109 "Xqwe\n"
110 "qwe\n"
111 "qwe\n"
112 "qwe\n";
113
114 static const char *replace_regex_entire_string__from = "^.*";
115 static const char *replace_regex_entire_string__to = "X\\0";
116
117 static const char *replace_regex_insert_char_at_begin_of_string__from = "^";
118 static const char *replace_regex_replace_first_char_of_string__from = "^.";
119 static const char *replace_regex_begin_of_string__to = "X";
120
121
122
123 void edit_dialog_replace_show (WEdit *edit, const char *search_default, const char *replace_default,
124 char **search_text, char **replace_text);
125 int edit_dialog_replace_prompt_show (WEdit *edit, char *from_text, char *to_text, int xpos,
126 int ypos);
127
128
129
130 void
131 message (int flags, const char *title, const char *text, ...)
132 {
133 (void) flags;
134 (void) title;
135 (void) text;
136 }
137
138
139
140 void
141 status_msg_init (status_msg_t *sm, const char *title, double delay, status_msg_cb init_cb,
142 status_msg_update_cb update_cb, status_msg_cb deinit_cb)
143 {
144 (void) sm;
145 (void) title;
146 (void) delay;
147 (void) init_cb;
148 (void) update_cb;
149 (void) deinit_cb;
150 }
151
152
153
154 void
155 status_msg_deinit (status_msg_t *sm)
156 {
157 (void) sm;
158 }
159
160
161
162 mc_search_cbret_t
163 edit_search_update_callback (const void *user_data, off_t char_offset)
164 {
165 (void) user_data;
166 (void) char_offset;
167
168 return MC_SEARCH_CB_OK;
169 }
170
171
172
173 void
174 edit_dialog_replace_show (WEdit *edit, const char *search_default, const char *replace_default,
175 char **search_text, char **replace_text)
176 {
177 (void) edit;
178 (void) search_default;
179 (void) replace_default;
180
181 *search_text = g_strdup (*replace_regex__from);
182 *replace_text = g_strdup (*replace_regex__to);
183
184 edit_search_options.type = MC_SEARCH_T_REGEX;
185 edit_search_options.only_in_selection = only_in_selection;
186 }
187
188
189
190 int
191 edit_dialog_replace_prompt_show (WEdit *edit, char *from_text, char *to_text, int xpos, int ypos)
192 {
193 (void) edit;
194 (void) from_text;
195 (void) to_text;
196 (void) xpos;
197 (void) ypos;
198
199 return B_REPLACE_ALL;
200 }
201
202
203
204 void
205 edit_load_syntax (WEdit *edit, GPtrArray *pnames, const char *type)
206 {
207 (void) edit;
208 (void) pnames;
209 (void) type;
210 }
211
212
213
214
215 int
216 edit_get_syntax_color (WEdit *edit, off_t byte_index)
217 {
218 (void) edit;
219 (void) byte_index;
220
221 return 0;
222 }
223
224
225
226
227 gboolean
228 edit_load_macro_cmd (WEdit *edit)
229 {
230 (void) edit;
231
232 return FALSE;
233 }
234
235
236
237
238 static void
239 setup (void)
240 {
241 WRect r;
242
243 str_init_strings (NULL);
244
245 mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
246 load_codepages_list ();
247
248 edit_options.filesize_threshold = (char *) "64M";
249
250 rect_init (&r, 0, 0, 24, 80);
251 test_edit = edit_init (NULL, &r, NULL);
252 memset (&owner, 0, sizeof (owner));
253 group_add_widget (&owner, WIDGET (test_edit));
254
255 mc_global.source_codepage = 0;
256 mc_global.display_codepage = 0;
257 cp_source = "ASCII";
258 cp_display = "ASCII";
259
260 do_set_codepage (0);
261 edit_set_codeset (test_edit);
262 }
263
264
265
266
267 static void
268 teardown (void)
269 {
270 edit_clean (test_edit);
271 group_remove_widget (test_edit);
272 g_free (test_edit);
273
274 free_codepages_list ();
275 str_uninit_strings ();
276 }
277
278
279
280 static void
281 test_replace_check (const char *test_out)
282 {
283 GString *actual_replaced_str;
284
285 actual_replaced_str = g_string_new ("");
286
287 for (off_t i = 0; i < test_edit->buffer.size; i++)
288 {
289 const int chr = edit_buffer_get_byte (&test_edit->buffer, i);
290
291 g_string_append_c (actual_replaced_str, chr);
292 }
293
294 mctest_assert_str_eq (actual_replaced_str->str, test_out);
295 g_string_free (actual_replaced_str, TRUE);
296 }
297
298
299
300
301 START_TEST (test_replace_regex__entire_string)
302 {
303
304 only_in_selection = FALSE;
305 replace_regex__from = &replace_regex_entire_string__from;
306 replace_regex__to = &replace_regex_entire_string__to;
307 test_edit->mark1 = 0;
308 test_edit->mark2 = 0;
309
310 for (const char *ti = test_regex_in; *ti != '\0'; ti++)
311 edit_buffer_insert (&test_edit->buffer, *ti);
312
313
314 edit_cursor_move (test_edit, 0);
315 edit_replace_cmd (test_edit, FALSE);
316
317
318 test_replace_check (test_regex_out1);
319 }
320 END_TEST
321
322
323
324
325 START_TEST (test_replace_regex__insert_char_at_begin_of_string)
326 {
327
328 only_in_selection = FALSE;
329 replace_regex__from = &replace_regex_insert_char_at_begin_of_string__from;
330 replace_regex__to = &replace_regex_begin_of_string__to;
331 test_edit->mark1 = 0;
332 test_edit->mark2 = 0;
333
334 for (const char *ti = test_regex_in; *ti != '\0'; ti++)
335 edit_buffer_insert (&test_edit->buffer, *ti);
336
337
338 edit_cursor_move (test_edit, 0);
339 edit_replace_cmd (test_edit, FALSE);
340
341
342 test_replace_check (test_regex_out1);
343 }
344 END_TEST
345
346
347
348
349 START_TEST (test_replace_regex__replace_first_char_of_string)
350 {
351
352 only_in_selection = FALSE;
353 replace_regex__from = &replace_regex_replace_first_char_of_string__from;
354 replace_regex__to = &replace_regex_begin_of_string__to;
355 test_edit->mark1 = 0;
356 test_edit->mark2 = 0;
357
358 for (const char *ti = test_regex_in; *ti != '\0'; ti++)
359 edit_buffer_insert (&test_edit->buffer, *ti);
360
361
362 edit_cursor_move (test_edit, 0);
363 edit_replace_cmd (test_edit, FALSE);
364
365
366 test_replace_check (test_regex_out2);
367 }
368 END_TEST
369
370
371
372
373 START_TEST (test_replace_regex__in_selection_top_down_1)
374 {
375
376 only_in_selection = TRUE;
377 replace_regex__from = &replace_regex_entire_string__from;
378 replace_regex__to = &replace_regex_entire_string__to;
379 test_edit->mark1 = 8;
380 test_edit->mark2 = 20;
381
382 for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
383 edit_buffer_insert (&test_edit->buffer, *ti);
384
385
386 edit_cursor_move (test_edit, 0);
387 edit_replace_cmd (test_edit, FALSE);
388
389
390 test_replace_check (test1_regex_selected_out);
391 }
392 END_TEST
393
394
395
396
397 START_TEST (test_replace_regex__in_selection_top_down_2)
398 {
399
400 only_in_selection = TRUE;
401 replace_regex__from = &replace_regex_entire_string__from;
402 replace_regex__to = &replace_regex_entire_string__to;
403 test_edit->mark1 = 9;
404 test_edit->mark2 = 20;
405
406 for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
407 edit_buffer_insert (&test_edit->buffer, *ti);
408
409
410 edit_cursor_move (test_edit, 0);
411 edit_replace_cmd (test_edit, FALSE);
412
413
414 test_replace_check (test2_regex_selected_out);
415 }
416 END_TEST
417
418
419
420
421 START_TEST (test_replace_regex__in_selection_bottom_up_1)
422 {
423
424 only_in_selection = TRUE;
425 replace_regex__from = &replace_regex_entire_string__from;
426 replace_regex__to = &replace_regex_entire_string__to;
427 test_edit->mark1 = 20;
428 test_edit->mark2 = 8;
429
430 for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
431 edit_buffer_insert (&test_edit->buffer, *ti);
432
433
434 edit_cursor_move (test_edit, 0);
435 edit_replace_cmd (test_edit, FALSE);
436
437
438 test_replace_check (test1_regex_selected_out);
439 }
440 END_TEST
441
442
443
444
445 START_TEST (test_replace_regex__in_selection_bottom_up_2)
446 {
447
448 only_in_selection = TRUE;
449 replace_regex__from = &replace_regex_entire_string__from;
450 replace_regex__to = &replace_regex_entire_string__to;
451 test_edit->mark1 = 20;
452 test_edit->mark2 = 9;
453
454 for (const char *ti = test_regex_selected_in; *ti != '\0'; ti++)
455 edit_buffer_insert (&test_edit->buffer, *ti);
456
457
458 edit_cursor_move (test_edit, 0);
459 edit_replace_cmd (test_edit, FALSE);
460
461
462 test_replace_check (test2_regex_selected_out);
463 }
464 END_TEST
465
466
467
468 int
469 main (void)
470 {
471 TCase *tc_core;
472
473 tc_core = tcase_create ("Core");
474
475 tcase_add_checked_fixture (tc_core, setup, teardown);
476
477
478 tcase_add_test (tc_core, test_replace_regex__entire_string);
479 tcase_add_test (tc_core, test_replace_regex__insert_char_at_begin_of_string);
480 tcase_add_test (tc_core, test_replace_regex__replace_first_char_of_string);
481 tcase_add_test (tc_core, test_replace_regex__in_selection_top_down_1);
482 tcase_add_test (tc_core, test_replace_regex__in_selection_top_down_2);
483 tcase_add_test (tc_core, test_replace_regex__in_selection_bottom_up_1);
484 tcase_add_test (tc_core, test_replace_regex__in_selection_bottom_up_2);
485
486
487 return mctest_run_all (tc_core);
488 }
489
490