config_string.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. libmc - check mcconfig submodule. read and write config files
  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/mcconfig"
  20. #include "tests/mctest.h"
  21. #include "lib/mcconfig.h"
  22. #include "lib/strutil.h"
  23. #include "lib/vfs/vfs.h"
  24. #include "src/vfs/local/local.c"
  25. static mc_config_t *mc_config;
  26. static char *ini_filename;
  27. /* --------------------------------------------------------------------------------------------- */
  28. static void
  29. config_object__init (void)
  30. {
  31. ini_filename = g_build_filename (WORKDIR, "config_string.ini", (char *) NULL);
  32. unlink (ini_filename);
  33. mc_config = mc_config_init (ini_filename, FALSE);
  34. }
  35. /* --------------------------------------------------------------------------------------------- */
  36. static void
  37. config_object__reopen (void)
  38. {
  39. GError *error = NULL;
  40. if (!mc_config_save_file (mc_config, &error))
  41. {
  42. ck_abort_msg ("Unable to save config file: %s", error->message);
  43. }
  44. mc_config_deinit (mc_config);
  45. mc_config = mc_config_init (ini_filename, FALSE);
  46. }
  47. /* --------------------------------------------------------------------------------------------- */
  48. static void
  49. config_object__deinit (void)
  50. {
  51. mc_config_deinit (mc_config);
  52. g_free (ini_filename);
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /* @Before */
  56. static void
  57. setup (void)
  58. {
  59. str_init_strings ("KOI8-R");
  60. vfs_init ();
  61. vfs_init_localfs ();
  62. config_object__init ();
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. /* @After */
  66. static void
  67. teardown (void)
  68. {
  69. config_object__deinit ();
  70. vfs_shut ();
  71. str_uninit_strings ();
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. /* @DataSource("test_create_ini_file_ds") */
  75. static const struct test_create_ini_file_ds
  76. {
  77. const char *input_group;
  78. const char *input_param;
  79. const char *input_default_value;
  80. const char *expected_value;
  81. const char *expected_raw_value;
  82. } test_create_ini_file_ds[] = {
  83. {
  84. // 0.
  85. "group-not-exists",
  86. "param-not_exists",
  87. NULL,
  88. NULL,
  89. NULL,
  90. },
  91. {
  92. // 1.
  93. "test-group1",
  94. "test-param1",
  95. "not-exists",
  96. " some value ",
  97. " some value ",
  98. },
  99. {
  100. // 2.
  101. "test-group1",
  102. "test-param2",
  103. "not-exists",
  104. // Should be represented as KOI8-R
  105. " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ",
  106. // Should be stored as UTF-8
  107. " \t Тестовое значение ",
  108. },
  109. {
  110. // 3.
  111. "test-group1",
  112. "test-param3",
  113. "not-exists",
  114. " \tsome value2\n\nf\b\005fff ",
  115. " \tsome value2\n\nf\b\005fff ",
  116. },
  117. {
  118. // 4.
  119. "test-group2",
  120. "test-param1",
  121. "not-exists",
  122. " some value ",
  123. " some value ",
  124. },
  125. {
  126. // 5.
  127. "test-group2",
  128. "test-param2",
  129. "not-exists",
  130. "not-exists",
  131. "not-exists",
  132. },
  133. {
  134. // 6.
  135. "test-group2",
  136. "test-param3",
  137. "not-exists",
  138. " \tsome value2\n\nf\b\005fff ",
  139. " \tsome value2\n\nf\b\005fff ",
  140. },
  141. };
  142. /* @Test(dataSource = "test_create_ini_file_ds") */
  143. START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
  144. {
  145. // given
  146. char *actual_value, *actual_raw_value;
  147. mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
  148. mc_config_set_string (mc_config, "test-group1", "test-param2",
  149. " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ");
  150. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  151. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  152. mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
  153. " \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5");
  154. mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
  155. " \tsome value2\n\nf\b\005fff ");
  156. config_object__reopen ();
  157. // when
  158. actual_value = mc_config_get_string (mc_config, data->input_group, data->input_param,
  159. data->input_default_value);
  160. actual_raw_value = mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
  161. data->input_default_value);
  162. // then
  163. mctest_assert_str_eq (actual_value, data->expected_value);
  164. mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
  165. g_free (actual_value);
  166. g_free (actual_raw_value);
  167. }
  168. END_PARAMETRIZED_TEST
  169. /* --------------------------------------------------------------------------------------------- */
  170. /* @Test(group='Integration') */
  171. START_TEST (emulate__learn_save)
  172. {
  173. // given
  174. char *actual_value;
  175. {
  176. char *esc_str;
  177. esc_str = str_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  178. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  179. g_free (esc_str);
  180. }
  181. config_object__reopen ();
  182. // when
  183. actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
  184. // then
  185. mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  186. g_free (actual_value);
  187. }
  188. END_TEST
  189. /* --------------------------------------------------------------------------------------------- */
  190. int
  191. main (void)
  192. {
  193. TCase *tc_core;
  194. tc_core = tcase_create ("Core");
  195. tcase_add_checked_fixture (tc_core, setup, teardown);
  196. // Add new tests here: ***************
  197. mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
  198. tcase_add_test (tc_core, emulate__learn_save);
  199. // ***********************************
  200. return mctest_run_all (tc_core);
  201. }
  202. /* --------------------------------------------------------------------------------------------- */