1 # Copyright 2022 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_carriage_return { } {
135 Term::_move_cursor 1 2
137 check "carriage return 1" {
145 check "carriage return 2" {
153 proc test_insert_characters { } {
154 Term::_move_cursor 1 2
157 check "insert characters 1" {
165 check "insert characters 2" {
172 Term::_move_cursor 0 1
174 check "insert characters 3" {
182 proc test_cursor_up { } {
183 Term::_move_cursor 2 3
186 check "cursor up 1" {
194 check "cursor up 2" {
202 check "cursor up 3" {
210 proc test_cursor_down { } {
211 Term::_move_cursor 1 0
214 check "cursor down 1" {
222 check "cursor down 2" {
230 check "cursor down 3" {
238 proc test_cursor_forward { } {
239 Term::_move_cursor 0 1
242 check "cursor forward 1" {
250 check "cursor forward 2" {
258 check "cursor forward 3" {
266 proc test_cursor_backward { } {
267 Term::_move_cursor 7 1
270 check "cursor backward 1" {
278 check "cursor backward 2" {
286 check "cursor backward 3" {
294 proc test_cursor_next_line { } {
295 Term::_move_cursor 2 0
298 check "cursor next line 1" {
305 Term::_move_cursor 2 1
307 check "cursor next line 2" {
314 Term::_move_cursor 2 3
316 check "cursor next line 3" {
324 proc test_cursor_previous_line { } {
325 Term::_move_cursor 2 3
328 check "cursor previous line 1" {
335 Term::_move_cursor 2 2
337 check "cursor previous line 2" {
344 Term::_move_cursor 2 0
346 check "cursor previous line 3" {
354 proc test_horizontal_absolute { } {
355 Term::_move_cursor 2 2
357 check "cursor horizontal absolute 1" {
364 Term::_move_cursor 2 2
366 check "cursor horizontal absolute 2" {
374 proc test_cursor_position { } {
375 Term::_move_cursor 1 1
378 check "cursor horizontal absolute 2" {
386 proc test_cursor_horizontal_forward_tabulation { } {
387 Term::_move_cursor 5 2
389 check_cursor_position "default param" 8 2
392 check_cursor_position "explicit param" 24 2
394 Term::_move_cursor 77 2
396 check_cursor_position "try to go past the end" 79 2
399 proc test_erase_in_display { } {
400 Term::_move_cursor 5 2
402 check "erase in display, cursor to end with default param" {
409 Term::_move_cursor 3 2
411 check "erase in display, cursor to end with explicit param" {
418 Term::_move_cursor 2 1
420 check "erase in display, beginning to cursor" {
427 Term::_move_cursor 5 1
429 check "erase in display, entire display" {
437 proc test_erase_in_line { } {
438 Term::_move_cursor 5 2
440 check "erase in line, cursor to end with default param" {
447 Term::_move_cursor 3 2
449 check "erase in line, cursor to end with explicit param" {
456 Term::_move_cursor 3 1
458 check "erase in line, beginning to cursor" {
465 Term::_move_cursor 3 0
467 check "erase in line, entire line" {
475 proc test_delete_line { } {
476 Term::_move_cursor 3 2
478 check "delete line, default param" {
485 Term::_move_cursor 3 0
487 check "delete line, explicit param" {
495 proc test_delete_character { } {
496 Term::_move_cursor 2 1
499 check "delete character, default param" {
507 check "delete character, explicit param" {
515 check "delete character, more than number of columns" {
523 proc test_erase_character { } {
524 Term::_move_cursor 3 2
526 check "erase character, default param" {
533 Term::_move_cursor 1 3
535 check "erase character, explicit param" {
543 proc test_cursor_backward_tabulation { } {
544 Term::_move_cursor 77 2
546 check_cursor_position "default param" 72 2
549 check_cursor_position "explicit param" 56 2
551 Term::_move_cursor 6 2
553 check_cursor_position "try to go past the beginning" 0 2
556 proc test_repeat { } {
557 Term::_move_cursor 2 1
558 set Term::_last_char X
569 proc test_vertical_line_position_absolute { } {
570 Term::_move_cursor 2 1
573 check "default param" {
581 check "explicit param" {
589 check "try to move off-display" {
597 # Run proc TEST_PROC_NAME with a "small" terminal.
599 proc run_one_test_small { test_proc_name } {
600 save_vars { env(TERM) stty_init } {
606 # Run proc TEST_PROC_NAME with a "large" terminal.
608 proc run_one_test_large { test_proc_name } {
609 save_vars { env(TERM) stty_init } {
615 foreach_with_prefix test {
619 test_insert_characters
624 test_cursor_next_line
625 test_cursor_previous_line
626 test_horizontal_absolute
628 test_erase_in_display
631 test_delete_character
634 test_vertical_line_position_absolute
636 run_one_test_small $test
639 foreach_with_prefix test {
640 test_cursor_horizontal_forward_tabulation
641 test_cursor_backward_tabulation
643 run_one_test_large $test