From faa9c76ba8ada274e56c5863a823f5baa017e409 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Thu, 14 Sep 2023 01:50:37 -0400 Subject: [PATCH] Test file drag-and-drop handling --- pyproject.toml | 3 +++ requirements.txt | 1 + tests/test_behavior.py | 41 +++++++++++++++++++++++++++++++++++++++++ tests/test_encoding.py | 5 ++++- tests/test_snapshots.py | 10 +++++++--- 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/test_behavior.py diff --git a/pyproject.toml b/pyproject.toml index fed528d..b829baf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,6 @@ [build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" + +[tool.pytest.ini_options] +asyncio_mode = "auto" diff --git a/requirements.txt b/requirements.txt index 6af68b5..0e1cc3d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,6 @@ watchdog==3.0.0 # for development; optional (only used when --restart-on-changes types-Pillow==10.0.0.1 # for development types-psutil==5.9.5.15 # for development pytest==7.4.1 # for development +pytest-asyncio==0.21.1 # for development pytest-textual-snapshot==0.4.0 # for development pyfakefs==5.2.4 # for development diff --git a/tests/test_behavior.py b/tests/test_behavior.py new file mode 100644 index 0000000..1947582 --- /dev/null +++ b/tests/test_behavior.py @@ -0,0 +1,41 @@ +"""General behavioral/functional tests. + +Run with `pytest tests/test_behavior.py`, or `pytest` to run all tests. +""" + +from pyfakefs.fake_filesystem import FakeFilesystem +from textual.events import Paste + +from textual_paint.char_input import CharInput +from textual_paint.paint import PaintApp + + +async def test_char_input_paste(): + app = PaintApp() + async with app.run_test() as pilot: # type: ignore + char_input = app.query_one(CharInput) + char_input.post_message(Paste("Hello, world!")) + await pilot.pause() + assert char_input.value == "!" + +async def test_file_drag_and_drop(my_fs: FakeFilesystem): + # File drag and drop is treated as a paste in the terminal. + # CharInput often has focus, and needs to propagate the event to the app. + my_fs.create_file("/test_file_to_load.txt", contents="Hello, world!") + app = PaintApp() + async with app.run_test() as pilot: # type: ignore + char_input = app.query_one(CharInput) + char_input.post_message(Paste("/test_file_to_load.txt")) + await pilot.pause() + # TODO: fix double handling of Paste event + # It should ONLY load the file in this case, not also paste the filename into the char input. + # assert char_input.value == " " # default, may become full block (█) in the future + assert app.query_one("Canvas").render_line(0).text == "Hello, world!" + # TODO: bring palette state into the app class, + # and remove this KLUDGE of resetting the palette. + # Without this, the palette becomes black and white when loading a txt file, + # and it affects all snapshot tests! Pretty stupid. + import textual_paint.paint + from textual_paint.palette_data import DEFAULT_PALETTE + textual_paint.paint.palette = DEFAULT_PALETTE + diff --git a/tests/test_encoding.py b/tests/test_encoding.py index cb047af..e444e4a 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -1,4 +1,7 @@ -"""Test that files are encoded correctly.""" +"""Test that files are encoded correctly. + +Run with `pytest tests/test_encoding.py`, or `pytest` to run all tests. +""" from pathlib import Path import pytest diff --git a/tests/test_snapshots.py b/tests/test_snapshots.py index d244b3d..d6f778f 100644 --- a/tests/test_snapshots.py +++ b/tests/test_snapshots.py @@ -1,9 +1,13 @@ -"""Visual regression tests, using pytest-textual-snapshot. Run with `pytest`.""" +"""Visual regression tests, using pytest-textual-snapshot. + +Run with `pytest tests/test_snapshots.py`, or `pytest` to run all tests. +""" from pathlib import Path, PurePath from typing import TYPE_CHECKING, Awaitable, Callable, Iterable, Protocol import pytest +from pyfakefs.fake_filesystem import FakeFilesystem from textual.geometry import Offset from textual.pilot import Pilot from textual.widgets import Input @@ -53,10 +57,10 @@ def test_paint_flip_rotate_dialog(snap_compare: SnapCompareType, each_theme: Non def test_paint_image_attributes_dialog(snap_compare: SnapCompareType, each_theme: None): assert snap_compare(PAINT, press=["ctrl+e"]) -def test_paint_open_dialog(snap_compare: SnapCompareType, each_theme: None, my_fs: None): +def test_paint_open_dialog(snap_compare: SnapCompareType, each_theme: None, my_fs: FakeFilesystem): assert snap_compare(PAINT, press=["ctrl+o"], terminal_size=LARGER) -def test_paint_save_dialog(snap_compare: SnapCompareType, each_theme: None, my_fs: None): +def test_paint_save_dialog(snap_compare: SnapCompareType, each_theme: None, my_fs: FakeFilesystem): assert snap_compare(PAINT, press=["ctrl+s"], terminal_size=LARGER) def test_paint_help_dialog(snap_compare: SnapCompareType, each_theme: None):