config_string.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. libmc - check mcconfig submodule. read and write config files
  3. Copyright (C) 2011-2024
  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 <http://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. /* *INDENT-OFF* */
  76. static const struct test_create_ini_file_ds
  77. {
  78. const char *input_group;
  79. const char *input_param;
  80. const char *input_default_value;
  81. const char *expected_value;
  82. const char *expected_raw_value;
  83. } test_create_ini_file_ds[] =
  84. {
  85. { /* 0. */
  86. "group-not-exists",
  87. "param-not_exists",
  88. NULL,
  89. NULL,
  90. NULL
  91. },
  92. { /* 1. */
  93. "test-group1",
  94. "test-param1",
  95. "not-exists",
  96. " some value ",
  97. " some value "
  98. },
  99. { /* 2. */
  100. "test-group1",
  101. "test-param2",
  102. "not-exists",
  103. /* Should be represented as KOI8-R */
  104. " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ",
  105. /* Should be stored as UTF-8 */
  106. " \t Тестовое значение "
  107. },
  108. { /* 3. */
  109. "test-group1",
  110. "test-param3",
  111. "not-exists",
  112. " \tsome value2\n\nf\b\005fff ",
  113. " \tsome value2\n\nf\b\005fff "
  114. },
  115. { /* 4. */
  116. "test-group2",
  117. "test-param1",
  118. "not-exists",
  119. " some value ",
  120. " some value "
  121. },
  122. { /* 5. */
  123. "test-group2",
  124. "test-param2",
  125. "not-exists",
  126. "not-exists",
  127. "not-exists"
  128. },
  129. { /* 6. */
  130. "test-group2",
  131. "test-param3",
  132. "not-exists",
  133. " \tsome value2\n\nf\b\005fff ",
  134. " \tsome value2\n\nf\b\005fff "
  135. },
  136. };
  137. /* *INDENT-ON* */
  138. /* @Test(dataSource = "test_create_ini_file_ds") */
  139. /* *INDENT-OFF* */
  140. START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
  141. /* *INDENT-ON* */
  142. {
  143. /* given */
  144. char *actual_value, *actual_raw_value;
  145. mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
  146. mc_config_set_string (mc_config, "test-group1", "test-param2",
  147. " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ");
  148. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  149. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  150. mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
  151. " \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5");
  152. mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
  153. " \tsome value2\n\nf\b\005fff ");
  154. config_object__reopen ();
  155. /* when */
  156. actual_value =
  157. mc_config_get_string (mc_config, data->input_group, data->input_param,
  158. data->input_default_value);
  159. actual_raw_value =
  160. 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. /* *INDENT-OFF* */
  169. END_PARAMETRIZED_TEST
  170. /* *INDENT-ON* */
  171. /* --------------------------------------------------------------------------------------------- */
  172. /* @Test(group='Integration') */
  173. /* *INDENT-OFF* */
  174. START_TEST (emulate__learn_save)
  175. /* *INDENT-ON* */
  176. {
  177. /* given */
  178. char *actual_value;
  179. {
  180. char *esc_str;
  181. esc_str = str_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  182. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  183. g_free (esc_str);
  184. }
  185. config_object__reopen ();
  186. /* when */
  187. actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
  188. /* then */
  189. mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  190. g_free (actual_value);
  191. }
  192. /* *INDENT-OFF* */
  193. END_TEST
  194. /* *INDENT-ON* */
  195. /* --------------------------------------------------------------------------------------------- */
  196. int
  197. main (void)
  198. {
  199. TCase *tc_core;
  200. tc_core = tcase_create ("Core");
  201. tcase_add_checked_fixture (tc_core, setup, teardown);
  202. /* Add new tests here: *************** */
  203. mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
  204. tcase_add_test (tc_core, emulate__learn_save);
  205. /* *********************************** */
  206. return mctest_run_all (tc_core);
  207. }
  208. /* --------------------------------------------------------------------------------------------- */