regex_process_escape_sequence.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. libmc - checks for processing esc sequences in replace string
  3. Copyright (C) 2011-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "lib/search/regex"
  20. #include "tests/mctest.h"
  21. #include "regex.c" // for testing static functions
  22. /* --------------------------------------------------------------------------------------------- */
  23. /* @DataSource("test_regex_process_escape_sequence_ds") */
  24. static const struct test_regex_process_escape_sequence_ds
  25. {
  26. const char *input_from;
  27. const replace_transform_type_t input_initial_flags;
  28. const gboolean input_use_utf;
  29. const char *expected_string;
  30. } test_regex_process_escape_sequence_ds[] = {
  31. {
  32. // 0.
  33. "{101}",
  34. REPLACE_T_NO_TRANSFORM,
  35. FALSE,
  36. "A",
  37. },
  38. {
  39. // 1.
  40. "x42",
  41. REPLACE_T_NO_TRANSFORM,
  42. FALSE,
  43. "B",
  44. },
  45. {
  46. // 2.
  47. "x{444}",
  48. REPLACE_T_NO_TRANSFORM,
  49. FALSE,
  50. "D",
  51. },
  52. {
  53. // 3.
  54. "x{444}",
  55. REPLACE_T_NO_TRANSFORM,
  56. TRUE,
  57. "ф",
  58. },
  59. {
  60. // 4.
  61. "n",
  62. REPLACE_T_NO_TRANSFORM,
  63. FALSE,
  64. "\n",
  65. },
  66. {
  67. // 5.
  68. "t",
  69. REPLACE_T_NO_TRANSFORM,
  70. FALSE,
  71. "\t",
  72. },
  73. {
  74. // 6.
  75. "v",
  76. REPLACE_T_NO_TRANSFORM,
  77. FALSE,
  78. "\v",
  79. },
  80. {
  81. // 7.
  82. "b",
  83. REPLACE_T_NO_TRANSFORM,
  84. FALSE,
  85. "\b",
  86. },
  87. {
  88. // 8.
  89. "r",
  90. REPLACE_T_NO_TRANSFORM,
  91. FALSE,
  92. "\r",
  93. },
  94. {
  95. // 9.
  96. "f",
  97. REPLACE_T_NO_TRANSFORM,
  98. FALSE,
  99. "\f",
  100. },
  101. {
  102. // 10.
  103. "a",
  104. REPLACE_T_NO_TRANSFORM,
  105. FALSE,
  106. "\a",
  107. },
  108. };
  109. /* @Test(dataSource = "test_regex_process_escape_sequence_ds") */
  110. START_PARAMETRIZED_TEST (test_regex_process_escape_sequence, test_regex_process_escape_sequence_ds)
  111. {
  112. // given
  113. GString *actual_string;
  114. replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
  115. replace_flags = data->input_initial_flags;
  116. actual_string = g_string_new ("");
  117. // when
  118. mc_search_regex__process_escape_sequence (actual_string, data->input_from, -1, &replace_flags,
  119. data->input_use_utf);
  120. // then
  121. mctest_assert_str_eq (actual_string->str, data->expected_string);
  122. g_string_free (actual_string, TRUE);
  123. }
  124. END_PARAMETRIZED_TEST
  125. /* --------------------------------------------------------------------------------------------- */
  126. int
  127. main (void)
  128. {
  129. TCase *tc_core;
  130. tc_core = tcase_create ("Core");
  131. // Add new tests here: ***************
  132. mctest_add_parameterized_test (tc_core, test_regex_process_escape_sequence,
  133. test_regex_process_escape_sequence_ds);
  134. // ***********************************
  135. return mctest_run_all (tc_core);
  136. }
  137. /* --------------------------------------------------------------------------------------------- */