1 # Copyright
2022-2024 Free Software Foundation
, Inc.
3 # This
program is free software
; you can redistribute it and
/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation
; either version
3 of the License
, or
6 #
(at your option
) any later version.
8 # This
program is distributed in the hope that it will be useful
,
9 # but WITHOUT
ANY WARRANTY
; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License
for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this
program.
If not
, see
<http
://www.gnu.org
/licenses
/>.
16 # Unit
-test Term
, the testsuite
's terminal implementation that is used to test
21 # Validate the cursor position.
23 # EXPECTED_CUR_COL and EXPECTED_CUR_ROW are the expected cursor column and row
26 proc check_cursor_position { test expected_cur_col expected_cur_row } {
27 with_test_prefix $test {
28 gdb_assert {$expected_cur_col == ${Term::_cur_col}} "column"
29 gdb_assert {$expected_cur_row == ${Term::_cur_row}} "row"
33 # Validate the terminal contents and cursor position.
35 # EXPECTED_CONTENTS must be a list of strings, one element for each terminal
38 # EXPECTED_CUR_COL and EXPECTED_CUR_ROW are passed to check_cursor_position.
40 proc check { test expected_contents expected_cur_col expected_cur_row } {
41 with_test_prefix $test {
42 # Check term contents.
45 foreach line $expected_contents {
51 Term::check_contents "contents" $regexp
54 check_cursor_position $test $expected_cur_col $expected_cur_row
57 proc setup_terminal { cols rows } {
59 Term::_setup $rows $cols
62 # Most tests are fine with a small terminal. This proc initializes the terminal
63 # with 8 columns and 4 rows, with the following content:
70 # The bottom right cell is left blank: trying to write to it using _insert
71 # would move the cursor past the screen, causing a scroll, but scrolling is
72 # not implemented at the moment.
77 Term::_insert "abcdefgh"
78 Term::_insert "ijklmnop"
79 Term::_insert "qrstuvwx"
80 Term::_insert "yz01234"
82 check "check after setup" {
90 # Some tests require a larger terminal. This proc initializes the terminal with
91 # 80 columns and 25 rows, but leaves the content empty.
97 # Each proc below tests a control character or sequence individually.
99 proc test_backspace {} {
100 # Note: the backspace (BS) control character only moves the cursor left,
101 # it does not delete characters.
103 Term::_move_cursor 1 2
106 check "backspace one" {
113 # Cursor should not move if it is already at column 0.
115 check "backspace 2" {
123 proc test_linefeed { } {
124 Term::_move_cursor 1 2
134 proc test_linefeed_scroll { } {
135 Term::_move_cursor 0 3
137 check "linefeed_scroll" {
146 proc test_carriage_return { } {
147 Term::_move_cursor 1 2
149 check "carriage return 1" {
157 check "carriage return 2" {
165 proc test_insert_characters { } {
166 Term::_move_cursor 1 2
169 check "insert characters 1" {
177 check "insert characters 2" {
184 Term::_move_cursor 0 1
186 check "insert characters 3" {
194 proc test_pan_down { } {
195 Term::_move_cursor 1 2
197 check "pan down, default arg" {
205 check "pan down, explicit arg" {
213 check "pan down, excessive arg" {
221 proc test_pan_up { } {
222 Term::_move_cursor 1 2
224 check "pan down, default arg" {
232 check "pan down, explicit arg" {
240 check "pan down, excessive arg" {
248 proc test_cursor_up { } {
249 Term::_move_cursor 2 3
252 check "cursor up 1" {
260 check "cursor up 2" {
268 check "cursor up 3" {
276 proc test_cursor_down { } {
277 Term::_move_cursor 1 0
280 check "cursor down 1" {
288 check "cursor down 2" {
296 check "cursor down 3" {
304 proc test_cursor_forward { } {
305 Term::_move_cursor 0 1
308 check "cursor forward 1" {
316 check "cursor forward 2" {
324 check "cursor forward 3" {
332 proc test_cursor_backward { } {
333 Term::_move_cursor 7 1
336 check "cursor backward 1" {
344 check "cursor backward 2" {
352 check "cursor backward 3" {
360 proc test_cursor_next_line { } {
361 Term::_move_cursor 2 0
364 check "cursor next line 1" {
371 Term::_move_cursor 2 1
373 check "cursor next line 2" {
380 Term::_move_cursor 2 3
382 check "cursor next line 3" {
390 proc test_cursor_previous_line { } {
391 Term::_move_cursor 2 3
394 check "cursor previous line 1" {
401 Term::_move_cursor 2 2
403 check "cursor previous line 2" {
410 Term::_move_cursor 2 0
412 check "cursor previous line 3" {
420 proc test_horizontal_absolute { } {
421 Term::_move_cursor 2 2
423 check "cursor horizontal absolute 1" {
430 Term::_move_cursor 2 2
432 check "cursor horizontal absolute 2" {
440 proc test_cursor_position { } {
441 Term::_move_cursor 1 1
444 check "cursor horizontal absolute 2" {
452 proc test_cursor_horizontal_forward_tabulation { } {
453 Term::_move_cursor 5 2
455 check_cursor_position "default param" 8 2
458 check_cursor_position "explicit param" 24 2
460 Term::_move_cursor 77 2
462 check_cursor_position "try to go past the end" 79 2
465 proc test_erase_in_display { } {
466 Term::_move_cursor 5 2
468 check "erase in display, cursor to end with default param" {
475 Term::_move_cursor 3 2
477 check "erase in display, cursor to end with explicit param" {
484 Term::_move_cursor 2 1
486 check "erase in display, beginning to cursor" {
493 Term::_move_cursor 5 1
495 check "erase in display, entire display" {
503 proc test_erase_in_line { } {
504 Term::_move_cursor 5 2
506 check "erase in line, cursor to end with default param" {
513 Term::_move_cursor 3 2
515 check "erase in line, cursor to end with explicit param" {
522 Term::_move_cursor 3 1
524 check "erase in line, beginning to cursor" {
531 Term::_move_cursor 3 0
533 check "erase in line, entire line" {
541 proc test_delete_line { } {
542 Term::_move_cursor 3 2
544 check "delete line, default param" {
551 Term::_move_cursor 3 0
553 check "delete line, explicit param" {
561 proc test_delete_character { } {
562 Term::_move_cursor 2 1
565 check "delete character, default param" {
573 check "delete character, explicit param" {
581 check "delete character, more than number of columns" {
589 proc test_erase_character { } {
590 Term::_move_cursor 3 2
592 check "erase character, default param" {
599 Term::_move_cursor 1 3
601 check "erase character, explicit param" {
609 proc test_cursor_backward_tabulation { } {
610 Term::_move_cursor 77 2
612 check_cursor_position "default param" 72 2
615 check_cursor_position "explicit param" 56 2
617 Term::_move_cursor 6 2
619 check_cursor_position "try to go past the beginning" 0 2
622 proc test_repeat { } {
623 Term::_move_cursor 2 1
624 set Term::_last_char X
635 proc test_vertical_line_position_absolute { } {
636 Term::_move_cursor 2 1
639 check "default param" {
647 check "explicit param" {
655 check "try to move off-display" {
663 proc test_insert_line { } {
664 Term::_move_cursor 2 1
666 check "insert line, default param" {
673 Term::_move_cursor 2 0
675 check "insert line, explicit param" {
683 check "insert line, insert more lines than display has" {
692 foreach { attr vals } {
739 foreach { arg val } $vals {
742 append re "a<$attr:$val>"
750 set line [Term::get_line_with_attrs 0]
751 gdb_assert { [regexp $re $line] } "attribute: $attr"
755 # Run proc TEST_PROC_NAME with a "small" terminal.
757 proc run_one_test_small { test_proc_name } {
758 save_vars { env(TERM) stty_init } {
764 # Run proc TEST_PROC_NAME with a "large" terminal.
766 proc run_one_test_large { test_proc_name } {
767 save_vars { env(TERM) stty_init } {
773 foreach_with_prefix test {
778 test_insert_characters
783 test_cursor_next_line
784 test_cursor_previous_line
785 test_horizontal_absolute
787 test_erase_in_display
790 test_delete_character
793 test_vertical_line_position_absolute
798 run_one_test_small $test
801 foreach_with_prefix test {
802 test_cursor_horizontal_forward_tabulation
803 test_cursor_backward_tabulation
805 run_one_test_large $test