root/tests/lib/search/regex_replace_esc_seq.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. START_PARAMETRIZED_TEST
  2. main

   1 /*
   2    libmc - checks for processing esc sequences in replace string
   3 
   4    Copyright (C) 2011-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "lib/search/regex"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "regex.c"  // for testing static functions
  31 
  32 /* --------------------------------------------------------------------------------------------- */
  33 #define test_helper_check_valid_data(a, b, c, d, e, f)                                             \
  34     {                                                                                              \
  35         ck_assert_msg (a == b, "ret_value != %s", (b) ? "TRUE" : "FALSE");                         \
  36         ck_assert_msg (c == d, "skip_len(%d) != %d", c, d);                                        \
  37         if (f != 0)                                                                                \
  38             ck_assert_msg (e == f, "ret(%d) != %d", e, f);                                         \
  39     }
  40 
  41 #define test_helper_handle_esc_seq(pos, r, skip, flag)                                             \
  42     {                                                                                              \
  43         skip_len = 0;                                                                              \
  44         test_helper_check_valid_data (                                                             \
  45             mc_search_regex__replace_handle_esc_seq (replace_str, pos, &skip_len, &ret), r,        \
  46             skip_len, skip, ret, flag);                                                            \
  47     }
  48 
  49 /* --------------------------------------------------------------------------------------------- */
  50 
  51 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
  52 static const struct test_regex_replace_esc_seq_prepare_ds
  53 {
  54     const char *input_string;
  55     const size_t input_pos;
  56 
  57     const gboolean expected_result;
  58     const gsize expected_skipped_len;
  59     const int expected_flags;
  60 } test_regex_replace_esc_seq_prepare_ds[] = {
  61     {
  62         // 0. \\{123}
  63         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  64         7,
  65         FALSE,
  66         6,
  67         REPLACE_PREPARE_T_ESCAPE_SEQ,
  68     },
  69     {
  70         // 1. \\xab
  71         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  72         20,
  73         FALSE,
  74         4,
  75         REPLACE_PREPARE_T_ESCAPE_SEQ,
  76     },
  77     {
  78         // 2. \\x{456abcd}
  79         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  80         36,
  81         FALSE,
  82         11,
  83         REPLACE_PREPARE_T_ESCAPE_SEQ,
  84     },
  85     {
  86         // 3. \\xtre
  87         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  88         54,
  89         FALSE,
  90         2,
  91         REPLACE_PREPARE_T_NOTHING_SPECIAL,
  92     },
  93     {
  94         // 4. \\n
  95         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  96         59,
  97         FALSE,
  98         2,
  99         REPLACE_PREPARE_T_ESCAPE_SEQ,
 100     },
 101     {
 102         // 5. \\t
 103         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 104         61,
 105         FALSE,
 106         2,
 107         REPLACE_PREPARE_T_ESCAPE_SEQ,
 108     },
 109     {
 110         // 6. \\v
 111         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 112         63,
 113         FALSE,
 114         2,
 115         REPLACE_PREPARE_T_ESCAPE_SEQ,
 116     },
 117     {
 118         // 7. \\b
 119         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 120         65,
 121         FALSE,
 122         2,
 123         REPLACE_PREPARE_T_ESCAPE_SEQ,
 124     },
 125     {
 126         // 8. \\r
 127         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 128         67,
 129         FALSE,
 130         2,
 131         REPLACE_PREPARE_T_ESCAPE_SEQ,
 132     },
 133     {
 134         // 9. \\f
 135         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 136         69,
 137         FALSE,
 138         2,
 139         REPLACE_PREPARE_T_ESCAPE_SEQ,
 140     },
 141     {
 142         // 10. \\a
 143         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 144         71,
 145         FALSE,
 146         2,
 147         REPLACE_PREPARE_T_ESCAPE_SEQ,
 148     },
 149     {
 150         // 11. \\{123
 151         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 152         0,
 153         TRUE,
 154         5,
 155         REPLACE_PREPARE_T_NOTHING_SPECIAL,
 156     },
 157     {
 158         // 12. \\x{qwerty}
 159         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 160         6,
 161         TRUE,
 162         3,
 163         REPLACE_PREPARE_T_NOTHING_SPECIAL,
 164     },
 165     {
 166         // 13. \\12}
 167         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 168         17,
 169         TRUE,
 170         0,
 171         0,
 172     },
 173     {
 174         // 14. \\x{456a-bcd}
 175         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 176         22,
 177         TRUE,
 178         7,
 179         REPLACE_PREPARE_T_NOTHING_SPECIAL,
 180     },
 181     {
 182         // 15. \\satre
 183         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 184         41,
 185         TRUE,
 186         0,
 187         0,
 188     },
 189 };
 190 
 191 /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
 192 START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 193 {
 194     // given
 195     GString *replace_str;
 196     gsize actual_skipped_len = 0;
 197     int actual_flags = 0;
 198     gboolean actual_result;
 199 
 200     replace_str = g_string_new (data->input_string);
 201 
 202     // when
 203     actual_result = mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos,
 204                                                              &actual_skipped_len, &actual_flags);
 205 
 206     // then
 207     ck_assert_int_eq (actual_result, data->expected_result);
 208     ck_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
 209     ck_assert_int_eq (actual_flags, data->expected_flags);
 210 
 211     g_string_free (replace_str, TRUE);
 212 }
 213 END_PARAMETRIZED_TEST
 214 
 215 /* --------------------------------------------------------------------------------------------- */
 216 
 217 int
 218 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 219 {
 220     TCase *tc_core;
 221 
 222     tc_core = tcase_create ("Core");
 223 
 224     // Add new tests here: ***************
 225     mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
 226                                    test_regex_replace_esc_seq_prepare_ds);
 227     // ***********************************
 228 
 229     return mctest_run_all (tc_core);
 230 }
 231 
 232 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */