Mercurial > p > roundup > code
diff 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 |
line wrap: on
line diff
--- a/test/test_templating.py Sat Oct 01 23:40:48 2022 -0400 +++ b/test/test_templating.py Sun Oct 02 23:18:43 2022 -0400 @@ -4,6 +4,7 @@ from roundup.cgi.templating import * from .test_actions import MockNull, true +from .html_norm import NormalizingHtmlParser import pytest @@ -854,29 +855,20 @@ self.assertEqual(p.markdown().strip().replace('\n\n', '\n'), u2s(u'<p>embedded code block <pre></p>\n<pre><code>line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) def test_string_markdown_code_block_attribute(self): - import sys - - _py3 = sys.version_info[0] > 2 + parser = NormalizingHtmlParser() ''' also verify that embedded html is escaped ''' 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')) - m = p.markdown().strip() + m = parser.normalize(p.markdown()) + parser.reset() print(m) if type(self) == MistuneTestCase: - self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block <pre></p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>') + self.assertEqual(m, parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) elif type(self) == MarkdownTestCase: - self.assertEqual(m.replace('\n\n','\n'), '<p>embedded code block <pre></p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>') + self.assertEqual(m, parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) else: - test_output = m.replace('\n\n', '\n') - if _py3: - nl = "\n" - else: - nl = "" - expected_result = '<p>embedded code block <pre></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 </pre> paragraph</p>' % { 'nl': nl } - if test_output != expected_result: - print("test_output:", test_output) - print("expected_result:", expected_result) - self.assertEqual( test_output, expected_result) + expected_result = parser.normalize('<p>embedded code block <pre></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 </pre> paragraph</p>') + self.assertEqual(m, expected_result) def test_markdown_return_text_on_exception(self): ''' string is invalid markdown. missing end of fenced code block '''
