execute__execute_external_editor_or_viewer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. src - tests for execute_external_editor_or_viewer() function
  3. Copyright (C) 2013-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 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 "/src"
  20. #include "tests/mctest.h"
  21. #include "execute__common.c"
  22. /* --------------------------------------------------------------------------------------------- */
  23. char *execute_get_external_cmd_opts_from_config (const char *command,
  24. const vfs_path_t * filename_vpath,
  25. long start_line);
  26. /* @CapturedValue */
  27. static char *execute_external_cmd_opts__command__captured;
  28. /* @CapturedValue */
  29. static vfs_path_t *execute_external_cmd_opts__filename_vpath__captured;
  30. /* @CapturedValue */
  31. static int execute_external_cmd_opts__start_line__captured;
  32. /* @ThenReturnValue */
  33. static char *execute_external_cmd_opts__return_value;
  34. /* @Mock */
  35. char *
  36. execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t *filename_vpath,
  37. long start_line)
  38. {
  39. execute_external_cmd_opts__command__captured = g_strdup (command);
  40. execute_external_cmd_opts__filename_vpath__captured = vfs_path_clone (filename_vpath);
  41. execute_external_cmd_opts__start_line__captured = start_line;
  42. return execute_external_cmd_opts__return_value;
  43. }
  44. static void
  45. execute_get_external_cmd_opts_from_config__init (void)
  46. {
  47. execute_external_cmd_opts__command__captured = NULL;
  48. execute_external_cmd_opts__filename_vpath__captured = NULL;
  49. execute_external_cmd_opts__start_line__captured = 0;
  50. }
  51. static void
  52. execute_get_external_cmd_opts_from_config__deinit (void)
  53. {
  54. g_free (execute_external_cmd_opts__command__captured);
  55. vfs_path_free (execute_external_cmd_opts__filename_vpath__captured, TRUE);
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. void do_executev (const char *lc_shell, int flags, char *const argv[]);
  59. /* @CapturedValue */
  60. static char *do_executev__lc_shell__captured;
  61. /* @CapturedValue */
  62. static int do_executev__flags__captured;
  63. /* @CapturedValue */
  64. static GPtrArray *do_executev__argv__captured;
  65. /* @Mock */
  66. void
  67. do_executev (const char *lc_shell, int flags, char *const argv[])
  68. {
  69. do_executev__lc_shell__captured = g_strdup (lc_shell);
  70. do_executev__flags__captured = flags;
  71. for (; argv != NULL && *argv != NULL; argv++)
  72. g_ptr_array_add (do_executev__argv__captured, g_strdup (*argv));
  73. }
  74. static void
  75. do_executev__init (void)
  76. {
  77. do_executev__lc_shell__captured = NULL;
  78. do_executev__argv__captured = g_ptr_array_new_with_free_func (g_free);
  79. do_executev__flags__captured = 0;
  80. }
  81. static void
  82. do_executev__deinit (void)
  83. {
  84. g_free (do_executev__lc_shell__captured);
  85. g_ptr_array_free (do_executev__argv__captured, TRUE);
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. /* @Before */
  89. static void
  90. my_setup (void)
  91. {
  92. setup ();
  93. execute_get_external_cmd_opts_from_config__init ();
  94. do_executev__init ();
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. /* @After */
  98. static void
  99. my_teardown (void)
  100. {
  101. do_executev__deinit ();
  102. execute_get_external_cmd_opts_from_config__deinit ();
  103. teardown ();
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. /* @Test */
  107. /* *INDENT-OFF* */
  108. START_TEST (do_open_external_editor_or_viewer)
  109. /* *INDENT-ON* */
  110. {
  111. /* given */
  112. vfs_path_t *filename_vpath;
  113. filename_vpath = vfs_path_from_str ("/path/to/file.txt");
  114. vfs_file_is_local__return_value = TRUE;
  115. execute_external_cmd_opts__return_value =
  116. g_strdup
  117. (" 'param 1 with spaces' \"param 2\" -a -b -cdef /path/to/file.txt +123");
  118. /* when */
  119. execute_external_editor_or_viewer ("editor_or_viewer", filename_vpath, 123);
  120. /* then */
  121. /* check call to execute_get_external_cmd_opts_from_config() */
  122. mctest_assert_str_eq (execute_external_cmd_opts__command__captured, "editor_or_viewer");
  123. mctest_assert_true (vfs_path_equal
  124. (execute_external_cmd_opts__filename_vpath__captured, filename_vpath));
  125. ck_assert_int_eq (execute_external_cmd_opts__start_line__captured, 123);
  126. /* check call to do_executev() */
  127. mctest_assert_str_eq (do_executev__lc_shell__captured, "editor_or_viewer");
  128. ck_assert_int_eq (do_executev__flags__captured, EXECUTE_INTERNAL);
  129. ck_assert_int_eq (do_executev__argv__captured->len, 7);
  130. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 0),
  131. "param 1 with spaces");
  132. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 1), "param 2");
  133. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 2), "-a");
  134. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 3), "-b");
  135. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 4), "-cdef");
  136. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 5), "/path/to/file.txt");
  137. mctest_assert_str_eq (g_ptr_array_index (do_executev__argv__captured, 6), "+123");
  138. vfs_path_free (filename_vpath, TRUE);
  139. }
  140. /* *INDENT-OFF* */
  141. END_TEST
  142. /* *INDENT-ON* */
  143. /* --------------------------------------------------------------------------------------------- */
  144. int
  145. main (void)
  146. {
  147. TCase *tc_core;
  148. tc_core = tcase_create ("Core");
  149. tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
  150. /* Add new tests here: *************** */
  151. tcase_add_test (tc_core, do_open_external_editor_or_viewer);
  152. /* *********************************** */
  153. return mctest_run_all (tc_core);
  154. }
  155. /* --------------------------------------------------------------------------------------------- */