|
| 1 | +/* |
| 2 | + lib/skin - mc_skin_init() function testing |
| 3 | +
|
| 4 | + Copyright (C) 2026 |
| 5 | + Free Software Foundation, Inc. |
| 6 | +
|
| 7 | + Written by: |
| 8 | + Manuel Einfalt <einfalt1@proton.me>, 2026 |
| 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/skin" |
| 27 | + |
| 28 | +#include "tests/mctest.h" |
| 29 | + |
| 30 | +#include "lib/util.h" |
| 31 | +#include "lib/skin.h" |
| 32 | + |
| 33 | +#include "lib/strutil.h" // str_init_strings |
| 34 | +#include "src/vfs/local/local.h" // vfs_init_localfs |
| 35 | + |
| 36 | +/* --------------------------------------------------------------------------------------------- */ |
| 37 | + |
| 38 | +static const struct skin_tests // xterm, xterm-256color, xterm-direct |
| 39 | +{ |
| 40 | + const char *term; |
| 41 | + const gboolean nocolor; |
| 42 | + const gchar *skin_name; |
| 43 | + const gboolean has_error; |
| 44 | + const gboolean ret; |
| 45 | +} skin_tests[] = { |
| 46 | + { "xterm", FALSE, "dark", FALSE, TRUE }, |
| 47 | + { "xterm", FALSE, "julia256", TRUE, FALSE }, |
| 48 | + { "xterm", FALSE, "seasons-summer16M", TRUE, FALSE }, |
| 49 | + { "xterm", TRUE, "dark", FALSE, TRUE }, |
| 50 | + { "xterm", TRUE, "julia256", TRUE, FALSE }, |
| 51 | + { "xterm", TRUE, "seasons-summer16M", TRUE, FALSE }, |
| 52 | + |
| 53 | + { "xterm-256color", FALSE, "dark", FALSE, TRUE }, |
| 54 | + { "xterm-256color", FALSE, "julia256", FALSE, TRUE }, |
| 55 | + { "xterm-256color", FALSE, "seasons-summer16M", TRUE, FALSE }, |
| 56 | + { "xterm-256color", TRUE, "dark", FALSE, TRUE }, |
| 57 | + { "xterm-256color", TRUE, "julia256", FALSE, TRUE }, |
| 58 | + { "xterm-256color", TRUE, "seasons-summer16M", TRUE, FALSE }, |
| 59 | + |
| 60 | + { "xterm-256direct", FALSE, "dark", FALSE, TRUE }, |
| 61 | + { "xterm-256direct", FALSE, "julia256", FALSE, TRUE }, |
| 62 | + { "xterm-256direct", FALSE, "seasons-summer16M", FALSE, TRUE }, |
| 63 | + { "xterm-256direct", TRUE, "dark", FALSE, TRUE }, |
| 64 | + { "xterm-256direct", TRUE, "julia256", FALSE, TRUE }, |
| 65 | + { "xterm-256direct", TRUE, "seasons-summer16M", FALSE, TRUE }, |
| 66 | +}; |
| 67 | + |
| 68 | +extern GHashTable *mc_tty_color__hashtable; |
| 69 | + |
| 70 | +/* --------------------------------------------------------------------------------------------- */ |
| 71 | + |
| 72 | +static void |
| 73 | +setup (void) |
| 74 | +{ |
| 75 | + mc_global.share_data_dir = (char *) TEST_SHARE_DIR; |
| 76 | + mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR; |
| 77 | + |
| 78 | + str_init_strings (NULL); |
| 79 | + vfs_init (); |
| 80 | + vfs_init_localfs (); |
| 81 | + tty_color_role_to_pair = g_new (int, COLOR_MAP_SIZE); |
| 82 | + mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 83 | +} |
| 84 | + |
| 85 | +static void |
| 86 | +teardown (void) |
| 87 | +{ |
| 88 | + vfs_shut (); |
| 89 | + str_uninit_strings (); |
| 90 | +} |
| 91 | + |
| 92 | +/* --------------------------------------------------------------------------------------------- */ |
| 93 | + |
| 94 | +// Mock |
| 95 | +gboolean |
| 96 | +tty_use_truecolors (GError **error) |
| 97 | +{ |
| 98 | + const char *termname = getenv ("TERM"); |
| 99 | + |
| 100 | + if (termname == NULL) |
| 101 | + { |
| 102 | + mc_propagate_error (error, 0, _ ("foobar")); |
| 103 | + return FALSE; |
| 104 | + } |
| 105 | + |
| 106 | + if (strcmp (termname, "xterm-256direct") == 0) |
| 107 | + return TRUE; |
| 108 | + |
| 109 | + mc_propagate_error (error, 0, _ ("foobar")); |
| 110 | + return FALSE; |
| 111 | +} |
| 112 | + |
| 113 | +// Mock |
| 114 | +gboolean |
| 115 | +tty_use_256colors (GError **error) |
| 116 | +{ |
| 117 | + const char *termname = getenv ("TERM"); |
| 118 | + |
| 119 | + if (termname == NULL) |
| 120 | + { |
| 121 | + mc_propagate_error (error, 0, _ ("foobar")); |
| 122 | + return FALSE; |
| 123 | + } |
| 124 | + |
| 125 | + if (strcmp (termname, "xterm-256color") == 0 || tty_use_truecolors (NULL)) |
| 126 | + return TRUE; |
| 127 | + |
| 128 | + mc_propagate_error (error, 0, _ ("foobar")); |
| 129 | + return FALSE; |
| 130 | +} |
| 131 | + |
| 132 | +/* --------------------------------------------------------------------------------------------- */ |
| 133 | + |
| 134 | +START_PARAMETRIZED_TEST (skin_test, skin_tests) |
| 135 | +{ |
| 136 | + GError *mcerror = NULL; |
| 137 | + |
| 138 | + mc_global.tty.disable_colors = data->nocolor; |
| 139 | + setenv ("TERM", data->term, 1); |
| 140 | + |
| 141 | + if (data->ret) |
| 142 | + { |
| 143 | + mctest_assert_true (mc_skin_init (data->skin_name, &mcerror)); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + mctest_assert_false (mc_skin_init (data->skin_name, &mcerror)); |
| 148 | + } |
| 149 | + |
| 150 | + if (data->has_error) |
| 151 | + { |
| 152 | + mctest_assert_not_null (mcerror); |
| 153 | + g_error_free (mcerror); |
| 154 | + } |
| 155 | + else |
| 156 | + mctest_assert_null (mcerror); |
| 157 | +} |
| 158 | +END_PARAMETRIZED_TEST |
| 159 | + |
| 160 | +/* --------------------------------------------------------------------------------------------- */ |
| 161 | + |
| 162 | +int |
| 163 | +main (void) |
| 164 | +{ |
| 165 | + TCase *tc_core; |
| 166 | + |
| 167 | + tc_core = tcase_create ("Core"); |
| 168 | + tcase_add_checked_fixture (tc_core, setup, teardown); |
| 169 | + |
| 170 | + // Add new tests here: *************** |
| 171 | + mctest_add_parameterized_test (tc_core, skin_test, skin_tests); |
| 172 | + // *********************************** |
| 173 | + |
| 174 | + return mctest_run_all (tc_core); |
| 175 | +} |
| 176 | + |
| 177 | +/* --------------------------------------------------------------------------------------------- */ |
0 commit comments