vfs_prefix_to_class.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. lib/vfs - test vfs_prefix_to_class() functionality
  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/vfs"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. #include "lib/vfs/xdirentry.h"
  23. #include "lib/vfs/vfs.c" // for testing static methods
  24. #include "src/vfs/local/local.c"
  25. static struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  26. /* --------------------------------------------------------------------------------------------- */
  27. static int
  28. test_which (struct vfs_class *me, const char *path)
  29. {
  30. (void) me;
  31. if ((strcmp (path, "test_1:") == 0) || (strcmp (path, "test_2:") == 0)
  32. || (strcmp (path, "test_3:") == 0) || (strcmp (path, "test_4:") == 0))
  33. return 1;
  34. return -1;
  35. }
  36. /* --------------------------------------------------------------------------------------------- */
  37. /* @Before */
  38. static void
  39. setup (void)
  40. {
  41. str_init_strings (NULL);
  42. vfs_init ();
  43. vfs_init_localfs ();
  44. vfs_setup_work_dir ();
  45. vfs_init_class (&vfs_test_ops1, "testfs1", VFSF_NOLINKS | VFSF_REMOTE, "test1");
  46. vfs_test_ops1.which = test_which;
  47. vfs_register_class (&vfs_test_ops1);
  48. vfs_init_class (&vfs_test_ops2, "testfs2", VFSF_UNKNOWN, "test2");
  49. vfs_register_class (&vfs_test_ops2);
  50. vfs_init_class (&vfs_test_ops3, "testfs3", VFSF_UNKNOWN, "test3");
  51. vfs_register_class (&vfs_test_ops3);
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. /* @After */
  55. static void
  56. teardown (void)
  57. {
  58. vfs_shut ();
  59. str_uninit_strings ();
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. /* @DataSource("test_vfs_prefix_to_class_ds") */
  63. static const struct test_vfs_prefix_to_class_ds
  64. {
  65. const char *input_string;
  66. const struct vfs_class *expected_result;
  67. } test_vfs_prefix_to_class_ds[] = {
  68. {
  69. // 0
  70. "test_1:",
  71. &vfs_test_ops1,
  72. },
  73. {
  74. // 1
  75. "test_2:",
  76. &vfs_test_ops1,
  77. },
  78. {
  79. // 2
  80. "test_3:",
  81. &vfs_test_ops1,
  82. },
  83. {
  84. // 3
  85. "test_4:",
  86. &vfs_test_ops1,
  87. },
  88. {
  89. // 4
  90. "test2:",
  91. &vfs_test_ops2,
  92. },
  93. {
  94. // 5
  95. "test3:",
  96. &vfs_test_ops3,
  97. },
  98. {
  99. "test1:",
  100. NULL,
  101. },
  102. {
  103. // 6
  104. "test_5:",
  105. NULL,
  106. },
  107. {
  108. // 7
  109. "test4:",
  110. NULL,
  111. },
  112. };
  113. /* @Test(dataSource = "test_vfs_prefix_to_class_ds") */
  114. START_PARAMETRIZED_TEST (test_vfs_prefix_to_class, test_vfs_prefix_to_class_ds)
  115. {
  116. // given
  117. struct vfs_class *actual_result;
  118. // when
  119. actual_result = vfs_prefix_to_class ((char *) data->input_string);
  120. // then
  121. mctest_assert_ptr_eq (actual_result, data->expected_result);
  122. }
  123. END_PARAMETRIZED_TEST
  124. /* --------------------------------------------------------------------------------------------- */
  125. int
  126. main (void)
  127. {
  128. TCase *tc_core;
  129. tc_core = tcase_create ("Core");
  130. tcase_add_checked_fixture (tc_core, setup, teardown);
  131. // Add new tests here: ***************
  132. mctest_add_parameterized_test (tc_core, test_vfs_prefix_to_class, test_vfs_prefix_to_class_ds);
  133. // ***********************************
  134. return mctest_run_all (tc_core);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */