summaryrefslogtreecommitdiff
path: root/creole/tests/test_creole2html.py
blob: d938a38421c9c73786e655edeca9c76827e5aa90 (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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

"""
    creole2html unittest
    ~~~~~~~~~~~~~~~~~~~~

    Here are only some tests witch doesn't work in the cross compare tests.

    Info: There exist some situations with different whitespace handling
        between creol2html and html2creole.

    Test the creole markup.

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


import sys
import unittest
from io import StringIO

from creole import creole2html
from creole.shared import example_macros
from creole.shared.utils import dict2string, string2dict
from creole.tests import test_macros
from creole.tests.utils.base_unittest import BaseCreoleTest


try:
    import pygments  # noqa flake8
    PYGMENTS = True
except ImportError:
    PYGMENTS = False


class TestCreole2html(BaseCreoleTest):
    """
    Tests around creole2html API and macro function.
    """

    def setUp(self):
        # For fallback tests
        example_macros.PYGMENTS = PYGMENTS

    def test_stderr(self):
        """
        Test if the traceback information send to a stderr handler.
        """
        my_stderr = StringIO()
        creole2html(
            markup_string="<<notexist1>><<notexist2>><</notexist2>>",
            emitter_kwargs={
                "verbose": 2,
                "stderr": my_stderr,
            }
        )
        error_msg = my_stderr.getvalue()

        # Note:
        # The error message change if macros are a dict or are a object!

        # Check if we get a traceback information into our stderr handler
        must_have = (
            "Traceback", "'notexist1'", "'notexist2'",
        )
        for part in must_have:
            tb_lines = [" -" * 40]
            tb_lines += error_msg.splitlines()
            tb_lines += [" -" * 40]
            tb = "\n".join([" >>> %s" % l for l in tb_lines])
            msg = f"{part!r} not found in:\n{tb}"
            # TODO: use assertIn if python 2.6 will be not support anymore.
            if part not in error_msg:
                raise self.failureException(msg)

    def test_example_macros1(self):
        """
        Test the default "html" macro, found in ./creole/default_macros.py
        """
        html = creole2html(
            markup_string="<<html>><p>foo</p><</html>><bar?>",
            emitter_kwargs={
                "verbose": 1,
                "macros": example_macros,
                "stderr": sys.stderr,
            }
        )
        self.assertEqual(html, '<p>foo</p>\n<p>&lt;bar?&gt;</p>')

    def test_example_macros2(self):
        html = creole2html(
            markup_string="<<html>>{{{&lt;nocode&gt;}}}<</html>>",
            emitter_kwargs={
                "verbose": 1,
                "macros": example_macros,
                "stderr": sys.stderr,
            }
        )
        self.assertEqual(html, '{{{&lt;nocode&gt;}}}')

    def test_example_macros3(self):
        html = creole2html(
            markup_string="<<html>>1<</html>><<html>>2<</html>>",
            emitter_kwargs={
                "verbose": 1,
                "macros": example_macros,
                "stderr": sys.stderr,
            }
        )
        self.assertEqual(html, '1\n2')

    def test_macro_dict(self):
        """
        simple test for the "macro API"
        """
        def test(text, foo, bar):
            return "|".join([foo, bar, text])

        html = creole2html(
            markup_string="<<test bar='b' foo='a'>>c<</test>>",
            emitter_kwargs={
                "verbose": 1,
                "macros": {"test": test},
                "stderr": sys.stderr,
            }
        )
        self.assertEqual(html, 'a|b|c')

    def test_macro_callable(self):
        """
        simple test for the "macro API"
        """
        def testmacro():
            pass

        self.assertRaises(TypeError,
                          creole2html,
                          markup_string="<<test no=1 arg2='foo'>>bar<</test>>",
                          emitter_kwargs={
                              "verbose": 1,
                              "macros": testmacro,
                              "stderr": sys.stderr,
                          }
                          )

    def test_macro_wrong_arguments_with_error_report(self):
        """
        simple test for the "macro API"
        """
        def test(text, foo):
            pass
        my_stderr = StringIO()

        html = creole2html(
            markup_string="<<test bar='foo'>>c<</test>>",
            emitter_kwargs={
                "verbose": 2,
                "macros": {"test": test},
                "stderr": my_stderr,
            }
        )
        self.assertEqual(html,
                         "[Error: Macro 'test' error: test() got an unexpected keyword argument 'bar']"
                         )
        error_msg = my_stderr.getvalue()

        # Check traceback information into our stderr handler
        must_have = (
            "TypeError: test() got an unexpected keyword argument 'bar'",
            "sourceline: 'def test(text, foo):' from",
            "tests/test_creole2html.py",
        )
        for part in must_have:
            self.assertIn(part, error_msg)

    def test_macro_wrong_arguments_quite(self):
        """
        simple test for the "macro API"
        """
        def test(text, foo):
            pass
        my_stderr = StringIO()

        html = creole2html(
            markup_string="<<test bar='foo'>>c<</test>>",
            emitter_kwargs={
                "verbose": 1,
                "macros": {"test": test},
                "stderr": my_stderr,
            }
        )
        self.assertEqual(html,
                         "[Error: Macro 'test' error: test() got an unexpected keyword argument 'bar']"
                         )
        error_msg = my_stderr.getvalue()
        self.assertEqual(error_msg, "")

    @unittest.skipIf(not PYGMENTS, "Pygments not installed")
    def test_code_macro(self):
        # due to https://bitbucket.org/birkenfeld/pygments-main/issues/1254/empty-at-the-begining-of-the-highlight
        # an empty <span></span> is now part of pygments output
        self.assert_creole2html(r"""
            Here a simple code macro test:
            <<code ext=".py">>
            for i in xrange(10):
                print('hello world')
            <</code>>
            """, """
            <p>Here a simple code macro test:</p>
            <div class="pygments"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span><br />
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">)</span><br />
            </pre></div><br />
            """,
                                macros={'code': example_macros.code}
                                )

    def test_code_macro_fallback(self):
        # force to use fallback. Will be reset in self.setUp()
        example_macros.PYGMENTS = False

        self.assert_creole2html(
            r"""
            Here a simple code macro test:
            <<code ext=".py">>
            for i in xrange(10):
                print('hello world')
            <</code>>
            """, """
            <p>Here a simple code macro test:</p>
            <pre>for i in xrange(10):
                print('hello world')</pre>
            """,
            macros={'code': example_macros.code}
        )

    def test_code_macro_fallback_escape(self):
        # force to use fallback. Will be reset in self.setUp()
        example_macros.PYGMENTS = False

        self.assert_creole2html(
            r"""
            <<code ext=".py">>
            print('This >>should<< be escaped!')
            <</code>>
            """, """
            <pre>print('This &gt;&gt;should&lt;&lt; be escaped!')</pre>
            """,
            macros={'code': example_macros.code}
        )


class TestCreole2htmlMarkup(BaseCreoleTest):

    def test_creole_basic(self):
        out_string = creole2html("a text line.")
        self.assertEqual(out_string, "<p>a text line.</p>")

    def test_lineendings(self):
        """ Test all existing lineending version """
        out_string = creole2html("first\nsecond")
        self.assertEqual(out_string, "<p>first<br />\nsecond</p>")

        out_string = creole2html("first\rsecond")
        self.assertEqual(out_string, "<p>first<br />\nsecond</p>")

        out_string = creole2html("first\r\nsecond")
        self.assertEqual(out_string, "<p>first<br />\nsecond</p>")

    # --------------------------------------------------------------------------

    def test_creole_linebreak(self):
        self.assert_creole2html(r"""
            Force\\linebreak
        """, """
            <p>Force<br />
            linebreak</p>
        """)

    def test_html_lines(self):
        self.assert_creole2html(r"""
            This is a normal Text block witch would
            escape html chars like < and > ;)

            So you can't insert <html> directly.

            <p>This escaped, too.</p>
        """, """
            <p>This is a normal Text block witch would<br />
            escape html chars like &lt; and &gt; ;)</p>

            <p>So you can't insert &lt;html&gt; directly.</p>

            <p>&lt;p&gt;This escaped, too.&lt;/p&gt;</p>
        """)

    def test_escape_char(self):
        self.assert_creole2html(r"""
            ~#1
            http://domain.tld/~bar/
            ~http://domain.tld/
            [[Link]]
            ~[[Link]]
        """, """
            <p>#1<br />
            <a href="http://domain.tld/~bar/">http://domain.tld/~bar/</a><br />
            http://domain.tld/<br />
            <a href="Link">Link</a><br />
            [[Link]]</p>
        """)

    def test_cross_paragraphs(self):
        self.assert_creole2html(r"""
            Bold and italics should //not be...

            ...able// to **cross

            paragraphs.**
        """, """
            <p>Bold and italics should //not be...</p>

            <p>...able// to **cross</p>

            <p>paragraphs.**</p>
        """)

    def test_list_special(self):
        """
        optional whitespace before the list
        """
        self.assert_creole2html(r"""
            * Item 1
            ** Item 1.1
             ** Item 1.2
                ** Item 1.3
                    * Item2

                # one
              ## two
        """, """
        <ul>
            <li>Item 1
            <ul>
                <li>Item 1.1</li>
                <li>Item 1.2</li>
                <li>Item 1.3</li>
            </ul></li>
            <li>Item2</li>
        </ul>
        <ol>
            <li>one
            <ol>
                <li>two</li>
            </ol></li>
        </ol>
        """)

    def test_macro_basic(self):
        """
        Test the three different macro types with a "unittest macro"
        """
        self.assert_creole2html(r"""
            There exist three different macro types:
            A <<unittest_macro1 args="foo1">>bar1<</unittest_macro1>> in a line...
            ...a single <<unittest_macro1 foo="bar">> tag,
            or: <<unittest_macro1 a=1 b=2 />> closed...

            a macro block:
            <<unittest_macro2 char="|">>
            the
            text
            <</unittest_macro2>>
            the end
        """, r"""
            <p>There exist three different macro types:<br />
            A [test macro1 - kwargs: args="foo1",text="bar1"] in a line...<br />
            ...a single [test macro1 - kwargs: foo="bar",text=null] tag,<br />
            or: [test macro1 - kwargs: a=1,b=2,text=null] closed...</p>

            <p>a macro block:</p>
            the|text
            <p>the end</p>
        """,
                                macros=test_macros,
                                )

    def test_macro_html1(self):
        self.assert_creole2html(r"""
                html macro:
                <<html>>
                <p><<this is broken 'html', but it will be pass throu>></p>
                <</html>>

                inline: <<html>>&#x7B;...&#x7D;<</html>> code
            """, r"""
                <p>html macro:</p>
                <p><<this is broken 'html', but it will be pass throu>></p>

                <p>inline: &#x7B;...&#x7D; code</p>
            """,
                                macros=example_macros,
                                )

    def test_macro_not_exist1(self):
        """
        not existing macro with creole2html.HtmlEmitter(verbose=1):
        A error message should be insertet into the generated code

        Two tests: with verbose=1 and verbose=2, witch write a Traceback
        information to a given "stderr"
        """
        source_string = r"""
            macro block:
            <<notexists>>
            foo bar
            <</notexists>>

            inline macro:
            <<notexisttoo foo="bar">>
        """
        should_string = r"""
            <p>macro block:</p>
            [Error: Macro 'notexists' doesn't exist]

            <p>inline macro:<br />
            [Error: Macro 'notexisttoo' doesn't exist]
            </p>
        """

        self.assert_creole2html(source_string, should_string, verbose=1)

        # ----------------------------------------------------------------------
        # Test with verbose=2 ans a StringIO stderr handler

    def test_wrong_macro_syntax(self):
        self.assert_creole2html(r"""
                wrong macro line:
                <<summary>Some funky page summary.<</summary>>
            """, r"""
                <p>wrong macro line:<br />
                [Error: Wrong macro arguments: ">Some funky page summary.<</summary" for macro 'summary' (maybe wrong macro tag syntax?)]
                </p>
            """,  # verbose=True
                                )

    def test_macro_not_exist2(self):
        """
        not existing macro with creole2html.HtmlEmitter(verbose=0):

        No error messages should be inserted.
        """
        self.assert_creole2html(r"""
            macro block:
            <<notexists>>
            foo bar
            <</notexists>>

            inline macro:
            <<notexisttoo foo="bar">>
        """, r"""
            <p>macro block:</p>

            <p>inline macro:<br />
            </p>
        """, verbose=False
                                )

    def test_toc_simple(self):
        """
        Simple test to check the table of content is correctly generated.
        """
        self.assert_creole2html(r"""
            <<toc>>
            = Headline
        """, """
            <ul>
                <li><a href="#Headline">Headline</a></li>
            </ul>
            <a name="Headline"><h1>Headline</h1></a>
        """)

    def test_toc_more_headlines(self):
        self.assert_creole2html(r"""
            Between text and toc must be a newline.

            <<toc>>
            = Headline 1
            == Sub-Headline 1.1
            == Sub-Headline 1.2
            = Headline 2
            == Sub-Headline 2.1
            == Sub-Headline 2.2
        """, """
            <p>Between text and toc must be a newline.</p>

            <ul>
                <li><a href="#Headline 1">Headline 1</a></li>
                <ul>
                    <li><a href="#Sub-Headline 1.1">Sub-Headline 1.1</a></li>
                    <li><a href="#Sub-Headline 1.2">Sub-Headline 1.2</a></li>
                </ul>
                <li><a href="#Headline 2">Headline 2</a></li>
                <ul>
                    <li><a href="#Sub-Headline 2.1">Sub-Headline 2.1</a></li>
                    <li><a href="#Sub-Headline 2.2">Sub-Headline 2.2</a></li>
                </ul>
            </ul>
            <a name="Headline 1"><h1>Headline 1</h1></a>
            <a name="Sub-Headline 1.1"><h2>Sub-Headline 1.1</h2></a>
            <a name="Sub-Headline 1.2"><h2>Sub-Headline 1.2</h2></a>
            <a name="Headline 2"><h1>Headline 2</h1></a>
            <a name="Sub-Headline 2.1"><h2>Sub-Headline 2.1</h2></a>
            <a name="Sub-Headline 2.2"><h2>Sub-Headline 2.2</h2></a>
        """)

    def test_toc_chaotic_headlines(self):
        self.assert_creole2html(r"""
            <<toc>>
            = level 1
            === level 3
            == level 2
            ==== level 4
            = level 1
        """, """
            <ul>
                <li><a href="#level 1">level 1</a></li>
                <ul>
                    <ul>
                        <li><a href="#level 3">level 3</a></li>
                    </ul>
                    <li><a href="#level 2">level 2</a></li>
                    <ul>
                        <ul>
                            <li><a href="#level 4">level 4</a></li>
                        </ul>
                    </ul>
                </ul>
                <li><a href="#level 1">level 1</a></li>
            </ul>
            <a name="level 1"><h1>level 1</h1></a>
            <a name="level 3"><h3>level 3</h3></a>
            <a name="level 2"><h2>level 2</h2></a>
            <a name="level 4"><h4>level 4</h4></a>
            <a name="level 1"><h1>level 1</h1></a>
        """)

    def test_toc_depth_1(self):
        self.assert_creole2html(r"""
            <<toc depth=1>>
            = Headline 1
            == Sub-Headline 1.1
            === Sub-Sub-Headline 1.1.1
            === Sub-Sub-Headline 1.1.2
            == Sub-Headline 1.2
            = Headline 2
            == Sub-Headline 2.1
            == Sub-Headline 2.2
            === Sub-Sub-Headline 2.2.1
        """, """
            <ul>
                <li><a href="#Headline 1">Headline 1</a></li>
                <li><a href="#Headline 2">Headline 2</a></li>
            </ul>
            <a name="Headline 1"><h1>Headline 1</h1></a>
            <a name="Sub-Headline 1.1"><h2>Sub-Headline 1.1</h2></a>
            <a name="Sub-Sub-Headline 1.1.1"><h3>Sub-Sub-Headline 1.1.1</h3></a>
            <a name="Sub-Sub-Headline 1.1.2"><h3>Sub-Sub-Headline 1.1.2</h3></a>
            <a name="Sub-Headline 1.2"><h2>Sub-Headline 1.2</h2></a>
            <a name="Headline 2"><h1>Headline 2</h1></a>
            <a name="Sub-Headline 2.1"><h2>Sub-Headline 2.1</h2></a>
            <a name="Sub-Headline 2.2"><h2>Sub-Headline 2.2</h2></a>
            <a name="Sub-Sub-Headline 2.2.1"><h3>Sub-Sub-Headline 2.2.1</h3></a>
        """)

    def test_toc_depth_2(self):
        self.assert_creole2html(r"""
            <<toc depth=2>>
            = Headline 1
            == Sub-Headline 1.1
            === Sub-Sub-Headline 1.1.1
            === Sub-Sub-Headline 1.1.2
            == Sub-Headline 1.2
            = Headline 2
            == Sub-Headline 2.1
            == Sub-Headline 2.2
            === Sub-Sub-Headline 2.2.1
        """, """
            <ul>
                <li><a href="#Headline 1">Headline 1</a></li>
                <ul>
                    <li><a href="#Sub-Headline 1.1">Sub-Headline 1.1</a></li>
                    <li><a href="#Sub-Headline 1.2">Sub-Headline 1.2</a></li>
                </ul>
                <li><a href="#Headline 2">Headline 2</a></li>
                <ul>
                    <li><a href="#Sub-Headline 2.1">Sub-Headline 2.1</a></li>
                    <li><a href="#Sub-Headline 2.2">Sub-Headline 2.2</a></li>
                </ul>
            </ul>
            <a name="Headline 1"><h1>Headline 1</h1></a>
            <a name="Sub-Headline 1.1"><h2>Sub-Headline 1.1</h2></a>
            <a name="Sub-Sub-Headline 1.1.1"><h3>Sub-Sub-Headline 1.1.1</h3></a>
            <a name="Sub-Sub-Headline 1.1.2"><h3>Sub-Sub-Headline 1.1.2</h3></a>
            <a name="Sub-Headline 1.2"><h2>Sub-Headline 1.2</h2></a>
            <a name="Headline 2"><h1>Headline 2</h1></a>
            <a name="Sub-Headline 2.1"><h2>Sub-Headline 2.1</h2></a>
            <a name="Sub-Headline 2.2"><h2>Sub-Headline 2.2</h2></a>
            <a name="Sub-Sub-Headline 2.2.1"><h3>Sub-Sub-Headline 2.2.1</h3></a>
        """)

    def test_toc_depth_3(self):
        self.assert_creole2html(r"""
            <<toc depth=3>>
            = Headline 1
            == Sub-Headline 1.1
            === Sub-Sub-Headline 1.1.1
            === Sub-Sub-Headline 1.1.2
            == Sub-Headline 1.2
            = Headline 2
            == Sub-Headline 2.1
            == Sub-Headline 2.2
            === Sub-Sub-Headline 2.2.1
        """, """
            <ul>
                <li><a href="#Headline 1">Headline 1</a></li>
                <ul>
                    <li><a href="#Sub-Headline 1.1">Sub-Headline 1.1</a></li>
                    <ul>
                        <li><a href="#Sub-Sub-Headline 1.1.1">Sub-Sub-Headline 1.1.1</a></li>
                        <li><a href="#Sub-Sub-Headline 1.1.2">Sub-Sub-Headline 1.1.2</a></li>
                    </ul>
                    <li><a href="#Sub-Headline 1.2">Sub-Headline 1.2</a></li>
                </ul>
                <li><a href="#Headline 2">Headline 2</a></li>
                <ul>
                    <li><a href="#Sub-Headline 2.1">Sub-Headline 2.1</a></li>
                    <li><a href="#Sub-Headline 2.2">Sub-Headline 2.2</a></li>
                    <ul>
                        <li><a href="#Sub-Sub-Headline 2.2.1">Sub-Sub-Headline 2.2.1</a></li>
                    </ul>
                </ul>
            </ul>
            <a name="Headline 1"><h1>Headline 1</h1></a>
            <a name="Sub-Headline 1.1"><h2>Sub-Headline 1.1</h2></a>
            <a name="Sub-Sub-Headline 1.1.1"><h3>Sub-Sub-Headline 1.1.1</h3></a>
            <a name="Sub-Sub-Headline 1.1.2"><h3>Sub-Sub-Headline 1.1.2</h3></a>
            <a name="Sub-Headline 1.2"><h2>Sub-Headline 1.2</h2></a>
            <a name="Headline 2"><h1>Headline 2</h1></a>
            <a name="Sub-Headline 2.1"><h2>Sub-Headline 2.1</h2></a>
            <a name="Sub-Headline 2.2"><h2>Sub-Headline 2.2</h2></a>
            <a name="Sub-Sub-Headline 2.2.1"><h3>Sub-Sub-Headline 2.2.1</h3></a>
        """)

    def test_toc_with_no_toc(self):
        self.assert_creole2html(r"""
            <<toc>>
            = This is the Headline
            Use {{{<<toc>>}}} to insert a table of contents.
        """, """
            <ul>
                <li><a href="#This is the Headline">This is the Headline</a></li>
            </ul>
            <a name="This is the Headline"><h1>This is the Headline</h1></a>
            <p>Use <tt>&lt;&lt;toc&gt;&gt;</tt> to insert a table of contents.</p>
        """)

    def test_toc_more_then_one_toc(self):
        self.assert_creole2html(r"""
            Not here:
            {{{
            print("<<toc>>")
            }}}

            and onle the first:

            <<toc>>

            <<toc>>
            <<toc>>
            = Headline
            == Sub-Headline
        """, """
            <p>Not here:</p>
            <pre>
            print("&lt;&lt;toc&gt;&gt;")
            </pre>

            <p>and onle the first:</p>

            <ul>
                <li><a href="#Headline">Headline</a></li>
                <ul>
                    <li><a href="#Sub-Headline">Sub-Headline</a></li>
                </ul>
            </ul>

            <p>&lt;&lt;toc&gt;&gt;<br />
            &lt;&lt;toc&gt;&gt;</p>
            <a name="Headline"><h1>Headline</h1></a>
            <a name="Sub-Headline"><h2>Sub-Headline</h2></a>
        """)

    def test_toc_headline_before_toc(self):
        self.assert_creole2html(r"""
            = headline
            == sub headline

            <<toc>>

            ok?
        """, """
            <a name="headline"><h1>headline</h1></a>
            <a name="sub headline"><h2>sub headline</h2></a>

            <ul>
                <li><a href="#headline">headline</a></li>
                <ul>
                    <li><a href="#sub headline">sub headline</a></li>
                </ul>
            </ul>

            <p>ok?</p>
        """)

    def test_image(self):
        """ test image tag with different picture text """
        self.assert_creole2html(r"""
            {{foobar1.jpg}}
            {{/path1/path2/foobar2.jpg}}
            {{/path1/path2/foobar3.jpg|foobar3.jpg}}
        """, """
            <p><img src="foobar1.jpg" title="foobar1.jpg" alt="foobar1.jpg" /><br />
            <img src="/path1/path2/foobar2.jpg" title="/path1/path2/foobar2.jpg" alt="/path1/path2/foobar2.jpg" /><br />
            <img src="/path1/path2/foobar3.jpg" title="foobar3.jpg" alt="foobar3.jpg" /></p>
        """)

    def test_image_with_size(self):
        """ test image tag with size dimention (good and bad) """
        self.assert_creole2html(r"""
            {{/path1/path2/foobar3.jpg|foo|160x90}}
            {{/path1/path2/foobar3.jpg|foo| 160 x 90 }}
            {{/path1/path2/foobar3.jpg|foo|160}}
            {{/path1/path2/foobar3.jpg||160x90}}
            {{/path1/path2/foobar3.jpg|foo|}}
        """, """
            <p><img src="/path1/path2/foobar3.jpg" title="foo" alt="foo" width="160" height="90" /><br />
            <img src="/path1/path2/foobar3.jpg" title="foo" alt="foo" width="160" height="90" /><br />
            <img src="/path1/path2/foobar3.jpg" title="foo|160" alt="foo|160" /><br />
            <img src="/path1/path2/foobar3.jpg" title="/path1/path2/foobar3.jpg" alt="/path1/path2/foobar3.jpg" width="160" height="90" /><br />
            <img src="/path1/path2/foobar3.jpg" title="foo|" alt="foo|" /></p>
        """)

    def test_image_with_size_strict(self):
        """ test image tag with size dimention (good and bad) """
        self.assert_creole2html(r"""
            {{/path1/path2/foobar3.jpg|foo|160x90}}
            {{/path1/path2/foobar3.jpg|foo|160}}
        """, """
            <p><img src="/path1/path2/foobar3.jpg" title="foo|160x90" alt="foo|160x90" /><br />
            <img src="/path1/path2/foobar3.jpg" title="foo|160" alt="foo|160" /></p>
        """, strict=True)

    def test_image_unknown_extension(self):
        self.assert_creole2html(r"""
            # {{/path/to/image.ext|image ext}} one
            # {{/no/extension|no extension}} two
            # {{/image.xyz}} tree
        """, """
            <ol>
                <li><img src="/path/to/image.ext" title="image ext" alt="image ext" /> one</li>
                <li><img src="/no/extension" title="no extension" alt="no extension" /> two</li>
                <li><img src="/image.xyz" title="/image.xyz" alt="/image.xyz" /> tree</li>
            </ol>
        """)

    def test_links(self):
        self.assert_creole2html(r"""
            [[/foobar/Creole_(Markup)]]
            [[http://de.wikipedia.org/wiki/Creole_(Markup)|Creole@wikipedia]]
        """, """
            <p><a href="/foobar/Creole_(Markup)">/foobar/Creole_(Markup)</a><br />
            <a href="http://de.wikipedia.org/wiki/Creole_(Markup)">Creole@wikipedia</a></p>
        """)

    def test_standalone_hyperlink(self):
        self.assert_creole2html(r"""
                a link to the http://www.pylucid.org page.
            """, """
                <p>a link to the <a href="http://www.pylucid.org">http://www.pylucid.org</a> page.</p>
            """
                                )

    def test_wiki_style_line_breaks1(self):
        html = creole2html(
            markup_string=self._prepare_text("""
                wiki style
                linebreaks

                ...and not blog styled.
            """),
            blog_line_breaks=False,
            debug=True, verbose=True
        )
        self.assertEqual(html, self._prepare_text("""
            <p>wiki style linebreaks</p>

            <p>...and not blog styled.</p>
        """))

    def test_wiki_style_line_breaks2(self):
        html = creole2html(
            markup_string=self._prepare_text("""
                **one**
                //two//

                * one
                * two
            """),
            blog_line_breaks=False,
            debug=True, verbose=True
        )
        self.assertEqual(html, self._prepare_text("""
            <p><strong>one</strong> <i>two</i></p>

            <ul>
            \t<li>one</li>
            \t<li>two</li>
            </ul>
        """))

    def test_wiki_style_line_breaks3(self):
        html = creole2html(
            markup_string=self._prepare_text("""
                with blog line breaks, every line break would be convertet into<br />
                with wiki style not.

                This is the first line,\\\\and this is the second.

                new line
                block 1

                new line
                block 2

                end
            """),
            blog_line_breaks=False,
        )
        self.assertEqual(html, self._prepare_text("""
            <p>with blog line breaks, every line break would be convertet into&lt;br /&gt; with wiki style not.</p>

            <p>This is the first line,<br />
            and this is the second.</p>

            <p>new line block 1</p>

            <p>new line block 2</p>

            <p>end</p>
        """))

    def test_headline_spaces(self):
        """
        https://code.google.com/p/python-creole/issues/detail?id=15
        """
        html = creole2html(markup_string="== Headline1 == \n== Headline2== ")
        self.assertEqual(html, self._prepare_text("""
            <h2>Headline1</h2>
            <h2>Headline2</h2>
        """))

    def test_tt(self):
        self.assert_creole2html(r"""
            inline {{{<escaped>}}} and {{{ **not strong** }}}...
            ...and ##**strong** Teletyper## ;)
        """, """
            <p>inline <tt>&lt;escaped&gt;</tt> and <tt> **not strong** </tt>...<br />
            ...and <tt><strong>strong</strong> Teletyper</tt> ;)</p>
        """)

    def test_protocol_in_brackets(self):
        self.assert_creole2html(r"""
            My Server ([[ftp://foo/bar]]) is ok.
        """, """
            <p>My Server (<a href="ftp://foo/bar">ftp://foo/bar</a>) is ok.</p>
        """)
        self.assert_creole2html(r"""
            My Server (ftp://foo/bar) is ok.
        """, """
            <p>My Server (ftp://foo/bar) is ok.</p>
        """)

    def test_protocol_with_brackets(self):
        self.assert_creole2html(r"""
            A http://en.wikipedia.org/wiki/Uri_(Island) link.
        """, """
            <p>A <a href="http://en.wikipedia.org/wiki/Uri_(Island)">http://en.wikipedia.org/wiki/Uri_(Island)</a> link.</p>
        """)

    def test_wrong_protocol(self):
        self.assert_creole2html(r"""
            ~ftp://ok
        """, """
            <p>ftp://ok</p>
        """)
        self.assert_creole2html(r"""
            ftp:
        """, """
            <p>ftp:</p>
        """)
        self.assert_creole2html(r"""
            ftp:/
        """, """
            <p>ftp:/</p>
        """)
        self.assert_creole2html(r"""
            missing space.ftp://ok
        """, """
            <p>missing space.ftp://ok</p>
        """)


class TestStr2Dict(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(
            string2dict('key1="value1" key2="value2"'),
            {'key2': 'value2', 'key1': 'value1'}
        )

    def test_bool(self):
        self.assertEqual(
            string2dict('unicode=True'),
            {'unicode': True}
        )

    def test_mixed1(self):
        self.assertEqual(
            string2dict('A="B" C=1 D=1.1 E=True F=False G=None'),
            {'A': 'B', 'C': 1, 'E': True, 'D': '1.1', 'G': None, 'F': False}
        )

    def test_mixed2(self):
        self.assertEqual(
            string2dict('''key1="'1'" key2='"2"' key3="""'3'""" '''),
            {'key3': 3, 'key2': 2, 'key1': 1}
        )


class TestDict2String(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(
            dict2string({'key': 'value'}),
            'key="value"'
        )

    def test_basic2(self):
        self.assertEqual(
            dict2string({'foo': "bar", "no": 123}),
            'foo="bar" no=123'
        )

    def test_basic3(self):
        self.assertEqual(
            dict2string({"foo": 'bar', "no": "ABC"}),
            'foo="bar" no="ABC"'
        )


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