root/tests/lib/mcconfig/config_string.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. config_object__init
  2. config_object__reopen
  3. config_object__deinit
  4. setup
  5. teardown
  6. START_PARAMETRIZED_TEST
  7. START_TEST
  8. main

   1 /*
   2    libmc - check mcconfig submodule. read and write config files
   3 
   4    Copyright (C) 2011-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "lib/mcconfig"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/mcconfig.h"
  31 #include "lib/strutil.h"
  32 #include "lib/strescape.h"
  33 #include "lib/vfs/vfs.h"
  34 #include "src/vfs/local/local.c"
  35 
  36 static mc_config_t *mc_config;
  37 static char *ini_filename;
  38 
  39 /* --------------------------------------------------------------------------------------------- */
  40 
  41 static void
  42 config_object__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  43 {
  44     ini_filename = g_build_filename (WORKDIR, "config_string.ini", (char *) NULL);
  45     unlink (ini_filename);
  46 
  47     mc_config = mc_config_init (ini_filename, FALSE);
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 static void
  53 config_object__reopen (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  54 {
  55     GError *error = NULL;
  56 
  57     if (!mc_config_save_file (mc_config, &error))
  58     {
  59         fail ("Unable to save config file: %s", error->message);
  60         g_error_free (error);
  61     }
  62 
  63     mc_config_deinit (mc_config);
  64     mc_config = mc_config_init (ini_filename, FALSE);
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 
  69 static void
  70 config_object__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  71 {
  72     mc_config_deinit (mc_config);
  73     g_free (ini_filename);
  74 }
  75 
  76 /* --------------------------------------------------------------------------------------------- */
  77 
  78 /* @Before */
  79 static void
  80 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  81 {
  82     mc_global.timer = mc_timer_new ();
  83     str_init_strings ("KOI8-R");
  84     vfs_init ();
  85     vfs_init_localfs ();
  86 
  87     config_object__init ();
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 /* @After */
  93 static void
  94 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  95 {
  96     config_object__deinit ();
  97 
  98     vfs_shut ();
  99     str_uninit_strings ();
 100     mc_timer_destroy (mc_global.timer);
 101 }
 102 
 103 /* --------------------------------------------------------------------------------------------- */
 104 
 105 /* @DataSource("test_create_ini_file_ds") */
 106 /* *INDENT-OFF* */
 107 static const struct test_create_ini_file_ds
 108 {
 109     const char *input_group;
 110     const char *input_param;
 111     const char *input_default_value;
 112     const char *expected_value;
 113     const char *expected_raw_value;
 114 } test_create_ini_file_ds[] =
 115 {
 116     { /* 0. */
 117         "group-not-exists",
 118         "param-not_exists",
 119         NULL,
 120         NULL,
 121         NULL
 122     },
 123     { /* 1. */
 124         "test-group1",
 125         "test-param1",
 126         "not-exists",
 127         " some value ",
 128         " some value "
 129     },
 130     { /* 2. */
 131         "test-group1",
 132         "test-param2",
 133         "not-exists",
 134         " \tkoi8-r: Тестовое значение ",
 135         " \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 "
 136     },
 137     { /* 3. */
 138         "test-group1",
 139         "test-param3",
 140         "not-exists",
 141         " \tsome value2\n\nf\b\005fff ",
 142         " \tsome value2\n\nf\b\005fff "
 143     },
 144     { /* 4. */
 145         "test-group2",
 146         "test-param1",
 147         "not-exists",
 148         " some value ",
 149         " some value "
 150     },
 151     { /* 5. */
 152         "test-group2",
 153         "test-param2",
 154         "not-exists",
 155         "not-exists",
 156         "not-exists"
 157     },
 158     { /* 6. */
 159         "test-group2",
 160         "test-param3",
 161         "not-exists",
 162         " \tsome value2\n\nf\b\005fff ",
 163         " \tsome value2\n\nf\b\005fff "
 164     },
 165 
 166 };
 167 /* *INDENT-ON* */
 168 
 169 /* @Test(dataSource = "test_create_ini_file_ds") */
 170 /* *INDENT-OFF* */
 171 START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 /* *INDENT-ON* */
 173 {
 174     /* given */
 175     char *actual_value, *actual_raw_value;
 176 
 177     mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
 178     mc_config_set_string (mc_config, "test-group1", "test-param2", " \tkoi8-r: Тестовое значение ");
 179     mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
 180     mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
 181     mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
 182                               " koi8-r: Тестовое значение");
 183     mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
 184                               " \tsome value2\n\nf\b\005fff ");
 185 
 186     config_object__reopen ();
 187 
 188     /* when */
 189     actual_value =
 190         mc_config_get_string (mc_config, data->input_group, data->input_param,
 191                               data->input_default_value);
 192     actual_raw_value =
 193         mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
 194                                   data->input_default_value);
 195 
 196     /* then */
 197     mctest_assert_str_eq (actual_value, data->expected_value);
 198     mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
 199 
 200     g_free (actual_value);
 201     g_free (actual_raw_value);
 202 }
 203 /* *INDENT-OFF* */
 204 END_PARAMETRIZED_TEST
 205 /* *INDENT-ON* */
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 
 209 /* @Test(group='Integration') */
 210 /* *INDENT-OFF* */
 211 START_TEST (emulate__learn_save)
     /* [previous][next][first][last][top][bottom][index][help]  */
 212 /* *INDENT-ON* */
 213 {
 214     /* given */
 215     char *actual_value;
 216 
 217     {
 218         char *esc_str;
 219 
 220         esc_str = strutils_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
 221         mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
 222         g_free (esc_str);
 223     }
 224 
 225     config_object__reopen ();
 226 
 227     /* when */
 228     actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
 229 
 230     /* then */
 231     mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
 232     g_free (actual_value);
 233 }
 234 /* *INDENT-OFF* */
 235 END_TEST
 236 /* *INDENT-ON* */
 237 
 238 /* --------------------------------------------------------------------------------------------- */
 239 
 240 int
 241 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 242 {
 243     int number_failed;
 244 
 245     Suite *s = suite_create (TEST_SUITE_NAME);
 246     TCase *tc_core = tcase_create ("Core");
 247     SRunner *sr;
 248 
 249     tcase_add_checked_fixture (tc_core, setup, teardown);
 250 
 251     /* Add new tests here: *************** */
 252     mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
 253     tcase_add_test (tc_core, emulate__learn_save);
 254     /* *********************************** */
 255 
 256     suite_add_tcase (s, tc_core);
 257     sr = srunner_create (s);
 258     srunner_set_log (sr, "config_string.log");
 259     srunner_run_all (sr, CK_ENV);
 260     number_failed = srunner_ntests_failed (sr);
 261     srunner_free (sr);
 262     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 263 }
 264 
 265 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */