2020-07-22 21:02:31 +02:00
from unittest . mock import MagicMock , call , patch
2016-05-24 14:06:34 +02:00
from django . template . loader import get_template
2019-08-10 18:24:59 +02:00
2018-12-23 00:13:57 +01:00
from zerver . lib . exceptions import InvalidMarkdownIncludeStatement
2020-06-11 00:54:34 +02:00
from zerver . lib . test_classes import ZulipTestCase
2016-05-24 14:06:34 +02:00
2016-08-23 02:08:42 +02:00
class TemplateTestCase ( ZulipTestCase ) :
2017-11-05 10:51:25 +01:00
def test_markdown_in_template ( self ) - > None :
2016-05-11 19:01:53 +02:00
template = get_template ( " tests/test_markdown.html " )
context = {
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
' markdown_test_file ' : " zerver/tests/markdown/test_markdown.md " ,
2016-05-11 19:01:53 +02:00
}
content = template . render ( context )
content_sans_whitespace = content . replace ( " " , " " ) . replace ( ' \n ' , ' ' )
self . assertEqual ( content_sans_whitespace ,
2016-11-09 21:52:42 +01:00
' header<h1id= " hello " >Hello!</h1><p>Thisissome<em>boldtext</em>.</p>footer ' )
2016-05-11 19:29:29 +02:00
2018-09-07 00:09:51 +02:00
def test_markdown_tabbed_sections_extension ( self ) - > None :
template = get_template ( " tests/test_markdown.html " )
context = {
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
' markdown_test_file ' : " zerver/tests/markdown/test_tabbed_sections.md " ,
2018-09-07 00:09:51 +02:00
}
content = template . render ( context )
content_sans_whitespace = content . replace ( " " , " " ) . replace ( ' \n ' , ' ' )
# Note that the expected HTML has a lot of stray <p> 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 <p> 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
< h1 id = " heading " > Heading < / h1 >
< p >
2019-02-08 04:55:50 +01:00
< div class = " code-section has-tabs " markdown = " 1 " >
2018-09-07 00:09:51 +02:00
< ul class = " nav " >
2020-07-29 12:54:39 +02:00
< li data - language = " ios " tabindex = " 0 " > iOS < / li >
< li data - language = " desktop-web " tabindex = " 0 " > Desktop / Web < / li >
2018-09-07 00:09:51 +02:00
< / ul >
< div class = " blocks " >
< div data - language = " ios " markdown = " 1 " > < / p >
< p > iOS instructions < / p >
< p > < / div >
< div data - language = " desktop-web " markdown = " 1 " > < / p >
< p > Desktop / browser instructions < / p >
< p > < / div >
< / div >
< / div >
< / p >
< h2 id = " heading-2 " > Heading 2 < / h2 >
< p >
2019-02-08 04:55:50 +01:00
< div class = " code-section has-tabs " markdown = " 1 " >
2018-09-07 00:09:51 +02:00
< ul class = " nav " >
2020-07-29 12:54:39 +02:00
< li data - language = " desktop-web " tabindex = " 0 " > Desktop / Web < / li >
< li data - language = " android " tabindex = " 0 " > Android < / li >
2018-09-07 00:09:51 +02:00
< / ul >
< div class = " blocks " >
< div data - language = " desktop-web " markdown = " 1 " > < / p >
< p > Desktop / browser instructions < / p >
< p > < / div >
< div data - language = " android " markdown = " 1 " > < / p >
< p > Android instructions < / p >
< p > < / div >
< / div >
< / div >
< / p >
2019-02-08 04:55:50 +01:00
< h2 id = " heading-3 " > Heading 3 < / h2 >
< p >
< div class = " code-section no-tabs " markdown = " 1 " >
< ul class = " nav " >
2020-07-29 12:54:39 +02:00
< li data - language = " null_tab " tabindex = " 0 " > None < / li >
2019-02-08 04:55:50 +01:00
< / ul >
< div class = " blocks " >
< div data - language = " null_tab " markdown = " 1 " > < / p >
< p > Instructions for all platforms < / p >
< p > < / div >
< / div >
< / div >
< / p >
2018-09-07 00:09:51 +02:00
footer
"""
expected_html_sans_whitespace = expected_html . replace ( " " , " " ) . replace ( ' \n ' , ' ' )
self . assertEqual ( content_sans_whitespace ,
expected_html_sans_whitespace )
2018-07-07 22:14:30 +02:00
def test_markdown_nested_code_blocks ( self ) - > None :
template = get_template ( " tests/test_markdown.html " )
context = {
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
' markdown_test_file ' : " zerver/tests/markdown/test_nested_code_blocks.md " ,
2018-07-07 22:14:30 +02:00
}
content = template . render ( context )
content_sans_whitespace = content . replace ( " " , " " ) . replace ( ' \n ' , ' ' )
expected = ( ' header<h1id= " this-is-a-heading " >Thisisaheading.</h1><ol> '
' <li><p>Alistitemwithanindentedcodeblock:</p><divclass= " codehilite " > '
' <pre>indentedcodeblockwithmultiplelines</pre></div></li></ol> '
2020-04-18 03:17:21 +02:00
' <divclass= " codehilite " ><pre><span></span><code> '
' non-indentedcodeblockwithmultiplelines</code></pre></div>footer ' )
2018-07-07 22:14:30 +02:00
self . assertEqual ( content_sans_whitespace , expected )
2020-07-22 21:02:31 +02:00
@patch ( ' builtins.print ' )
def test_custom_markdown_include_extension ( self , mock_print : MagicMock ) - > None :
2018-12-23 00:13:57 +01:00
template = get_template ( " tests/test_markdown.html " )
context = {
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
' markdown_test_file ' : " zerver/tests/markdown/test_custom_include_extension.md " ,
2018-12-23 00:13:57 +01:00
}
2020-08-11 01:47:49 +02:00
with self . assertRaisesRegex ( InvalidMarkdownIncludeStatement , " Invalid Markdown include statement " ) :
2018-12-23 00:13:57 +01:00
template . render ( context )
2020-07-22 21:02:31 +02:00
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 ' " )
] )
2018-12-23 00:13:57 +01:00
def test_custom_markdown_include_extension_empty_macro ( self ) - > None :
template = get_template ( " tests/test_markdown.html " )
context = {
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
' markdown_test_file ' : " zerver/tests/markdown/test_custom_include_extension_empty.md " ,
2018-12-23 00:13:57 +01:00
}
content = template . render ( context )
content_sans_whitespace = content . replace ( " " , " " ) . replace ( ' \n ' , ' ' )
expected = ' headerfooter '
self . assertEqual ( content_sans_whitespace , expected )