root/tests/src/filemanager/get_random_hint.c

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

DEFINITIONS

This source file includes following definitions.
  1. guess_message_value
  2. rand
  3. setup
  4. teardown
  5. START_TEST
  6. START_PARAMETRIZED_TEST
  7. main

   1 /*
   2    src/filemanager - filemanager functions.
   3    Tests for getting random hints.
   4 
   5    Copyright (C) 2013-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2013
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  25  */
  26 
  27 #define TEST_SUITE_NAME "/src/filemanager"
  28 
  29 #include "tests/mctest.h"
  30 
  31 #include "lib/strutil.h"
  32 #include "lib/util.h"
  33 
  34 #include "src/filemanager/filemanager.h"
  35 
  36 /* --------------------------------------------------------------------------------------------- */
  37 /* mocked functions */
  38 
  39 /* @Mock */
  40 char *
  41 guess_message_value (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  42 {
  43     return g_strdup ("not_exists");
  44 }
  45 
  46 /* --------------------------------------------------------------------------------------------- */
  47 
  48 /* @ThenReturnValue */
  49 static gboolean rand__return_value = 0;
  50 
  51 /* @Mock */
  52 int
  53 rand (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  54 {
  55     return rand__return_value;
  56 }
  57 
  58 /* --------------------------------------------------------------------------------------------- */
  59 
  60 static void
  61 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  62 {
  63     mc_global.share_data_dir = (char *) TEST_SHARE_DIR;
  64     str_init_strings (NULL);
  65 }
  66 
  67 static void
  68 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70     str_uninit_strings ();
  71 }
  72 
  73 /* --------------------------------------------------------------------------------------------- */
  74 
  75 START_TEST (test_not_force)
     /* [previous][next][first][last][top][bottom][index][help]  */
  76 {
  77     // given
  78     char *first_hint_for_ignore;
  79     char *actual_hint1;
  80     char *actual_hint2;
  81     char *actual_hint3;
  82 
  83     // when
  84     first_hint_for_ignore = get_random_hint (FALSE);
  85     actual_hint1 = get_random_hint (FALSE);
  86     actual_hint2 = get_random_hint (FALSE);
  87     actual_hint3 = get_random_hint (FALSE);
  88 
  89     // then
  90     mctest_assert_ptr_ne (first_hint_for_ignore, NULL);
  91     mctest_assert_str_eq (actual_hint1, "");
  92     mctest_assert_str_eq (actual_hint2, "");
  93     mctest_assert_str_eq (actual_hint3, "");
  94 
  95     g_free (actual_hint3);
  96     g_free (actual_hint2);
  97     g_free (actual_hint1);
  98     g_free (first_hint_for_ignore);
  99 }
 100 END_TEST
 101 
 102 /* --------------------------------------------------------------------------------------------- */
 103 #define MC_HINT_FILE_SIZE 58
 104 /* @DataSource("get_random_ds") */
 105 static const struct get_random_ds
 106 {
 107     int input_random_value;
 108 
 109     const char *expected_value;
 110 } get_random_ds[] = {
 111     {
 112         // 0.
 113         MC_HINT_FILE_SIZE + 2,
 114         "Para_1",
 115     },
 116     {
 117         // 1.
 118         MC_HINT_FILE_SIZE + 10,
 119         "Para_2_line_1 Para_2_line_2",
 120     },
 121     {
 122         // 2.
 123         MC_HINT_FILE_SIZE + 25,
 124         "Para_2_line_1 Para_2_line_2",
 125     },
 126     {
 127         // 3.
 128         MC_HINT_FILE_SIZE + 40,
 129         "Para_3",
 130     },
 131     {
 132         // 4.
 133         MC_HINT_FILE_SIZE + 50,
 134         "P A R A _ 4 ",  // the trailing space it's a bug, but not critical and may be omitted
 135     },
 136 };
 137 // @Test(dataSource = "get_random_ds")
 138 START_PARAMETRIZED_TEST (get_random, get_random_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     // given
 141     char *actual_value;
 142 
 143     rand__return_value = data->input_random_value;
 144 
 145     // when
 146     actual_value = get_random_hint (TRUE);
 147 
 148     // then
 149     mctest_assert_str_eq (actual_value, data->expected_value);
 150     g_free (actual_value);
 151 }
 152 END_PARAMETRIZED_TEST
 153 
 154 /* --------------------------------------------------------------------------------------------- */
 155 
 156 int
 157 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 158 {
 159     TCase *tc_core;
 160 
 161     tc_core = tcase_create ("Core");
 162 
 163     tcase_add_checked_fixture (tc_core, setup, teardown);
 164 
 165     // Add new tests here: ***************
 166     tcase_add_test (tc_core, test_not_force);
 167     mctest_add_parameterized_test (tc_core, get_random, get_random_ds);
 168     // ***********************************
 169 
 170     return mctest_run_all (tc_core);
 171 }
 172 
 173 /* --------------------------------------------------------------------------------------------- */

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