mc_build_filename.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. lib - mc_build_filename() function testing
  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 <stdio.h>
  22. #include "lib/strutil.h"
  23. #include "lib/util.h"
  24. /* --------------------------------------------------------------------------------------------- */
  25. /* @Before */
  26. static void
  27. setup (void)
  28. {
  29. }
  30. /* --------------------------------------------------------------------------------------------- */
  31. /* @After */
  32. static void
  33. teardown (void)
  34. {
  35. }
  36. /* --------------------------------------------------------------------------------------------- */
  37. static char *
  38. run_mc_build_filename (int iteration)
  39. {
  40. switch (iteration)
  41. {
  42. case 0:
  43. return mc_build_filename ("test", "path", (char *) NULL);
  44. case 1:
  45. return mc_build_filename ("/test", "path/", (char *) NULL);
  46. case 2:
  47. return mc_build_filename ("/test", "pa/th", (char *) NULL);
  48. case 3:
  49. return mc_build_filename ("/test", "#vfsprefix:", "path ", (char *) NULL);
  50. case 4:
  51. return mc_build_filename ("/test", "vfsprefix://", "path ", (char *) NULL);
  52. case 5:
  53. return mc_build_filename ("/test", "vfs/../prefix:///", "p\\///ath", (char *) NULL);
  54. case 6:
  55. return mc_build_filename ("/test", "path", "..", "/test", "path/", (char *) NULL);
  56. case 7:
  57. return mc_build_filename ("", "path", (char *) NULL);
  58. case 8:
  59. return mc_build_filename ("", "/path", (char *) NULL);
  60. case 9:
  61. return mc_build_filename ("path", "", (char *) NULL);
  62. case 10:
  63. return mc_build_filename ("/path", "", (char *) NULL);
  64. case 11:
  65. return mc_build_filename ("pa", "", "th", (char *) NULL);
  66. case 12:
  67. return mc_build_filename ("/pa", "", "/th", (char *) NULL);
  68. default:
  69. return NULL;
  70. }
  71. }
  72. /* @DataSource("test_mc_build_filename_ds") */
  73. static const struct test_mc_build_filename_ds
  74. {
  75. const char *expected_result;
  76. } test_mc_build_filename_ds[] = {
  77. { "test/path" },
  78. { "/test/path" },
  79. { "/test/pa/th" },
  80. { "/test/#vfsprefix:/path " },
  81. { "/test/vfsprefix://path " },
  82. { "/test/prefix://p\\/ath" },
  83. { "/test/test/path" },
  84. { "path" },
  85. { "path" },
  86. { "path" },
  87. { "/path" },
  88. { "pa/th" },
  89. { "/pa/th" },
  90. };
  91. /* @Test(dataSource = "test_mc_build_filename_ds") */
  92. START_PARAMETRIZED_TEST (test_mc_build_filename, test_mc_build_filename_ds)
  93. {
  94. // given
  95. char *actual_result;
  96. // when
  97. actual_result = run_mc_build_filename (_i);
  98. // then
  99. mctest_assert_str_eq (actual_result, data->expected_result);
  100. g_free (actual_result);
  101. }
  102. END_PARAMETRIZED_TEST
  103. /* --------------------------------------------------------------------------------------------- */
  104. int
  105. main (void)
  106. {
  107. TCase *tc_core;
  108. tc_core = tcase_create ("Core");
  109. tcase_add_checked_fixture (tc_core, setup, teardown);
  110. // Add new tests here: ***************
  111. mctest_add_parameterized_test (tc_core, test_mc_build_filename, test_mc_build_filename_ds);
  112. // ***********************************
  113. return mctest_run_all (tc_core);
  114. }
  115. /* --------------------------------------------------------------------------------------------- */