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/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         ck_abort_msg ("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     str_init_strings ("KOI8-R");
  83     vfs_init ();
  84     vfs_init_localfs ();
  85 
  86     config_object__init ();
  87 }
  88 
  89 /* --------------------------------------------------------------------------------------------- */
  90 
  91 /* @After */
  92 static void
  93 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  94 {
  95     config_object__deinit ();
  96 
  97     vfs_shut ();
  98     str_uninit_strings ();
  99 }
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 
 103 /* @DataSource("test_create_ini_file_ds") */
 104 /* *INDENT-OFF* */
 105 static const struct test_create_ini_file_ds
 106 {
 107     const char *input_group;
 108     const char *input_param;
 109     const char *input_default_value;
 110     const char *expected_value;
 111     const char *expected_raw_value;
 112 } test_create_ini_file_ds[] =
 113 {
 114     { /* 0. */
 115         "group-not-exists",
 116         "param-not_exists",
 117         NULL,
 118         NULL,
 119         NULL
 120     },
 121     { /* 1. */
 122         "test-group1",
 123         "test-param1",
 124         "not-exists",
 125         " some value ",
 126         " some value "
 127     },
 128     { /* 2. */
 129         "test-group1",
 130         "test-param2",
 131         "not-exists",
 132         " \tkoi8-r: Тестовое значение ",
 133         " \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 "
 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", " \tkoi8-r: Тестовое значение ");
 177     mc_config_set_string (mc_config, "test-group1", "test-param3", " \tsome value2\n\nf\b\005fff ");
 178     mc_config_set_string_raw (mc_config, "test-group2", "test-param1", " some value ");
 179     mc_config_set_string_raw (mc_config, "test-group2", "test-param2",
 180                               " koi8-r: Тестовое значение");
 181     mc_config_set_string_raw (mc_config, "test-group2", "test-param3",
 182                               " \tsome value2\n\nf\b\005fff ");
 183 
 184     config_object__reopen ();
 185 
 186     /* when */
 187     actual_value =
 188         mc_config_get_string (mc_config, data->input_group, data->input_param,
 189                               data->input_default_value);
 190     actual_raw_value =
 191         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 /* *INDENT-OFF* */
 202 END_PARAMETRIZED_TEST
 203 /* *INDENT-ON* */
 204 
 205 /* --------------------------------------------------------------------------------------------- */
 206 
 207 /* @Test(group='Integration') */
 208 /* *INDENT-OFF* */
 209 START_TEST (emulate__learn_save)
     /* [previous][next][first][last][top][bottom][index][help]  */
 210 /* *INDENT-ON* */
 211 {
 212     /* given */
 213     char *actual_value;
 214 
 215     {
 216         char *esc_str;
 217 
 218         esc_str = strutils_escape ("T;E\\X;T-FOR-\\T;E;S\\TI;N'G", -1, ";", TRUE);
 219         mc_config_set_string_raw (mc_config, "test-group1", "test-param1", esc_str);
 220         g_free (esc_str);
 221     }
 222 
 223     config_object__reopen ();
 224 
 225     /* when */
 226     actual_value = mc_config_get_string_raw (mc_config, "test-group1", "test-param1", "not-exists");
 227 
 228     /* then */
 229     mctest_assert_str_eq (actual_value, "T\\;E\\X\\;T-FOR-\\T\\;E\\;S\\TI\\;N'G");
 230     g_free (actual_value);
 231 }
 232 /* *INDENT-OFF* */
 233 END_TEST
 234 /* *INDENT-ON* */
 235 
 236 /* --------------------------------------------------------------------------------------------- */
 237 
 238 int
 239 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 240 {
 241     TCase *tc_core;
 242 
 243     tc_core = tcase_create ("Core");
 244 
 245     tcase_add_checked_fixture (tc_core, setup, teardown);
 246 
 247     /* Add new tests here: *************** */
 248     mctest_add_parameterized_test (tc_core, test_create_ini_file_paths, test_create_ini_file_ds);
 249     tcase_add_test (tc_core, emulate__learn_save);
 250     /* *********************************** */
 251 
 252     return mctest_run_all (tc_core);
 253 }
 254 
 255 /* --------------------------------------------------------------------------------------------- */

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