name_quote.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. lib - Quote file names
  3. Copyright (C) 2011-2024
  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 <http://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. /* *INDENT-OFF* */
  36. static const struct data_source1
  37. {
  38. gboolean input_quote_percent;
  39. const char *input_string;
  40. const char *expected_string;
  41. } data_source1[] = {
  42. { TRUE, "%%", "%%%%" },
  43. { FALSE, "%%", "%%" },
  44. };
  45. /* *INDENT-ON* */
  46. /* @Test(dataSource = "data_source1") */
  47. /* *INDENT-OFF* */
  48. START_PARAMETRIZED_TEST (quote_percent_test, data_source1)
  49. /* *INDENT-ON* */
  50. {
  51. /* given */
  52. char *actual_string;
  53. /* when */
  54. actual_string = name_quote (data->input_string, data->input_quote_percent);
  55. /* then */
  56. mctest_assert_str_eq (actual_string, data->expected_string);
  57. g_free (actual_string);
  58. }
  59. /* *INDENT-OFF* */
  60. END_PARAMETRIZED_TEST
  61. /* *INDENT-ON* */
  62. /* --------------------------------------------------------------------------------------------- */
  63. /* @DataSource("data_source2") */
  64. /* *INDENT-OFF* */
  65. static const struct data_source2
  66. {
  67. const char *input_string;
  68. const char *expected_string;
  69. } data_source2[] = {
  70. { NULL, NULL },
  71. { "", NULL },
  72. { "-", "./-" },
  73. { "blabla-", "blabla-" },
  74. { "\r\n\t", "\\\r\\\n\\\t" },
  75. { "'\\\";?|[]{}<>`!$&*()", "\\'\\\\\\\"\\;\\?\\|\\[\\]\\{\\}\\<\\>\\`\\!\\$\\&\\*\\(\\)" },
  76. { "a b c ", "a\\ b\\ c\\ " },
  77. { "#", "\\#" },
  78. { "blabla#", "blabla#" },
  79. { "~", "\\~" },
  80. { "blabla~", "blabla~" },
  81. };
  82. /* *INDENT-ON* */
  83. /* @Test(dataSource = "data_source2") */
  84. /* *INDENT-OFF* */
  85. START_PARAMETRIZED_TEST (name_quote_test, data_source2)
  86. /* *INDENT-ON* */
  87. {
  88. /* given */
  89. char *actual_string;
  90. /* when */
  91. actual_string = name_quote (data->input_string, FALSE);
  92. /* then */
  93. mctest_assert_str_eq (actual_string, data->expected_string);
  94. g_free (actual_string);
  95. }
  96. /* *INDENT-OFF* */
  97. END_PARAMETRIZED_TEST
  98. /* *INDENT-ON* */
  99. /* --------------------------------------------------------------------------------------------- */
  100. int
  101. main (void)
  102. {
  103. TCase *tc_core;
  104. tc_core = tcase_create ("Core");
  105. tcase_add_checked_fixture (tc_core, setup, teardown);
  106. /* Add new tests here: *************** */
  107. mctest_add_parameterized_test (tc_core, quote_percent_test, data_source1);
  108. mctest_add_parameterized_test (tc_core, name_quote_test, data_source2);
  109. /* *********************************** */
  110. return mctest_run_all (tc_core);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */