Martin von Löwis avatar Martin von Löwis committed ae133ee

Wrap expected bytes results with b() function.

Comments (0)

Files changed (1)

creoleparser/tests.py

 # This module is part of Creoleparser and is released under
 # the MIT License: http://www.opensource.org/licenses/mit-license.php
 #
+import sys
 import urllib
 import unittest
 import re
 base_url = ''
 inter_wiki_url = 'http://wikiohana.net/cgi-bin/wiki.pl/'
 
+if sys.version_info <= (3,):
+    def b(s):
+        return s
+else:
+    def b(s):
+        return s.encode("ascii")
+
 
 def class_name_function(page_name):
     if page_name == 'NewPage':
 
 
 def wrap_result(expected):
-    return "<p>%s</p>\n" % expected
+    return b("<p>%s</p>\n" % expected)
 
 
         
     def test_horizontal_line(self):
         self.assertEquals(
             self.parse(r"----"),
-            "<hr />\n")
+            b("<hr />\n"))
 
     def test_raw_links(self):
         self.assertEquals(
             wrap_result('<code class="unknown_macro" style="white-space:pre-wrap">&lt;&lt;<span class="macro_name">unknown</span><span class="macro_arg_string"></span>&gt;&gt;<span class="macro_body">foo with\na line break</span>&lt;&lt;/unknown&gt;&gt;</code>'))
         self.assertEquals(
             self.parse('<<unknown>>\nfoo\n<</unknown>>'),
-            '<pre class="unknown_macro">&lt;&lt;<span class="macro_name">unknown</span><span class="macro_arg_string"></span>&gt;&gt;\n<span class="macro_body">foo\n</span>&lt;&lt;/unknown&gt;&gt;</pre>')
+            b('<pre class="unknown_macro">&lt;&lt;<span class="macro_name">unknown</span><span class="macro_arg_string"></span>&gt;&gt;\n<span class="macro_body">foo\n</span>&lt;&lt;/unknown&gt;&gt;</pre>'))
         self.assertEquals(
             self.parse('start\n\n<<unknown>>\n\nend'),
             wrap_result('start</p>\n<p><code class="unknown_macro">&lt;&lt;<span class="macro_name">unknown</span><span class="macro_arg_string"></span>&gt;&gt;</code></p>\n<p>end'))
   |= Item|= Size|= Price
   | fish | big  |//cheap//
   | crab | small|**very\\expesive**"""),
-            """<table><tr><th>Item</th><th>Size</th><th>Price</th></tr>
+            b("""<table><tr><th>Item</th><th>Size</th><th>Price</th></tr>
 <tr><td>fish</td><td><strong>big</strong></td><td>cheap</td></tr>
 <tr><td>crab</td><td>small</td><td>expesive</td></tr>
 </table>
 <table><tr><th>Item</th><th>Size</th><th>Price</th></tr>
 <tr><td>fish</td><td>big</td><td><em>cheap</em></td></tr>
 <tr><td>crab</td><td>small</td><td><strong>very<br />expesive</strong></td></tr>
-</table>\n""")
+</table>\n"""))
 
     def test_headings(self):
         self.assertEquals(
             self.parse("= Level 1 (largest)"),
-            "<h1>Level 1 (largest)</h1>\n")
+            b("<h1>Level 1 (largest)</h1>\n"))
         self.assertEquals(
             self.parse("== Level 2"),
-            "<h2>Level 2</h2>\n")
+            b("<h2>Level 2</h2>\n"))
         self.assertEquals(
             self.parse("=== Level 3"),
-            "<h3>Level 3</h3>\n")
+            b("<h3>Level 3</h3>\n"))
         self.assertEquals(
             self.parse("==== Level 4"),
-            "<h4>Level 4</h4>\n")
+            b("<h4>Level 4</h4>\n"))
         self.assertEquals(
             self.parse("===== Level 5"),
-            "<h5>Level 5</h5>\n")
+            b("<h5>Level 5</h5>\n"))
         self.assertEquals(
             self.parse("====== Level 6"),
-            "<h6>Level 6</h6>\n")
+            b("<h6>Level 6</h6>\n"))
         self.assertEquals(
             self.parse("=== Also Level 3 ="),
-            "<h3>Also Level 3</h3>\n")
+            b("<h3>Also Level 3</h3>\n"))
         self.assertEquals(
             self.parse("=== Also Level 3 =="),
-            "<h3>Also Level 3</h3>\n")
+            b("<h3>Also Level 3</h3>\n"))
         self.assertEquals(
             self.parse("=== Also Level 3 ==="),
-            "<h3>Also Level 3</h3>\n")
+            b("<h3>Also Level 3</h3>\n"))
         self.assertEquals(
             self.parse("= Also Level = 1 ="),
-            "<h1>Also Level = 1</h1>\n")
+            b("<h1>Also Level = 1</h1>\n"))
         
         self.assertEquals(
             self.parse("=== This **is** //parsed// ===\n"),
-            "<h3>This <strong>is</strong> <em>parsed</em></h3>\n")
+            b("<h3>This <strong>is</strong> <em>parsed</em></h3>\n"))
 
     def test_escape(self):
         self.assertEquals(
             wrap_result("preventing markup for = headings"))
         self.assertEquals(
             self.parse("|preventing markup|for a pipe ~| in a table|\n"),
-            "<table><tr><td>preventing markup</td><td>for a pipe | in a table</td></tr>\n</table>\n")
+            b("<table><tr><td>preventing markup</td><td>for a pipe | in a table</td></tr>\n</table>\n"))
 
     def test_preformat(self):
         self.assertEquals(
 
  }}}
 }}}"""),
-            """\
+            b("""\
 <pre>** some ** unformatted {{{ stuff }}} ~~~
 
 }}}
 </pre>
-""")
+"""))
 
     def test_inline_unformatted(self):
         self.assertEquals(
     def test_link_in_table(self):
         self.assertEquals(
             self.parse("|http://www.google.com|Google|\n"),
-            """<table><tr><td><a href="http://www.google.com">http://www.google.com</a></td><td>Google</td></tr>\n</table>\n""")
+            b("""<table><tr><td><a href="http://www.google.com">http://www.google.com</a></td><td>Google</td></tr>\n</table>\n"""))
 
     def test_link_in_bold(self):
         self.assertEquals(
     def test_link_in_heading(self):
         self.assertEquals(
             self.parse("= [[http://www.google.com|Google]]\n"),
-            """<h1><a href="http://www.google.com">Google</a></h1>\n""")
+            b("""<h1><a href="http://www.google.com">Google</a></h1>\n"""))
         self.assertEquals(
             self.parse("== http://www.google.com\n"),
-            """<h2><a href="http://www.google.com">http://www.google.com</a></h2>\n""")
+            b("""<h2><a href="http://www.google.com">http://www.google.com</a></h2>\n"""))
         self.assertEquals(
             self.parse("== ~http://www.google.com\n"),
-            "<h2>http://www.google.com</h2>\n")
+            b("<h2>http://www.google.com</h2>\n"))
 
     def test_unordered_lists(self):
         self.assertEquals(
 * **item three**
 *# item four
             """),
-            "<ul><li>this is list <strong>item one</strong>\n<ul><li><em>subitem 1</em>\n</li><li><em>subitem 2</em>\n<ul><li>A\n</li><li>B\n</li></ul></li><li><em>subitem 3</em>\n</li></ul></li><li><strong>item two</strong>\n</li><li><strong>item three</strong>\n</li><li># item four\n</li></ul>\n")
+            b("<ul><li>this is list <strong>item one</strong>\n<ul><li><em>subitem 1</em>\n</li><li><em>subitem 2</em>\n<ul><li>A\n</li><li>B\n</li></ul></li><li><em>subitem 3</em>\n</li></ul></li><li><strong>item two</strong>\n</li><li><strong>item three</strong>\n</li><li># item four\n</li></ul>\n"))
 
     def test_ordered_lists(self):
         self.assertEquals(
 # **item two
 # **item three**
             """),
-            "<ol><li>this is list <strong>item one</strong>\n<ol><li><em>subitem 1</em>\n</li><li><em>subitem 2</em>\n<ol><li>A\n</li><li>B\n</li></ol></li></ol></li><li><strong>item two</strong>\n</li><li><strong>item three</strong>\n</li></ol>\n")
+            b("<ol><li>this is list <strong>item one</strong>\n<ol><li><em>subitem 1</em>\n</li><li><em>subitem 2</em>\n<ol><li>A\n</li><li>B\n</li></ol></li></ol></li><li><strong>item two</strong>\n</li><li><strong>item three</strong>\n</li></ol>\n"))
 
     def test_mixed_lists(self):
         self.assertEquals(
 ** Unorder subitem 1
 ** Unorder subitem 2
 # **item three**"""),
-            "<ol><li>this is list <strong>item one</strong>\n<ul><li><em>unordered subitem 1</em>\n</li><li><em>unordered subitem 2</em>\n</li></ul></li>\
-<li><strong>item two</strong>\n<ul><li>Unorder subitem 1\n</li><li>Unorder subitem 2\n</li></ul></li><li><strong>item three</strong></li></ol>\n")
+            b("<ol><li>this is list <strong>item one</strong>\n<ul><li><em>unordered subitem 1</em>\n</li><li><em>unordered subitem 2</em>\n</li></ul></li>\
+<li><strong>item two</strong>\n<ul><li>Unorder subitem 1\n</li><li>Unorder subitem 2\n</li></ul></li><li><strong>item three</strong></li></ol>\n"))
 
     def test_definition_lists(self):
         self.assertEquals(
 : definition 1
 : defintioins 2
 """),
-            "<dl><dt>This is a title:</dt>\n<dd>this is its entry</dd>\n<dt>Another title</dt>\n<dd>it's definition entry</dd>\n<dt>This is : a another title:</dt>\n<dd>this is its entry\n<strong> and this emphasized!</strong></dd>\n<dt>Title</dt>\n<dd>definition 1</dd>\n<dd>defintioins 2</dd>\n</dl>\n")
+            b("<dl><dt>This is a title:</dt>\n<dd>this is its entry</dd>\n<dt>Another title</dt>\n<dd>it's definition entry</dd>\n<dt>This is : a another title:</dt>\n<dd>this is its entry\n<strong> and this emphasized!</strong></dd>\n<dt>Title</dt>\n<dd>definition 1</dd>\n<dd>defintioins 2</dd>\n</dl>\n"))
 
     def test_image(self):
         self.assertEquals(
     def test_image_in_table(self):
         self.assertEquals(
             self.parse("|nice picture |{{campfire.jpg}}|\n"),
-            """<table><tr><td>nice picture</td><td><img src="campfire.jpg" alt="campfire.jpg" title="campfire.jpg" /></td></tr>\n</table>\n""")
+            b("""<table><tr><td>nice picture</td><td><img src="campfire.jpg" alt="campfire.jpg" title="campfire.jpg" /></td></tr>\n</table>\n"""))
 
     def test_super_and_sub_scripts(self):
         self.assertEquals(
             wrap_result('This block of <span style="color:red">text <strong>should</strong> be monospace</span> now'))
         self.assertEquals(
             parse("<<red>>\ntext **should** be monospace\n<</red>>"),
-            '<div style="color:red"><p>text <strong>should</strong> be monospace</p>\n</div>')
+            b('<div style="color:red"><p>text <strong>should</strong> be monospace</p>\n</div>'))
         self.assertEquals(
             parse("This block of <<blockquote>>text **should** be monospace<</blockquote>> now"),
             wrap_result('This block of </p><blockquote>text <strong>should</strong> be monospace</blockquote><p> now'))
         self.assertEquals(
             parse("<<blockquote>>\ntext **should** be monospace\n<</blockquote>>"),
-            '<blockquote><p>text <strong>should</strong> be monospace</p>\n</blockquote>')
+            b('<blockquote><p>text <strong>should</strong> be monospace</p>\n</blockquote>'))
 
     def test_external_links_class_option(self):
         dialect = create_dialect(creole10_base, external_links_class='external')
             wrap_result('<q cite="http://example.org">foo</q>'))
         self.assertEquals(
             self.parse('<<html2>><b>hello</b><</html2>>'),
-            '<b>hello</b>\n')
+            b('<b>hello</b>\n'))
         self.assertEquals(
             self.parse('<<htmlblock>><q cite="http://example.org">foo</q><</htmlblock>>'),
-                '<q cite="http://example.org">foo</q>\n')
+                b('<q cite="http://example.org">foo</q>\n'))
         self.assertEquals(
             self.parse('<<pre>>//no wiki//<</pre>>'),
-            '<pre>**//no wiki//**</pre>\n')
+            b('<pre>**//no wiki//**</pre>\n'))
         self.assertEquals(
             self.parse('<<pre>>one<</pre>>\n<<pre>>two<</pre>>'),
-            '<pre>**one**</pre>\n<pre>**two**</pre>\n')
+            b('<pre>**one**</pre>\n<pre>**two**</pre>\n'))
         self.assertEquals(
             self.parse('<<pre>>one<<pre>>\n<</pre>>two<</pre>>'),
-            '<pre>**one&lt;&lt;pre&gt;&gt;\n&lt;&lt;/pre&gt;&gt;two**</pre>\n')
+            b('<pre>**one&lt;&lt;pre&gt;&gt;\n&lt;&lt;/pre&gt;&gt;two**</pre>\n'))
         self.assertEquals(
             self.parse(u'<<mateo>>fooα<</mateo>>'),
             wrap_result('<em>foo\xce\xb1</em>'))
 part 2
 <</div>>
 part 1b
-<</div>>"""),'<div id="one"><p>part 1a</p>\n<div id="two"><p>part 2</p>\n</div><p>part 1b</p>\n</div>')
+<</div>>"""),b('<div id="one"><p>part 1a</p>\n<div id="two"><p>part 2</p>\n</div><p>part 1b</p>\n</div>'))
         self.assertEquals(
             self.parse("""
 <<div one>>
 <</div>>
 <<div one>>
 part 2
-<</div>>"""),'<div id="one"><p>part 1</p>\n</div><div id="one"><p>part 2</p>\n</div>')
+<</div>>"""),b('<div id="one"><p>part 1</p>\n</div><div id="one"><p>part 2</p>\n</div>'))
 
         
     def test_links(self):
 >
 >Boo2
 """),
-            """<p>Foo</p>\n<div><p>Boo</p>\n<p>Boo2</p>\n</div>\n""")
+            b("""<p>Foo</p>\n<div><p>Boo</p>\n<p>Boo2</p>\n</div>\n"""))
         self.assertEquals(
             self.parse("""\
 Foo
 >>Poo
 >>>Foo
 """),
-            """<p>Foo</p>\n<div><p>Boo\nToo</p>\n<div><p>Poo</p>\n<div><p>Foo</p>\n</div>\n</div>\n</div>\n""")
+            b("""<p>Foo</p>\n<div><p>Boo\nToo</p>\n<div><p>Poo</p>\n<div><p>Foo</p>\n</div>\n</div>\n</div>\n"""))
         self.assertEquals(
             self.parse("""\
 Foo
 
 >>Blaa
 """),
-            """<p>Foo</p>\n<div><p>Boo\nToo</p>\n<div><p>Poo</p>\n</div>\n<p>Foo</p>\n</div>\n<div><div><p>Blaa</p>\n</div>\n</div>\n""")        
+            b("""<p>Foo</p>\n<div><p>Boo\nToo</p>\n<div><p>Poo</p>\n</div>\n<p>Foo</p>\n</div>\n<div><div><p>Blaa</p>\n</div>\n</div>\n"""))        
 class LongDocumentTest(unittest.TestCase):
     """
     """
                 expected_lines.append('<p>'+line+'</p>\n')
         expected = ''.join(expected_lines)
         rendered = text2html(doc)
-        self.assertEquals(text2html(doc), expected)
+        self.assertEquals(text2html(doc), b(expected))
 
     def test_very_long_list(self):
         lines = ['* blaa blaa' for x in range(1000)]
         expected_lines.append('</ul>\n')        
         expected = ''.join(expected_lines)
         rendered = text2html(doc)
-        self.assertEquals(text2html(doc), expected)
+        self.assertEquals(text2html(doc), b(expected))
 
     def test_very_long_table(self):
         lines = ['| blaa blaa' for x in range(1000)]
         expected_lines.append('</table>\n')        
         expected = ''.join(expected_lines)
         rendered = text2html(doc)
-        self.assertEquals(text2html(doc), expected)
+        self.assertEquals(text2html(doc), b(expected))
 
 
 class ContextTest(unittest.TestCase):
 
     def test_inline_context(self):
         result = text2html.render(self.markup, context="inline")
-        self.assertEqual(result, "steve <em>rad</em>")
+        self.assertEqual(result, b("steve <em>rad</em>"))
 
     def test_inline_elements_context(self):
         context = text2html.dialect.inline_elements
         result = text2html.render(self.markup, context=context)
-        self.assertEqual(result, "steve <em>rad</em>")
+        self.assertEqual(result, b("steve <em>rad</em>"))
 
     def test_block_elements_context(self):
         context = text2html.dialect.block_elements
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.