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-2025
   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 <https://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 static const struct test_create_ini_file_ds
 103 {
 104     const char *input_group;
 105     const char *input_param;
 106     const char *input_default_value;
 107     const char *expected_value;
 108     const char *expected_raw_value;
 109 } test_create_ini_file_ds[] = {
 110     {
 111         // 0.
 112         "group-not-exists",
 113         "param-not_exists",
 114         NULL,
 115         NULL,
 116         NULL,
 117     },
 118     {
 119         // 1.
 120         "test-group1",
 121         "test-param1",
 122         "not-exists",
 123         " some value ",
 124         " some value ",
 125     },
 126     {
 127         // 2.
 128         "test-group1",
 129         "test-param2",
 130         "not-exists",
 131         // Should be represented as KOI8-R
 132         " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ",
 133         // Should be stored as UTF-8
 134         " \t Тестовое значение ",
 135     },
 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     {
 145         // 4.
 146         "test-group2",
 147         "test-param1",
 148         "not-exists",
 149         " some value ",
 150         " some value ",
 151     },
 152     {
 153         // 5.
 154         "test-group2",
 155         "test-param2",
 156         "not-exists",
 157         "not-exists",
 158         "not-exists",
 159     },
 160     {
 161         // 6.
 162         "test-group2",
 163         "test-param3",
 164         "not-exists",
 165         " \tsome value2\n\nf\b\005fff ",
 166         " \tsome value2\n\nf\b\005fff ",
 167     },
 168 };
 169 
 170 /* @Test(dataSource = "test_create_ini_file_ds") */
 171 START_PARAMETRIZED_TEST (test_create_ini_file_paths, test_create_ini_file_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     // given
 174     char *actual_value, *actual_raw_value;
 175 
 176     mc_config_set_string (mc_config, "test-group1", "test-param1", " some value ");
 177     mc_config_set_string (mc_config, "test-group1", "test-param2",
 178                           " \t \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5 ");
 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                               " \xF4\xC5\xD3\xD4\xCF\xD7\xCF\xC5 \xDA\xCE\xC1\xDE\xC5\xCE\xC9\xC5");
 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 = mc_config_get_string (mc_config, data->input_group, data->input_param,
 190                                          data->input_default_value);
 191     actual_raw_value = mc_config_get_string_raw (mc_config, data->input_group, data->input_param,
 192                                                  data->input_default_value);
 193 
 194     // then
 195     mctest_assert_str_eq (actual_value, data->expected_value);
 196     mctest_assert_str_eq (actual_raw_value, data->expected_raw_value);
 197 
 198     g_free (actual_value);
 199     g_free (actual_raw_value);
 200 }
 201 END_PARAMETRIZED_TEST
 202 
 203 /* --------------------------------------------------------------------------------------------- */
 204 
 205 /* @Test(group='Integration') */
 206 START_TEST (emulate__learn_save)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     // given
 209     char *actual_value;
 210 
 211     {
 212         char *esc_str;
 213 
 214         esc_str = str_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
 215         mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
 216         g_free (esc_str);
 217     }
 218 
 219     config_object__reopen ();
 220 
 221     // when
 222     actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
 223 
 224     // then
 225     mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
 226     g_free (actual_value);
 227 }
 228 END_TEST
 229 
 230 /* --------------------------------------------------------------------------------------------- */
 231 
 232 int
 233 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 234 {
 235     TCase *tc_core;
 236 
 237     tc_core = tcase_create ("Core");
 238 
 239     tcase_add_checked_fixture (tc_core, setup, teardown);
 240 
 241     // Add new tests here: ***************
 242     mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
 243     tcase_add_test (tc_core, emulate__learn_save);
 244     // ***********************************
 245 
 246     return mctest_run_all (tc_core);
 247 }
 248 
 249 /* --------------------------------------------------------------------------------------------- */

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