config_string.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* libmc - check mcconfig submodule. read and write config files
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by:
  4. Slava Zanko <slavazanko@gmail.com>, 2011
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #define TEST_SUITE_NAME "lib/mcconfig"
  18. #include <check.h>
  19. #include "lib/global.h"
  20. #include "lib/mcconfig.h"
  21. #include "lib/strutil.h"
  22. #include "lib/strescape.h"
  23. #include "lib/vfs/vfs.h"
  24. #include "src/vfs/local/local.c"
  25. static void
  26. setup (void)
  27. {
  28. str_init_strings("KOI8-R");
  29. vfs_init ();
  30. init_localfs ();
  31. }
  32. static void
  33. teardown (void)
  34. {
  35. vfs_shut ();
  36. str_uninit_strings();
  37. }
  38. /* --------------------------------------------------------------------------------------------- */
  39. #define fail_unless_strcmp( etalon ) \
  40. fail_unless( \
  41. strcmp(actual_value, etalon) == 0, \
  42. "Actial value '%s' doesn't equal to etalon '%s'", actual_value, etalon \
  43. )
  44. START_TEST (create_ini_file)
  45. {
  46. mc_config_t *mc_config;
  47. GError *error = NULL;
  48. char *actual_value;
  49. char *ini_filename = NULL;
  50. ini_filename = g_build_filename(WORKDIR, "test-create_ini_file.ini",NULL);
  51. unlink(ini_filename);
  52. mc_config = mc_config_init (ini_filename);
  53. if (mc_config == NULL)
  54. {
  55. fail("unable to create mc_congif_t object!");
  56. return;
  57. }
  58. mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
  59. mc_config_set_string (mc_config, "test-group1", "test-param2", " \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  60. mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
  61. mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
  62. mc_config_set_string_raw (mc_config, "test-group2", "test-param2", " koi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ");
  63. mc_config_set_string_raw (mc_config, "test-group2", "test-param3", " \tsome value2\n\nf\b\005fff ");
  64. if (!mc_config_save_file (mc_config, &error))
  65. {
  66. fail("Unable to save config file: %s",error->message);
  67. g_error_free(error);
  68. }
  69. mc_config_deinit (mc_config);
  70. mc_config = mc_config_init (ini_filename);
  71. actual_value = mc_config_get_string(mc_config, "group-not-exists", "param-not_exists", NULL);
  72. fail_unless(actual_value == NULL, "return value for nonexistent ini-parameters isn't NULL (default value)!");
  73. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param1", "not-exists");
  74. fail_unless_strcmp(" some value ");
  75. g_free(actual_value);
  76. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param2", "not-exists");
  77. fail_unless_strcmp(" \tkoi8-r: ôÅÓÔÏ×ÏÅ ÚÎÁÞÅÎÉÅ ");
  78. g_free(actual_value);
  79. actual_value = mc_config_get_string(mc_config, "test-group1", "test-param3", "not-exists");
  80. fail_unless_strcmp(" \tsome value2\n\nf\b\005fff ");
  81. g_free(actual_value);
  82. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param1", "not-exists");
  83. fail_unless_strcmp(" some value ");
  84. g_free(actual_value);
  85. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param2", "not-exists");
  86. fail_unless_strcmp("not-exists");
  87. g_free(actual_value);
  88. actual_value = mc_config_get_string_raw( mc_config, "test-group2", "test-param3", "not-exists");
  89. fail_unless_strcmp(" \tsome value2\n\nf\b\005fff ");
  90. g_free(actual_value);
  91. mc_config_deinit (mc_config);
  92. g_free(ini_filename);
  93. }
  94. END_TEST
  95. /* --------------------------------------------------------------------------------------------- */
  96. START_TEST (emulate__learn_save)
  97. {
  98. mc_config_t *mc_config;
  99. char *actual_value, *esc_str;
  100. char *ini_filename = NULL;
  101. GError *error = NULL;
  102. ini_filename = g_build_filename(WORKDIR, "test-emulate__learn_save.ini",NULL);
  103. unlink(ini_filename);
  104. mc_config = mc_config_init (ini_filename);
  105. if (mc_config == NULL)
  106. {
  107. fail("unable to create mc_congif_t object!");
  108. return;
  109. }
  110. esc_str = strutils_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
  111. mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
  112. g_free (esc_str);
  113. if (!mc_config_save_file (mc_config, &error))
  114. {
  115. fail("Unable to save config file: %s",error->message);
  116. g_error_free(error);
  117. }
  118. mc_config_deinit (mc_config);
  119. mc_config = mc_config_init (ini_filename);
  120. actual_value = mc_config_get_string_raw( mc_config, "test-group1", "test-param1", "not-exists");
  121. fail_unless_strcmp("T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
  122. g_free(actual_value);
  123. mc_config_deinit (mc_config);
  124. g_free(ini_filename);
  125. }
  126. END_TEST
  127. /* --------------------------------------------------------------------------------------------- */
  128. int
  129. main (void)
  130. {
  131. int number_failed;
  132. Suite *s = suite_create (TEST_SUITE_NAME);
  133. TCase *tc_core = tcase_create ("Core");
  134. SRunner *sr;
  135. tcase_add_checked_fixture (tc_core, setup, teardown);
  136. /* Add new tests here: *************** */
  137. tcase_add_test (tc_core, create_ini_file);
  138. tcase_add_test (tc_core, emulate__learn_save);
  139. /* *********************************** */
  140. suite_add_tcase (s, tc_core);
  141. sr = srunner_create (s);
  142. // srunner_set_fork_status (sr, CK_NOFORK);
  143. srunner_run_all (sr, CK_NORMAL);
  144. number_failed = srunner_ntests_failed (sr);
  145. srunner_free (sr);
  146. return (number_failed == 0) ? 0 : 1;
  147. }
  148. /* --------------------------------------------------------------------------------------------- */