comparison test/test_templating.py @ 6995:dc83ebff4c90

change test to use html normalizer when comparing html output. Update to Markdown2 parser changed text output keeping same html semantics. Broke test_string_markdown_code_block_attribute test. I hand patched it to get tests working but it needed a better solution. Write a simple html normalizer using HTMLParser so I don't need third party (lxml, beautifulsoup) library to clean up the test. Use the normalizer to parser the expected result and the result returned by the various markdown libraries. Hopefully this will make the test less fragile. This can have multiple uses in template testing where html is compared. I expect to have to change html_norm.py to make test writing easier in the future.
author John Rouillard <rouilj@ieee.org>
date Sun, 02 Oct 2022 23:18:43 -0400
parents a5f30372ef7f
children f529cca242dc
comparison
equal deleted inserted replaced
6994:4336e655b2be 6995:dc83ebff4c90
2 import unittest 2 import unittest
3 from cgi import FieldStorage, MiniFieldStorage 3 from cgi import FieldStorage, MiniFieldStorage
4 4
5 from roundup.cgi.templating import * 5 from roundup.cgi.templating import *
6 from .test_actions import MockNull, true 6 from .test_actions import MockNull, true
7 from .html_norm import NormalizingHtmlParser
7 8
8 9
9 import pytest 10 import pytest
10 from .pytest_patcher import mark_class 11 from .pytest_patcher import mark_class
11 12
852 ''' also verify that embedded html is escaped ''' 853 ''' also verify that embedded html is escaped '''
853 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n```\nline 1\nline 2\n```\n\nnew </pre> paragraph')) 854 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n```\nline 1\nline 2\n```\n\nnew </pre> paragraph'))
854 self.assertEqual(p.markdown().strip().replace('\n\n', '\n'), u2s(u'<p>embedded code block &lt;pre&gt;</p>\n<pre><code>line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>')) 855 self.assertEqual(p.markdown().strip().replace('\n\n', '\n'), u2s(u'<p>embedded code block &lt;pre&gt;</p>\n<pre><code>line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>'))
855 856
856 def test_string_markdown_code_block_attribute(self): 857 def test_string_markdown_code_block_attribute(self):
857 import sys 858 parser = NormalizingHtmlParser()
858
859 _py3 = sys.version_info[0] > 2
860 859
861 ''' also verify that embedded html is escaped ''' 860 ''' also verify that embedded html is escaped '''
862 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n``` python\nline 1\nline 2\n```\n\nnew </pre> paragraph')) 861 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n``` python\nline 1\nline 2\n```\n\nnew </pre> paragraph'))
863 m = p.markdown().strip() 862 m = parser.normalize(p.markdown())
863 parser.reset()
864 print(m) 864 print(m)
865 if type(self) == MistuneTestCase: 865 if type(self) == MistuneTestCase:
866 self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>') 866 self.assertEqual(m, parser.normalize('<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>'))
867 elif type(self) == MarkdownTestCase: 867 elif type(self) == MarkdownTestCase:
868 self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>') 868 self.assertEqual(m, parser.normalize('<p>embedded code block &lt;pre&gt;</p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new &lt;/pre&gt; paragraph</p>'))
869 else: 869 else:
870 test_output = m.replace('\n\n', '\n') 870 expected_result = parser.normalize('<p>embedded code block &lt;pre&gt;</p>\n<div class="codehilite"><pre><span></span><code><span class="n">line</span> <span class="mi">1</span>\n<span class="n">line</span> <span class="mi">2</span>\n</code></pre></div>\n<p>new &lt;/pre&gt; paragraph</p>')
871 if _py3: 871 self.assertEqual(m, expected_result)
872 nl = "\n"
873 else:
874 nl = ""
875 expected_result = '<p>embedded code block &lt;pre&gt;</p>\n<div class="codehilite">%(nl)s<pre><span></span><code><span class="n">line</span> <span class="mi">1</span>\n<span class="n">line</span> <span class="mi">2</span>\n</code></pre>%(nl)s</div>\n<p>new &lt;/pre&gt; paragraph</p>' % { 'nl': nl }
876 if test_output != expected_result:
877 print("test_output:", test_output)
878 print("expected_result:", expected_result)
879 self.assertEqual( test_output, expected_result)
880 872
881 def test_markdown_return_text_on_exception(self): 873 def test_markdown_return_text_on_exception(self):
882 ''' string is invalid markdown. missing end of fenced code block ''' 874 ''' string is invalid markdown. missing end of fenced code block '''
883 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n``` python\nline 1\nline 2\n\n\nnew </pre> paragraph')) 875 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'embedded code block <pre>\n\n``` python\nline 1\nline 2\n\n\nnew </pre> paragraph'))
884 m = p.markdown().strip() 876 m = p.markdown().strip()

Roundup Issue Tracker: http://roundup-tracker.org/