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