Debian Codegraph @

tests/test_format_utils.py

raw
1 """Tests for formiko.format_utils.compute_toggle_format."""
2
3 from typing import ClassVar
4
5 import pytest
6
7 from formiko.format_utils import (
8 RST_HEADER_CHARS,
9 compute_toggle_format,
10 compute_toggle_line_exclusive,
11 compute_toggle_line_format,
12 compute_toggle_ordered,
13 compute_toggle_rst_header,
14 )
15
16 # ---------------------------------------------------------------------------
17 # Helpers
18 # ---------------------------------------------------------------------------
19
20 # Per-parser known-formats lists (mirrors window.py _KNOWN_FORMATS)
21 RST_FORMATS = [
22 (":del:`", "`"),
23 ("**", "**"),
24 ("``", "``"),
25 ("*", "*"),
26 ]
27 MD_FORMATS = [
28 ("**", "**"),
29 ("~~", "~~"),
30 ("*", "*"),
31 ("`", "`"),
32 ]
33 HTML_FORMATS = [
34 ("<code>", "</code>"),
35 ("<b>", "</b>"),
36 ("<i>", "</i>"),
37 ("<s>", "</s>"),
38 ]
39
40 BOLD_MD = ("**", "**")
41 ITALIC_MD = ("*", "*")
42 STRIKE_MD = ("~~", "~~")
43 CODE_MD = ("`", "`")
44
45 BOLD_RST = ("**", "**")
46 ITALIC_RST = ("*", "*")
47 STRIKE_RST = (":del:`", "`")
48 CODE_RST = ("``", "``")
49
50 BOLD_HTML = ("<b>", "</b>")
51 ITALIC_HTML = ("<i>", "</i>")
52 STRIKE_HTML = ("<s>", "</s>")
53 CODE_HTML = ("<code>", "</code>")
54
55
56 def toggle(buf, sel_start, sel_end, before, after, known):
57 """Shortcut: return (result_buf, new_sel_start, new_sel_end)."""
58 eff_start, eff_end, result, inner_start, inner_end = compute_toggle_format(
59 buf,
60 sel_start,
61 sel_end,
62 before,
63 after,
64 known,
65 )
66 new_buf = buf[:eff_start] + result + buf[eff_end:]
67 new_sel_start = eff_start + inner_start
68 new_sel_end = eff_start + inner_end
69 return new_buf, new_sel_start, new_sel_end
70
71
72 # ---------------------------------------------------------------------------
73 # Markdown / RST — markers INSIDE the selection
74 # ---------------------------------------------------------------------------
75
76
77 class TestMarkdownInsideSelection:
78 """Selection includes the formatting markers."""
79
80 def test_no_format_apply_bold(self):
81 buf = "hello text world"
82 # ^---^ sel: "text"
83 start, end = 6, 10
84 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
85 assert new_buf == "hello **text** world"
86 assert new_buf[ss:se] == "text"
87
88 def test_no_format_apply_italic(self):
89 buf = "hello text world"
90 start, end = 6, 10
91 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
92 assert new_buf == "hello *text* world"
93 assert new_buf[ss:se] == "text"
94
95 def test_bold_inside_sel_toggle_off(self):
96 buf = "hello **text** world"
97 # ^-------^ sel: "**text**"
98 start, end = 6, 14
99 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
100 assert new_buf == "hello text world"
101 assert new_buf[ss:se] == "text"
102
103 def test_bold_inside_sel_switch_to_italic(self):
104 buf = "hello **text** world"
105 start, end = 6, 14
106 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
107 assert new_buf == "hello *text* world"
108 assert new_buf[ss:se] == "text"
109
110 def test_italic_inside_sel_toggle_off(self):
111 buf = "hello *text* world"
112 # ^------^ sel: "*text*"
113 start, end = 6, 12
114 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
115 assert new_buf == "hello text world"
116 assert new_buf[ss:se] == "text"
117
118 def test_italic_inside_sel_switch_to_bold(self):
119 buf = "hello *text* world"
120 start, end = 6, 12
121 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
122 assert new_buf == "hello **text** world"
123 assert new_buf[ss:se] == "text"
124
125
126 # ---------------------------------------------------------------------------
127 # Markdown / RST — markers OUTSIDE the selection
128 # ---------------------------------------------------------------------------
129
130
131 class TestMarkdownOutsideSelection:
132 """User selected only the inner text; markers sit just outside."""
133
134 def test_bold_outside_sel_toggle_off(self):
135 buf = "hello **text** world"
136 # ^--^ sel: "text" (offsets 8..12)
137 start, end = 8, 12
138 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
139 assert new_buf == "hello text world"
140 assert new_buf[ss:se] == "text"
141
142 def test_bold_outside_sel_switch_to_italic(self):
143 buf = "hello **text** world"
144 start, end = 8, 12
145 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
146 assert new_buf == "hello *text* world"
147 assert new_buf[ss:se] == "text"
148
149 def test_italic_outside_sel_toggle_off(self):
150 buf = "hello *text* world"
151 # ^--^ sel: "text" (offsets 7..11)
152 start, end = 7, 11
153 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
154 assert new_buf == "hello text world"
155 assert new_buf[ss:se] == "text"
156
157 def test_italic_outside_sel_switch_to_bold(self):
158 buf = "hello *text* world"
159 start, end = 7, 11
160 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
161 assert new_buf == "hello **text** world"
162 assert new_buf[ss:se] == "text"
163
164
165 # ---------------------------------------------------------------------------
166 # HTML — markers INSIDE the selection
167 # ---------------------------------------------------------------------------
168
169
170 class TestHtmlInsideSelection:
171 def test_no_format_apply_bold(self):
172 buf = "hello text world"
173 start, end = 6, 10
174 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
175 assert new_buf == "hello <b>text</b> world"
176 assert new_buf[ss:se] == "text"
177
178 def test_no_format_apply_italic(self):
179 buf = "hello text world"
180 start, end = 6, 10
181 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
182 assert new_buf == "hello <i>text</i> world"
183 assert new_buf[ss:se] == "text"
184
185 def test_bold_inside_sel_toggle_off(self):
186 buf = "hello <b>text</b> world"
187 # ^----------^ sel: "<b>text</b>" (6..17)
188 start, end = 6, 17
189 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
190 assert new_buf == "hello text world"
191 assert new_buf[ss:se] == "text"
192
193 def test_bold_inside_sel_switch_to_italic(self):
194 buf = "hello <b>text</b> world"
195 start, end = 6, 17
196 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
197 assert new_buf == "hello <i>text</i> world"
198 assert new_buf[ss:se] == "text"
199
200 def test_italic_inside_sel_toggle_off(self):
201 buf = "hello <i>text</i> world"
202 # ^----------^ sel: "<i>text</i>" (6..17)
203 start, end = 6, 17
204 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
205 assert new_buf == "hello text world"
206 assert new_buf[ss:se] == "text"
207
208 def test_italic_inside_sel_switch_to_bold(self):
209 buf = "hello <i>text</i> world"
210 start, end = 6, 17
211 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
212 assert new_buf == "hello <b>text</b> world"
213 assert new_buf[ss:se] == "text"
214
215
216 # ---------------------------------------------------------------------------
217 # HTML — markers OUTSIDE the selection
218 # ---------------------------------------------------------------------------
219
220
221 class TestHtmlOutsideSelection:
222 def test_bold_outside_sel_toggle_off(self):
223 buf = "hello <b>text</b> world"
224 # ^--^ sel: "text" (9..13)
225 start, end = 9, 13
226 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
227 assert new_buf == "hello text world"
228 assert new_buf[ss:se] == "text"
229
230 def test_bold_outside_sel_switch_to_italic(self):
231 buf = "hello <b>text</b> world"
232 start, end = 9, 13
233 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
234 assert new_buf == "hello <i>text</i> world"
235 assert new_buf[ss:se] == "text"
236
237 def test_italic_outside_sel_toggle_off(self):
238 buf = "hello <i>text</i> world"
239 # ^--^ sel: "text" (9..13)
240 start, end = 9, 13
241 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
242 assert new_buf == "hello text world"
243 assert new_buf[ss:se] == "text"
244
245 def test_italic_outside_sel_switch_to_bold(self):
246 buf = "hello <i>text</i> world"
247 start, end = 9, 13
248 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
249 assert new_buf == "hello <b>text</b> world"
250 assert new_buf[ss:se] == "text"
251
252
253 # ---------------------------------------------------------------------------
254 # Selection preservation — inner_start / inner_end offsets
255 # ---------------------------------------------------------------------------
256
257
258 class TestSelectionPreservation:
259 """After the operation the selection must cover only the inner text."""
260
261 @pytest.mark.parametrize(
262 ("apply_bold", "apply_italic"),
263 [
264 (True, False),
265 (False, True),
266 ],
267 )
268 def test_apply_format_sel_covers_inner(self, apply_bold, apply_italic):
269 buf = "hello text world"
270 start, end = 6, 10 # "text"
271 fmt = BOLD_MD if apply_bold else ITALIC_MD
272 new_buf, ss, se = toggle(buf, start, end, *fmt, MD_FORMATS)
273 assert new_buf[ss:se] == "text"
274
275 def test_toggle_off_sel_covers_inner(self):
276 buf = "**text**"
277 start, end = 0, 8
278 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
279 assert new_buf == "text"
280 assert new_buf[ss:se] == "text"
281
282 def test_switch_sel_covers_inner(self):
283 buf = "**text**"
284 start, end = 0, 8
285 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
286 assert new_buf == "*text*"
287 assert new_buf[ss:se] == "text"
288
289 def test_outer_markers_sel_covers_inner(self):
290 buf = "**text**"
291 # Select only "text" (inside the markers)
292 start, end = 2, 6
293 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
294 assert new_buf == "text"
295 assert new_buf[ss:se] == "text"
296
297
298 # ---------------------------------------------------------------------------
299 # Edge cases
300 # ---------------------------------------------------------------------------
301
302
303 class TestEdgeCases:
304 def test_no_adjacent_markers_no_change_on_toggle_off(self):
305 """Plain text with no markers — toggle off is impossible, applies."""
306 buf = "text"
307 start, end = 0, 4
308 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
309 assert new_buf == "**text**"
310 assert new_buf[ss:se] == "text"
311
312 def test_at_start_of_buffer(self):
313 buf = "text rest"
314 start, end = 0, 4
315 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
316 assert new_buf == "**text** rest"
317 assert new_buf[ss:se] == "text"
318
319 def test_at_end_of_buffer(self):
320 buf = "hello text"
321 start, end = 6, 10
322 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
323 assert new_buf == "hello **text**"
324 assert new_buf[ss:se] == "text"
325
326 def test_whole_buffer_bold(self):
327 buf = "text"
328 start, end = 0, 4
329 new_buf, _, _ = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
330 assert new_buf == "**text**"
331
332 def test_empty_inner_text(self):
333 """Toggling off bold on '****' yields empty string."""
334 buf = "****"
335 start, end = 0, 4
336 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
337 assert new_buf == ""
338 assert ss == se # empty selection
339
340 def test_markers_not_adjacent_no_expand(self):
341 """Markers elsewhere in buffer must not affect selection."""
342 buf = "**other** text **more**"
343 # ^---^ "text" at 10..14
344 start, end = 10, 14
345 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
346 assert new_buf == "**other** **text** **more**"
347 assert new_buf[ss:se] == "text"
348
349 def test_multiword_selection(self):
350 buf = "hello world foo"
351 start, end = 0, 11 # "hello world"
352 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
353 assert new_buf == "*hello world* foo"
354 assert new_buf[ss:se] == "hello world"
355
356
357 # ---------------------------------------------------------------------------
358 # Markdown — strikethrough and inline code
359 # ---------------------------------------------------------------------------
360
361
362 class TestMarkdownStrikethrough:
363 """Strikethrough (~~) for markdown."""
364
365 def test_no_format_apply_strikethrough(self):
366 buf = "hello text world"
367 start, end = 6, 10
368 new_buf, ss, se = toggle(buf, start, end, *STRIKE_MD, MD_FORMATS)
369 assert new_buf == "hello ~~text~~ world"
370 assert new_buf[ss:se] == "text"
371
372 def test_strikethrough_inside_sel_toggle_off(self):
373 buf = "hello ~~text~~ world"
374 start, end = 6, 14
375 new_buf, ss, se = toggle(buf, start, end, *STRIKE_MD, MD_FORMATS)
376 assert new_buf == "hello text world"
377 assert new_buf[ss:se] == "text"
378
379 def test_strikethrough_inside_sel_switch_to_bold(self):
380 buf = "hello ~~text~~ world"
381 start, end = 6, 14
382 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
383 assert new_buf == "hello **text** world"
384 assert new_buf[ss:se] == "text"
385
386 def test_strikethrough_outside_sel_toggle_off(self):
387 buf = "hello ~~text~~ world"
388 start, end = 8, 12 # "text" inside ~~
389 new_buf, ss, se = toggle(buf, start, end, *STRIKE_MD, MD_FORMATS)
390 assert new_buf == "hello text world"
391 assert new_buf[ss:se] == "text"
392
393 def test_strikethrough_outside_sel_switch_to_italic(self):
394 buf = "hello ~~text~~ world"
395 start, end = 8, 12
396 new_buf, ss, se = toggle(buf, start, end, *ITALIC_MD, MD_FORMATS)
397 assert new_buf == "hello *text* world"
398 assert new_buf[ss:se] == "text"
399
400 def test_bold_switch_to_strikethrough(self):
401 buf = "hello **text** world"
402 start, end = 6, 14
403 new_buf, ss, se = toggle(buf, start, end, *STRIKE_MD, MD_FORMATS)
404 assert new_buf == "hello ~~text~~ world"
405 assert new_buf[ss:se] == "text"
406
407
408 class TestMarkdownCode:
409 """Inline code (`) for markdown."""
410
411 def test_no_format_apply_code(self):
412 buf = "hello text world"
413 start, end = 6, 10
414 new_buf, ss, se = toggle(buf, start, end, *CODE_MD, MD_FORMATS)
415 assert new_buf == "hello `text` world"
416 assert new_buf[ss:se] == "text"
417
418 def test_code_inside_sel_toggle_off(self):
419 buf = "hello `text` world"
420 start, end = 6, 12
421 new_buf, ss, se = toggle(buf, start, end, *CODE_MD, MD_FORMATS)
422 assert new_buf == "hello text world"
423 assert new_buf[ss:se] == "text"
424
425 def test_code_inside_sel_switch_to_bold(self):
426 buf = "hello `text` world"
427 start, end = 6, 12
428 new_buf, ss, se = toggle(buf, start, end, *BOLD_MD, MD_FORMATS)
429 assert new_buf == "hello **text** world"
430 assert new_buf[ss:se] == "text"
431
432 def test_code_outside_sel_toggle_off(self):
433 buf = "hello `text` world"
434 start, end = 7, 11 # "text" inside `
435 new_buf, ss, se = toggle(buf, start, end, *CODE_MD, MD_FORMATS)
436 assert new_buf == "hello text world"
437 assert new_buf[ss:se] == "text"
438
439 def test_italic_switch_to_code(self):
440 buf = "hello *text* world"
441 start, end = 6, 12
442 new_buf, ss, se = toggle(buf, start, end, *CODE_MD, MD_FORMATS)
443 assert new_buf == "hello `text` world"
444 assert new_buf[ss:se] == "text"
445
446
447 # ---------------------------------------------------------------------------
448 # RST — all four formats (including asymmetric strikethrough)
449 # ---------------------------------------------------------------------------
450
451
452 class TestRstFormats:
453 """RST-specific marker combinations."""
454
455 def test_no_format_apply_bold(self):
456 buf = "hello text world"
457 start, end = 6, 10
458 new_buf, ss, se = toggle(buf, start, end, *BOLD_RST, RST_FORMATS)
459 assert new_buf == "hello **text** world"
460 assert new_buf[ss:se] == "text"
461
462 def test_no_format_apply_italic(self):
463 buf = "hello text world"
464 start, end = 6, 10
465 new_buf, ss, se = toggle(buf, start, end, *ITALIC_RST, RST_FORMATS)
466 assert new_buf == "hello *text* world"
467 assert new_buf[ss:se] == "text"
468
469 def test_no_format_apply_code(self):
470 buf = "hello text world"
471 start, end = 6, 10
472 new_buf, ss, se = toggle(buf, start, end, *CODE_RST, RST_FORMATS)
473 assert new_buf == "hello ``text`` world"
474 assert new_buf[ss:se] == "text"
475
476 def test_no_format_apply_strikethrough(self):
477 buf = "hello text world"
478 start, end = 6, 10
479 new_buf, ss, se = toggle(buf, start, end, *STRIKE_RST, RST_FORMATS)
480 assert new_buf == "hello :del:`text` world"
481 assert new_buf[ss:se] == "text"
482
483 def test_strikethrough_inside_sel_toggle_off(self):
484 buf = "hello :del:`text` world"
485 # ":del:`text`" is at 6..17
486 start, end = 6, 17
487 new_buf, ss, se = toggle(buf, start, end, *STRIKE_RST, RST_FORMATS)
488 assert new_buf == "hello text world"
489 assert new_buf[ss:se] == "text"
490
491 def test_strikethrough_inside_sel_switch_to_bold(self):
492 buf = "hello :del:`text` world"
493 start, end = 6, 17
494 new_buf, ss, se = toggle(buf, start, end, *BOLD_RST, RST_FORMATS)
495 assert new_buf == "hello **text** world"
496 assert new_buf[ss:se] == "text"
497
498 def test_strikethrough_outside_sel_toggle_off(self):
499 """Select only 'text' with :del:` outside."""
500 buf = "hello :del:`text` world"
501 # ":del:`" ends at offset 12, "`" starts at offset 16
502 start, end = 12, 16
503 new_buf, ss, se = toggle(buf, start, end, *STRIKE_RST, RST_FORMATS)
504 assert new_buf == "hello text world"
505 assert new_buf[ss:se] == "text"
506
507 def test_strikethrough_outside_sel_switch_to_italic(self):
508 buf = "hello :del:`text` world"
509 start, end = 12, 16
510 new_buf, ss, se = toggle(buf, start, end, *ITALIC_RST, RST_FORMATS)
511 assert new_buf == "hello *text* world"
512 assert new_buf[ss:se] == "text"
513
514 def test_code_inside_sel_toggle_off(self):
515 buf = "hello ``text`` world"
516 start, end = 6, 14
517 new_buf, ss, se = toggle(buf, start, end, *CODE_RST, RST_FORMATS)
518 assert new_buf == "hello text world"
519 assert new_buf[ss:se] == "text"
520
521 def test_code_outside_sel_switch_to_bold(self):
522 buf = "hello ``text`` world"
523 start, end = 8, 12 # "text" inside ``
524 new_buf, ss, se = toggle(buf, start, end, *BOLD_RST, RST_FORMATS)
525 assert new_buf == "hello **text** world"
526 assert new_buf[ss:se] == "text"
527
528 def test_bold_outside_sel_switch_to_code(self):
529 buf = "hello **text** world"
530 start, end = 8, 12 # "text" inside **
531 new_buf, ss, se = toggle(buf, start, end, *CODE_RST, RST_FORMATS)
532 assert new_buf == "hello ``text`` world"
533 assert new_buf[ss:se] == "text"
534
535
536 # ---------------------------------------------------------------------------
537 # HTML — strikethrough and code
538 # ---------------------------------------------------------------------------
539
540
541 class TestHtmlStrikethrough:
542 def test_no_format_apply_strikethrough(self):
543 buf = "hello text world"
544 start, end = 6, 10
545 new_buf, ss, se = toggle(buf, start, end, *STRIKE_HTML, HTML_FORMATS)
546 assert new_buf == "hello <s>text</s> world"
547 assert new_buf[ss:se] == "text"
548
549 def test_strikethrough_inside_sel_toggle_off(self):
550 buf = "hello <s>text</s> world"
551 start, end = 6, 17
552 new_buf, ss, se = toggle(buf, start, end, *STRIKE_HTML, HTML_FORMATS)
553 assert new_buf == "hello text world"
554 assert new_buf[ss:se] == "text"
555
556 def test_strikethrough_inside_sel_switch_to_bold(self):
557 buf = "hello <s>text</s> world"
558 start, end = 6, 17
559 new_buf, ss, se = toggle(buf, start, end, *BOLD_HTML, HTML_FORMATS)
560 assert new_buf == "hello <b>text</b> world"
561 assert new_buf[ss:se] == "text"
562
563 def test_strikethrough_outside_sel_toggle_off(self):
564 buf = "hello <s>text</s> world"
565 start, end = 9, 13 # "text" inside <s>
566 new_buf, ss, se = toggle(buf, start, end, *STRIKE_HTML, HTML_FORMATS)
567 assert new_buf == "hello text world"
568 assert new_buf[ss:se] == "text"
569
570
571 class TestHtmlCode:
572 def test_no_format_apply_code(self):
573 buf = "hello text world"
574 start, end = 6, 10
575 new_buf, ss, se = toggle(buf, start, end, *CODE_HTML, HTML_FORMATS)
576 assert new_buf == "hello <code>text</code> world"
577 assert new_buf[ss:se] == "text"
578
579 def test_code_inside_sel_toggle_off(self):
580 buf = "hello <code>text</code> world"
581 # "<code>text</code>" is at 6..23
582 start, end = 6, 23
583 new_buf, ss, se = toggle(buf, start, end, *CODE_HTML, HTML_FORMATS)
584 assert new_buf == "hello text world"
585 assert new_buf[ss:se] == "text"
586
587 def test_code_outside_sel_switch_to_italic(self):
588 buf = "hello <code>text</code> world"
589 start, end = 12, 16 # "text" inside <code>
590 new_buf, ss, se = toggle(buf, start, end, *ITALIC_HTML, HTML_FORMATS)
591 assert new_buf == "hello <i>text</i> world"
592 assert new_buf[ss:se] == "text"
593
594 def test_bold_switch_to_code(self):
595 buf = "hello <b>text</b> world"
596 start, end = 6, 17
597 new_buf, ss, se = toggle(buf, start, end, *CODE_HTML, HTML_FORMATS)
598 assert new_buf == "hello <code>text</code> world"
599 assert new_buf[ss:se] == "text"
600
601
602 # ---------------------------------------------------------------------------
603 # Cross-format switching (all → all) for markdown
604 # ---------------------------------------------------------------------------
605
606
607 class TestMarkdownCrossFormatSwitching:
608 """Every format can be switched to every other format."""
609
610 FORMATS: ClassVar = [BOLD_MD, ITALIC_MD, STRIKE_MD, CODE_MD]
611 FORMAT_NAMES: ClassVar = ["bold", "italic", "strikethrough", "code"]
612
613 @pytest.mark.parametrize(
614 ("src_fmt", "dst_fmt"),
615 [
616 (src, dst)
617 for src in [BOLD_MD, ITALIC_MD, STRIKE_MD, CODE_MD]
618 for dst in [BOLD_MD, ITALIC_MD, STRIKE_MD, CODE_MD]
619 if src != dst
620 ],
621 )
622 def test_switch(self, src_fmt, dst_fmt):
623 src_b, src_a = src_fmt
624 dst_b, dst_a = dst_fmt
625 buf = f"{src_b}text{src_a}"
626 start, end = 0, len(buf)
627 new_buf, ss, se = toggle(buf, start, end, *dst_fmt, MD_FORMATS)
628 assert new_buf == f"{dst_b}text{dst_a}"
629 assert new_buf[ss:se] == "text"
630
631
632 # ===========================================================================
633 # Tests for compute_toggle_line_format
634 # ===========================================================================
635
636
637 class TestToggleLineFormat:
638 """Tests for compute_toggle_line_format (block / paragraph formatting)."""
639
640 # -------------------------------------------------------------------
641 # Markdown blockquote (before="> ", after="")
642 # -------------------------------------------------------------------
643
644 def test_md_blockquote_toggle_on(self):
645 result = compute_toggle_line_format("Hello world", "> ")
646 assert result == "> Hello world"
647
648 def test_md_blockquote_toggle_off(self):
649 result = compute_toggle_line_format("> Hello world", "> ")
650 assert result == "Hello world"
651
652 def test_md_blockquote_empty_line_on(self):
653 assert compute_toggle_line_format("", "> ") == "> "
654
655 def test_md_blockquote_empty_line_off(self):
656 assert compute_toggle_line_format("> ", "> ") == ""
657
658 def test_md_blockquote_no_false_strip(self):
659 # Line starts with ">" but not "> " — should add, not strip
660 assert compute_toggle_line_format(">text", "> ") == "> >text"
661
662 # -------------------------------------------------------------------
663 # RST blockquote — spaces variant (before=" ", after="")
664 # -------------------------------------------------------------------
665
666 def test_rst_blockquote_spaces_toggle_on(self):
667 assert compute_toggle_line_format("Hello", " ") == " Hello"
668
669 def test_rst_blockquote_spaces_toggle_off(self):
670 assert compute_toggle_line_format(" Hello", " ") == "Hello"
671
672 def test_rst_blockquote_spaces_empty_on(self):
673 assert compute_toggle_line_format("", " ") == " "
674
675 def test_rst_blockquote_spaces_empty_off(self):
676 assert compute_toggle_line_format(" ", " ") == ""
677
678 # -------------------------------------------------------------------
679 # RST blockquote — tab variant (before="\t", after="")
680 # -------------------------------------------------------------------
681
682 def test_rst_blockquote_tab_toggle_on(self):
683 assert compute_toggle_line_format("Hello", "\t") == "\tHello"
684
685 def test_rst_blockquote_tab_toggle_off(self):
686 assert compute_toggle_line_format("\tHello", "\t") == "Hello"
687
688 # -------------------------------------------------------------------
689 # HTML blockquote (before="<blockquote>", after="</blockquote>")
690 # -------------------------------------------------------------------
691
692 HTML_B = "<blockquote>"
693 HTML_A = "</blockquote>"
694
695 def test_html_blockquote_toggle_on(self):
696 result = compute_toggle_line_format(
697 "Hello world",
698 self.HTML_B,
699 self.HTML_A,
700 )
701 assert result == "<blockquote>Hello world</blockquote>"
702
703 def test_html_blockquote_toggle_off(self):
704 result = compute_toggle_line_format(
705 "<blockquote>Hello world</blockquote>",
706 self.HTML_B,
707 self.HTML_A,
708 )
709 assert result == "Hello world"
710
711 def test_html_blockquote_empty_line_on(self):
712 result = compute_toggle_line_format("", self.HTML_B, self.HTML_A)
713 assert result == "<blockquote></blockquote>"
714
715 def test_html_blockquote_empty_line_off(self):
716 result = compute_toggle_line_format(
717 "<blockquote></blockquote>",
718 self.HTML_B,
719 self.HTML_A,
720 )
721 assert result == ""
722
723 def test_html_blockquote_only_before_adds_both(self):
724 # Only the before-marker is present — should add, not strip
725 result = compute_toggle_line_format(
726 "<blockquote>Hello world",
727 self.HTML_B,
728 self.HTML_A,
729 )
730 assert result == ("<blockquote><blockquote>Hello world</blockquote>")
731
732 def test_html_blockquote_only_after_adds_both(self):
733 # Only the after-marker is present — should add, not strip
734 result = compute_toggle_line_format(
735 "Hello world</blockquote>",
736 self.HTML_B,
737 self.HTML_A,
738 )
739 assert result == ("<blockquote>Hello world</blockquote></blockquote>")
740
741 # -------------------------------------------------------------------
742 # Idempotency: toggling on and then off returns original text
743 # -------------------------------------------------------------------
744
745 @pytest.mark.parametrize(
746 ("before", "after", "text"),
747 [
748 ("> ", "", "Hello world"),
749 ("> ", "", ""),
750 (" ", "", "Some RST text"),
751 ("\t", "", "Tab indented"),
752 ("<blockquote>", "</blockquote>", "HTML paragraph"),
753 ("<blockquote>", "</blockquote>", ""),
754 ],
755 )
756 def test_idempotent_round_trip(self, before, after, text):
757 toggled_on = compute_toggle_line_format(text, before, after)
758 # Must change (unless text was already empty and prefix is also empty)
759 assert toggled_on != text or not text
760 toggled_off = compute_toggle_line_format(toggled_on, before, after)
761 assert toggled_off == text
762
763
764 # ===========================================================================
765 # Tests for compute_toggle_line_exclusive (header levels)
766 # ===========================================================================
767
768
769 class TestToggleLineExclusive:
770 """Tests for compute_toggle_line_exclusive (mutually exclusive formats)."""
771
772 MD_VARIANTS: ClassVar = [(f"{'#' * n} ", "") for n in range(1, 7)]
773 HTML_VARIANTS: ClassVar = [(f"<h{n}>", f"</h{n}>") for n in range(1, 7)]
774
775 # --- Markdown -----------------------------------------------------------
776
777 def test_md_h1_toggle_on(self):
778 result = compute_toggle_line_exclusive(
779 "Hello",
780 "# ",
781 "",
782 self.MD_VARIANTS,
783 )
784 assert result == "# Hello"
785
786 def test_md_h3_toggle_on(self):
787 result = compute_toggle_line_exclusive(
788 "Hello",
789 "### ",
790 "",
791 self.MD_VARIANTS,
792 )
793 assert result == "### Hello"
794
795 def test_md_h1_toggle_off(self):
796 result = compute_toggle_line_exclusive(
797 "# Hello",
798 "# ",
799 "",
800 self.MD_VARIANTS,
801 )
802 assert result == "Hello"
803
804 def test_md_switch_h1_to_h3(self):
805 result = compute_toggle_line_exclusive(
806 "# Hello",
807 "### ",
808 "",
809 self.MD_VARIANTS,
810 )
811 assert result == "### Hello"
812
813 def test_md_switch_h3_to_h6(self):
814 result = compute_toggle_line_exclusive(
815 "### Hello",
816 "###### ",
817 "",
818 self.MD_VARIANTS,
819 )
820 assert result == "###### Hello"
821
822 def test_md_all_levels_round_trip(self):
823 for level in range(1, 7):
824 before = "#" * level + " "
825 line = f"{before}Text"
826 # Toggle off
827 stripped = compute_toggle_line_exclusive(
828 line,
829 before,
830 "",
831 self.MD_VARIANTS,
832 )
833 assert stripped == "Text"
834
835 # --- HTML ---------------------------------------------------------------
836
837 def test_html_h1_toggle_on(self):
838 result = compute_toggle_line_exclusive(
839 "Hello",
840 "<h1>",
841 "</h1>",
842 self.HTML_VARIANTS,
843 )
844 assert result == "<h1>Hello</h1>"
845
846 def test_html_h2_toggle_off(self):
847 result = compute_toggle_line_exclusive(
848 "<h2>Hello</h2>",
849 "<h2>",
850 "</h2>",
851 self.HTML_VARIANTS,
852 )
853 assert result == "Hello"
854
855 def test_html_switch_h1_to_h4(self):
856 result = compute_toggle_line_exclusive(
857 "<h1>Hello</h1>",
858 "<h4>",
859 "</h4>",
860 self.HTML_VARIANTS,
861 )
862 assert result == "<h4>Hello</h4>"
863
864
865 # ===========================================================================
866 # Tests for compute_toggle_rst_header
867 # ===========================================================================
868
869
870 class TestToggleRstHeader:
871 """Tests for compute_toggle_rst_header."""
872
873 # --- Add underline (no existing underline) ------------------------------
874
875 def test_add_h1(self):
876 underline, had = compute_toggle_rst_header("Hello", None, "=")
877 assert underline == "====="
878 assert not had
879
880 def test_add_h2_with_next_line_unrelated(self):
881 underline, had = compute_toggle_rst_header("Hi", "Some text", "-")
882 assert underline == "--"
883 assert not had
884
885 def test_underline_matches_text_length(self):
886 text = "A longer title"
887 underline, _ = compute_toggle_rst_header(text, None, "=")
888 assert underline == "=" * len(text)
889
890 # --- Toggle off (same underline char) -----------------------------------
891
892 def test_toggle_off_h1(self):
893 underline, had = compute_toggle_rst_header("Hello", "=====", "=")
894 assert underline is None
895 assert had
896
897 def test_toggle_off_longer_underline(self):
898 # Underline longer than title — still recognised
899 underline, had = compute_toggle_rst_header("Hi", "=====", "=")
900 assert underline is None
901 assert had
902
903 # --- Replace (different underline char) ---------------------------------
904
905 def test_replace_h1_with_h2(self):
906 underline, had = compute_toggle_rst_header("Hello", "=====", "-")
907 assert underline == "-----"
908 assert had
909
910 def test_replace_h3_with_h5(self):
911 underline, had = compute_toggle_rst_header("Test", "^^^^", "~")
912 assert underline == "~~~~"
913 assert had
914
915 # --- Edge cases ---------------------------------------------------------
916
917 def test_empty_line_no_op(self):
918 underline, had = compute_toggle_rst_header("", None, "=")
919 assert underline is None
920 assert not had
921
922 def test_whitespace_only_line_no_op(self):
923 underline, had = compute_toggle_rst_header(" ", None, "=")
924 assert underline is None
925 assert not had
926
927 def test_all_known_chars_recognised(self):
928 for char in RST_HEADER_CHARS:
929 underline, had = compute_toggle_rst_header("X", char * 5, char)
930 assert underline is None
931 assert had
932
933 def test_unknown_char_not_recognised(self):
934 # '#' is not an RST underline char
935 underline, had = compute_toggle_rst_header("Hello", "#####", "=")
936 assert underline == "====="
937 assert not had
938
939
940 # ===========================================================================
941 # Tests for compute_toggle_bullet
942 # ===========================================================================
943
944 from formiko.format_utils import compute_toggle_bullet # noqa: E402
945
946 MD_BLOCK_VARIANTS = (
947 ("###### ", ""),
948 ("##### ", ""),
949 ("#### ", ""),
950 ("### ", ""),
951 ("## ", ""),
952 ("# ", ""),
953 ("> ", ""),
954 )
955 HTML_BLOCK_VARIANTS = tuple(
956 [(f"<h{n}>", f"</h{n}>") for n in range(1, 7)]
957 + [("<blockquote>", "</blockquote>")],
958 )
959
960
961 class TestToggleBullet:
962 """Tests for compute_toggle_bullet."""
963
964 # -------------------------------------------------------------------
965 # Basic toggle on / off
966 # -------------------------------------------------------------------
967
968 def test_toggle_on_plain_line(self):
969 text, blank = compute_toggle_bullet("Hello", None, "- ")
970 assert text == "- Hello"
971 assert not blank
972
973 def test_toggle_off(self):
974 text, blank = compute_toggle_bullet("- Hello", None, "- ")
975 assert text == "Hello"
976 assert not blank
977
978 def test_toggle_off_never_inserts_blank(self):
979 # Even if prev line has content, toggle-off never requests blank line
980 text, blank = compute_toggle_bullet("- Hello", "Some text", "- ")
981 assert text == "Hello"
982 assert not blank
983
984 # -------------------------------------------------------------------
985 # Blank-line separator logic
986 # -------------------------------------------------------------------
987
988 def test_blank_line_when_prev_has_text(self):
989 _, blank = compute_toggle_bullet("Hello", "Previous text", "- ")
990 assert blank
991
992 def test_no_blank_when_prev_is_empty(self):
993 _, blank = compute_toggle_bullet("Hello", "", "- ")
994 assert not blank
995
996 def test_no_blank_when_prev_is_whitespace_only(self):
997 _, blank = compute_toggle_bullet("Hello", " ", "- ")
998 assert not blank
999
1000 def test_no_blank_when_prev_is_list_item(self):
1001 _, blank = compute_toggle_bullet("Hello", "- Previous", "- ")
1002 assert not blank
1003
1004 def test_no_blank_when_no_prev_line(self):
1005 _, blank = compute_toggle_bullet("Hello", None, "- ")
1006 assert not blank
1007
1008 # -------------------------------------------------------------------
1009 # Block-format stripping (Markdown)
1010 # -------------------------------------------------------------------
1011
1012 def test_strips_h1_before_adding_bullet(self):
1013 text, _ = compute_toggle_bullet(
1014 "# Title",
1015 None,
1016 "- ",
1017 all_block_variants=MD_BLOCK_VARIANTS,
1018 )
1019 assert text == "- Title"
1020
1021 def test_strips_h3_before_adding_bullet(self):
1022 text, _ = compute_toggle_bullet(
1023 "### Title",
1024 None,
1025 "- ",
1026 all_block_variants=MD_BLOCK_VARIANTS,
1027 )
1028 assert text == "- Title"
1029
1030 def test_strips_blockquote_before_adding_bullet(self):
1031 text, _ = compute_toggle_bullet(
1032 "> Text",
1033 None,
1034 "- ",
1035 all_block_variants=MD_BLOCK_VARIANTS,
1036 )
1037 assert text == "- Text"
1038
1039 def test_strips_h6_and_adds_blank(self):
1040 text, blank = compute_toggle_bullet(
1041 "###### Title",
1042 "Previous",
1043 "- ",
1044 all_block_variants=MD_BLOCK_VARIANTS,
1045 )
1046 assert text == "- Title"
1047 assert blank
1048
1049 # -------------------------------------------------------------------
1050 # Block-format stripping (HTML)
1051 # -------------------------------------------------------------------
1052
1053 def test_html_strips_h2_tag(self):
1054 text, _ = compute_toggle_bullet(
1055 "<h2>Title</h2>",
1056 None,
1057 "<li>",
1058 "</li>",
1059 HTML_BLOCK_VARIANTS,
1060 )
1061 assert text == "<li>Title</li>"
1062
1063 def test_html_strips_blockquote(self):
1064 text, _ = compute_toggle_bullet(
1065 "<blockquote>Text</blockquote>",
1066 None,
1067 "<li>",
1068 "</li>",
1069 HTML_BLOCK_VARIANTS,
1070 )
1071 assert text == "<li>Text</li>"
1072
1073 def test_html_no_blank_line_logic(self):
1074 # prev_line_text=None suppresses blank line for HTML
1075 _, blank = compute_toggle_bullet(
1076 "Text",
1077 None,
1078 "<li>",
1079 "</li>",
1080 HTML_BLOCK_VARIANTS,
1081 )
1082 assert not blank
1083
1084 # -------------------------------------------------------------------
1085 # Nested lists: leading indentation is preserved
1086 # -------------------------------------------------------------------
1087
1088 def test_nested_toggle_on_preserves_indent(self):
1089 text, _ = compute_toggle_bullet(" Text", None, "- ")
1090 assert text == " - Text"
1091
1092 def test_nested_tab_indent_preserved(self):
1093 text, _ = compute_toggle_bullet("\tText", None, "- ")
1094 assert text == "\t- Text"
1095
1096 def test_nested_toggle_off_preserves_indent(self):
1097 text, _ = compute_toggle_bullet(" - Item", None, "- ")
1098 assert text == " Item"
1099
1100 def test_nested_blank_line_with_prev_text(self):
1101 _, blank = compute_toggle_bullet("Text", "Previous", "- ")
1102 assert blank
1103
1104 def test_nested_no_blank_if_prev_is_indented_bullet(self):
1105 _, blank = compute_toggle_bullet(" Text", " - Prev", "- ")
1106 assert not blank
1107
1108
1109 # ---------------------------------------------------------------------------
1110 # TestToggleOrdered
1111 # ---------------------------------------------------------------------------
1112
1113 _MD_BLOCK_VARIANTS = [
1114 ("> ", ""),
1115 ("# ", ""),
1116 ("## ", ""),
1117 ("### ", ""),
1118 ("#### ", ""),
1119 ("##### ", ""),
1120 ("###### ", ""),
1121 ]
1122
1123
1124 class TestToggleOrdered:
1125 """Tests for compute_toggle_ordered (MD / RST / HTML via toggle_bullet)."""
1126
1127 # ------- toggle-off ------------------------------------------------------
1128
1129 def test_md_toggle_off(self):
1130 text, blank = compute_toggle_ordered("1. Item", None)
1131 assert text == "Item"
1132 assert not blank
1133
1134 def test_md_toggle_off_preserves_content(self):
1135 text, _ = compute_toggle_ordered("3. Hello world", None)
1136 assert text == "Hello world"
1137
1138 def test_rst_toggle_off(self):
1139 text, blank = compute_toggle_ordered(
1140 "2. Next item",
1141 None,
1142 auto_number=True,
1143 )
1144 assert text == "Next item"
1145 assert not blank
1146
1147 # ------- toggle-on — Markdown --------------------------------------------
1148
1149 def test_md_toggle_on_plain(self):
1150 text, blank = compute_toggle_ordered("Plain text", None)
1151 assert text == "1. Plain text"
1152 assert not blank
1153
1154 def test_md_toggle_on_no_prev(self):
1155 _, blank = compute_toggle_ordered("Text", None)
1156 assert not blank
1157
1158 def test_md_toggle_on_prev_is_ordered(self):
1159 _, blank = compute_toggle_ordered("New item", "1. Previous")
1160 assert not blank
1161
1162 def test_md_toggle_on_prev_not_ordered(self):
1163 _, blank = compute_toggle_ordered("New item", "Some text")
1164 assert blank
1165
1166 def test_md_toggle_on_prev_empty(self):
1167 _, blank = compute_toggle_ordered("New item", "")
1168 assert not blank
1169
1170 def test_md_toggle_on_strips_block_variant(self):
1171 text, _ = compute_toggle_ordered(
1172 "> Quoted text",
1173 None,
1174 all_block_variants=_MD_BLOCK_VARIANTS,
1175 )
1176 assert text == "1. Quoted text"
1177
1178 def test_md_toggle_on_strips_header(self):
1179 text, _ = compute_toggle_ordered(
1180 "## Header text",
1181 None,
1182 all_block_variants=_MD_BLOCK_VARIANTS,
1183 )
1184 assert text == "1. Header text"
1185
1186 # ------- toggle-on — RST -------------------------------------------------
1187
1188 def test_rst_toggle_on_plain(self):
1189 text, blank = compute_toggle_ordered("Item", None, auto_number=True)
1190 assert text == "1. Item"
1191 assert not blank
1192
1193 def test_rst_toggle_on_prev_is_ordered_increments(self):
1194 text, blank = compute_toggle_ordered(
1195 "Next",
1196 "1. Previous",
1197 auto_number=True,
1198 )
1199 assert text == "2. Next"
1200 assert not blank
1201
1202 def test_rst_toggle_on_prev_ordered_high_number(self):
1203 text, _ = compute_toggle_ordered("Item", "9. Last", auto_number=True)
1204 assert text == "10. Item"
1205
1206 def test_rst_toggle_on_prev_not_ordered_uses_one(self):
1207 text, blank = compute_toggle_ordered(
1208 "Item",
1209 "Some text",
1210 auto_number=True,
1211 )
1212 assert text == "1. Item"
1213 assert blank
1214
1215 def test_rst_nested_toggle_on_preserves_indent(self):
1216 text, _ = compute_toggle_ordered(" Indented", None, auto_number=True)
1217 assert text == " 1. Indented"
1218
1219 def test_rst_toggle_off_no_blank(self):
1220 _, blank = compute_toggle_ordered(
1221 "4. Item",
1222 "3. Prev",
1223 auto_number=True,
1224 )
1225 assert not blank
1226
1227 # ------- invariants ------------------------------------------------------
1228
1229 def test_toggle_off_then_on_md(self):
1230 original = "Hello"
1231 toggled_on, _ = compute_toggle_ordered(original, None)
1232 toggled_off, _ = compute_toggle_ordered(toggled_on, None)
1233 assert toggled_off == original
1234
1235 def test_toggle_off_then_on_rst(self):
1236 original = "Hello"
1237 toggled_on, _ = compute_toggle_ordered(
1238 original,
1239 None,
1240 auto_number=True,
1241 )
1242 toggled_off, _ = compute_toggle_ordered(
1243 toggled_on,
1244 None,
1245 auto_number=True,
1246 )
1247 assert toggled_off == original
1248
1249
1250 # ---------------------------------------------------------------------------
1251 # TestCrossBlockStripping
1252 # ---------------------------------------------------------------------------
1253
1254 _MD_ALL_BLOCK = (
1255 ("###### ", ""),
1256 ("##### ", ""),
1257 ("#### ", ""),
1258 ("### ", ""),
1259 ("## ", ""),
1260 ("# ", ""),
1261 ("> ", ""),
1262 ("- ", ""),
1263 )
1264
1265
1266 class TestCrossBlockStripping:
1267 """Block formats strip each other's markers when toggling on."""
1268
1269 # ------- blockquote strips others ----------------------------------------
1270
1271 def test_blockquote_strips_bullet_md(self):
1272 text = compute_toggle_line_format(
1273 "- Item",
1274 "> ",
1275 all_block_variants=_MD_ALL_BLOCK,
1276 )
1277 assert text == "> Item"
1278
1279 def test_blockquote_strips_header_md(self):
1280 text = compute_toggle_line_format(
1281 "## Heading",
1282 "> ",
1283 all_block_variants=_MD_ALL_BLOCK,
1284 )
1285 assert text == "> Heading"
1286
1287 def test_blockquote_strips_ordered_md(self):
1288 text = compute_toggle_line_format(
1289 "3. Item",
1290 "> ",
1291 strip_ordered=True,
1292 )
1293 assert text == "> Item"
1294
1295 def test_blockquote_strips_ordered_and_bullet(self):
1296 # ordered takes priority (applied first), then variants loop skipped
1297 text = compute_toggle_line_format(
1298 "2. Text",
1299 "> ",
1300 all_block_variants=_MD_ALL_BLOCK,
1301 strip_ordered=True,
1302 )
1303 assert text == "> Text"
1304
1305 # ------- header strips others --------------------------------------------
1306
1307 _MD_HEADER_VARIANTS: ClassVar = [
1308 ("###### ", ""),
1309 ("##### ", ""),
1310 ("#### ", ""),
1311 ("### ", ""),
1312 ("## ", ""),
1313 ("# ", ""),
1314 ]
1315 _EXTRA_MD: ClassVar = (("> ", ""), ("- ", ""))
1316
1317 def test_header_strips_blockquote_md(self):
1318 text = compute_toggle_line_exclusive(
1319 "> Text",
1320 "## ",
1321 "",
1322 self._MD_HEADER_VARIANTS,
1323 extra_strip_variants=self._EXTRA_MD,
1324 )
1325 assert text == "## Text"
1326
1327 def test_header_strips_bullet_md(self):
1328 text = compute_toggle_line_exclusive(
1329 "- Item",
1330 "# ",
1331 "",
1332 self._MD_HEADER_VARIANTS,
1333 extra_strip_variants=self._EXTRA_MD,
1334 )
1335 assert text == "# Item"
1336
1337 def test_header_strips_ordered_md(self):
1338 text = compute_toggle_line_exclusive(
1339 "1. Item",
1340 "# ",
1341 "",
1342 self._MD_HEADER_VARIANTS,
1343 extra_strip_variants=self._EXTRA_MD,
1344 strip_ordered=True,
1345 )
1346 assert text == "# Item"
1347
1348 # ------- bullet strips others --------------------------------------------
1349
1350 def test_bullet_strips_header_md(self):
1351 text, _ = compute_toggle_bullet(
1352 "## Heading",
1353 None,
1354 "- ",
1355 all_block_variants=_MD_ALL_BLOCK,
1356 )
1357 assert text == "- Heading"
1358
1359 def test_bullet_strips_blockquote_md(self):
1360 text, _ = compute_toggle_bullet(
1361 "> Quote",
1362 None,
1363 "- ",
1364 all_block_variants=_MD_ALL_BLOCK,
1365 )
1366 assert text == "- Quote"
1367
1368 def test_bullet_strips_ordered_md(self):
1369 text, _ = compute_toggle_bullet(
1370 "2. Item",
1371 None,
1372 "- ",
1373 all_block_variants=_MD_ALL_BLOCK,
1374 strip_ordered=True,
1375 )
1376 assert text == "- Item"
1377
1378 def test_bullet_nested_preserves_indent(self):
1379 text, _ = compute_toggle_bullet(" Indented", None, "- ")
1380 assert text == " - Indented"
1381
1382 def test_bullet_strips_ordered_rst(self):
1383 text, _ = compute_toggle_bullet(
1384 "3. Item",
1385 None,
1386 "- ",
1387 strip_ordered=True,
1388 )
1389 assert text == "- Item"
1390
1391 # ------- ordered strips others -------------------------------------------
1392
1393 def test_ordered_strips_header_md(self):
1394 text, _ = compute_toggle_ordered(
1395 "## Heading",
1396 None,
1397 all_block_variants=_MD_ALL_BLOCK,
1398 )
1399 assert text == "1. Heading"
1400
1401 def test_ordered_strips_blockquote_md(self):
1402 text, _ = compute_toggle_ordered(
1403 "> Quote",
1404 None,
1405 all_block_variants=_MD_ALL_BLOCK,
1406 )
1407 assert text == "1. Quote"
1408
1409 def test_ordered_strips_bullet_md(self):
1410 text, _ = compute_toggle_ordered(
1411 "- Item",
1412 None,
1413 all_block_variants=_MD_ALL_BLOCK,
1414 )
1415 assert text == "1. Item"
1416
1417 def test_ordered_strips_bullet_rst(self):
1418 text, _ = compute_toggle_ordered(
1419 "- Item",
1420 None,
1421 all_block_variants=(("- ", ""),),
1422 auto_number=True,
1423 )
1424 assert text == "1. Item"
1425
1426 def test_ordered_nested_preserves_indent(self):
1427 text, _ = compute_toggle_ordered(
1428 " Indented",
1429 None,
1430 all_block_variants=(("- ", ""),),
1431 auto_number=True,
1432 )
1433 assert text == " 1. Indented"
1434
1435 def test_nested_bullet_to_ordered(self):
1436 # indented bullet → indented ordered
1437 text, _ = compute_toggle_ordered(
1438 " - Item",
1439 None,
1440 all_block_variants=(("- ", ""),),
1441 auto_number=True,
1442 )
1443 assert text == " 1. Item"
1444
1445 def test_nested_ordered_to_bullet(self):
1446 # indented ordered → indented bullet
1447 text, _ = compute_toggle_bullet(
1448 " 1. Item",
1449 None,
1450 "- ",
1451 all_block_variants=(("- ", ""),),
1452 strip_ordered=True,
1453 )
1454 assert text == " - Item"
1455
1456 def test_nested_ordered_toggle_off_preserves_indent(self):
1457 text, _ = compute_toggle_ordered(" 3. Item", None, auto_number=True)
1458 assert text == " Item"
1459
1460 def test_nested_auto_number_from_indented_prev(self):
1461 # previous line is also indented
1462 text, _ = compute_toggle_ordered(
1463 " Item",
1464 " 2. Prev",
1465 auto_number=True,
1466 )
1467 assert text == " 3. Item"
1468
1469 def test_md_with_text(self):
1470 from formiko.format_utils import compute_link
1471
1472 assert (
1473 compute_link("Python", "https://python.org", "m2r")
1474 == "[Python](https://python.org)"
1475 )
1476
1477 def test_rst_with_text(self):
1478 from formiko.format_utils import compute_link
1479
1480 assert (
1481 compute_link("Python", "https://python.org", "rst")
1482 == "`Python <https://python.org>`_"
1483 )
1484
1485 def test_html_with_text(self):
1486 from formiko.format_utils import compute_link
1487
1488 assert (
1489 compute_link("Python", "https://python.org", "html")
1490 == '<a href="https://python.org">Python</a>'
1491 )
1492
1493 def test_html_no_text_uses_url(self):
1494 from formiko.format_utils import compute_link
1495
1496 assert (
1497 compute_link("", "https://python.org", "html")
1498 == '<a href="https://python.org">https://python.org</a>'
1499 )
1500
1501 def test_no_text_returns_url(self):
1502 from formiko.format_utils import compute_link
1503
1504 assert (
1505 compute_link("", "https://python.org", "m2r")
1506 == "https://python.org"
1507 )
1508 assert (
1509 compute_link("", "https://python.org", "rst")
1510 == "https://python.org"
1511 )
1512
1513 def test_whitespace_text_returns_url(self):
1514 from formiko.format_utils import compute_link
1515
1516 assert (
1517 compute_link(" ", "https://python.org", "m2r")
1518 == "https://python.org"
1519 )
1520
1521
1522 class TestParseLink:
1523 def test_md_link(self):
1524 from formiko.format_utils import parse_link
1525
1526 assert parse_link("[Python](https://python.org)", "m2r") == (
1527 "Python",
1528 "https://python.org",
1529 )
1530
1531 def test_rst_link(self):
1532 from formiko.format_utils import parse_link
1533
1534 assert parse_link("`Python <https://python.org>`_", "rst") == (
1535 "Python",
1536 "https://python.org",
1537 )
1538
1539 def test_html_link(self):
1540 from formiko.format_utils import parse_link
1541
1542 assert parse_link(
1543 '<a href="https://python.org">Python</a>',
1544 "html",
1545 ) == ("Python", "https://python.org")
1546
1547 def test_bare_url_md(self):
1548 from formiko.format_utils import parse_link
1549
1550 assert parse_link("https://python.org", "m2r") == (
1551 "",
1552 "https://python.org",
1553 )
1554
1555 def test_bare_url_rst(self):
1556 from formiko.format_utils import parse_link
1557
1558 assert parse_link("https://python.org", "rst") == (
1559 "",
1560 "https://python.org",
1561 )
1562
1563 def test_plain_text_md(self):
1564 from formiko.format_utils import parse_link
1565
1566 assert parse_link("hello world", "m2r") == ("hello world", "")
1567
1568 def test_plain_text_rst(self):
1569 from formiko.format_utils import parse_link
1570
1571 assert parse_link("hello world", "rst") == ("hello world", "")
1572
1573 def test_empty_string(self):
1574 from formiko.format_utils import parse_link
1575
1576 assert parse_link("", "m2r") == ("", "")