2024-02-18 02:07:07 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021-2024 Leroy Hopson <godot-xterm@leroy.nix.nz>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
class_name TerminalTest extends GodotXtermTest
|
2024-02-17 23:50:38 +01:00
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
|
|
|
|
func get_described_class():
|
|
|
|
return Terminal
|
2024-02-17 23:50:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
func before_each():
|
2024-03-03 03:34:18 +01:00
|
|
|
super.before_each()
|
|
|
|
subject.size = Vector2(400, 200)
|
|
|
|
|
|
|
|
|
2024-03-03 10:25:14 +01:00
|
|
|
# Helper function to fill the screen with the given character.
|
|
|
|
func fill_screen(char: String = "A") -> String:
|
|
|
|
var result = char.repeat(subject.get_cols() * subject.get_rows())
|
|
|
|
subject.write(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-03-03 03:34:18 +01:00
|
|
|
class TestInterface:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
## API V2.
|
|
|
|
|
|
|
|
# Properties.
|
|
|
|
|
|
|
|
func test_has_property_bell_muted():
|
|
|
|
assert_has_property_with_default_value("bell_muted", false)
|
|
|
|
|
|
|
|
func test_has_property_bell_cooldown():
|
|
|
|
assert_has_property_with_default_value("bell_cooldown", 0.1)
|
|
|
|
|
|
|
|
func test_has_property_blink_on_time():
|
|
|
|
assert_has_property_with_default_value("blink_on_time", 0.6)
|
|
|
|
|
|
|
|
func test_has_property_blink_off_time():
|
|
|
|
assert_has_property_with_default_value("blink_off_time", 0.3)
|
|
|
|
|
2024-03-03 07:11:16 +01:00
|
|
|
func test_has_property_copy_on_selection():
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_has_property_with_default_value("copy_on_selection", false)
|
|
|
|
|
|
|
|
# TODO: Implement update_mode property.
|
|
|
|
func xtest_has_property_update_mode():
|
|
|
|
#assert_has_property_with_default_value("update_mode", UPDATE_MODE_AUTO)
|
|
|
|
pass
|
|
|
|
|
|
|
|
# cols and rows removed.
|
|
|
|
|
|
|
|
# Methods.
|
|
|
|
|
2024-03-03 10:25:14 +01:00
|
|
|
func test_has_method_clear():
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_has_method_with_return_type("clear", TYPE_NIL)
|
|
|
|
|
2024-03-03 08:27:59 +01:00
|
|
|
func test_has_method_copy_all():
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_has_method_with_return_type("copy_all", TYPE_STRING)
|
|
|
|
|
2024-03-03 07:11:16 +01:00
|
|
|
func test_has_method_copy_selection():
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_has_method_with_return_type("copy_selection", TYPE_STRING)
|
|
|
|
|
|
|
|
func test_has_method_get_cols():
|
|
|
|
assert_has_method_with_return_type("get_cols", TYPE_INT)
|
|
|
|
|
|
|
|
func test_has_method_get_rows():
|
|
|
|
assert_has_method_with_return_type("get_rows", TYPE_INT)
|
|
|
|
|
|
|
|
func test_has_method_write():
|
|
|
|
assert_has_method(subject, "write")
|
|
|
|
|
|
|
|
# Signals.
|
|
|
|
|
|
|
|
func test_has_signal_data_sent():
|
|
|
|
assert_has_signal(subject, "data_sent")
|
|
|
|
|
|
|
|
func test_has_signal_key_pressed():
|
|
|
|
assert_has_signal(subject, "key_pressed")
|
|
|
|
|
|
|
|
func test_has_signal_size_changed():
|
|
|
|
assert_has_signal(subject, "size_changed")
|
|
|
|
|
|
|
|
func test_has_signal_bell():
|
|
|
|
assert_has_signal(subject, "bell")
|
|
|
|
|
|
|
|
# Enums.
|
|
|
|
|
|
|
|
# TODO: Implement UpdateMode enum.
|
|
|
|
func xtest_has_enum_update_mode():
|
|
|
|
assert_eq(described_class.UPDATE_MODE_DISABLED, 0)
|
|
|
|
assert_eq(described_class.AUTO, 1)
|
|
|
|
assert_eq(described_class.ALL, 2)
|
|
|
|
assert_eq(described_class.ALL_NEXT_FRAME, 3)
|
|
|
|
|
|
|
|
## API Next.
|
|
|
|
|
|
|
|
# Methods.
|
|
|
|
|
|
|
|
func test_has_method_get_cursor_pos():
|
|
|
|
assert_has_method_with_return_type("get_cursor_pos", TYPE_VECTOR2I)
|
|
|
|
|
|
|
|
func test_has_method_get_cell_size():
|
|
|
|
assert_has_method_with_return_type("get_cell_size", TYPE_VECTOR2)
|
|
|
|
|
|
|
|
func test_has_method_write_with_response():
|
|
|
|
assert_has_method_with_return_type("write", TYPE_STRING)
|
|
|
|
|
|
|
|
# Enums.
|
|
|
|
|
|
|
|
func test_has_enum_inverse_mode():
|
|
|
|
assert_eq(described_class.INVERSE_MODE_INVERT, 0)
|
|
|
|
assert_eq(described_class.INVERSE_MODE_SWAP, 1)
|
|
|
|
|
|
|
|
## Other tests.
|
|
|
|
|
|
|
|
func test_has_no_visible_children():
|
|
|
|
# We add children like the bell timer for private use that should not
|
|
|
|
# be visible outside of the node itself.
|
|
|
|
assert_eq(subject.get_child_count(), 0)
|
2024-02-17 23:50:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestBell:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
func test_bell() -> void:
|
2024-03-03 03:34:18 +01:00
|
|
|
watch_signals(subject)
|
|
|
|
subject.bell_cooldown = 0
|
|
|
|
subject.write(char(7))
|
|
|
|
subject.write(char(0x07))
|
|
|
|
subject.write("\a")
|
|
|
|
subject.write("\u0007")
|
|
|
|
subject.write("'Ask not for whom the \a tolls; it tolls for thee' - John Donne")
|
|
|
|
assert_signal_emit_count(subject, "bell", 5)
|
2024-02-17 23:50:38 +01:00
|
|
|
|
2024-02-18 02:07:07 +01:00
|
|
|
func test_bell_mute() -> void:
|
2024-03-03 03:34:18 +01:00
|
|
|
watch_signals(subject)
|
|
|
|
subject.bell_muted = true
|
|
|
|
subject.write("\a")
|
|
|
|
assert_signal_emit_count(subject, "bell", 0)
|
2024-02-18 02:07:07 +01:00
|
|
|
|
|
|
|
func test_bell_cooldown() -> void:
|
2024-03-03 03:34:18 +01:00
|
|
|
watch_signals(subject)
|
|
|
|
subject.bell_cooldown = 10000
|
|
|
|
subject.write("\a")
|
|
|
|
subject.write("\a")
|
|
|
|
assert_signal_emit_count(subject, "bell", 1)
|
2024-02-18 02:07:07 +01:00
|
|
|
|
|
|
|
func test_change_cooldown_while_active() -> void:
|
2024-03-03 03:34:18 +01:00
|
|
|
watch_signals(subject)
|
|
|
|
subject.bell_cooldown = 10000
|
|
|
|
subject.write("\a")
|
|
|
|
subject.bell_cooldown = 0
|
|
|
|
subject.write("\a")
|
|
|
|
assert_signal_emit_count(subject, "bell", 2)
|
2024-02-18 02:07:07 +01:00
|
|
|
|
2024-02-17 23:50:38 +01:00
|
|
|
|
|
|
|
class TestCursorPos:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
func test_get_cursor_pos_initial():
|
2024-03-03 03:34:18 +01:00
|
|
|
assert_eq(subject.get_cursor_pos(), Vector2i.ZERO)
|
2024-02-17 23:50:38 +01:00
|
|
|
|
|
|
|
func test_get_cursor_pos_x():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.write("_")
|
|
|
|
assert_eq(subject.get_cursor_pos().x, 1)
|
2024-02-17 23:50:38 +01:00
|
|
|
|
|
|
|
func test_get_cursor_pos_y():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.write("_".repeat(subject.cols + 1))
|
|
|
|
assert_eq(subject.get_cursor_pos().y, 1)
|
2024-02-25 08:25:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestWrite:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
func test_returns_response_when_input_contains_query():
|
2024-03-03 03:34:18 +01:00
|
|
|
var response = subject.write("\u001b[6n") # Query cursor position.
|
2024-02-25 08:25:32 +01:00
|
|
|
assert_eq(response, "\u001b[1;1R")
|
|
|
|
|
|
|
|
func test_returns_response_to_multiple_queries():
|
2024-03-03 03:34:18 +01:00
|
|
|
var response = subject.write("\u001b[6n\u001b[5n") # Query cursor position and status.
|
2024-02-25 08:25:32 +01:00
|
|
|
assert_eq(response, "\u001b[1;1R\u001b[0n")
|
|
|
|
|
|
|
|
func test_returns_response_to_multiple_queries_among_other_data():
|
2024-03-03 03:34:18 +01:00
|
|
|
var response = subject.write("hello\r\nworld\u001b[6nother\r\ndata\u001b[5ntest")
|
2024-02-25 08:25:32 +01:00
|
|
|
assert_eq(response, "\u001b[2;6R\u001b[0n")
|
2024-02-25 09:14:03 +01:00
|
|
|
|
|
|
|
func test_data_sent_emitted_on_query():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.write("\u001b[6n")
|
|
|
|
assert_signal_emitted(subject, "data_sent")
|
2024-02-25 09:14:03 +01:00
|
|
|
|
|
|
|
func test_data_sent_emitted_with_response():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.write("\u001b[6n")
|
2024-02-25 09:14:03 +01:00
|
|
|
assert_signal_emitted_with_parameters(
|
2024-03-03 03:34:18 +01:00
|
|
|
subject, "data_sent", ["\u001b[1;1R".to_utf8_buffer()]
|
2024-02-25 09:14:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func test_data_sent_not_emitted_when_empty_string_written():
|
2024-03-03 03:34:18 +01:00
|
|
|
subject.write("")
|
|
|
|
assert_signal_emit_count(subject, "data_sent", 0)
|
2024-03-03 08:27:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestCopy:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
func test_copy_all_copies_the_entire_screen():
|
|
|
|
var text = fill_screen()
|
|
|
|
# The text will be wrapped over multiple lines and copy_all() preserves
|
|
|
|
# these line wraps, therefore we need to strip them.
|
|
|
|
assert_eq(subject.copy_all().replace("\n", ""), text)
|
|
|
|
|
|
|
|
func test_copy_all_empty_screen():
|
|
|
|
assert_eq(subject.copy_all(), "\n".repeat(subject.get_rows()))
|
|
|
|
|
|
|
|
func test_copy_all_copies_the_scrollback_buffer():
|
|
|
|
var text = fill_screen()
|
|
|
|
text += fill_screen("B")
|
|
|
|
text += fill_screen("C")
|
|
|
|
assert_eq(subject.copy_all().replace("\n", ""), text)
|
|
|
|
|
|
|
|
func test_copy_all_copies_unicode_text():
|
|
|
|
var text = "アイウエオカキクケコサシスセソ"
|
|
|
|
subject.write(text)
|
|
|
|
assert_string_contains(subject.copy_all(), text)
|
2024-03-03 10:25:14 +01:00
|
|
|
|
2024-04-28 05:25:26 +02:00
|
|
|
func test_copy_selection_when_nothing_selected():
|
|
|
|
assert_eq(subject.copy_selection(), "")
|
|
|
|
|
2024-03-03 10:25:14 +01:00
|
|
|
|
|
|
|
class TestClear:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
func test_clear_an_empty_screen_changes_nothing():
|
|
|
|
var empty_screen = subject.copy_all()
|
|
|
|
subject.clear()
|
|
|
|
var screen_after = subject.copy_all()
|
|
|
|
assert_eq(screen_after, empty_screen)
|
|
|
|
|
|
|
|
func test_clear_when_screen_is_full_clears_all_but_the_bottommost_row():
|
|
|
|
fill_screen()
|
|
|
|
var final_line = "THIS SHOULDN'T BE CLEARED"
|
|
|
|
subject.write(final_line)
|
|
|
|
subject.clear()
|
|
|
|
var screen_after = subject.copy_all()
|
|
|
|
var expected = final_line + "\n".repeat(subject.get_rows())
|
|
|
|
assert_eq(screen_after, expected)
|
2024-04-26 03:58:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestSelect:
|
|
|
|
extends TerminalTest
|
|
|
|
|
|
|
|
# Use the behavior of TextEdit's select() method as a reference.
|
|
|
|
var text_edit: TextEdit
|
|
|
|
|
|
|
|
func assert_select_eq(argv, expected):
|
|
|
|
text_edit.callv("select", argv)
|
|
|
|
subject.callv("select", argv)
|
|
|
|
assert_eq(
|
|
|
|
expected,
|
|
|
|
text_edit.get_selected_text(),
|
|
|
|
"expected does not match reference implementation"
|
|
|
|
)
|
|
|
|
assert_eq(subject.copy_selection(), expected)
|
|
|
|
|
|
|
|
func before_each():
|
|
|
|
super.before_each()
|
|
|
|
text_edit = TextEdit.new()
|
|
|
|
text_edit.text = "0123456789\nABCDEFGHIJ\n)!@#$%^&*(\n\n\n\n\n\n\n"
|
|
|
|
add_child_autofree(text_edit)
|
|
|
|
subject.write("0123456789\r\nABCDEFGHIJ\r\n)!@#$%^&*(")
|
|
|
|
|
|
|
|
func test_select_nothing():
|
|
|
|
assert_select_eq([0, 0, 0, 0], "")
|
|
|
|
|
|
|
|
func test_select_first_character():
|
|
|
|
assert_select_eq([0, 0, 0, 1], "0")
|
|
|
|
|
|
|
|
func test_select_last_character():
|
|
|
|
assert_select_eq([2, 9, 2, 10], "(")
|
|
|
|
|
|
|
|
func test_select_reverse_column():
|
|
|
|
assert_select_eq([0, 6, 0, 1], "12345")
|
|
|
|
|
|
|
|
func test_select_preceeds_column_bounds():
|
|
|
|
assert_select_eq([0, -2, 0, -1], "")
|
|
|
|
assert_select_eq([0, -2, 0, 0], "")
|
|
|
|
assert_select_eq([0, -2, 0, 1], "0")
|
|
|
|
|
|
|
|
func test_select_exceeds_column_bounds():
|
|
|
|
assert_select_eq([0, 5, 0, 999], "56789")
|
|
|
|
|
|
|
|
func test_select_first_row():
|
|
|
|
assert_select_eq([0, 0, 0, 10], "0123456789")
|
|
|
|
|
|
|
|
func test_select_second_row():
|
|
|
|
assert_select_eq([1, 0, 1, 10], "ABCDEFGHIJ")
|
|
|
|
|
|
|
|
func test_select_multiple_rows():
|
|
|
|
assert_select_eq([0, 0, 1, 10], "0123456789\nABCDEFGHIJ")
|
|
|
|
|
|
|
|
func test_select_rows_reverse():
|
|
|
|
assert_select_eq([1, 5, 0, 0], "0123456789\nABCDE")
|
|
|
|
|
|
|
|
func test_select_preceeds_row_bounds():
|
|
|
|
assert_select_eq([-2, 0, -1, 10], "0123456789")
|
|
|
|
assert_select_eq([-2, 0, 0, 10], "0123456789")
|
|
|
|
assert_select_eq([-2, 0, 1, 10], "0123456789\nABCDEFGHIJ")
|
|
|
|
|
|
|
|
func test_select_exceeds_row_bounds():
|
|
|
|
assert_select_eq([1, 5, 999, 999], "FGHIJ\n)!@#$%^&*(\n\n\n\n\n\n\n")
|
|
|
|
|
|
|
|
func test_wide_bounds():
|
|
|
|
assert_select_eq([-999, -999, 999, 999], "0123456789\nABCDEFGHIJ\n)!@#$%^&*(\n\n\n\n\n\n\n")
|