1212from mdformat .renderer import MDRenderer , RenderContext , RenderTreeNode
1313
1414
15- def example_formatter (code , info ):
16- return "dummy\n "
17-
18-
1915def test_code_formatter (monkeypatch ):
20- monkeypatch .setitem (CODEFORMATTERS , "lang" , example_formatter )
16+ def fmt_func (code , info ):
17+ return "dummy\n "
18+
19+ monkeypatch .setitem (CODEFORMATTERS , "lang" , fmt_func )
2120 text = mdformat .text (
2221 dedent (
2322 """\
@@ -37,6 +36,82 @@ def test_code_formatter(monkeypatch):
3736 )
3837
3938
39+ def test_code_formatter__empty_str (monkeypatch ):
40+ def fmt_func (code , info ):
41+ return ""
42+
43+ monkeypatch .setitem (CODEFORMATTERS , "lang" , fmt_func )
44+ text = mdformat .text (
45+ dedent (
46+ """\
47+ ~~~lang
48+ aag
49+ gw
50+ ~~~
51+ """
52+ ),
53+ codeformatters = {"lang" },
54+ )
55+ assert text == dedent (
56+ """\
57+ ```lang
58+ ```
59+ """
60+ )
61+
62+
63+ def test_code_formatter__no_end_newline (monkeypatch ):
64+ def fmt_func (code , info ):
65+ return "dummy\n dum"
66+
67+ monkeypatch .setitem (CODEFORMATTERS , "lang" , fmt_func )
68+ text = mdformat .text (
69+ dedent (
70+ """\
71+ ```lang
72+ ```
73+ """
74+ ),
75+ codeformatters = {"lang" },
76+ )
77+ assert text == dedent (
78+ """\
79+ ```lang
80+ dummy
81+ dum
82+ ```
83+ """
84+ )
85+
86+
87+ def test_code_formatter__interface (monkeypatch ):
88+ def fmt_func (code , info ):
89+ return info + code * 2
90+
91+ monkeypatch .setitem (CODEFORMATTERS , "lang" , fmt_func )
92+ text = mdformat .text (
93+ dedent (
94+ """\
95+ ``` lang long
96+ multi
97+ mul
98+ ```
99+ """
100+ ),
101+ codeformatters = {"lang" },
102+ )
103+ assert text == dedent (
104+ """\
105+ ```lang long
106+ lang longmulti
107+ mul
108+ multi
109+ mul
110+ ```
111+ """
112+ )
113+
114+
40115class TextEditorPlugin :
41116 """A plugin that makes all text the same."""
42117
0 commit comments