summaryrefslogtreecommitdiffstats
path: root/tests/test_api
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/test_api/test_main.py279
-rw-r--r--tests/test_api/test_main/test_table_tokens.yml492
-rw-r--r--tests/test_api/test_plugin_creation.py88
-rw-r--r--tests/test_api/test_token.py38
4 files changed, 897 insertions, 0 deletions
diff --git a/tests/test_api/test_main.py b/tests/test_api/test_main.py
new file mode 100644
index 0000000..007259e
--- /dev/null
+++ b/tests/test_api/test_main.py
@@ -0,0 +1,279 @@
+from markdown_it import MarkdownIt
+from markdown_it.token import Token
+
+
+def test_get_rules():
+ md = MarkdownIt("zero")
+ # print(md.get_all_rules())
+ assert md.get_all_rules() == {
+ "core": [
+ "normalize",
+ "block",
+ "inline",
+ "linkify",
+ "replacements",
+ "smartquotes",
+ ],
+ "block": [
+ "table",
+ "code",
+ "fence",
+ "blockquote",
+ "hr",
+ "list",
+ "reference",
+ "html_block",
+ "heading",
+ "lheading",
+ "paragraph",
+ ],
+ "inline": [
+ "text",
+ "newline",
+ "escape",
+ "backticks",
+ "strikethrough",
+ "emphasis",
+ "link",
+ "image",
+ "autolink",
+ "html_inline",
+ "entity",
+ ],
+ "inline2": ["balance_pairs", "strikethrough", "emphasis", "text_collapse"],
+ }
+
+
+def test_load_presets():
+ md = MarkdownIt("zero")
+ assert md.get_active_rules() == {
+ "block": ["paragraph"],
+ "core": ["normalize", "block", "inline"],
+ "inline": ["text"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+ md = MarkdownIt("commonmark")
+ assert md.get_active_rules() == {
+ "core": ["normalize", "block", "inline"],
+ "block": [
+ "code",
+ "fence",
+ "blockquote",
+ "hr",
+ "list",
+ "reference",
+ "html_block",
+ "heading",
+ "lheading",
+ "paragraph",
+ ],
+ "inline": [
+ "text",
+ "newline",
+ "escape",
+ "backticks",
+ "emphasis",
+ "link",
+ "image",
+ "autolink",
+ "html_inline",
+ "entity",
+ ],
+ "inline2": ["balance_pairs", "emphasis", "text_collapse"],
+ }
+
+
+def test_override_options():
+ md = MarkdownIt("zero")
+ assert md.options["maxNesting"] == 20
+ md = MarkdownIt("zero", {"maxNesting": 99})
+ assert md.options["maxNesting"] == 99
+
+
+def test_enable():
+ md = MarkdownIt("zero").enable("heading")
+ assert md.get_active_rules() == {
+ "block": ["heading", "paragraph"],
+ "core": ["normalize", "block", "inline"],
+ "inline": ["text"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+ md.enable(["backticks", "autolink"])
+ assert md.get_active_rules() == {
+ "block": ["heading", "paragraph"],
+ "core": ["normalize", "block", "inline"],
+ "inline": ["text", "backticks", "autolink"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+
+
+def test_disable():
+ md = MarkdownIt("zero").disable("inline")
+ assert md.get_active_rules() == {
+ "block": ["paragraph"],
+ "core": ["normalize", "block"],
+ "inline": ["text"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+ md.disable(["text"])
+ assert md.get_active_rules() == {
+ "block": ["paragraph"],
+ "core": ["normalize", "block"],
+ "inline": [],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+
+
+def test_reset():
+ md = MarkdownIt("zero")
+ with md.reset_rules():
+ md.disable("inline")
+ assert md.get_active_rules() == {
+ "block": ["paragraph"],
+ "core": ["normalize", "block"],
+ "inline": ["text"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+ assert md.get_active_rules() == {
+ "block": ["paragraph"],
+ "core": ["normalize", "block", "inline"],
+ "inline": ["text"],
+ "inline2": ["balance_pairs", "text_collapse"],
+ }
+
+
+def test_parseInline():
+ md = MarkdownIt()
+ tokens = md.parseInline("abc\n\n> xyz")
+ assert tokens == [
+ Token(
+ type="inline",
+ tag="",
+ nesting=0,
+ attrs=None,
+ map=[0, 1],
+ level=0,
+ children=[
+ Token(
+ type="text",
+ tag="",
+ nesting=0,
+ attrs=None,
+ map=None,
+ level=0,
+ children=None,
+ content="abc",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ ),
+ Token(
+ type="softbreak",
+ tag="br",
+ nesting=0,
+ attrs=None,
+ map=None,
+ level=0,
+ children=None,
+ content="",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ ),
+ Token(
+ type="softbreak",
+ tag="br",
+ nesting=0,
+ attrs=None,
+ map=None,
+ level=0,
+ children=None,
+ content="",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ ),
+ Token(
+ type="text",
+ tag="",
+ nesting=0,
+ attrs=None,
+ map=None,
+ level=0,
+ children=None,
+ content="> xyz",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ ),
+ ],
+ content="abc\n\n> xyz",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ )
+ ]
+
+
+def test_renderInline():
+ md = MarkdownIt("zero")
+ tokens = md.renderInline("abc\n\n*xyz*")
+ assert tokens == "abc\n\n*xyz*"
+
+
+def test_emptyStr():
+ md = MarkdownIt()
+ tokens = md.parseInline("")
+ assert tokens == [
+ Token(
+ type="inline",
+ tag="",
+ nesting=0,
+ attrs=None,
+ map=[0, 1],
+ level=0,
+ children=[],
+ content="",
+ markup="",
+ info="",
+ meta={},
+ block=False,
+ hidden=False,
+ )
+ ]
+
+
+def test_empty_env():
+ """Test that an empty `env` is mutated, not copied and mutated."""
+ md = MarkdownIt()
+
+ env = {}
+ md.render("[foo]: /url\n[foo]", env)
+ assert "references" in env
+
+ env = {}
+ md.parse("[foo]: /url\n[foo]", env)
+ assert "references" in env
+
+
+def test_table_tokens(data_regression):
+ md = MarkdownIt("js-default")
+ tokens = md.parse(
+ """
+| Heading 1 | Heading 2
+| --------- | ---------
+| Cell 1 | Cell 2
+| Cell 3 | Cell 4
+ """
+ )
+ data_regression.check([t.as_dict() for t in tokens])
diff --git a/tests/test_api/test_main/test_table_tokens.yml b/tests/test_api/test_main/test_table_tokens.yml
new file mode 100644
index 0000000..5bac044
--- /dev/null
+++ b/tests/test_api/test_main/test_table_tokens.yml
@@ -0,0 +1,492 @@
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 0
+ map:
+ - 1
+ - 5
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: table
+ type: table_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 1
+ map:
+ - 1
+ - 2
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: thead
+ type: thead_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map:
+ - 1
+ - 2
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: tr
+ type: tr_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: th
+ type: th_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Heading 1
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Heading 1
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 1
+ - 2
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: th
+ type: th_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: th
+ type: th_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Heading 2
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Heading 2
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 1
+ - 2
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: th
+ type: th_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: tr
+ type: tr_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 1
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: thead
+ type: thead_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 1
+ map:
+ - 3
+ - 5
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: tbody
+ type: tbody_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map:
+ - 3
+ - 4
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: tr
+ type: tr_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: td
+ type: td_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Cell 1
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Cell 1
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 3
+ - 4
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: td
+ type: td_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: td
+ type: td_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Cell 2
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Cell 2
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 3
+ - 4
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: td
+ type: td_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: tr
+ type: tr_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map:
+ - 4
+ - 5
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: tr
+ type: tr_open
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: td
+ type: td_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Cell 3
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Cell 3
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 4
+ - 5
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: td
+ type: td_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 1
+ tag: td
+ type: td_open
+- attrs: null
+ block: true
+ children:
+ - attrs: null
+ block: false
+ children: null
+ content: Cell 4
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: text
+ content: Cell 4
+ hidden: false
+ info: ''
+ level: 4
+ map:
+ - 4
+ - 5
+ markup: ''
+ meta: {}
+ nesting: 0
+ tag: ''
+ type: inline
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 3
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: td
+ type: td_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 2
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: tr
+ type: tr_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 1
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: tbody
+ type: tbody_close
+- attrs: null
+ block: true
+ children: null
+ content: ''
+ hidden: false
+ info: ''
+ level: 0
+ map: null
+ markup: ''
+ meta: {}
+ nesting: -1
+ tag: table
+ type: table_close
diff --git a/tests/test_api/test_plugin_creation.py b/tests/test_api/test_plugin_creation.py
new file mode 100644
index 0000000..3a9af8b
--- /dev/null
+++ b/tests/test_api/test_plugin_creation.py
@@ -0,0 +1,88 @@
+"""Test basic plugin creation functionality:
+that they can be added and are called correctly
+"""
+from markdown_it import MarkdownIt
+
+
+def inline_rule(state, silent):
+ print("plugin called")
+
+
+def test_inline_after(capsys):
+ def _plugin(_md):
+ _md.inline.ruler.after("text", "new_rule", inline_rule)
+
+ MarkdownIt().use(_plugin).parse("[")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_inline_before(capsys):
+ def _plugin(_md):
+ _md.inline.ruler.before("text", "new_rule", inline_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_inline_at(capsys):
+ def _plugin(_md):
+ _md.inline.ruler.at("text", inline_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def block_rule(state, startLine, endLine, silent):
+ print("plugin called")
+
+
+def test_block_after(capsys):
+ def _plugin(_md):
+ _md.block.ruler.after("hr", "new_rule", block_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_block_before(capsys):
+ def _plugin(_md):
+ _md.block.ruler.before("hr", "new_rule", block_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_block_at(capsys):
+ def _plugin(_md):
+ _md.block.ruler.at("hr", block_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def core_rule(state):
+ print("plugin called")
+
+
+def test_core_after(capsys):
+ def _plugin(_md):
+ _md.core.ruler.after("normalize", "new_rule", core_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_core_before(capsys):
+ def _plugin(_md):
+ _md.core.ruler.before("normalize", "new_rule", core_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
+
+
+def test_core_at(capsys):
+ def _plugin(_md):
+ _md.core.ruler.at("normalize", core_rule)
+
+ MarkdownIt().use(_plugin).parse("a")
+ assert "plugin called" in capsys.readouterr().out
diff --git a/tests/test_api/test_token.py b/tests/test_api/test_token.py
new file mode 100644
index 0000000..e3806b5
--- /dev/null
+++ b/tests/test_api/test_token.py
@@ -0,0 +1,38 @@
+import warnings
+
+from markdown_it.token import Token
+
+
+def test_token():
+ token = Token("name", "tag", 0)
+ assert token.as_dict() == {
+ "type": "name",
+ "tag": "tag",
+ "nesting": 0,
+ "attrs": None,
+ "map": None,
+ "level": 0,
+ "children": None,
+ "content": "",
+ "markup": "",
+ "info": "",
+ "meta": {},
+ "block": False,
+ "hidden": False,
+ }
+ token.attrSet("a", "b")
+ assert token.attrGet("a") == "b"
+ token.attrJoin("a", "c")
+ assert token.attrGet("a") == "b c"
+ token.attrPush(["x", "y"])
+ assert token.attrGet("x") == "y"
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ assert token.attrIndex("a") == 0
+ assert token.attrIndex("x") == 1
+ assert token.attrIndex("j") == -1
+
+
+def test_serialization():
+ token = Token("name", "tag", 0, children=[Token("other", "tag2", 0)])
+ assert token == Token.from_dict(token.as_dict())