Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Oct 16, 2024
1 parent fe9e353 commit 11bdf37
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pymdownx/blocks/caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def run(self, doc):
# Handle a specified relative nesting depth
if '__figure_level' in el.attrib:
stack += int(el.attrib['__figure_level']) + 1
if self.auto_level and stack >= (self.auto_level - 1):
if self.auto_level and stack >= self.auto_level:
continue
else:
stack += 1
Expand Down Expand Up @@ -291,7 +291,7 @@ def on_end(self, block):
if prefix and not self.auto:
# Levels should not be used in manual mode, but if they are, give a generic result.
if self.level:
self.fig_num = '.'.join(['1'] * int(self.level))
self.fig_num = '.'.join(['1'] * (int(self.level) + 1))
if self.fig_num:
update_tag(
block,
Expand Down
261 changes: 261 additions & 0 deletions tests/test_extensions/test_blocks/test_captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,60 @@ def test_manual_prepend(self):
True
)

def test_depth(self):
"""Depth is not really supported in manual, so a generic response is expected."""

self.check_markdown(
R"""
Paragraph
/// figure-caption
Caption 1
///
Paragraph
/// figure-caption | ^1
Caption 1.1
///
Paragraph
/// figure-caption | ^2
Caption 1.1.1
///
Paragraph
/// figure-caption | ^3
Caption 1.1.1.1
///
""",
"""
<figure>
<p>Paragraph</p>
<figcaption>
<p>Caption 1</p>
</figcaption>
</figure>
<figure id="__figure-caption_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.</span> Caption 1.1</p>
</figcaption>
</figure>
<figure id="__figure-caption_1_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.1.</span> Caption 1.1.1</p>
</figcaption>
</figure>
<figure id="__figure-caption_1_1_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.1.1.</span> Caption 1.1.1.1</p>
</figcaption>
</figure>
""",
True
)


class TestBlocksCaptionAutoPrefix(util.MdCase):
"""Test Blocks caption cases with enabled `auto`."""
Expand Down Expand Up @@ -844,6 +898,127 @@ def test_nested_captions_manual_id(self):
True
)

def test_depth(self):
"""Test level depth."""

self.check_markdown(
R"""
Paragraph
/// figure-caption
Caption 1
///
Paragraph
/// figure-caption
Caption 1.1.1
///
/// figure-caption | ^1
Caption 1.1
///
Paragraph
/// figure-caption | ^1
Caption 2.1
///
/// figure-caption
Caption 2
///
""",
"""
<figure id="__figure-caption_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.</span> Caption 1</p>
</figcaption>
</figure>
<figure id="__figure-caption_1_1">
<figure id="__figure-caption_1_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.1.</span> Caption 1.1.1</p>
</figcaption>
</figure>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.</span> Caption 1.1</p>
</figcaption>
</figure>
<figure id="__figure-caption_2">
<figure id="__figure-caption_2_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 2.1.1.</span> Caption 2.1</p>
</figcaption>
</figure>
<figcaption>
<p><span class="caption-prefix">Figure 2.</span> Caption 2</p>
</figcaption>
</figure>
""",
True
)

def test_manual_number(self):
"""Test manual number."""

self.check_markdown(
R"""
Paragraph
/// figure-caption
Caption 4.2.1
///
/// figure-caption | 4.2
Caption 4.2
///
Paragraph
/// figure-caption
Caption 5.2.1
///
/// figure-caption | 5.2
Caption 5.2
///
/// figure-caption
Caption 5
///
""",
"""
<figure id="__figure-caption_4_2">
<figure id="__figure-caption_4_2_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 4.2.1.</span> Caption 4.2.1</p>
</figcaption>
</figure>
<figcaption>
<p><span class="caption-prefix">Figure 4.2.</span> Caption 4.2</p>
</figcaption>
</figure>
<figure id="__figure-caption_5">
<figure id="__figure-caption_5_2">
<figure id="__figure-caption_5_2_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 5.2.1.</span> Caption 5.2.1</p>
</figcaption>
</figure>
<figcaption>
<p><span class="caption-prefix">Figure 5.2.</span> Caption 5.2</p>
</figcaption>
</figure>
<figcaption>
<p><span class="caption-prefix">Figure 5.</span> Caption 5</p>
</figcaption>
</figure>
""",
True
)


class TestBlocksCaptionAutoLevel(util.MdCase):
"""Test Blocks caption cases with `auto` level."""
Expand Down Expand Up @@ -969,6 +1144,92 @@ def test_nested_consecutive_captions(self):
True
)

def test_depth(self):
"""Test depth with auto level limit."""

self.check_markdown(
R"""
Paragraph
/// figure-caption
Caption 1
///
Paragraph
/// figure-caption | ^1
Caption 1.1
///
Paragraph
/// figure-caption | ^2
Caption None
///
""",
"""
<figure id="__figure-caption_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.</span> Caption 1</p>
</figcaption>
</figure>
<figure id="__figure-caption_1_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.1.</span> Caption 1.1</p>
</figcaption>
</figure>
<figure>
<p>Paragraph</p>
<figcaption>
<p>Caption None</p>
</figcaption>
</figure>
""",
True
)

def test_manual_numbers(self):
"""Test manual numbers with auto level limit."""

self.check_markdown(
R"""
Paragraph
/// figure-caption | 1
Caption 1
///
Paragraph
/// figure-caption | 2.3
Caption 2.3
///
Paragraph
/// figure-caption | 4.3.1
Caption None
///
""",
"""
<figure id="__figure-caption_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 1.</span> Caption 1</p>
</figcaption>
</figure>
<figure id="__figure-caption_2_3">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 2.3.</span> Caption 2.3</p>
</figcaption>
</figure>
<figure id="__figure-caption_4_3_1">
<p>Paragraph</p>
<figcaption>
<p><span class="caption-prefix">Figure 4.3.1.</span> Caption None</p>
</figcaption>
</figure>
""",
True
)


class TestBlocksCaptionAutoLevelPrepend(util.MdCase):
"""Test Blocks caption cases with `auto` level."""
Expand Down

0 comments on commit 11bdf37

Please sign in to comment.