name_quote.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. lib - Quote file names
  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/util"
  20. #include "tests/mctest.h"
  21. #include "lib/util.h"
  22. /* --------------------------------------------------------------------------------------------- */
  23. /* @Before */
  24. static void
  25. setup (void)
  26. {
  27. }
  28. /* @After */
  29. static void
  30. teardown (void)
  31. {
  32. }
  33. /* --------------------------------------------------------------------------------------------- */
  34. /* @DataSource("data_source1") */
  35. static const struct data_source1
  36. {
  37. gboolean input_quote_percent;
  38. const char *input_string;
  39. const char *expected_string;
  40. } data_source1[] = {
  41. { TRUE, "%%", "%%%%" },
  42. { FALSE, "%%", "%%" },
  43. };
  44. /* @Test(dataSource = "data_source1") */
  45. START_PARAMETRIZED_TEST (quote_percent_test, data_source1)
  46. {
  47. // given
  48. char *actual_string;
  49. // when
  50. actual_string = name_quote (data->input_string, data->input_quote_percent);
  51. // then
  52. mctest_assert_str_eq (actual_string, data->expected_string);
  53. g_free (actual_string);
  54. }
  55. END_PARAMETRIZED_TEST
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* @DataSource("data_source2") */
  58. static const struct data_source2
  59. {
  60. const char *input_string;
  61. const char *expected_string;
  62. } data_source2[] = {
  63. { "", NULL },
  64. { "-", "./-" },
  65. { "blabla-", "blabla-" },
  66. { "\r\n\t", "\\\r\\\n\\\t" },
  67. { "'\\\";?|[]{}<>`!$&*()", "\\'\\\\\\\"\\;\\?\\|\\[\\]\\{\\}\\<\\>\\`\\!\\$\\&\\*\\(\\)" },
  68. { "a b c ", "a\\ b\\ c\\ " },
  69. { "#", "\\#" },
  70. { "blabla#", "blabla#" },
  71. { "~", "\\~" },
  72. { "blabla~", "blabla~" },
  73. {
  74. NULL,
  75. NULL,
  76. },
  77. };
  78. /* @Test(dataSource = "data_source2") */
  79. START_PARAMETRIZED_TEST (name_quote_test, data_source2)
  80. {
  81. // given
  82. char *actual_string;
  83. // when
  84. actual_string = name_quote (data->input_string, FALSE);
  85. // then
  86. mctest_assert_str_eq (actual_string, data->expected_string);
  87. g_free (actual_string);
  88. }
  89. END_PARAMETRIZED_TEST
  90. /* --------------------------------------------------------------------------------------------- */
  91. int
  92. main (void)
  93. {
  94. TCase *tc_core;
  95. tc_core = tcase_create ("Core");
  96. tcase_add_checked_fixture (tc_core, setup, teardown);
  97. // Add new tests here: ***************
  98. mctest_add_parameterized_test (tc_core, quote_percent_test, data_source1);
  99. mctest_add_parameterized_test (tc_core, name_quote_test, data_source2);
  100. // ***********************************
  101. return mctest_run_all (tc_core);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */