config_string.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. libmc - check mcconfig submodule. read and write config files
  3. Copyright (C) 2011-2015
  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/strescape.h"
  24. #include "lib/vfs/vfs.h"
  25. #include "src/vfs/local/local.c"
  26. static mc_config_t *mc_config;
  27. static char *ini_filename;
  28. /* --------------------------------------------------------------------------------------------- */
  29. static void
  30. config_object__init (void)
  31. {
  32. ini_filename = g_build_filename (WORKDIR, "config_string.ini", NULL);
  33. unlink (ini_filename);
  34. mc_config = mc_config_init (ini_filename, FALSE);
  35. }
  36. /* --------------------------------------------------------------------------------------------- */
  37. static void
  38. config_object__reopen (void)
  39. {
  40. GError *error = NULL;
  41. if (!mc_config_save_file (mc_config, &error))
  42. {
  43. fail ("Unable to save config file: %s", error->message);
  44. g_error_free (error);
  45. }
  46. mc_config_deinit (mc_config);
  47. mc_config = mc_config_init (ini_filename, FALSE);
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. static void
  51. config_object__deinit (void)
  52. {
  53. mc_config_deinit (mc_config);
  54. g_free (ini_filename);
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* @Before */
  58. static void
  59. setup (void)
  60. {
  61. str_init_strings ("KOI8-R");
  62. vfs_init ();
  63. init_localfs ();
  64. config_object__init ();
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. /* @After */
  68. static void
  69. teardown (void)
  70. {
  71. config_object__deinit ();
  72. vfs_shut ();
  73. str_uninit_strings ();
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. /* @DataSource("test_create_ini_file_ds") */
  77. /* *INDENT-OFF* */
  78. static const struct test_create_ini_file_ds
  79. {
  80. const char *input_group;
  81. const char *input_param;
  82. const char *input_default_value;
  83. const char *expected_value;
  84. const char *expected_raw_value;
  85. } test_create_ini_file_ds[] =
  86. {
  87. { /* 0. */
  88. "group-not-exists",
  89. "param-not_exists",
  90. NULL,
  91. NULL,
  92. NULL
  93. },
  94. { /* 1. */
  95. "test-group1",
  96. "test-param1",
  97. "not-exists",
  98. " some value ",
  99. " some value "
  100. },
  101. { /* 2. */
  102. "test-group1",
  103. "test-param2",
  104. "not-exists",
  105. " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ",
  106. " \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 "
  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", " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  147. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  148. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  149. mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
  150. " koi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ");
  151. mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
  152. " \tsome value2\n\nf\b\005fff ");
  153. config_object__reopen ();
  154. /* when */
  155. actual_value =
  156. mc_config_get_string (mc_config, data->input_group, data->input_param,
  157. data->input_default_value);
  158. actual_raw_value =
  159. mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
  160. data->input_default_value);
  161. /* then */
  162. mctest_assert_str_eq (actual_value, data->expected_value);
  163. mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
  164. g_free (actual_value);
  165. g_free (actual_raw_value);
  166. }
  167. /* *INDENT-OFF* */
  168. END_PARAMETRIZED_TEST
  169. /* *INDENT-ON* */
  170. /* --------------------------------------------------------------------------------------------- */
  171. /* @Test(group='Integration') */
  172. /* *INDENT-OFF* */
  173. START_TEST (emulate__learn_save)
  174. /* *INDENT-ON* */
  175. {
  176. /* given */
  177. char *actual_value;
  178. {
  179. char *esc_str;
  180. esc_str = strutils_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  181. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  182. g_free (esc_str);
  183. }
  184. config_object__reopen ();
  185. /* when */
  186. actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
  187. /* then */
  188. mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  189. g_free (actual_value);
  190. }
  191. /* *INDENT-OFF* */
  192. END_TEST
  193. /* *INDENT-ON* */
  194. /* --------------------------------------------------------------------------------------------- */
  195. int
  196. main (void)
  197. {
  198. int number_failed;
  199. Suite *s = suite_create (TEST_SUITE_NAME);
  200. TCase *tc_core = tcase_create ("Core");
  201. SRunner *sr;
  202. tcase_add_checked_fixture (tc_core, setup, teardown);
  203. /* Add new tests here: *************** */
  204. mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
  205. tcase_add_test (tc_core, emulate__learn_save);
  206. /* *********************************** */
  207. suite_add_tcase (s, tc_core);
  208. sr = srunner_create (s);
  209. /* srunner_set_fork_status (sr, CK_NOFORK); */
  210. srunner_run_all (sr, CK_NORMAL);
  211. number_failed = srunner_ntests_failed (sr);
  212. srunner_free (sr);
  213. return (number_failed == 0) ? 0 : 1;
  214. }
  215. /* --------------------------------------------------------------------------------------------- */