summaryrefslogtreecommitdiff
path: root/creole/tests/test_cross_compare_all.py
blob: c09b550118a5948397ab28a9b527f0897b7f67e2 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568


"""
    cross compare unittest
    ~~~~~~~~~~~~~~~~~~~~~~

    Compare all similarities between:
        * creole2html
        * html2creole
        * textile2html (used the python textile module)
        * html2textile

    Note: This only works fine if there is no problematic whitespace handling.
        In this case, we must test in test_creole2html.py or test_html2creole.py

    :copyleft: 2008-2014 by python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""


import unittest

from creole.tests.utils.base_unittest import BaseCreoleTest


class CrossCompareTests(BaseCreoleTest):
    """
    Cross compare tests for creol2html _and_ html2creole with the same test
    strings. Used BaseCreoleTest.assertCreole()
    """

    def test_bold_italics(self):
        self.cross_compare(
            creole_string=r"""
                **bold** //italics//
                //italics and **bold**.//
                **bold and //italics//.**
            """,
            html_string="""
                <p><strong>bold</strong> <i>italics</i><br />
                <i>italics and <strong>bold</strong>.</i><br />
                <strong>bold and <i>italics</i>.</strong></p>
            """,
        )
        self.cross_compare(
            textile_string="""
                *bold* __italics__
                __italics and *bold*.__
                *bold and __italics__.*
            """,
            html_string="""
                <p><strong>bold</strong> <i>italics</i><br />

                <i>italics and <strong>bold</strong>.</i><br />

                <strong>bold and <i>italics</i>.</strong></p>
            """,
        )
        # Note: In ReSt inline markup may not be nested.
        self.cross_compare(
            html_string="""
                <p><strong>bold</strong> <em>italics</em></p>
            """,
            rest_string="""
                **bold** *italics*
            """,
        )

    def test_bold_italics2(self):
        self.cross_compare(
            creole_string=r"""
                **//bold italics//**
                //**bold italics**//
                //This is **also** good.//
            """,
            html_string="""
                <p><strong><i>bold italics</i></strong><br />
                <i><strong>bold italics</strong></i><br />
                <i>This is <strong>also</strong> good.</i></p>
            """,
        )
        self.cross_compare(
            textile_string="""
                *__bold italics__*
                __*bold italics*__
                __This is *also* good.__
            """,
            html_string="""
                <p><strong><i>bold italics</i></strong><br />

                <i><strong>bold italics</strong></i><br />

                <i>This is <strong>also</strong> good.</i></p>
            """,
        )

    def test_headlines1(self):
        self.cross_compare(
            creole_string=r"""
                = Section Title 1

                == Section Title 2

                === Section Title 3

                ==== Section Title 4

                ===== Section Title 5

                ====== Section Title 6
            """,
            textile_string="""
                h1. Section Title 1

                h2. Section Title 2

                h3. Section Title 3

                h4. Section Title 4

                h5. Section Title 5

                h6. Section Title 6
            """,
            html_string="""
                <h1>Section Title 1</h1>

                <h2>Section Title 2</h2>

                <h3>Section Title 3</h3>

                <h4>Section Title 4</h4>

                <h5>Section Title 5</h5>

                <h6>Section Title 6</h6>
            """
        )
        self.cross_compare(
            rest_string="""
                ===============
                Section Title 1
                ===============

                ---------------
                Section Title 2
                ---------------

                Section Title 3
                ===============

                Section Title 4
                ---------------

                Section Title 5
                ```````````````

                Section Title 6
                '''''''''''''''
            """,
            html_string="""
                <h1>Section Title 1</h1>
                <h2>Section Title 2</h2>
                <h3>Section Title 3</h3>
                <h4>Section Title 4</h4>
                <h5>Section Title 5</h5>
                <h6>Section Title 6</h6>
            """
        )

    def test_horizontal_rule(self):
        all_markups = """
            Text before horizontal rule.

            ----

            Text after the line.
        """
        self.cross_compare(
            creole_string=all_markups,
            # textile_string=all_markups, # FIXME: textile and <hr> ?
            html_string="""
                <p>Text before horizontal rule.</p>

                <hr />

                <p>Text after the line.</p>
            """
        )
        self.cross_compare(
            rest_string=all_markups,
            html_string="""
                <p>Text before horizontal rule.</p>
                <hr />
                <p>Text after the line.</p>
            """
        )

    def test_link(self):
        self.cross_compare(
            creole_string=r"""
                X [[http://domain.tld|link B]] test.
            """,
            textile_string="""
                X "link B":http://domain.tld test.
            """,
            rest_string="""
                X `link B <http://domain.tld>`_ test.
            """,
            html_string="""
                <p>X <a href="http://domain.tld">link B</a> test.</p>
            """
        )

    def test_link_without_title(self):
        self.cross_compare(
            creole_string=r"""
                [[http://www.pylucid.org]]
            """,
            textile_string="""
                "http://www.pylucid.org":http://www.pylucid.org
            """,
            rest_string="""
                `http://www.pylucid.org <http://www.pylucid.org>`_
            """,
            html_string="""
                <p><a href="http://www.pylucid.org">http://www.pylucid.org</a></p>
            """
        )

    def test_link_with_unknown_protocol(self):
        self.cross_compare(
            creole_string=r"""
                X [[foo://bar|unknown protocol]] Y
            """,
            # textile will return '#' if url scheme is unknown!
            # textile_string="""
            #     X "unknown protocol":foo://bar Y
            # """,
            rest_string="""
                X `unknown protocol <foo://bar>`_ Y
            """,
            html_string="""
                <p>X <a href="foo://bar">unknown protocol</a> Y</p>
            """
        )

    def test_link_with_at_sign(self):
        self.cross_compare(
            creole_string=r"""
                X [[http://de.wikipedia.org/wiki/Creole_(Markup)|Creole@wikipedia]]
            """,
            html_string="""
                <p>X <a href="http://de.wikipedia.org/wiki/Creole_(Markup)">Creole@wikipedia</a></p>
            """
        )
        self.cross_compare(
            rest_string="""
                X `Creole@wikipedia <http://de.wikipedia.org/wiki/Creole_(Markup)>`_
            """,
            html_string="""
                <p>X <a href="http://de.wikipedia.org/wiki/Creole_(Markup)">Creole&#64;wikipedia</a></p>
            """
        )
        self.cross_compare_textile(
            textile_string="""
                X "foo@domain":http://domain.tld
            """,
            html_string="""
                <p>X <a href="http://domain.tld">foo@domain</a></p>
            """
        )

    def test_image(self):
        self.cross_compare(
            creole_string=r"""
                a {{/image.jpg|JPG pictures}} and
                a {{/image.jpeg|JPEG pictures}} and
                a {{/image.gif|GIF pictures}} and
                a {{/image.png|PNG pictures}} !
                {{/path1/path2/image|Image without files ext?}}
            """,
            html_string="""
                <p>a <img src="/image.jpg" title="JPG pictures" alt="JPG pictures" /> and<br />
                a <img src="/image.jpeg" title="JPEG pictures" alt="JPEG pictures" /> and<br />
                a <img src="/image.gif" title="GIF pictures" alt="GIF pictures" /> and<br />
                a <img src="/image.png" title="PNG pictures" alt="PNG pictures" /> !<br />
                <img src="/path1/path2/image" title="Image without files ext?" alt="Image without files ext?" /></p>
            """
        )
        self.cross_compare(
            textile_string="""
                a !/image.jpg(JPG pictures)! and
                a !/image.jpeg(JPEG pictures)! and
                a !/image.gif(GIF pictures)! and
                a !/image.png(PNG pictures)! !
                !/path1/path2/image(Image without files ext?)!
            """,
            html_string="""
                <p>a <img alt="JPG pictures" src="/image.jpg" title="JPG pictures" /> and<br />

                a <img alt="JPEG pictures" src="/image.jpeg" title="JPEG pictures" /> and<br />

                a <img alt="GIF pictures" src="/image.gif" title="GIF pictures" /> and<br />

                a <img alt="PNG pictures" src="/image.png" title="PNG pictures" /> !<br />

                <img alt="Image without files ext?" src="/path1/path2/image" title="Image without files ext?" /></p>
            """
        )
        self.cross_compare(
            rest_string="""
                1 |JPG pictures| one

                .. |JPG pictures| image:: /image.jpg

                2 |JPEG pictures| two

                .. |JPEG pictures| image:: /image.jpeg

                3 |GIF pictures| tree

                .. |GIF pictures| image:: /image.gif

                4 |PNG pictures| four

                .. |PNG pictures| image:: /image.png

                5 |Image without files ext?| five

                .. |Image without files ext?| image:: /path1/path2/image
            """,
            html_string="""
                <p>1 <img alt="JPG pictures" src="/image.jpg" /> one</p>
                <p>2 <img alt="JPEG pictures" src="/image.jpeg" /> two</p>
                <p>3 <img alt="GIF pictures" src="/image.gif" /> tree</p>
                <p>4 <img alt="PNG pictures" src="/image.png" /> four</p>
                <p>5 <img alt="Image without files ext?" src="/path1/path2/image" /> five</p>
            """
        )

    def test_link_image(self):
        """ FIXME: ReSt. and linked images """
        self.cross_compare(
            creole_string=r"""
                Linked [[http://example.com/|{{myimage.jpg|example site}} image]]
            """,
            html_string="""
                <p>Linked <a href="http://example.com/"><img src="myimage.jpg" title="example site" alt="example site" /> image</a></p>
            """
        )
        self.cross_compare(
            textile_string="""
                Linked "!myimage.jpg(example site)! image":http://example.com/
            """,
            html_string="""
                <p>Linked <a href="http://example.com/"><img alt="example site" src="myimage.jpg" title="example site" /> image</a></p>
            """
        )


#        self.cross_compare(# FIXME: ReSt
#            rest_string="""
#                I recommend you try |PyLucid CMS|_.
#
#                .. |PyLucid CMS| image:: /images/pylucid.png
#                .. _PyLucid CMS: http://www.pylucid.org/
#            """,
#            html_string="""
#                <p>I recommend you try <a href="http://www.pylucid.org/"><img alt="PyLucid CMS" src="/images/pylucid.png" /></a>.</p>
#            """
#        )

    def test_pre1(self):
        self.cross_compare(
            creole_string=r"""
                {{{
                * no list
                }}}
                """,
            textile_string="""
                <pre>
                * no list
                </pre>
                """,
            html_string="""
                <pre>
                * no list
                </pre>
            """)
        self.cross_compare(  # FIXME: Not the best html2rest output
            rest_string="""
                Preformatting text:

                ::

                    Here some performatting with
                    no `link <http://domain.tld>`_ here.
                    text... end.

                Under pre block
            """,
            html_string="""
                <p>Preformatting text:</p>
                <pre>
                Here some performatting with
                no `link &lt;http://domain.tld&gt;`_ here.
                text... end.
                </pre>
                <p>Under pre block</p>
            """
        )


#    def test_pre2(self):
#        """ TODO: html2creole: wrong lineendings """
#        self.cross_compare(
#            creole_string=r"""
#                start
#
#                {{{
#                * no list
#                }}}
#
#                end
#                """,
#            textile_string="""
#                start
#
#                <pre>
#                * no list
#                </pre>
#
#                end
#                """,
#            html_string="""
#                <p>start</p>
#
#                <pre>
#                * no list
#                </pre>
#
#                <p>end</p>
#            """)

    def test_pre_contains_braces(self):
        self.cross_compare(
            creole_string="""
                {{{
                # Closing braces in nowiki:
                if (x != NULL) {
                  for (i = 0) {
                    if (x = 1) {
                      x[i]--;
                  }}}
                }}}
                """,
            textile_string="""
                <pre>
                # Closing braces in nowiki:
                if (x != NULL) {
                  for (i = 0) {
                    if (x = 1) {
                      x[i]--;
                  }}}
                </pre>
                """,
            rest_string="""
                ::

                    # Closing braces in nowiki:
                    if (x != NULL) {
                      for (i = 0) {
                        if (x = 1) {
                          x[i]--;
                      }}}
                """,
            html_string="""
                <pre>
                # Closing braces in nowiki:
                if (x != NULL) {
                  for (i = 0) {
                    if (x = 1) {
                      x[i]--;
                  }}}
                </pre>
            """)

    def test_list(self):
        """ Bold, Italics, Links, Pre in Lists """
        self.cross_compare(
            creole_string=r"""
                * **bold** item
                * //italic// item

                # item about a [[/foo/bar|certain_page]]
                """,
            textile_string="""
                * *bold* item
                * __italic__ item

                # item about a "certain_page":/foo/bar
            """,
            html_string="""
                <ul>
                  <li><strong>bold</strong> item</li>
                  <li><i>italic</i> item</li>
                </ul>
                <ol>
                  <li>item about a <a href="/foo/bar">certain_page</a></li>
                </ol>
            """,
            strip_lines=True
        )

    def test_simple_table(self):
        self.cross_compare(
            creole_string="""
                |= Headline 1 |= Headline 2 |
                | cell one    | cell two    |
                """,
            textile_string="""
                |_. Headline 1|_. Headline 2|
                |cell one|cell two|
                """,
            html_string="""
                <table>
                <tr>
                    <th>Headline 1</th>
                    <th>Headline 2</th>
                </tr>
                <tr>
                    <td>cell one</td>
                    <td>cell two</td>
                </tr>
                </table>
            """,
            # debug=True
            strip_lines=True,
        )
        self.cross_compare(
            rest_string="""
                +------------+------------+
                | Headline 1 | Headline 2 |
                +============+============+
                | cell one   | cell two   |
                +------------+------------+
                """,
            html_string="""
                <table>
                <colgroup>
                <col width="50%" />
                <col width="50%" />
                </colgroup>
                <tr><th>Headline 1</th>
                <th>Headline 2</th>
                </tr>
                <tr><td>cell one</td>
                <td>cell two</td>
                </tr>
                </table>
            """,
            # debug=True
        )


if __name__ == '__main__':
    unittest.main()