Files
MDHelper/tests/test_interaction_revision_056.py
T

115 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Conflicts during transactions, cursor-based search and empty style regions."""
import pytest
from PySide6.QtCore import QSettings, Qt
from PySide6.QtGui import QTextCursor
from mdhelper import batch_documents as batch, rename_references as rename, main_window
from mdhelper.markdown_editor import MarkdownEditorDialog
from mdhelper.markdown_renderer import MarkdownRenderer
@pytest.mark.parametrize('changed', ['next', 'rollback', 'created'])
def test_batch_preserves_external_changes_between_writes(tmp_path, monkeypatch, changed):
first, second = tmp_path / 'first.out', tmp_path / 'second.out'
staged = tmp_path / 'stage'; staged.write_bytes(b'exported')
first.write_bytes(b'original')
if changed != 'created': second.write_bytes(b'original')
expected = {p: batch.fingerprint(p) for p in (first, second)}
write = batch.atomic_write
def concurrent_write(path, data):
write(path, data)
if path == first and data == b'exported':
second.write_bytes(b'external second')
if changed == 'rollback': first.write_bytes(b'external first')
monkeypatch.setattr(batch, 'atomic_write', concurrent_write)
with pytest.raises(OSError, match='输出恢复失败' if changed == 'rollback' else '发生变化'):
batch.publish_outputs({first: staged, second: staged}, expected, tmp_path)
assert second.read_bytes() == b'external second'
assert first.read_bytes() == (b'external first' if changed == 'rollback' else b'original')
assert (tmp_path / 'previous-0').read_bytes() == b'original'
@pytest.mark.parametrize('when', ['backup', 'write', 'rollback'])
def test_rename_preserves_external_edits_during_transaction(tmp_path, monkeypatch, when):
source, target, ref = (tmp_path / name for name in ('old.md', 'new.md', 'ref.md'))
source.write_text('[self](old.md)', encoding='utf8')
ref.write_text('[ref](old.md)', encoding='utf8')
plan = rename.plan_rename(tmp_path, source, target)
write = rename.atomic_write
def concurrent_write(path, data):
write(path, data)
if (when == 'backup' and path.name == 'manifest.json') or (when != 'backup' and path == target):
ref.write_bytes(b'external ref')
if when == 'rollback': target.write_bytes(b'external source')
monkeypatch.setattr(rename, 'atomic_write', concurrent_write)
with pytest.raises(OSError, match='恢复未完全成功' if when == 'rollback' else '已变化'):
rename.execute_rename(plan, tmp_path / 'backups')
assert ref.read_bytes() == b'external ref'
assert not target.exists()
assert source.read_bytes() == (b'external source' if when == 'rollback' else plan.originals[source])
@pytest.fixture
def editor(qt_application, tmp_path):
dialog = MarkdownEditorDialog(tmp_path / 'note.md', '', MarkdownRenderer(tmp_path), 'light', 1, '', 16)
yield dialog
dialog.mark_saved(tmp_path / 'note.md', dialog.source_text(), None)
dialog.close(); dialog.deleteLater()
def select(editor, start, end=None):
cursor = editor.textCursor(); cursor.setPosition(start)
if end is not None: cursor.setPosition(end, QTextCursor.MoveMode.KeepAnchor)
editor.setTextCursor(cursor)
def test_whole_word_replace_rejects_manually_selected_substring(editor):
editor.editor.setPlainText('catalog cat')
editor.editor_find_box.setText('cat'); editor.whole_word_checkbox.setChecked(True)
editor.editor_replace_box.setText('dog'); select(editor.editor, 0, 3)
editor.editor_replace_button.click()
assert editor.source_text() == 'catalog cat'
assert editor.editor.textCursor().selectionStart() == 8
editor.editor_replace_button.click()
assert editor.source_text() == 'catalog dog'
editor.editor.undo(); assert editor.source_text() == 'catalog cat'
def test_previous_wraps_and_follows_manual_cursor_changes(editor):
editor.editor.setPlainText('cat cat cat'); select(editor.editor, 0)
editor.editor_find_box.setText('cat'); editor.editor_find_previous_button.click()
assert editor.editor.textCursor().selectionStart() == 8
assert editor.find_status_label.text() == '3/3'
editor.editor_find_previous_button.click(); assert editor.editor.textCursor().selectionStart() == 4
editor.editor_find_next_button.click(); assert editor.editor.textCursor().selectionStart() == 8
select(editor.editor, 4); editor.editor_find_previous_button.click()
assert editor.editor.textCursor().selectionStart() == 0
select(editor.editor, 4); editor.editor_find_next_button.click()
assert editor.editor.textCursor().selectionStart() == 4
def test_replace_uses_same_unicode_case_matching_as_find(editor):
editor.editor.setPlainText('🙂 ſ cat')
editor.editor_find_box.setText('s'); editor.editor_replace_box.setText('X')
editor.editor_find_next_button.click(); editor.editor_replace_button.click()
assert editor.source_text() == '🙂 X cat'
def test_style_panel_clears_highlight_on_blank_editor_line(qt_application, tmp_path, monkeypatch):
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
note = tmp_path / 'note.md'; note.write_text('# Heading\n\nBody', encoding='utf8')
window = main_window.MainWindow(tmp_path)
try:
window.open_document(note); window.edit_current_markdown(); window.show_style_panel()
panel = window.style_panel; panel.refresh(force=True)
assert '标题 1' in panel.current.text()
select(window.tabs.currentWidget().editor, 10); panel.refresh()
assert '当前行' in panel.current.text() and '阅读模式' not in panel.current.text()
assert all(panel.listing.item(i).background().color().alpha() == 0 for i in range(panel.listing.count()))
select(window.tabs.currentWidget().editor, 11); panel.refresh()
assert '正文' in panel.current.text()
active = [panel.listing.item(i).data(Qt.ItemDataRole.UserRole) for i in range(panel.listing.count())
if panel.listing.item(i).background().color().alpha()]
assert active == ['paragraph']
finally: window.close()