1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
import pytest
from textual.widgets.text_area import Document, EditResult
TEXT = """I must not fear.
Fear is the mind-killer.
I forgot the rest of the quote.
Sorry Will."""
@pytest.fixture
def document():
document = Document(TEXT)
return document
def test_delete_single_character(document):
replace_result = document.replace_range((0, 0), (0, 1), "")
assert replace_result == EditResult(end_location=(0, 0), replaced_text="I")
assert document.lines == [
" must not fear.",
"Fear is the mind-killer.",
"I forgot the rest of the quote.",
"Sorry Will.",
]
def test_delete_single_newline(document):
"""Testing deleting newline from right to left"""
replace_result = document.replace_range((1, 0), (0, 16), "")
assert replace_result == EditResult(end_location=(0, 16), replaced_text="\n")
assert document.lines == [
"I must not fear.Fear is the mind-killer.",
"I forgot the rest of the quote.",
"Sorry Will.",
]
def test_delete_near_end_of_document(document):
"""Test deleting a range near the end of a document."""
replace_result = document.replace_range((1, 0), (3, 11), "")
assert replace_result == EditResult(
end_location=(1, 0),
replaced_text="Fear is the mind-killer.\n"
"I forgot the rest of the quote.\n"
"Sorry Will.",
)
assert document.lines == [
"I must not fear.",
"",
]
def test_delete_clearing_the_document(document):
replace_result = document.replace_range((0, 0), (4, 0), "")
assert replace_result == EditResult(
end_location=(0, 0),
replaced_text=TEXT,
)
assert document.lines == [""]
def test_delete_multiple_characters_on_one_line(document):
replace_result = document.replace_range((0, 2), (0, 7), "")
assert replace_result == EditResult(
end_location=(0, 2),
replaced_text="must ",
)
assert document.lines == [
"I not fear.",
"Fear is the mind-killer.",
"I forgot the rest of the quote.",
"Sorry Will.",
]
def test_delete_multiple_lines_partially_spanned(document):
"""Deleting a selection that partially spans the first and final lines of the selection."""
replace_result = document.replace_range((0, 2), (2, 2), "")
assert replace_result == EditResult(
end_location=(0, 2),
replaced_text="must not fear.\nFear is the mind-killer.\nI ",
)
assert document.lines == [
"I forgot the rest of the quote.",
"Sorry Will.",
]
def test_delete_end_of_line(document):
"""Testing deleting newline from left to right"""
replace_result = document.replace_range((0, 16), (1, 0), "")
assert replace_result == EditResult(
end_location=(0, 16),
replaced_text="\n",
)
assert document.lines == [
"I must not fear.Fear is the mind-killer.",
"I forgot the rest of the quote.",
"Sorry Will.",
]
def test_delete_single_line_excluding_newline(document):
"""Delete from the start to the end of the line."""
replace_result = document.replace_range((2, 0), (2, 31), "")
assert replace_result == EditResult(
end_location=(2, 0),
replaced_text="I forgot the rest of the quote.",
)
assert document.lines == [
"I must not fear.",
"Fear is the mind-killer.",
"",
"Sorry Will.",
]
def test_delete_single_line_including_newline(document):
"""Delete from the start of a line to the start of the line below."""
replace_result = document.replace_range((2, 0), (3, 0), "")
assert replace_result == EditResult(
end_location=(2, 0),
replaced_text="I forgot the rest of the quote.\n",
)
assert document.lines == [
"I must not fear.",
"Fear is the mind-killer.",
"Sorry Will.",
]
TEXT_NEWLINE_EOF = """\
I must not fear.
Fear is the mind-killer.
"""
def test_delete_end_of_file_newline():
document = Document(TEXT_NEWLINE_EOF)
replace_result = document.replace_range((2, 0), (1, 24), "")
assert replace_result == EditResult(end_location=(1, 24), replaced_text="\n")
assert document.lines == [
"I must not fear.",
"Fear is the mind-killer.",
]
|