File: test_markdown.py

package info (click to toggle)
blag 2.3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: python: 1,037; makefile: 71
file content (113 lines) | stat: -rw-r--r-- 3,198 bytes parent folder | download
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
"""Test markdown module."""

from datetime import datetime
from typing import Any

import markdown
import pytest

from blag.markdown import convert_markdown, markdown_factory


@pytest.mark.parametrize(
    "input_, expected",
    [
        # inline
        ("[test](test.md)", "test.html"),
        ('[test](test.md "test")', "test.html"),
        ("[test](a/test.md)", "a/test.html"),
        ('[test](a/test.md "test")', "a/test.html"),
        ("[test](/test.md)", "/test.html"),
        ('[test](/test.md "test")', "/test.html"),
        ("[test](/a/test.md)", "/a/test.html"),
        ('[test](/a/test.md "test")', "/a/test.html"),
        # reference
        ("[test][]\n[test]: test.md " "", "test.html"),
        ('[test][]\n[test]: test.md "test"', "test.html"),
        ("[test][]\n[test]: a/test.md", "a/test.html"),
        ('[test][]\n[test]: a/test.md "test"', "a/test.html"),
        ("[test][]\n[test]: /test.md", "/test.html"),
        ('[test][]\n[test]: /test.md "test"', "/test.html"),
        ("[test][]\n[test]: /a/test.md", "/a/test.html"),
        ('[test][]\n[test]: /a/test.md "test"', "/a/test.html"),
    ],
)
def test_convert_markdown_links(input_: str, expected: str) -> None:
    """Test convert_markdown."""
    md = markdown_factory()
    html, _ = convert_markdown(md, input_)
    assert expected in html


@pytest.mark.parametrize(
    "input_, expected",
    [
        # scheme
        ("[test](https://)", "https://"),
        # netloc
        ("[test](//test.md)", "//test.md"),
        # no path
        ("[test]()", ""),
    ],
)
def test_dont_convert_normal_links(input_: str, expected: str) -> None:
    """Test convert_markdown doesn't convert normal links."""
    md = markdown_factory()
    html, _ = convert_markdown(md, input_)
    assert expected in html


@pytest.mark.parametrize(
    "input_, expected",
    [
        ("foo: bar", {"foo": "bar"}),
        ("foo: those are several words", {"foo": "those are several words"}),
        ("tags: this, is, a, test\n", {"tags": ["this", "is", "a", "test"]}),
        ("tags: this, IS, a, test", {"tags": ["this", "is", "a", "test"]}),
        (
            "date: 2020-01-01 12:10",
            {"date": datetime(2020, 1, 1, 12, 10).astimezone()},
        ),
    ],
)
def test_convert_metadata(input_: str, expected: dict[str, Any]) -> None:
    """Test convert_markdown converts metadata correctly."""
    md = markdown_factory()
    _, meta = convert_markdown(md, input_)
    assert expected == meta


def test_markdown_factory() -> None:
    """Test markdown_factory."""
    md = markdown_factory()
    assert isinstance(md, markdown.Markdown)


def test_smarty() -> None:
    """Test smarty."""
    md = markdown_factory()

    md1 = """

this --- is -- a test ...

    """
    html, meta = convert_markdown(md, md1)
    assert "mdash" in html
    assert "ndash" in html
    assert "hellip" in html


def test_smarty_code() -> None:
    """Test smarty doesn't touch code."""
    md = markdown_factory()

    md1 = """
```
this --- is -- a test ...
```
    """
    html, meta = convert_markdown(md, md1)
    assert "mdash" not in html
    assert "ndash" not in html
    assert "hellip" not in html