config_string.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. libmc - check mcconfig submodule. read and write config files
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  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 <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/mcconfig.h"
  24. #include "lib/strutil.h"
  25. #include "lib/strescape.h"
  26. #include "lib/vfs/vfs.h"
  27. #include "src/vfs/local/local.c"
  28. static void
  29. setup (void)
  30. {
  31. str_init_strings("KOI8-R");
  32. vfs_init ();
  33. init_localfs ();
  34. }
  35. static void
  36. teardown (void)
  37. {
  38. vfs_shut ();
  39. str_uninit_strings();
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. #define fail_unless_strcmp( etalon ) \
  43. fail_unless( \
  44. strcmp(actual_value, etalon) == 0, \
  45. "Actial value '%s' doesn't equal to etalon '%s'", actual_value, etalon \
  46. )
  47. START_TEST (create_ini_file)
  48. {
  49. mc_config_t *mc_config;
  50. GError *error = NULL;
  51. char *actual_value;
  52. char *ini_filename = NULL;
  53. ini_filename = g_build_filename(WORKDIR, "test-create_ini_file.ini",NULL);
  54. unlink(ini_filename);
  55. mc_config = mc_config_init (ini_filename, FALSE);
  56. if (mc_config == NULL)
  57. {
  58. fail("unable to create mc_congif_t object!");
  59. return;
  60. }
  61. mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
  62. mc_config_set_string (mc_config, "test-group1", "test-param2", " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  63. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  64. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  65. mc_config_set_string_raw (mc_config, "test-group2", "test-param2", " koi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ");
  66. mc_config_set_string_raw (mc_config, "test-group2", "test-param3", " \tsome value2\n\nf\b\005fff ");
  67. if (!mc_config_save_file (mc_config, &error))
  68. {
  69. fail("Unable to save config file: %s",error->message);
  70. g_error_free(error);
  71. }
  72. mc_config_deinit (mc_config);
  73. mc_config = mc_config_init (ini_filename, FALSE);
  74. actual_value = mc_config_get_string(mc_config, "group-not-exists", "param-not_exists", NULL);
  75. fail_unless(actual_value == NULL, "return value for nonexistent ini-parameters isn't NULL (default value)!");
  76. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param1", "not-exists");
  77. fail_unless_strcmp(" some value ");
  78. g_free(actual_value);
  79. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param2", "not-exists");
  80. fail_unless_strcmp(" \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  81. g_free(actual_value);
  82. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param3", "not-exists");
  83. fail_unless_strcmp(" \tsome value2\n\nf\b\005fff ");
  84. g_free(actual_value);
  85. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param1", "not-exists");
  86. fail_unless_strcmp(" some value ");
  87. g_free(actual_value);
  88. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param2", "not-exists");
  89. fail_unless_strcmp("not-exists");
  90. g_free(actual_value);
  91. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param3", "not-exists");
  92. fail_unless_strcmp(" \tsome value2\n\nf\b\005fff ");
  93. g_free(actual_value);
  94. mc_config_deinit (mc_config);
  95. g_free(ini_filename);
  96. }
  97. END_TEST
  98. /* --------------------------------------------------------------------------------------------- */
  99. START_TEST (emulate__learn_save)
  100. {
  101. mc_config_t *mc_config;
  102. char *actual_value, *esc_str;
  103. char *ini_filename = NULL;
  104. GError *error = NULL;
  105. ini_filename = g_build_filename(WORKDIR, "test-emulate__learn_save.ini",NULL);
  106. unlink(ini_filename);
  107. mc_config = mc_config_init (ini_filename, FALSE);
  108. if (mc_config == NULL)
  109. {
  110. fail("unable to create mc_congif_t object!");
  111. return;
  112. }
  113. esc_str = strutils_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  114. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  115. g_free (esc_str);
  116. if (!mc_config_save_file (mc_config, &error))
  117. {
  118. fail("Unable to save config file: %s",error->message);
  119. g_error_free(error);
  120. }
  121. mc_config_deinit (mc_config);
  122. mc_config = mc_config_init (ini_filename, FALSE);
  123. actual_value = mc_config_get_string_raw( mc_config, "test-group1", "test-param1", "not-exists");
  124. fail_unless_strcmp("T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  125. g_free(actual_value);
  126. mc_config_deinit (mc_config);
  127. g_free(ini_filename);
  128. }
  129. END_TEST
  130. /* --------------------------------------------------------------------------------------------- */
  131. int
  132. main (void)
  133. {
  134. int number_failed;
  135. Suite *s = suite_create (TEST_SUITE_NAME);
  136. TCase *tc_core = tcase_create ("Core");
  137. SRunner *sr;
  138. tcase_add_checked_fixture (tc_core, setup, teardown);
  139. /* Add new tests here: *************** */
  140. tcase_add_test (tc_core, create_ini_file);
  141. tcase_add_test (tc_core, emulate__learn_save);
  142. /* *********************************** */
  143. suite_add_tcase (s, tc_core);
  144. sr = srunner_create (s);
  145. // srunner_set_fork_status (sr, CK_NOFORK);
  146. srunner_run_all (sr, CK_NORMAL);
  147. number_failed = srunner_ntests_failed (sr);
  148. srunner_free (sr);
  149. return (number_failed == 0) ? 0 : 1;
  150. }
  151. /* --------------------------------------------------------------------------------------------- */