name_quote.c 3.6 KB

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