Code cleanup, more tests

This commit is contained in:
Miguel Jacq 2025-11-11 13:12:30 +11:00
parent 1c0052a0cf
commit bfd0314109
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
16 changed files with 1212 additions and 478 deletions

View file

@ -1,6 +1,7 @@
import pytest
from PySide6.QtGui import QImage, QColor
from PySide6.QtCore import Qt, QPoint
from PySide6.QtGui import QImage, QColor, QKeyEvent, QTextCursor
from bouquin.markdown_editor import MarkdownEditor
from bouquin.theme import ThemeManager, ThemeConfig, Theme
@ -61,3 +62,84 @@ def test_apply_code_inline(editor):
editor.apply_code()
md = editor.to_markdown()
assert ("`" in md) or ("```" in md)
@pytest.mark.gui
def test_auto_close_code_fence(editor, qtbot):
# Place caret at start and type exactly `` then ` to trigger expansion
editor.setPlainText("")
qtbot.keyClicks(editor, "``")
qtbot.keyClicks(editor, "`") # third backtick triggers fence insertion
txt = editor.toPlainText()
assert "```" in txt and txt.count("```") >= 2
@pytest.mark.gui
def test_checkbox_toggle_by_click(editor, qtbot):
# Load a markdown checkbox
editor.from_markdown("- [ ] task here")
# Verify display token present
display = editor.toPlainText()
assert "" in display
# Click on the first character region to toggle
c = editor.textCursor()
from PySide6.QtGui import QTextCursor
c.movePosition(QTextCursor.StartOfBlock)
editor.setTextCursor(c)
r = editor.cursorRect()
center = r.center()
# Send click slightly right to land within checkbox icon region
pos = QPoint(r.left() + 2, center.y())
qtbot.mouseClick(editor.viewport(), Qt.LeftButton, pos=pos)
# Should have toggled to checked icon
display2 = editor.toPlainText()
assert "" in display2
@pytest.mark.gui
def test_apply_heading_levels(editor, qtbot):
editor.setPlainText("hello")
editor.selectAll()
# H2
editor.apply_heading(18)
assert editor.toPlainText().startswith("## ")
# H3
editor.selectAll()
editor.apply_heading(14)
assert editor.toPlainText().startswith("### ")
# Normal (no heading)
editor.selectAll()
editor.apply_heading(12)
assert not editor.toPlainText().startswith("#")
@pytest.mark.gui
def test_enter_on_nonempty_list_continues(qtbot, editor):
qtbot.addWidget(editor)
editor.show()
editor.from_markdown("- item")
c = editor.textCursor()
c.movePosition(QTextCursor.End)
editor.setTextCursor(c)
ev = QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Return, Qt.NoModifier, "\n")
editor.keyPressEvent(ev)
txt = editor.toPlainText()
assert "\n- " in txt
@pytest.mark.gui
def test_enter_on_empty_list_marks_empty(qtbot, editor):
qtbot.addWidget(editor)
editor.show()
editor.from_markdown("- ")
c = editor.textCursor()
c.movePosition(QTextCursor.End)
editor.setTextCursor(c)
ev = QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Return, Qt.NoModifier, "\n")
editor.keyPressEvent(ev)
assert editor.toPlainText().startswith("- \n")