from unittest.mock import MagicMock, call, patch from django.template.loader import get_template from zerver.lib.exceptions import InvalidMarkdownIncludeStatement from zerver.lib.test_classes import ZulipTestCase class TemplateTestCase(ZulipTestCase): def test_markdown_in_template(self) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_markdown.md", } content = template.render(context) content_sans_whitespace = content.replace(" ", "").replace("\n", "") self.assertEqual( content_sans_whitespace, 'headerHello!

Thisissomeboldtext.

footer', ) def test_markdown_tabbed_sections_extension(self) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_tabbed_sections.md", } content = template.render(context) content_sans_whitespace = content.replace(" ", "").replace("\n", "") # Note that the expected HTML has a lot of stray

tags. This is a # consequence of how the Markdown renderer converts newlines to HTML # and how elements are delimited by newlines and so forth. However, # stray

tags are usually matched with closing tags by HTML renderers # so this doesn't affect the final rendered UI in any visible way. expected_html = """ header

Heading

iOS instructions

Desktop/browser instructions

Heading 2

Desktop/browser instructions

Android instructions

Heading 3

Instructions for all platforms

footer """ expected_html_sans_whitespace = expected_html.replace(" ", "").replace("\n", "") self.assertEqual(content_sans_whitespace, expected_html_sans_whitespace) def test_markdown_tabbed_sections_missing_tabs(self) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_tabbed_sections_missing_tabs.md", } expected_regex = "^Tab 'minix' is not present in TAB_SECTION_LABELS in zerver/lib/markdown/tabbed_sections.py$" with self.assertRaisesRegex(ValueError, expected_regex): template.render(context) def test_markdown_nested_code_blocks(self) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_nested_code_blocks.md", } content = template.render(context) content_sans_whitespace = content.replace(" ", "").replace("\n", "") expected = ( 'headerThisisaheading.
    ' '
  1. Alistitemwithanindentedcodeblock:

    ' "
    indentedcodeblockwithmultiplelines
" '
'
            "non-indentedcodeblockwithmultiplelines
footer" ) self.assertEqual(content_sans_whitespace, expected) @patch("builtins.print") def test_custom_markdown_include_extension(self, mock_print: MagicMock) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_custom_include_extension.md", } with self.assertRaisesRegex( InvalidMarkdownIncludeStatement, "Invalid Markdown include statement" ): template.render(context) self.assertEqual( mock_print.mock_calls, [ call( "Warning: could not find file templates/zerver/help/include/nonexistent-macro.md. Error: [Errno 2] No such file or directory: 'templates/zerver/help/include/nonexistent-macro.md'" ) ], ) def test_custom_markdown_include_extension_empty_macro(self) -> None: template = get_template("tests/test_markdown.html") context = { "markdown_test_file": "zerver/tests/markdown/test_custom_include_extension_empty.md", } content = template.render(context) content_sans_whitespace = content.replace(" ", "").replace("\n", "") expected = "headerfooter" self.assertEqual(content_sans_whitespace, expected)