""" 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="<><><>", 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="<>

foo

<>", emitter_kwargs={ "verbose": 1, "macros": example_macros, "stderr": sys.stderr, } ) self.assertEqual(html, '

foo

\n

<bar?>

') def test_example_macros2(self): html = creole2html( markup_string="<>{{{<nocode>}}}<>", emitter_kwargs={ "verbose": 1, "macros": example_macros, "stderr": sys.stderr, } ) self.assertEqual(html, '{{{<nocode>}}}') def test_example_macros3(self): html = creole2html( markup_string="<>1<><>2<>", 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="<>c<>", 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="<>bar<>", 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="<>c<>", 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="<>c<>", 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 is now part of pygments output self.assert_creole2html(r""" Here a simple code macro test: <> for i in xrange(10): print('hello world') <> """, """

Here a simple code macro test:

for i in xrange(10):
print('hello world')

""", 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: <> for i in xrange(10): print('hello world') <> """, """

Here a simple code macro test:

for i in xrange(10):
                print('hello world')
""", 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""" <> print('This >>should<< be escaped!') <> """, """
print('This >>should<< be escaped!')
""", macros={'code': example_macros.code} ) class TestCreole2htmlMarkup(BaseCreoleTest): def test_creole_basic(self): out_string = creole2html("a text line.") self.assertEqual(out_string, "

a text line.

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

first
\nsecond

") out_string = creole2html("first\rsecond") self.assertEqual(out_string, "

first
\nsecond

") out_string = creole2html("first\r\nsecond") self.assertEqual(out_string, "

first
\nsecond

") # -------------------------------------------------------------------------- def test_creole_linebreak(self): self.assert_creole2html(r""" Force\\linebreak """, """

Force
linebreak

""") 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 directly.

This escaped, too.

""", """

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>

""") def test_escape_char(self): self.assert_creole2html(r""" ~#1 http://domain.tld/~bar/ ~http://domain.tld/ [[Link]] ~[[Link]] """, """

#1
http://domain.tld/~bar/
http://domain.tld/
Link
[[Link]]

""") def test_cross_paragraphs(self): self.assert_creole2html(r""" Bold and italics should //not be... ...able// to **cross paragraphs.** """, """

Bold and italics should //not be...

...able// to **cross

paragraphs.**

""") 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 """, """
  1. one
    1. two
""") 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 <>bar1<> in a line... ...a single <> tag, or: <> closed... a macro block: <> the text <> the end """, r"""

There exist three different macro types:
A [test macro1 - kwargs: args="foo1",text="bar1"] in a line...
...a single [test macro1 - kwargs: foo="bar",text=null] tag,
or: [test macro1 - kwargs: a=1,b=2,text=null] closed...

a macro block:

the|text

the end

""", macros=test_macros, ) def test_macro_html1(self): self.assert_creole2html(r""" html macro: <>

<>

<> inline: <>{...}<> code """, r"""

html macro:

<>

inline: {...} code

""", 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: <> foo bar <> inline macro: <> """ should_string = r"""

macro block:

[Error: Macro 'notexists' doesn't exist]

inline macro:
[Error: Macro 'notexisttoo' doesn't exist]

""" 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: <Some funky page summary.<> """, r"""

wrong macro line:
[Error: Wrong macro arguments: ">Some funky page summary.< """, # 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: <> foo bar <> inline macro: <> """, r"""

macro block:

inline macro:

""", verbose=False ) def test_toc_simple(self): """ Simple test to check the table of content is correctly generated. """ self.assert_creole2html(r""" <> = Headline """, """

Headline

""") def test_toc_more_headlines(self): self.assert_creole2html(r""" Between text and toc must be a newline. <> = Headline 1 == Sub-Headline 1.1 == Sub-Headline 1.2 = Headline 2 == Sub-Headline 2.1 == Sub-Headline 2.2 """, """

Between text and toc must be a newline.

Headline 1

Sub-Headline 1.1

Sub-Headline 1.2

Headline 2

Sub-Headline 2.1

Sub-Headline 2.2

""") def test_toc_chaotic_headlines(self): self.assert_creole2html(r""" <> = level 1 === level 3 == level 2 ==== level 4 = level 1 """, """

level 1

level 3

level 2

level 4

level 1

""") def test_toc_depth_1(self): self.assert_creole2html(r""" <> = 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 """, """

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

""") def test_toc_depth_2(self): self.assert_creole2html(r""" <> = 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 """, """

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

""") def test_toc_depth_3(self): self.assert_creole2html(r""" <> = 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 """, """

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

""") def test_toc_with_no_toc(self): self.assert_creole2html(r""" <> = This is the Headline Use {{{<>}}} to insert a table of contents. """, """

This is the Headline

Use <<toc>> to insert a table of contents.

""") def test_toc_more_then_one_toc(self): self.assert_creole2html(r""" Not here: {{{ print("<>") }}} and onle the first: <> <> <> = Headline == Sub-Headline """, """

Not here:

            print("<<toc>>")
            

and onle the first:

<<toc>>
<<toc>>

Headline

Sub-Headline

""") def test_toc_headline_before_toc(self): self.assert_creole2html(r""" = headline == sub headline <> ok? """, """

headline

sub headline

ok?

""") 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}} """, """

foobar1.jpg
/path1/path2/foobar2.jpg
foobar3.jpg

""") 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|}} """, """

foo
foo
foo|160
/path1/path2/foobar3.jpg
foo|

""") 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}} """, """

foo|160x90
foo|160

""", 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 """, """
  1. image ext one
  2. no extension two
  3. /image.xyz tree
""") def test_links(self): self.assert_creole2html(r""" [[/foobar/Creole_(Markup)]] [[http://de.wikipedia.org/wiki/Creole_(Markup)|Creole@wikipedia]] """, """

/foobar/Creole_(Markup)
Creole@wikipedia

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

a link to the http://www.pylucid.org page.

""" ) 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("""

wiki style linebreaks

...and not blog styled.

""")) 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("""

one two

    \t
  • one
  • \t
  • two
""")) 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
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("""

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

""")) 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("""

Headline1

Headline2

""")) def test_tt(self): self.assert_creole2html(r""" inline {{{}}} and {{{ **not strong** }}}... ...and ##**strong** Teletyper## ;) """, """

inline <escaped> and **not strong** ...
...and strong Teletyper ;)

""") def test_protocol_in_brackets(self): self.assert_creole2html(r""" My Server ([[ftp://foo/bar]]) is ok. """, """

My Server (ftp://foo/bar) is ok.

""") self.assert_creole2html(r""" My Server (ftp://foo/bar) is ok. """, """

My Server (ftp://foo/bar) is ok.

""") def test_protocol_with_brackets(self): self.assert_creole2html(r""" A http://en.wikipedia.org/wiki/Uri_(Island) link. """, """

A http://en.wikipedia.org/wiki/Uri_(Island) link.

""") def test_wrong_protocol(self): self.assert_creole2html(r""" ~ftp://ok """, """

ftp://ok

""") self.assert_creole2html(r""" ftp: """, """

ftp:

""") self.assert_creole2html(r""" ftp:/ """, """

ftp:/

""") self.assert_creole2html(r""" missing space.ftp://ok """, """

missing space.ftp://ok

""") 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 )