| 1 |
import importlib.util |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from fenrirscreenreader.utils import char_utils |
| 8 |
|
| 9 |
|
| 10 |
COMMANDS_DIR = ( |
| 11 |
Path(__file__).resolve().parents[2] |
| 12 |
/ "fenrirscreenreader" |
| 13 |
/ "commands" |
| 14 |
/ "commands" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def load_command(name): |
| 19 |
spec = importlib.util.spec_from_file_location( |
| 20 |
f"fenrir_{name}", COMMANDS_DIR / f"{name}.py" |
| 21 |
) |
| 22 |
module = importlib.util.module_from_spec(spec) |
| 23 |
spec.loader.exec_module(module) |
| 24 |
return module.command() |
| 25 |
|
| 26 |
|
| 27 |
def build_environment(cursor): |
| 28 |
settings_manager = Mock() |
| 29 |
settings_manager.get_setting_as_bool.return_value = True |
| 30 |
output_manager = Mock() |
| 31 |
cursor_manager = Mock() |
| 32 |
cursor_manager.enter_review_mode_curr_text_cursor.return_value = None |
| 33 |
cursor_manager.get_review_or_text_cursor.return_value = cursor |
| 34 |
|
| 35 |
return { |
| 36 |
"punctuation": {"PUNCTDICT": {" ": "space"}}, |
| 37 |
"screen": { |
| 38 |
"newCursorReview": cursor.copy(), |
| 39 |
"new_cursor": cursor.copy(), |
| 40 |
"new_content_text": "abc\ndef", |
| 41 |
}, |
| 42 |
"runtime": { |
| 43 |
"AttributeManager": Mock(has_attributes=Mock(return_value=False)), |
| 44 |
"CursorManager": cursor_manager, |
| 45 |
"OutputManager": output_manager, |
| 46 |
"SettingsManager": settings_manager, |
| 47 |
"TableManager": Mock(is_table_mode=Mock(return_value=False)), |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def run_command(name, cursor): |
| 53 |
env = build_environment(cursor) |
| 54 |
command = load_command(name) |
| 55 |
command.initialize(env) |
| 56 |
command.run() |
| 57 |
return env["runtime"]["OutputManager"] |
| 58 |
|
| 59 |
|
| 60 |
def boundary_call(output_manager): |
| 61 |
return output_manager.present_text.call_args_list[-1] |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.unit |
| 65 |
def test_previous_line_uses_start_of_screen_sound_at_top(): |
| 66 |
output_manager = run_command("review_prev_line", {"x": 0, "y": 0}) |
| 67 |
|
| 68 |
call = boundary_call(output_manager) |
| 69 |
assert call.args[0] == "start of screen" |
| 70 |
assert call.kwargs["sound_icon"] == "StartOfScreen" |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.unit |
| 74 |
def test_next_line_uses_end_of_screen_sound_at_bottom(): |
| 75 |
output_manager = run_command("review_next_line", {"x": 0, "y": 1}) |
| 76 |
|
| 77 |
call = boundary_call(output_manager) |
| 78 |
assert call.args[0] == "end of screen" |
| 79 |
assert call.kwargs["sound_icon"] == "EndOfScreen" |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.unit |
| 83 |
def test_previous_character_uses_start_of_screen_sound_at_top_left(): |
| 84 |
output_manager = run_command("review_prev_char", {"x": 0, "y": 0}) |
| 85 |
|
| 86 |
call = boundary_call(output_manager) |
| 87 |
assert call.args[0] == "start of screen" |
| 88 |
assert call.kwargs["sound_icon"] == "StartOfScreen" |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.unit |
| 92 |
def test_next_character_uses_end_of_screen_sound_at_bottom_right(): |
| 93 |
output_manager = run_command("review_next_char", {"x": 2, "y": 1}) |
| 94 |
|
| 95 |
call = boundary_call(output_manager) |
| 96 |
assert call.args[0] == "end of screen" |
| 97 |
assert call.kwargs["sound_icon"] == "EndOfScreen" |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.unit |
| 101 |
def test_vertical_character_navigation_reports_boundaries_only_at_edges(): |
| 102 |
assert char_utils.get_up_char(0, 1, "abc\ndef") == ( |
| 103 |
0, |
| 104 |
0, |
| 105 |
"a", |
| 106 |
False, |
| 107 |
) |
| 108 |
assert char_utils.get_up_char(0, 0, "abc\ndef") == ( |
| 109 |
0, |
| 110 |
0, |
| 111 |
"", |
| 112 |
True, |
| 113 |
) |
| 114 |
assert char_utils.get_down_char(0, 0, "abc\ndef") == ( |
| 115 |
0, |
| 116 |
1, |
| 117 |
"d", |
| 118 |
False, |
| 119 |
) |
| 120 |
assert char_utils.get_down_char(0, 1, "abc\ndef") == ( |
| 121 |
0, |
| 122 |
1, |
| 123 |
"", |
| 124 |
True, |
| 125 |
) |