2016-08-04 00:35:53 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
try:
|
|
|
|
from tools.lib.template_parser import (
|
2016-08-08 02:21:36 +02:00
|
|
|
html_tag_tree,
|
2016-08-04 00:35:53 +02:00
|
|
|
is_django_block_tag,
|
2016-08-08 01:46:44 +02:00
|
|
|
tokenize,
|
2016-08-04 00:35:53 +02:00
|
|
|
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 = '''
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>foo</td>
|
|
|
|
</tr>
|
|
|
|
</table>'''
|
|
|
|
validate(text=my_html)
|
2016-08-08 01:46:44 +02:00
|
|
|
|
2016-08-08 16:26:33 +02:00
|
|
|
def test_validate_handlebars(self):
|
|
|
|
# type: () -> None
|
|
|
|
my_html = '''
|
|
|
|
{{#with stream}}
|
|
|
|
<p>{{stream}}</p>
|
|
|
|
{{/with}}
|
|
|
|
'''
|
|
|
|
validate(text=my_html)
|
|
|
|
|
2016-08-08 01:46:44 +02:00
|
|
|
def test_tokenize(self):
|
|
|
|
# type: () -> None
|
|
|
|
tag = '<meta whatever>bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'html_special')
|
|
|
|
|
|
|
|
tag = '<a>bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'html_start')
|
|
|
|
self.assertEqual(token.tag, 'a')
|
|
|
|
|
|
|
|
tag = '<br />bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'html_singleton')
|
|
|
|
self.assertEqual(token.tag, 'br')
|
|
|
|
|
|
|
|
tag = '</a>bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'html_end')
|
|
|
|
self.assertEqual(token.tag, 'a')
|
|
|
|
|
|
|
|
tag = '{{#with foo}}bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'handlebars_start')
|
|
|
|
self.assertEqual(token.tag, 'with')
|
|
|
|
|
|
|
|
tag = '{{/with}}bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'handlebars_end')
|
|
|
|
self.assertEqual(token.tag, 'with')
|
|
|
|
|
|
|
|
tag = '{% if foo %}bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'django_start')
|
|
|
|
self.assertEqual(token.tag, 'if')
|
|
|
|
|
|
|
|
tag = '{% endif %}bla'
|
|
|
|
token = tokenize(tag)[0]
|
|
|
|
self.assertEqual(token.kind, 'django_end')
|
|
|
|
self.assertEqual(token.tag, 'if')
|
2016-08-08 02:21:36 +02:00
|
|
|
|
|
|
|
def test_html_tag_tree(self):
|
|
|
|
# type: () -> None
|
|
|
|
html = '''
|
|
|
|
<body><p>Hello world</p></body>
|
|
|
|
'''
|
|
|
|
tree = html_tag_tree(html)
|
|
|
|
self.assertEqual(tree.children[0].children[0].token.s, '<p>')
|