File: test_languages.py

package info (click to toggle)
textual 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,056 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (95 lines) | stat: -rw-r--r-- 2,801 bytes parent folder | download
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
import pytest

from textual.app import App, ComposeResult
from textual.widgets import TextArea
from textual.widgets.text_area import LanguageDoesNotExist


class TextAreaApp(App):
    def compose(self) -> ComposeResult:
        yield TextArea("print('hello')", language="python")


async def test_setting_builtin_language_via_constructor():
    class MyTextAreaApp(App):
        def compose(self) -> ComposeResult:
            yield TextArea("print('hello')", language="python")

    app = MyTextAreaApp()

    async with app.run_test():
        text_area = app.query_one(TextArea)
        assert text_area.language == "python"

        text_area.language = "markdown"
        assert text_area.language == "markdown"


async def test_setting_builtin_language_via_attribute():
    class MyTextAreaApp(App):
        def compose(self) -> ComposeResult:
            text_area = TextArea("print('hello')")
            text_area.language = "python"
            yield text_area

    app = MyTextAreaApp()

    async with app.run_test():
        text_area = app.query_one(TextArea)
        assert text_area.language == "python"

        text_area.language = "markdown"
        assert text_area.language == "markdown"


async def test_setting_language_to_none():
    app = TextAreaApp()
    async with app.run_test():
        text_area = app.query_one(TextArea)
        text_area.language = None
        assert text_area.language is None


@pytest.mark.syntax
async def test_setting_unknown_language():
    app = TextAreaApp()
    async with app.run_test():
        text_area = app.query_one(TextArea)

        with pytest.raises(LanguageDoesNotExist):
            text_area.language = "this-language-doesnt-exist"


@pytest.mark.syntax
async def test_register_language():
    app = TextAreaApp()

    async with app.run_test():
        text_area = app.query_one(TextArea)

        from textual._tree_sitter import get_language

        language = get_language("python")
        assert language is not None
        # ...and register it with no highlights
        text_area.register_language("python-test", language, "")

        assert "python-test" in text_area.available_languages
        text_area.language = "python-test"
        assert text_area.language == "python-test"


@pytest.mark.syntax
async def test_update_highlight_query():
    app = TextAreaApp()
    async with app.run_test():
        text_area = app.query_one(TextArea)

        # Before registering the language, we have highlights as expected.
        assert len(text_area._highlights) > 0

        # Overwriting the highlight query for Python...
        text_area.update_highlight_query("python", "")

        # We've overridden the highlight query with a blank one, so there are no highlights.
        assert text_area._highlights == {}