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-2021
   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 <http://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,\
  47         ret, flag\
  48     ); \
  49 }
  50 
  51 /* --------------------------------------------------------------------------------------------- */
  52 
  53 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
  54 /* *INDENT-OFF* */
  55 static const struct test_regex_replace_esc_seq_prepare_ds
  56 {
  57     const char *input_string;
  58     const size_t input_pos;
  59 
  60     const gboolean expected_result;
  61     const gsize expected_skipped_len;
  62     const int expected_flags;
  63 } test_regex_replace_esc_seq_prepare_ds[] =
  64 {
  65     { /* 0. \\{123} */
  66         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  67         7,
  68         FALSE,
  69         6,
  70         REPLACE_PREPARE_T_ESCAPE_SEQ
  71     },
  72     { /* 1. \\xab */
  73         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  74         20,
  75         FALSE,
  76         4,
  77         REPLACE_PREPARE_T_ESCAPE_SEQ
  78     },
  79     { /* 2. \\x{456abcd}  */
  80         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  81         36,
  82         FALSE,
  83         11,
  84         REPLACE_PREPARE_T_ESCAPE_SEQ
  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     { /* 4. \\n */
  94         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  95         59,
  96         FALSE,
  97         2,
  98         REPLACE_PREPARE_T_ESCAPE_SEQ
  99     },
 100     { /* 5. \\t */
 101         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 102         61,
 103         FALSE,
 104         2,
 105         REPLACE_PREPARE_T_ESCAPE_SEQ
 106     },
 107     { /* 6. \\v */
 108         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 109         63,
 110         FALSE,
 111         2,
 112         REPLACE_PREPARE_T_ESCAPE_SEQ
 113     },
 114     { /* 7. \\b */
 115         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 116         65,
 117         FALSE,
 118         2,
 119         REPLACE_PREPARE_T_ESCAPE_SEQ
 120     },
 121     { /* 8. \\r */
 122         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 123         67,
 124         FALSE,
 125         2,
 126         REPLACE_PREPARE_T_ESCAPE_SEQ
 127     },
 128     { /* 9. \\f */
 129         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 130         69,
 131         FALSE,
 132         2,
 133         REPLACE_PREPARE_T_ESCAPE_SEQ
 134     },
 135     {  /* 10. \\a */
 136         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 137         71,
 138         FALSE,
 139         2,
 140         REPLACE_PREPARE_T_ESCAPE_SEQ
 141     },
 142     { /* 11. \\{123 */
 143         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 144         0,
 145         TRUE,
 146         5,
 147         REPLACE_PREPARE_T_NOTHING_SPECIAL
 148     },
 149     { /* 12. \\x{qwerty} */
 150         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 151         6,
 152         TRUE,
 153         3,
 154         REPLACE_PREPARE_T_NOTHING_SPECIAL
 155     },
 156     { /* 13. \\12} */
 157         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 158         17,
 159         TRUE,
 160         0,
 161         0
 162     },
 163     { /* 14. \\x{456a-bcd} */
 164         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 165         22,
 166         TRUE,
 167         7,
 168         REPLACE_PREPARE_T_NOTHING_SPECIAL
 169     },
 170     { /* 15. \\satre */
 171         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 172         41,
 173         TRUE,
 174         0,
 175         0
 176     },
 177 };
 178 /* *INDENT-ON* */
 179 
 180 /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
 181 /* *INDENT-OFF* */
 182 START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 183 /* *INDENT-ON* */
 184 {
 185     /* given */
 186     GString *replace_str;
 187     gsize actual_skipped_len = 0;
 188     int actual_flags = 0;
 189     gboolean actual_result;
 190 
 191     replace_str = g_string_new (data->input_string);
 192 
 193     /* when */
 194     actual_result =
 195         mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos, &actual_skipped_len,
 196                                                  &actual_flags);
 197 
 198     /* then */
 199     mctest_assert_int_eq (actual_result, data->expected_result);
 200     mctest_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
 201     mctest_assert_int_eq (actual_flags, data->expected_flags);
 202 
 203     g_string_free (replace_str, TRUE);
 204 }
 205 /* *INDENT-OFF* */
 206 END_PARAMETRIZED_TEST
 207 /* *INDENT-ON* */
 208 
 209 /* --------------------------------------------------------------------------------------------- */
 210 
 211 int
 212 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 213 {
 214     TCase *tc_core;
 215 
 216     tc_core = tcase_create ("Core");
 217 
 218     /* Add new tests here: *************** */
 219     mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
 220                                    test_regex_replace_esc_seq_prepare_ds);
 221     /* *********************************** */
 222 
 223     return mctest_run_all (tc_core);
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */

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