• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tired-labs / tiredize / 22530199229

28 Feb 2026 09:42PM UTC coverage: 90.826% (-3.8%) from 94.577%
22530199229

push

github

sludgework
Incorporating peer review feedback

Fix tautological assertion in test_document_load_twice_replaces_state,
strengthen test_line_col_offset_beyond_length to verify exact clamped
values, differentiate test_image_reference_dead_lookbehind input from
test_image_reference_preceded_by_bracket, and correct issue file
description for closing fence with extra backticks.

4 of 4 new or added lines in 2 files covered. (100.0%)

142 existing lines in 7 files now uncovered.

3594 of 3957 relevant lines covered (90.83%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.06
/tests/markdown/types/test_codeblock.py
1
# Standard library
2
from __future__ import annotations
2✔
3

4
# Third-party
5
import pytest
2✔
6

7
# Local
8
from tiredize.core_types import Position
2✔
9
from tiredize.markdown.types.code import CodeBlock
2✔
10

11
md_section = """# Markdown Test Section - Lorem Ipsum
2✔
12

13
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dapibus auctor
14
mattis. Donec eu posuere ex. Morbi faucibus, diam vitae interdum viverra, diam
15
nulla tempor tellus, non maximus nulla elit eget arcu. Nam quam turpis, finibus
16
non velit vel, pellentesque mattis magna. Nunc tempus ultricies pharetra.
17
Mauris rutrum vel purus eu facilisis. Proin laoreet facilisis libero ac
18
ultricies. Quisque lectus tellus, maximus nec tellus ut, vestibulum faucibus
19
nulla. Pellentesque vel ante egestas, aliquet neque a, egestas mauris. Quisque
20
vulputate metus imperdiet, rhoncus odio eu, dapibus justo. Fusce porta magna in
21
efficitur tincidunt. Vestibulum efficitur ex porttitor neque suscipit pulvinar.
22
Nulla mauris libero, semper in ultricies eu, interdum eget justo.
23

24
Line 15, Offset 00
25
{}
26

27
Donec quis erat non diam sollicitudin faucibus quis quis arcu. In posuere vel
28
dolor vitae aliquet. Maecenas ultrices dignissim orci, id aliquet arcu
29
malesuada eu. Quisque congue ex ac dictum faucibus. Curabitur mollis leo et
30
enim pretium rhoncus. Etiam at lorem vel diam viverra malesuada vel ut erat.
31
Mauris vehicula condimentum consequat. Phasellus fermentum rhoncus enim nec
32
volutpat.
33

34
Line 25, Offset 00
35
{}
36

37
Nulla facilisi. Vestibulum ut turpis ut ipsum euismod varius. Integer et
38
egestas leo. Etiam et porttitor turpis, et dignissim diam. Suspendisse nec
39
maximus ipsum, eget convallis lorem. Donec consequat blandit nisi at porttitor.
40
Vivamus dictum ante a odio varius fringilla. Donec scelerisque nisi dolor, at
41
volutpat nibh aliquam in. Maecenas vestibulum nulla a efficitur vestibulum.
42
Nulla vulputate pulvinar diam, non sollicitudin leo. Suspendisse id porta orci,
43
a fringilla ex. In hac habitasse platea dictumst.
44

45
Line 36, Offset 00
46
{}
47

48
Cras venenatis semper justo, eget feugiat turpis mollis non. Suspendisse risus
49
lacus, pulvinar ut ipsum nec, pharetra blandit leo. Vivamus ullamcorper magna
50
sit amet dolor dapibus porta. Pellentesque habitant morbi tristique senectus et
51
netus et malesuada fames ac turpis egestas. Aenean eget mollis nulla. Donec
52
risus ex, malesuada fermentum sem in, molestie viverra sem. Ut odio massa,
53
luctus egestas maximus non, venenatis id justo. Suspendisse eleifend est id
54
arcu porta tempus.
55

56
Line 47, Offset 00
57
{}
58

59
Curabitur id nulla sit amet felis porta tempus. Morbi placerat malesuada dolor,
60
pulvinar tempor enim laoreet eget. Nullam consequat, magna ac dapibus bibendum,
61
magna enim aliquet turpis, vitae facilisis sapien velit sed odio. Duis eu
62
condimentum lorem. Maecenas quam magna, condimentum ac ultrices sit amet,
63
pellentesque et metus. Donec placerat et sem ut auctor. Suspendisse molestie,
64
quam ac pretium varius, libero enim placerat dolor, eget sagittis urna sapien
65
eu tortor.
66

67
Line 58, Offset 00
68
{}"""
69

70

71
def test_no_codeblock():
2✔
72
    md_test = md_section
2✔
73
    regex_matches = CodeBlock.extract(md_test)
2✔
74
    assert len(regex_matches) == 0
2✔
75

76

77
def test_single_codeblock_normal():
2✔
78
    actual_code = '_RE_CODEBLOCK = r""""\n'
2✔
79
    actual_code += r'(?<![^\\\\n' + '\n'
2✔
80
    actual_code += '])\n'
2✔
81
    actual_code += '(?P<delimiter>``[`]+)\n'
2✔
82
    actual_code += '(?P<language>.*)\n'
2✔
83
    actual_code += '(?P<code>[\\\\s\\\\S]*?)\n'
2✔
84
    actual_code += '\\1"""'
2✔
85

86
    actual_language = "python"
2✔
87

88
    actual_string = '```' + actual_language + '\n'
2✔
89
    actual_string += actual_code + '\n'
2✔
90
    actual_string += '```'
2✔
91

92
    exp_code = actual_code
2✔
93
    exp_delimiter = "```"
2✔
94
    exp_language = actual_language
2✔
95
    exp_string = actual_string
2✔
96
    exp_position = Position(offset=1235, length=len(exp_string))
2✔
97

98
    md_text = md_section.format("", actual_string, "", "", "")
2✔
99
    regex_matches = CodeBlock.extract(md_text)
2✔
100

101
    assert len(regex_matches) == 1
2✔
102
    assert regex_matches[0] == CodeBlock(
2✔
103
        code=exp_code,
104
        delimiter=exp_delimiter,
105
        language=exp_language,
106
        position=exp_position,
107
        string=exp_string
108
    )
109

110

111
def test_five_codeblocks_repeated():
2✔
112
    actual_code = '_RE_CODEBLOCK = r""""\n'
2✔
113
    actual_code += r'(?<![^\\\\n' + '\n'
2✔
114
    actual_code += '])\n'
2✔
115
    actual_code += '(?P<delimiter>``[`]+)\n'
2✔
116
    actual_code += '(?P<language>.*)\n'
2✔
117
    actual_code += '(?P<code>[\\\\s\\\\S]*?)\n'
2✔
118
    actual_code += '\\1"""'
2✔
119

120
    actual_language = "python"
2✔
121

122
    actual_string = '```' + actual_language + '\n'
2✔
123
    actual_string += actual_code + '\n'
2✔
124
    actual_string += '```'
2✔
125

126
    exp_code = actual_code
2✔
127
    exp_delimiter = "```"
2✔
128
    exp_language = actual_language
2✔
129
    exp_string = actual_string
2✔
130

131
    md_text = md_section.format(
2✔
132
        actual_string,
133
        actual_string,
134
        actual_string,
135
        actual_string,
136
        actual_string
137
    )
138

139
    positions = [
2✔
140
        Position(offset=825, length=len(exp_string)),
141
        Position(offset=1351, length=len(exp_string)),
142
        Position(offset=2001, length=len(exp_string)),
143
        Position(offset=2622, length=len(exp_string)),
144
        Position(offset=3235, length=len(exp_string))
145
    ]
146

147
    regex_matches = CodeBlock.extract(md_text)
2✔
148
    assert len(regex_matches) == 5
2✔
149
    for i, match in enumerate(regex_matches):
2✔
150
        assert match == CodeBlock(
2✔
151
            code=exp_code,
152
            delimiter=exp_delimiter,
153
            language=exp_language,
154
            position=positions[i],
155
            string=exp_string
156
        )
157

158

159
# =========================================================================
160
#  Sanitize method
161
# =========================================================================
162

163

164
def test_codeblock_sanitize_preserves_length():
2✔
165
    text = "Before\n```python\nprint('hello')\n```\nAfter"
2✔
166
    sanitized = CodeBlock.sanitize(text)
2✔
167
    assert len(sanitized) == len(text)
2✔
168
    assert "print" not in sanitized
2✔
169

170

171
def test_codeblock_sanitize_no_code_blocks():
2✔
172
    text = "Just text, no code blocks here."
2✔
173
    sanitized = CodeBlock.sanitize(text)
2✔
174
    assert sanitized == text
2✔
175

176

177
def test_codeblock_sanitize_idempotent():
2✔
178
    text = "Before\n```python\nprint('hello')\n```\nAfter"
2✔
179
    first = CodeBlock.sanitize(text)
2✔
180
    second = CodeBlock.sanitize(first)
2✔
181
    assert first == second
2✔
182
    assert len(second) == len(text)
2✔
183

184

185
# =========================================================================
186
#  Edge cases
187
# =========================================================================
188

189

190
def test_codeblock_four_backticks():
2✔
191
    """Four backtick delimiter should match with four backtick closing."""
192
    text = "````\ncode\n````"
2✔
193
    results = CodeBlock.extract(text)
2✔
194
    assert len(results) == 1
2✔
195
    assert results[0].delimiter == "````"
2✔
196

197

198
def test_codeblock_no_language():
2✔
199
    text = "```\nplain code\n```"
2✔
200
    results = CodeBlock.extract(text)
2✔
201
    assert len(results) == 1
2✔
202
    assert results[0].language == ""
2✔
203

204

205
def test_codeblock_base_offset():
2✔
206
    text = "```\ncode\n```"
2✔
207
    results = CodeBlock.extract(text, base_offset=50)
2✔
208
    assert len(results) == 1
2✔
209
    assert results[0].position.offset == 50
2✔
210

211

212
# =========================================================================
213
#  Syntax variant tests (GFM spec compliance)
214
# =========================================================================
215

216

217
@pytest.mark.skip(reason="gfm-parity: tilde-fenced code blocks not supported")
2✔
218
def test_codeblock_tilde_fence():
2✔
219
    """GFM supports ~~~ fences. Should match like backtick fences."""
UNCOV
220
    text = "~~~\ncode\n~~~"
×
UNCOV
221
    results = CodeBlock.extract(text)
×
UNCOV
222
    assert len(results) == 1
×
UNCOV
223
    assert results[0].code == "code"
×
224

225

226
def test_codeblock_closing_with_extra_backticks():
2✔
227
    """GFM allows closing fence >= opening length. The backreference
228
    finds the 3-backtick substring within the 5-backtick closing,
229
    so this correctly matches."""
230
    text = "```\ncode\n`````"
2✔
231
    results = CodeBlock.extract(text)
2✔
232
    assert len(results) == 1
2✔
233
    assert results[0].code == "code"
2✔
234

235

236
@pytest.mark.skip(reason="gfm-parity: indented code fences not supported")
2✔
237
def test_codeblock_indented_fence():
2✔
238
    """GFM allows 1-3 spaces before opening/closing fence."""
UNCOV
239
    text = "   ```\ncode\n   ```"
×
UNCOV
240
    results = CodeBlock.extract(text)
×
UNCOV
241
    assert len(results) == 1
×
UNCOV
242
    assert results[0].code == "code"
×
243

244

245
# ===================================================================
246
#  Boundary and degenerate inputs
247
# ===================================================================
248

249

250
def test_codeblock_extract_empty_string():
2✔
251
    assert CodeBlock.extract("") == []
2✔
252

253

254
def test_codeblock_extract_single_char():
2✔
255
    assert CodeBlock.extract("`") == []
2✔
256

257

258
# ===================================================================
259
#  State mutation
260
# ===================================================================
261

262

263
def test_codeblock_extract_does_not_mutate_input():
2✔
264
    text = "```\ncode\n```"
2✔
265
    original = text
2✔
266
    CodeBlock.extract(text)
2✔
267
    assert text == original
2✔
268

269

270
# ===================================================================
271
#  Unicode
272
# ===================================================================
273

274

275
def test_codeblock_unicode_content():
2✔
276
    text = "```\n日本語コード = 42\ncafé = True\n```"
2✔
277
    results = CodeBlock.extract(text)
2✔
278
    assert len(results) == 1
2✔
279
    assert "日本語コード" in results[0].code
2✔
280

281

282
def test_codeblock_sanitize_unicode_preserves_length():
2✔
283
    text = "```\nvar café = '☕'\n```"
2✔
284
    sanitized = CodeBlock.sanitize(text)
2✔
285
    assert len(sanitized) == len(text)
2✔
286

287

288
# ===================================================================
289
#  Additional syntax variant tests
290
# ===================================================================
291

292

293
@pytest.mark.skip(
2✔
294
    reason="gfm-parity: closing fence with trailing spaces not matched"
295
)
296
def test_codeblock_closing_fence_trailing_spaces():
2✔
297
    """GFM allows trailing spaces after closing fence."""
UNCOV
298
    text = "```\ncode\n```   "
×
UNCOV
299
    results = CodeBlock.extract(text)
×
UNCOV
300
    assert len(results) == 1
×
UNCOV
301
    assert results[0].code == "code"
×
302

303

304
@pytest.mark.skip(
2✔
305
    reason="gfm-parity: empty code block not matched"
306
)
307
def test_codeblock_empty():
2✔
308
    """GFM allows empty code blocks with no content lines."""
UNCOV
309
    text = "```\n```"
×
UNCOV
310
    results = CodeBlock.extract(text)
×
UNCOV
311
    assert len(results) == 1
×
UNCOV
312
    assert results[0].code == ""
×
313

314

315
def test_codeblock_after_pipe_char():
2✔
316
    """The (?<![^|\\n]) anchor treats | as valid start-of-line.
317
    Code fence after | produces a false positive match."""
318
    text = "|```\ncode\n```"
2✔
319
    results = CodeBlock.extract(text)
2✔
320
    # Per GFM, | before ``` should not start a code block.
321
    # The anchor (?<![^|\\n]) means "preceding char must be |
322
    # or \\n (or start of string)", so | is accepted.
323
    # This is a false positive.
324
    assert len(results) == 1  # documents actual behavior
2✔
325

326

327
# ===================================================================
328
#  Cross-type: nested code
329
# ===================================================================
330

331

332
def test_inline_code_inside_code_block():
2✔
333
    """Inline code backticks inside a fenced code block should not
334
    produce CodeInline matches when CodeInline sanitizes CodeBlock
335
    first (which it doesn't -- CodeInline has no sanitization)."""
336
    from tiredize.markdown.types.code import CodeInline
2✔
337
    text = "```\n`inline` code\n```"
2✔
338
    results = CodeInline.extract(text)
2✔
339
    # CodeInline does NOT sanitize CodeBlock, so it finds `inline`
340
    # inside the fence. This is a false positive.
341
    # We document actual behavior: it matches.
342
    assert len(results) == 1
2✔
343
    assert results[0].code == "inline"
2✔
344

345

346
def test_code_block_fence_inside_inline_code():
2✔
347
    """Fenced code markers inside backticks should not produce
348
    CodeBlock matches. This is an edge case."""
349
    text = "Use ```` to start code."
2✔
350
    results = CodeBlock.extract(text)
2✔
351
    assert len(results) == 0
2✔
352

353

354
# ===================================================================
355
#  Cross-cutting: CRLF line endings
356
# ===================================================================
357

358

359
@pytest.mark.skip(
2✔
360
    reason="gfm-parity: CRLF line endings not supported"
361
)
362
def test_codeblock_crlf():
2✔
363
    """Code blocks with CRLF line endings should match."""
UNCOV
364
    text = "```\r\ncode\r\n```"
×
UNCOV
365
    results = CodeBlock.extract(text)
×
UNCOV
366
    assert len(results) == 1
×
UNCOV
367
    assert results[0].code == "code"
×
368

369

370
# ===================================================================
371
#  Cross-cutting: leading indentation
372
# ===================================================================
373

374

375
@pytest.mark.skip(
2✔
376
    reason="gfm-parity: indented code fences not supported"
377
)
378
def test_codeblock_one_space_indent():
2✔
379
    """GFM allows 1 space before opening fence."""
UNCOV
380
    text = " ```\ncode\n ```"
×
UNCOV
381
    results = CodeBlock.extract(text)
×
UNCOV
382
    assert len(results) == 1
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc