from __future__ import absolute_import from __future__ import print_function import sys import unittest try: from tools.lib.template_parser import ( html_tag_tree, is_django_block_tag, tokenize, validate, ) except ImportError: print('ERROR!!! You need to run this via tools/test-tools.') sys.exit(1) class ParserTest(unittest.TestCase): def test_is_django_block_tag(self): # type: () -> None self.assertTrue(is_django_block_tag('block')) self.assertFalse(is_django_block_tag('not a django tag')) def test_validate_vanilla_html(self): # type: () -> None ''' Verify that validate() does not raise errors for well-formed HTML. ''' my_html = '''
foo |
{{stream}}
{{/with}} ''' validate(text=my_html) def test_validate_django(self): # type: () -> None my_html = ''' {% include "some_other.html" %} {% if foo %}bar
{% endif %} ''' validate(text=my_html) def test_code_blocks(self): # type: () -> None # This is fine. my_html = '''
x = 5
y = x + 1
'''
validate(text=my_html)
# This is also fine.
my_html = "process_widgets()
"
validate(text=my_html)
# This is illegal.
my_html = '''
x =
5
'''
# See https://github.com/python/typeshed/issues/372
# for why we have to ingore types here.
with self.assertRaisesRegexp(Exception, 'split across two lines'): # type: ignore
validate(text=my_html)
def test_anchor_blocks(self):
# type: () -> None
# This is allowed, although strange.
my_html = '''
Click here
for more info.
'''
validate(text=my_html)
# This is fine.
my_html = 'click here'
validate(text=my_html)
# Even this is fine.
my_html = '''
@ZulipStatus on Twitter.
'''
validate(text=my_html)
def test_tokenize(self):
# type: () -> None
tag = 'bla'
token = tokenize(tag)[0]
self.assertEqual(token.kind, 'html_special')
tag = 'bla'
token = tokenize(tag)[0]
self.assertEqual(token.kind, 'html_start')
self.assertEqual(token.tag, 'a')
tag = 'Hello world
''' tree = html_tag_tree(html) self.assertEqual(tree.children[0].children[0].token.s, '')