~ferruck/yafg

ref: 54e6ba18105effd3f9f131b3f56a5c5b4dfb16f0 yafg/test/test_yafg.py -rw-r--r-- 7.3 KiB
54e6ba18 — Philipp Trommler Refactor 7 months ago
                                                                                
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# yafg: Yet Another Figure Generator
#
# Copyright (c) 2019 Philipp Trommler
#
# SPDX-License-Identifier: GPL-3.0-or-later
import markdown
import unittest

import yafg

class YafgTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_empty_input(self):
        inString = ""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension()])
        self.assertEqual(inString, outString)

    def test_no_images(self):
        inString = """\
This is a test text.

It contains multiple paragraphs as well as [links](https://example.com).

* Itemize
* is
* used,
* as well.

# This is a headline.

[This is a link without an image in it](https://example.com)

Nothing should change here whilst using yafg."""
        expectedString = markdown.markdown(inString)
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension()])
        self.assertEqual(expectedString, outString)

    def test_simple_image(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension()])
        self.assertEqual(expectedString, outString)

    def test_multiline_alt(self):
        inString = """\
![This is a rather long alt text that spans multiple lines. This may be
necessary to describe a picture for the blind.](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="This is a rather long alt text that spans multiple lines. This may be
necessary to describe a picture for the blind." src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension()])
        self.assertEqual(expectedString, outString)

    def test_multiline_title(self):
        inString = """\
![alt text](/path/to/image.png "This is a very long title. It is used to give
the readers a good figcaption. It may contain a description of the image as well
as sources.")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="This is a very long title. It is used to give
the readers a good figcaption. It may contain a description of the image as well
as sources." />
<figcaption>This is a very long title. It is used to give
the readers a good figcaption. It may contain a description of the image as well
as sources.</figcaption>
</figure>"""

    def test_strip_title(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(stripTitle=True)])
        self.assertEqual(expectedString, outString)

    def test_figure_class(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure class="testclass" id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figureClass="testclass")])
        self.assertEqual(expectedString, outString)

    def test_figcaption_class(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption class="testclass">Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figcaptionClass="testclass")])
        self.assertEqual(expectedString, outString)

    def test_figure_numbering(self):
        inString = """\
![alt text](/path/to/image.png "Title")

This is a paragraph without image.

![alt text 2](/path/to/image2.png "Title 2")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span>Figure&nbsp;1:</span> Title</figcaption>
</figure>
<p>This is a paragraph without image.</p>
<figure id="__yafg-figure-2">
<img alt="alt text 2" src="/path/to/image2.png" title="Title 2" />
<figcaption><span>Figure&nbsp;2:</span> Title 2</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figureNumbering=True)])
        self.assertEqual(expectedString, outString)

    def test_figure_number_class(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span class="testclass">Figure&nbsp;1:</span> Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figureNumbering=True, figureNumberClass="testclass")])
        self.assertEqual(expectedString, outString)

    def test_figure_number_text(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span>Abbildung&nbsp;1:</span> Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figureNumbering=True, figureNumberText="Abbildung")])
        self.assertEqual(expectedString, outString)

    def test_attribute_preservation(self):
        inString = """\
![alt text](/path/to/image.png "Title"){: #someid .someclass somekey='some value' }"""
        expectedString = """\
<figure id="__yafg-figure-1">
<img alt="alt text" class="someclass" id="someid" somekey="some value" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = ["attr_list", yafg.YafgExtension()])
        self.assertEqual(expectedString, outString)

    def test_image_in_link(self):
        inString = """\
[![alt text](/path/to/image.png "Title")](/path/to/link.html)"""
        expectedString = """\
<figure id="__yafg-figure-1">
<a href="/path/to/link.html"><img alt="alt text" src="/path/to/image.png" title="Title" /></a>
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension()])
        self.assertEqual(expectedString, outString)

    def test_combined_options(self):
        inString = """\
[![alt text](/path/to/image.png "Title"){: #someid .someclass somekey='some value' }](/path/to/link.html)"""
        expectedString = """\
<figure class="testclass1" id="__yafg-figure-1">
<a href="/path/to/link.html"><img alt="alt text" class="someclass" id="someid" somekey="some value" src="/path/to/image.png" /></a>
<figcaption class="testclass2"><span class="testclass3">Abbildung&nbsp;1:</span> Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = ["attr_list", yafg.YafgExtension(stripTitle=True, figureClass="testclass1", figcaptionClass="testclass2", figureNumbering=True, figureNumberClass="testclass3", figureNumberText="Abbildung")])
        self.assertEqual(expectedString, outString)