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-2024
   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/vfs/vfs.h"
  33 #include "src/vfs/local/local.c"
  34 
  35 static mc_config_t *mc_config;
  36 static char *ini_filename;
  37 
  38 /* --------------------------------------------------------------------------------------------- */
  39 
  40 static void
  41 config_object__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  42 {
  43     ini_filename = g_build_filename (WORKDIR, "config_string.ini", (char *) NULL);
  44     unlink (ini_filename);
  45 
  46     mc_config = mc_config_init (ini_filename, FALSE);
  47 }
  48 
  49 /* --------------------------------------------------------------------------------------------- */
  50 
  51 static void
  52 config_object__reopen (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54     GError *error = NULL;
  55 
  56     if (!mc_config_save_file (mc_config, &error))
  57     {
  58         ck_abort_msg ("Unable to save config file: %s", error->message);
  59     }
  60 
  61     mc_config_deinit (mc_config);
  62     mc_config = mc_config_init (ini_filename, FALSE);
  63 }
  64 
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 static void
  68 config_object__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70     mc_config_deinit (mc_config);
  71     g_free (ini_filename);
  72 }
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 
  76 /* @Before */
  77 static void
  78 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  79 {
  80     str_init_strings ("KOI8-R");
  81     vfs_init ();
  82     vfs_init_localfs ();
  83 
  84     config_object__init ();
  85 }
  86 
  87 /* --------------------------------------------------------------------------------------------- */
  88 
  89 /* @After */
  90 static void
  91 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     config_object__deinit ();
  94 
  95     vfs_shut ();
  96     str_uninit_strings ();
  97 }
  98 
  99 /* --------------------------------------------------------------------------------------------- */
 100 
 101 /* @DataSource("test_create_ini_file_ds") */
 102 /* *INDENT-OFF* */
 103 static const struct test_create_ini_file_ds
 104 {
 105     const char *input_group;
 106     const char *input_param;
 107     const char *input_default_value;
 108     const char *expected_value;
 109     const char *expected_raw_value;
 110 } test_create_ini_file_ds[] =
 111 {
 112     { /* 0. */
 113         "group-not-exists",
 114         "param-not_exists",
 115         NULL,
 116         NULL,
 117         NULL
 118     },
 119     { /* 1. */
 120         "test-group1",
 121         "test-param1",
 122         "not-exists",
 123         " some value ",
 124         " some value "
 125     },
 126     { /* 2. */
 127         "test-group1",
 128         "test-param2",
 129         "not-exists",
 130         /* Should be represented as KOI8-R */
 131         " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ",
 132         /* Should be stored as UTF-8 */
 133         " \t Тестовое значение "
 134     },
 135     { /* 3. */
 136         "test-group1",
 137         "test-param3",
 138         "not-exists",
 139         " \tsome value2\n\nf\b\005fff ",
 140         " \tsome value2\n\nf\b\005fff "
 141     },
 142     { /* 4. */
 143         "test-group2",
 144         "test-param1",
 145         "not-exists",
 146         " some value ",
 147         " some value "
 148     },
 149     { /* 5. */
 150         "test-group2",
 151         "test-param2",
 152         "not-exists",
 153         "not-exists",
 154         "not-exists"
 155     },
 156     { /* 6. */
 157         "test-group2",
 158         "test-param3",
 159         "not-exists",
 160         " \tsome value2\n\nf\b\005fff ",
 161         " \tsome value2\n\nf\b\005fff "
 162     },
 163 
 164 };
 165 /* *INDENT-ON* */
 166 
 167 /* @Test(dataSource = "test_create_ini_file_ds") */
 168 /* *INDENT-OFF* */
 169 START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 170 /* *INDENT-ON* */
 171 {
 172     /* given */
 173     char *actual_value, *actual_raw_value;
 174 
 175     mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
 176     mc_config_set_string (mc_config, "test-group1", "test-param2",
 177                           " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ");
 178     mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
 179     mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
 180     mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
 181                               " \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5");
 182     mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
 183                               " \tsome value2\n\nf\b\005fff ");
 184 
 185     config_object__reopen ();
 186 
 187     /* when */
 188     actual_value =
 189         mc_config_get_string (mc_config, data->input_group, data->input_param,
 190                               data->input_default_value);
 191     actual_raw_value =
 192         mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
 193                                   data->input_default_value);
 194 
 195     /* then */
 196     mctest_assert_str_eq (actual_value, data->expected_value);
 197     mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
 198 
 199     g_free (actual_value);
 200     g_free (actual_raw_value);
 201 }
 202 /* *INDENT-OFF* */
 203 END_PARAMETRIZED_TEST
 204 /* *INDENT-ON* */
 205 
 206 /* --------------------------------------------------------------------------------------------- */
 207 
 208 /* @Test(group='Integration') */
 209 /* *INDENT-OFF* */
 210 START_TEST (emulate__learn_save)
     /* [previous][next][first][last][top][bottom][index][help]  */
 211 /* *INDENT-ON* */
 212 {
 213     /* given */
 214     char *actual_value;
 215 
 216     {
 217         char *esc_str;
 218 
 219         esc_str = str_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
 220         mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
 221         g_free (esc_str);
 222     }
 223 
 224     config_object__reopen ();
 225 
 226     /* when */
 227     actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
 228 
 229     /* then */
 230     mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
 231     g_free (actual_value);
 232 }
 233 /* *INDENT-OFF* */
 234 END_TEST
 235 /* *INDENT-ON* */
 236 
 237 /* --------------------------------------------------------------------------------------------- */
 238 
 239 int
 240 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 241 {
 242     TCase *tc_core;
 243 
 244     tc_core = tcase_create ("Core");
 245 
 246     tcase_add_checked_fixture (tc_core, setup, teardown);
 247 
 248     /* Add new tests here: *************** */
 249     mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
 250     tcase_add_test (tc_core, emulate__learn_save);
 251     /* *********************************** */
 252 
 253     return mctest_run_all (tc_core);
 254 }
 255 
 256 /* --------------------------------------------------------------------------------------------- */

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