config_string.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. g_error_free (error);
  44. }
  45. mc_config_deinit (mc_config);
  46. mc_config = mc_config_init (ini_filename, FALSE);
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. static void
  50. config_object__deinit (void)
  51. {
  52. mc_config_deinit (mc_config);
  53. g_free (ini_filename);
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. /* @Before */
  57. static void
  58. setup (void)
  59. {
  60. str_init_strings ("KOI8-R");
  61. vfs_init ();
  62. vfs_init_localfs ();
  63. config_object__init ();
  64. }
  65. /* --------------------------------------------------------------------------------------------- */
  66. /* @After */
  67. static void
  68. teardown (void)
  69. {
  70. config_object__deinit ();
  71. vfs_shut ();
  72. str_uninit_strings ();
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. /* @DataSource("test_create_ini_file_ds") */
  76. /* *INDENT-OFF* */
  77. static const struct test_create_ini_file_ds
  78. {
  79. const char *input_group;
  80. const char *input_param;
  81. const char *input_default_value;
  82. const char *expected_value;
  83. const char *expected_raw_value;
  84. } test_create_ini_file_ds[] =
  85. {
  86. { /* 0. */
  87. "group-not-exists",
  88. "param-not_exists",
  89. NULL,
  90. NULL,
  91. NULL
  92. },
  93. { /* 1. */
  94. "test-group1",
  95. "test-param1",
  96. "not-exists",
  97. " some value ",
  98. " some value "
  99. },
  100. { /* 2. */
  101. "test-group1",
  102. "test-param2",
  103. "not-exists",
  104. " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ",
  105. " \tkoi8-r: \320\242\320\265\321\201\321\202\320\276\320\262\320\276\320\265 \320\267\320\275\320\260\321\207\320\265\320\275\320\270\320\265 "
  106. },
  107. { /* 3. */
  108. "test-group1",
  109. "test-param3",
  110. "not-exists",
  111. " \tsome value2\n\nf\b\005fff ",
  112. " \tsome value2\n\nf\b\005fff "
  113. },
  114. { /* 4. */
  115. "test-group2",
  116. "test-param1",
  117. "not-exists",
  118. " some value ",
  119. " some value "
  120. },
  121. { /* 5. */
  122. "test-group2",
  123. "test-param2",
  124. "not-exists",
  125. "not-exists",
  126. "not-exists"
  127. },
  128. { /* 6. */
  129. "test-group2",
  130. "test-param3",
  131. "not-exists",
  132. " \tsome value2\n\nf\b\005fff ",
  133. " \tsome value2\n\nf\b\005fff "
  134. },
  135. };
  136. /* *INDENT-ON* */
  137. /* @Test(dataSource = "test_create_ini_file_ds") */
  138. /* *INDENT-OFF* */
  139. START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
  140. /* *INDENT-ON* */
  141. {
  142. /* given */
  143. char *actual_value, *actual_raw_value;
  144. mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
  145. mc_config_set_string (mc_config, "test-group1", "test-param2", " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  146. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  147. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  148. mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
  149. " koi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ");
  150. mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
  151. " \tsome value2\n\nf\b\005fff ");
  152. config_object__reopen ();
  153. /* when */
  154. actual_value =
  155. mc_config_get_string (mc_config, data->input_group, data->input_param,
  156. data->input_default_value);
  157. actual_raw_value =
  158. mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
  159. data->input_default_value);
  160. /* then */
  161. mctest_assert_str_eq (actual_value, data->expected_value);
  162. mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
  163. g_free (actual_value);
  164. g_free (actual_raw_value);
  165. }
  166. /* *INDENT-OFF* */
  167. END_PARAMETRIZED_TEST
  168. /* *INDENT-ON* */
  169. /* --------------------------------------------------------------------------------------------- */
  170. /* @Test(group='Integration') */
  171. /* *INDENT-OFF* */
  172. START_TEST (emulate__learn_save)
  173. /* *INDENT-ON* */
  174. {
  175. /* given */
  176. char *actual_value;
  177. {
  178. char *esc_str;
  179. esc_str = str_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  180. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  181. g_free (esc_str);
  182. }
  183. config_object__reopen ();
  184. /* when */
  185. actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
  186. /* then */
  187. mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  188. g_free (actual_value);
  189. }
  190. /* *INDENT-OFF* */
  191. END_TEST
  192. /* *INDENT-ON* */
  193. /* --------------------------------------------------------------------------------------------- */
  194. int
  195. main (void)
  196. {
  197. TCase *tc_core;
  198. tc_core = tcase_create ("Core");
  199. tcase_add_checked_fixture (tc_core, setup, teardown);
  200. /* Add new tests here: *************** */
  201. mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
  202. tcase_add_test (tc_core, emulate__learn_save);
  203. /* *********************************** */
  204. return mctest_run_all (tc_core);
  205. }
  206. /* --------------------------------------------------------------------------------------------- */