1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
from textwrap import dedent
import unittest
from mistletoe import Document
from mistletoe.span_token import Strong
from mistletoe.utils import traverse
class TestTraverse(unittest.TestCase):
def test(self):
doc = Document("a **b** c **d**")
filtered = [t.node.__class__.__name__ for t in traverse(doc)]
self.assertEqual(
filtered,
[
"Paragraph",
"RawText",
"Strong",
"RawText",
"Strong",
"RawText",
"RawText",
],
)
def test_with_included_source(self):
doc = Document(
dedent(
"""\
a **b**
c [*d*](link)
"""
)
)
tree = [
(
t.node.__class__.__name__,
t.parent.__class__.__name__ if t.parent else None,
t.depth
)
for t in traverse(doc, include_source=True)
]
self.assertEqual(
tree,
[
('Document', None, 0),
('Paragraph', 'Document', 1),
('Paragraph', 'Document', 1),
('RawText', 'Paragraph', 2),
('Strong', 'Paragraph', 2),
('RawText', 'Paragraph', 2),
('Link', 'Paragraph', 2),
('RawText', 'Strong', 3),
('Emphasis', 'Link', 3),
('RawText', 'Emphasis', 4),
]
)
def test_with_class_filter(self):
doc = Document("a **b** c **d**")
filtered = [t.node.__class__.__name__ for t in traverse(doc, klass=Strong)]
self.assertEqual(filtered, ["Strong", "Strong"])
def test_with_included_source_and_class_filter(self):
doc = Document("a **b** c **d**")
filtered = [
t.node.__class__.__name__
for t in traverse(doc, include_source=True, klass=Strong)
]
self.assertEqual(filtered, ["Strong", "Strong"])
def test_with_depth_limit(self):
doc = Document("a **b** c **d**")
# Zero depth with root not included yields no nodes.
filtered = [t.node.__class__.__name__ for t in traverse(doc, depth=0)]
self.assertEqual(filtered, [])
# Zero depth with root included yields the root node.
filtered = [
t.node.__class__.__name__
for t in traverse(doc, depth=0, include_source=True)
]
self.assertEqual(filtered, ["Document"])
# Depth=1 correctly returns the single node at that level.
filtered = [t.node.__class__.__name__ for t in traverse(doc, depth=1)]
self.assertEqual(filtered, ["Paragraph"])
# Depth=2 returns the correct nodes.
filtered = [t.node.__class__.__name__ for t in traverse(doc, depth=2)]
self.assertEqual(
filtered, ["Paragraph", "RawText", "Strong", "RawText", "Strong"]
)
# Depth=3 returns the correct nodes (all nodes in the tree).
filtered = [t.node.__class__.__name__ for t in traverse(doc, depth=3)]
self.assertEqual(
filtered,
[
"Paragraph",
"RawText",
"Strong",
"RawText",
"Strong",
"RawText",
"RawText",
],
)
# Verify there are no additional nodes at depth=4.
filtered = [t.node.__class__.__name__ for t in traverse(doc, depth=4)]
self.assertEqual(
filtered,
[
"Paragraph",
"RawText",
"Strong",
"RawText",
"Strong",
"RawText",
"RawText",
],
)
|