name_quote.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. lib - Quote file names
  3. Copyright (C) 2011-2019
  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. {"-", "./-"},
  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. int number_failed;
  104. Suite *s = suite_create (TEST_SUITE_NAME);
  105. TCase *tc_core = tcase_create ("Core");
  106. SRunner *sr;
  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. suite_add_tcase (s, tc_core);
  113. sr = srunner_create (s);
  114. srunner_set_log (sr, "name_quote.log");
  115. srunner_run_all (sr, CK_ENV);
  116. number_failed = srunner_ntests_failed (sr);
  117. srunner_free (sr);
  118. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  119. }
  120. /* --------------------------------------------------------------------------------------------- */