File size: 4,859 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from textual._text_backend import TextEditorBackend

CONTENT = "Hello, world!"


def test_set_content():
    editor = TextEditorBackend()
    editor.set_content(CONTENT)
    assert editor.content == CONTENT


def test_delete_back_cursor_at_start_is_noop():
    editor = TextEditorBackend(CONTENT)
    assert not editor.delete_back()
    assert editor == TextEditorBackend(CONTENT, 0)


def test_delete_back_cursor_at_end():
    editor = TextEditorBackend(CONTENT)
    assert editor.cursor_text_end()
    assert editor.delete_back()
    assert editor == TextEditorBackend("Hello, world", 12)


def test_delete_back_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, 5)
    assert editor.delete_back()
    assert editor == TextEditorBackend("Hell, world!", 4)


def test_delete_forward_cursor_at_start():
    editor = TextEditorBackend(CONTENT)
    assert editor.delete_forward()
    assert editor.content == "ello, world!"


def test_delete_forward_cursor_at_end_is_noop():
    editor = TextEditorBackend(CONTENT)
    assert editor.cursor_text_end()
    assert not editor.delete_forward()
    assert editor == TextEditorBackend(CONTENT, len(CONTENT))


def test_delete_forward_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, 5)
    editor.cursor_index = 5
    assert editor.delete_forward()
    assert editor == TextEditorBackend("Hello world!", 5)


def test_cursor_left_cursor_at_start_is_noop():
    editor = TextEditorBackend(CONTENT)
    assert not editor.cursor_left()
    assert editor == TextEditorBackend(CONTENT)


def test_cursor_left_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, 6)
    assert editor.cursor_left()
    assert editor == TextEditorBackend(CONTENT, 5)


def test_cursor_left_cursor_at_end():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert editor.cursor_left()
    assert editor == TextEditorBackend(CONTENT, len(CONTENT) - 1)


def test_cursor_right_cursor_at_start():
    editor = TextEditorBackend(CONTENT)
    assert editor.cursor_right()
    assert editor == TextEditorBackend(CONTENT, 1)


def test_cursor_right_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, 5)
    assert editor.cursor_right()
    assert editor == TextEditorBackend(CONTENT, 6)


def test_cursor_right_cursor_at_end_is_noop():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    editor.cursor_right()
    assert editor == TextEditorBackend(CONTENT, len(CONTENT))


def test_query_cursor_left_cursor_at_start_returns_false():
    editor = TextEditorBackend(CONTENT)
    assert not editor.query_cursor_left()


def test_query_cursor_left_cursor_at_end_returns_true():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert editor.query_cursor_left()


def test_query_cursor_left_cursor_in_middle_returns_true():
    editor = TextEditorBackend(CONTENT, 6)
    assert editor.query_cursor_left()


def test_query_cursor_right_cursor_at_start_returns_true():
    editor = TextEditorBackend(CONTENT)
    assert editor.query_cursor_right()


def test_query_cursor_right_cursor_in_middle_returns_true():
    editor = TextEditorBackend(CONTENT, 6)
    assert editor.query_cursor_right()


def test_query_cursor_right_cursor_at_end_returns_false():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert not editor.query_cursor_right()

def test_cursor_text_start_cursor_already_at_start():
    editor = TextEditorBackend(CONTENT)
    assert not editor.cursor_text_start()
    assert editor.cursor_index == 0

def test_cursor_text_start_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, 6)
    assert editor.cursor_text_start()
    assert editor.cursor_index == 0

def test_cursor_text_end_cursor_already_at_end():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert not editor.cursor_text_end()
    assert editor.cursor_index == len(CONTENT)

def test_cursor_text_end_cursor_in_middle():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert not editor.cursor_text_end()
    assert editor.cursor_index == len(CONTENT)


def test_insert_at_cursor_cursor_at_start():
    editor = TextEditorBackend(CONTENT)
    assert editor.insert("ABC")
    assert editor.content == "ABC" + CONTENT
    assert editor.cursor_index == len("ABC")

def test_insert_at_cursor_cursor_in_middle():
    start_cursor_index = 6
    editor = TextEditorBackend(CONTENT, start_cursor_index)
    assert editor.insert("ABC")
    assert editor.content == "Hello,ABC world!"
    assert editor.cursor_index == start_cursor_index + len("ABC")


def test_insert_at_cursor_cursor_at_end():
    editor = TextEditorBackend(CONTENT, len(CONTENT))
    assert editor.insert("ABC")
    assert editor.content == CONTENT + "ABC"
    assert editor.cursor_index == len(editor.content)

def test_get_range():
    editor = TextEditorBackend(CONTENT)
    assert editor.get_range(0, 5) == "Hello"