forked from vemel/handsdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_md_document.py
More file actions
87 lines (71 loc) · 3.23 KB
/
Copy pathtest_md_document.py
File metadata and controls
87 lines (71 loc) · 3.23 KB
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
from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
from handsdown.md_document import MDDocument
class TestMDDocument:
def test_init(self):
with NamedTemporaryFile(mode="w+", encoding="utf-8") as temp_f:
temp_f.write(
"# my title\n\nsubtitle\n\n\n\nsubtitle2\n\n- [TOC](#toc)\n-"
" [TOC2](#toc2)\n\n## my title 2\n\nsome content\nnew line"
)
temp_f.flush()
md_doc = MDDocument(Path(temp_f.name))
md_doc.read(Path(temp_f.name))
assert md_doc.path == Path(temp_f.name)
assert md_doc.title == "my title"
assert md_doc.subtitle == "subtitle\n\nsubtitle2"
assert md_doc.toc_section == "- [TOC](#toc)\n- [TOC2](#toc2)"
assert md_doc.sections[0] == "## my title 2"
assert md_doc.sections[1] == "some content\nnew line"
md_doc.subtitle = "my subtitle"
assert md_doc.subtitle == "my subtitle"
with NamedTemporaryFile(mode="w+", encoding="utf-8") as temp_f:
temp_f.write(
"# my title\n\n- [TOC](#toc)\n- [TOC2](#toc2)\n\n\nsome content"
)
temp_f.flush()
md_doc = MDDocument(Path(temp_f.name))
md_doc.read(Path(temp_f.name))
assert md_doc.subtitle == "some content"
def test_context_manager(self):
with NamedTemporaryFile(mode="w+", encoding="utf-8") as temp_f:
with MDDocument(Path(temp_f.name)) as md_doc:
md_doc.title = "test"
assert temp_f.read() == "# test\n"
with pytest.raises(ValueError), MDDocument(Path(temp_f.name)):
raise ValueError("test")
def test_append(self):
md_doc = MDDocument(Path("/test.md"))
md_doc.append("subtitle")
md_doc.append("test")
md_doc.append("")
assert md_doc.subtitle == "subtitle"
assert md_doc.sections[0] == "test"
def test_get_anchor(self):
assert MDDocument.get_anchor("s T_e-s%t") == "s-t_e-st"
assert MDDocument.get_anchor("test") == "test"
def test_render_doc_link(self):
md_doc = MDDocument(Path("/root/test.md"))
assert md_doc.render_doc_link("title", anchor="tag") == "[title](#tag)"
assert (
md_doc.render_doc_link("title", anchor="tag", target_path=Path("/root/test.md"))
== "[title](#tag)"
)
assert (
md_doc.render_doc_link("title", anchor="tag", target_path=Path("/root/test2.md"))
== "[title](./test2.md#tag)"
)
assert md_doc.render_doc_link("title", target_path=Path("/root/test.md")) == "[title]()"
assert (
md_doc.render_doc_link("title", target_path=Path("/root/test2.md"))
== "[title](./test2.md)"
)
assert md_doc.render_doc_link("title") == "[title]()"
def test_render_link(self):
assert MDDocument.render_link("title", link="#tag") == "[title](#tag)"
assert MDDocument.render_link("title", link="") == "[title]()"
def test_is_toc(self):
assert MDDocument.is_toc("- [TOC](#toc)\n- [TOC2](#toc2)")
assert not MDDocument.is_toc("- [TOC](#toc)\n- [TOC2](#toc2)\nTOC3")
assert not MDDocument.is_toc("- [TOC](#toc)\n")