summaryrefslogtreecommitdiffstats
path: root/tests/test_document.py
blob: d052d537ce098eb2034737402e8aa1b7fb5ed80f (plain)
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
from __future__ import annotations

import pytest

from prompt_toolkit.document import Document


@pytest.fixture
def document():
    return Document(
        "line 1\n" + "line 2\n" + "line 3\n" + "line 4\n", len("line 1\n" + "lin")
    )


def test_current_char(document):
    assert document.current_char == "e"
    assert document.char_before_cursor == "n"


def test_text_before_cursor(document):
    assert document.text_before_cursor == "line 1\nlin"


def test_text_after_cursor(document):
    assert document.text_after_cursor == "e 2\n" + "line 3\n" + "line 4\n"


def test_lines(document):
    assert document.lines == ["line 1", "line 2", "line 3", "line 4", ""]


def test_line_count(document):
    assert document.line_count == 5


def test_current_line_before_cursor(document):
    assert document.current_line_before_cursor == "lin"


def test_current_line_after_cursor(document):
    assert document.current_line_after_cursor == "e 2"


def test_current_line(document):
    assert document.current_line == "line 2"


def test_cursor_position(document):
    assert document.cursor_position_row == 1
    assert document.cursor_position_col == 3

    d = Document("", 0)
    assert d.cursor_position_row == 0
    assert d.cursor_position_col == 0


def test_translate_index_to_position(document):
    pos = document.translate_index_to_position(len("line 1\nline 2\nlin"))

    assert pos[0] == 2
    assert pos[1] == 3

    pos = document.translate_index_to_position(0)
    assert pos == (0, 0)


def test_is_cursor_at_the_end(document):
    assert Document("hello", 5).is_cursor_at_the_end
    assert not Document("hello", 4).is_cursor_at_the_end