src/diffviewer/ydiff.c: fix use of HAVE_CHARSET.
[midnight-commander.git] / tests / src / execute__execute_get_external_cmd_opts_from_config.c
blob26cd161a3469b4fb5898618c42c829adcfe218b5
1 /*
2 src - tests for execute_external_editor_or_viewer() function
4 Copyright (C) 2013-2015
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/src"
28 #include "tests/mctest.h"
30 #include "lib/mcconfig.h"
31 #include "lib/strutil.h"
32 #include "lib/vfs/vfs.h"
33 #include "src/vfs/local/local.c"
35 char *execute_get_external_cmd_opts_from_config (const char *command,
36 const vfs_path_t * filename_vpath, int start_line);
38 /* --------------------------------------------------------------------------------------------- */
40 /* @CapturedValue */
41 static GPtrArray *mc_config_get_string__group__captured;
42 /* @CapturedValue */
43 static GPtrArray *mc_config_get_string__param__captured;
44 /* @CapturedValue */
45 static GPtrArray *mc_config_get_string__default_value__captured;
46 /* @ThenReturnValue */
47 static GPtrArray *mc_config_get_string__return_value;
49 /* @Mock */
50 gchar *
51 mc_config_get_string_raw (const mc_config_t * config_ignored, const gchar * group,
52 const gchar * param, const gchar * default_value)
54 char *return_value;
55 (void) config_ignored;
57 g_ptr_array_add (mc_config_get_string__group__captured, g_strdup (group));
58 g_ptr_array_add (mc_config_get_string__param__captured, g_strdup (param));
59 g_ptr_array_add (mc_config_get_string__default_value__captured, g_strdup (default_value));
61 return_value = g_ptr_array_index (mc_config_get_string__return_value, 0);
62 g_ptr_array_remove_index (mc_config_get_string__return_value, 0);
63 return return_value;
66 static void
67 mc_config_get_string__init (void)
69 mc_config_get_string__group__captured = g_ptr_array_new ();
70 mc_config_get_string__param__captured = g_ptr_array_new ();
71 mc_config_get_string__default_value__captured = g_ptr_array_new ();
73 mc_config_get_string__return_value = g_ptr_array_new ();
76 static void
77 mc_config_get_string__deinit (void)
79 g_ptr_array_foreach (mc_config_get_string__group__captured, (GFunc) g_free, NULL);
80 g_ptr_array_free (mc_config_get_string__group__captured, TRUE);
82 g_ptr_array_foreach (mc_config_get_string__param__captured, (GFunc) g_free, NULL);
83 g_ptr_array_free (mc_config_get_string__param__captured, TRUE);
85 g_ptr_array_foreach (mc_config_get_string__default_value__captured, (GFunc) g_free, NULL);
86 g_ptr_array_free (mc_config_get_string__default_value__captured, TRUE);
88 g_ptr_array_foreach (mc_config_get_string__return_value, (GFunc) g_free, NULL);
89 g_ptr_array_free (mc_config_get_string__return_value, TRUE);
92 /* --------------------------------------------------------------------------------------------- */
94 /* @Before */
95 static void
96 setup (void)
98 str_init_strings (NULL);
99 vfs_init ();
100 init_localfs ();
101 vfs_setup_work_dir ();
103 mc_config_get_string__init ();
106 /* --------------------------------------------------------------------------------------------- */
108 /* @After */
109 static void
110 teardown (void)
112 mc_config_get_string__deinit ();
114 vfs_shut ();
115 str_uninit_strings ();
118 /* --------------------------------------------------------------------------------------------- */
120 /* @DataSource("check_subtitute_ds") */
121 /* *INDENT-OFF* */
122 static const struct check_subtitute_ds
124 const char *config_opts_string;
125 const char *app_name;
126 const char *file_name;
127 int start_line;
128 const char *expected_result;
129 } check_subtitute_ds[] =
132 "-a -b -c %filename \\%filename %filename:%lineno \\%lineno +%lineno",
133 "some-editor",
134 "/path/to/file",
135 1234,
136 "-a -b -c '/path/to/file' %filename '/path/to/file':1234 %lineno +1234",
139 "%filename:\\\\\\\\\\\\%lineno",
140 "some-editor",
141 "/path/to/'f i\" l e \t\t\n",
142 1234,
143 "'/path/to/'\\''f i\" l e \t\t\n':\\\\\\\\\\\\1234",
146 /* *INDENT-ON* */
148 /* @Test(dataSource = "check_subtitute_ds") */
149 /* *INDENT-OFF* */
150 START_PARAMETRIZED_TEST (check_if_filename_and_lineno_will_be_subtituted, check_subtitute_ds)
151 /* *INDENT-ON* */
153 /* given */
154 char *actual_result;
155 vfs_path_t *filename_vpath;
157 g_ptr_array_add (mc_config_get_string__return_value, g_strdup (data->config_opts_string));
158 filename_vpath = vfs_path_from_str (data->file_name);
160 /* when */
161 actual_result =
162 execute_get_external_cmd_opts_from_config (data->app_name, filename_vpath,
163 data->start_line);
165 /* then */
167 /* check returned value */
168 mctest_assert_str_eq (actual_result, data->expected_result);
170 /* check calls to mc_config_get_string() function */
171 mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__group__captured, 0),
172 CONFIG_EXT_EDITOR_VIEWER_SECTION);
173 mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__param__captured, 0),
174 data->app_name);
175 mctest_assert_str_eq (g_ptr_array_index (mc_config_get_string__default_value__captured, 0),
176 NULL);
178 vfs_path_free (filename_vpath);
181 /* *INDENT-OFF* */
182 END_PARAMETRIZED_TEST
183 /* *INDENT-ON* */
185 /* --------------------------------------------------------------------------------------------- */
188 main (void)
190 int number_failed;
192 Suite *s = suite_create (TEST_SUITE_NAME);
193 TCase *tc_core = tcase_create ("Core");
194 SRunner *sr;
196 tcase_add_checked_fixture (tc_core, setup, teardown);
198 /* Add new tests here: *************** */
199 mctest_add_parameterized_test (tc_core, check_if_filename_and_lineno_will_be_subtituted,
200 check_subtitute_ds);
201 /* *********************************** */
203 suite_add_tcase (s, tc_core);
204 sr = srunner_create (s);
205 srunner_set_log (sr, "execute__execute_get_external_cmd_opts_from_config.log");
206 srunner_run_all (sr, CK_ENV);
207 number_failed = srunner_ntests_failed (sr);
208 srunner_free (sr);
209 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
212 /* --------------------------------------------------------------------------------------------- */