Manual pages: mcmcdiffmceditmcview

root/tests/lib/terminal.c

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

DEFINITIONS

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

   1 /*
   2    lib/terminal - tests for terminal emulation functions
   3 
   4    Copyright (C) 2025-2026
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Johannes Altmanninger, 2025
   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/terminal"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include <string.h>
  31 
  32 #include "lib/global.h"  // include <glib.h>
  33 #include "lib/terminal.h"
  34 #include "lib/strutil.h"
  35 
  36 /* --------------------------------------------------------------------------------------------- */
  37 
  38 static void
  39 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  40 {
  41     str_init_strings ("UTF-8");
  42 }
  43 
  44 static void
  45 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  46 {
  47     str_uninit_strings ();
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 START_TEST (test_parse_csi)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54     const char *s = &"\x1b[=5uRest"[2];
  55     const char *end = s + strlen (s);
  56     const gboolean ok = parse_csi (NULL, &s, end);
  57 
  58     ck_assert_msg (ok, "failed to parse CSI");
  59     ck_assert_str_eq (s, "Rest");
  60 }
  61 END_TEST
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 START_TEST (test_strip_ctrl_codes)
     /* [previous][next][first][last][top][bottom][index][help]  */
  66 {
  67     char *s;
  68     char *actual;
  69 
  70     // clang-format off
  71     s = g_strdup (ESC_STR "]0;~\a"                   ESC_STR "[30m" ESC_STR "(B" ESC_STR "[m"
  72                   ESC_STR "]133;A;special_key=1\a$ " ESC_STR "[K"   ESC_STR "[?2004h"
  73                   ESC_STR "[>4;1m" ESC_STR "[=5u"    ESC_STR "="    ESC_STR "[?2004l"
  74                   ESC_STR "[>4;0m" ESC_STR "[=0u"    ESC_STR ">"    ESC_STR "[?2004h"
  75                   ESC_STR "[>4;1m" ESC_STR "[=5u"    ESC_STR "="    ESC_STR "[?2004l"
  76                   ESC_STR "[>4;0m" ESC_STR "[=0u"    ESC_STR ">"    ESC_STR "[?2004h"
  77                   ESC_STR "[>4;1m" ESC_STR "[=5u"    ESC_STR "=");
  78     // clang-format on
  79 
  80     actual = strip_ctrl_codes (s);
  81 
  82     const char *expected = "$ ";
  83 
  84     ck_assert_str_eq (actual, expected);
  85     g_free (s);
  86 }
  87 END_TEST
  88 
  89 // Test the handling of inner and final incomplete UTF-8, also make sure there's no overrun.
  90 // Ticket #4801. Invalid UTF-8 fragments are left in the string as-is.
  91 START_TEST (test_strip_ctrl_codes2)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     char *s;
  94     char *actual;
  95     // U+2764 heart in UTF-8, followed by " ábcdéfghíjklnmó\000pqrst" in Latin-1
  96     const char s_orig[] = "\342\235\244 \341bcd\351fgh\355jklm\363\000pqrst";
  97 
  98     ck_assert_int_eq (sizeof (s_orig), 25);
  99 
 100     // copy the entire string, with embedded '\0'
 101     s = g_malloc (sizeof (s_orig));
 102     memcpy (s, s_orig, sizeof (s_orig));
 103 
 104     actual = strip_ctrl_codes (s);
 105 
 106     ck_assert_str_eq (actual, s_orig);
 107     g_free (s);
 108 }
 109 END_TEST
 110 
 111 /* --------------------------------------------------------------------------------------------- */
 112 
 113 int
 114 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 115 {
 116     TCase *tc_core;
 117 
 118     tc_core = tcase_create ("Core");
 119 
 120     tcase_add_checked_fixture (tc_core, setup, teardown);
 121 
 122     // Add new tests here: ***************
 123     tcase_add_test (tc_core, test_parse_csi);
 124     tcase_add_test (tc_core, test_strip_ctrl_codes);
 125     tcase_add_test (tc_core, test_strip_ctrl_codes2);
 126     // ***********************************
 127 
 128     return mctest_run_all (tc_core);
 129 }
 130 
 131 /* --------------------------------------------------------------------------------------------- */

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