Commit b21972e8 authored by Guido van Rossum's avatar Guido van Rossum

Add tests detailing when <foo/> should be generated and when

<foo></foo>.  (This fails until the next checkins.)
parent cb139821
...@@ -11,12 +11,13 @@ from TAL import HTMLTALParser ...@@ -11,12 +11,13 @@ from TAL import HTMLTALParser
class HTMLTALParserTestCases(unittest.TestCase): class HTMLTALParserTestCases(unittest.TestCase):
def _run_check(self, source, program, macros={}): def _run_check(self, source, program, macros={}):
parser = HTMLTALParser.HTMLTALParser() parser = HTMLTALParser.HTMLTALParser()
parser.parseString(source) parser.parseString(source)
got_program, got_macros = parser.getCode() got_program, got_macros = parser.getCode()
assert got_program == program, got_program self.assert_(got_program == program, got_program)
assert got_macros == macros, got_macros self.assert_(got_macros == macros, got_macros)
def _get_check(self, source, xxx=None): def _get_check(self, source, xxx=None):
parser = HTMLTALParser.HTMLTALParser() parser = HTMLTALParser.HTMLTALParser()
...@@ -40,34 +41,34 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -40,34 +41,34 @@ class HTMLTALParserTestCases(unittest.TestCase):
('setPosition', (1, 4)), ('setPosition', (1, 4)),
('rawtext', '<li>'), ('rawtext', '<li>'),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
('rawtext', '<p/>'), ('rawtext', '<p></p>'),
('setPosition', (1, 11)), ('setPosition', (1, 11)),
('rawtext', '<p/></li>'), ('rawtext', '<p></p></li>'),
('setPosition', (1, 14)), ('setPosition', (1, 14)),
('rawtext', '<li/></ul>'), ('rawtext', '<li></li></ul>'),
]) ])
self._run_check("""<dl><dt><dt><dd><dd><ol><li><li></ol></dl>""", [ self._run_check("""<dl><dt><dt><dd><dd><ol><li><li></ol></dl>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<dl>'), ('rawtext', '<dl>'),
('setPosition', (1, 4)), ('setPosition', (1, 4)),
('rawtext', '<dt/>'), ('rawtext', '<dt></dt>'),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
('rawtext', '<dt/>'), ('rawtext', '<dt></dt>'),
('setPosition', (1, 12)), ('setPosition', (1, 12)),
('rawtext', '<dd/>'), ('rawtext', '<dd></dd>'),
('setPosition', (1, 16)), ('setPosition', (1, 16)),
('rawtext', '<dd>'), ('rawtext', '<dd>'),
('setPosition', (1, 20)), ('setPosition', (1, 20)),
('rawtext', '<ol>'), ('rawtext', '<ol>'),
('setPosition', (1, 24)), ('setPosition', (1, 24)),
('rawtext', '<li/>'), ('rawtext', '<li></li>'),
('setPosition', (1, 28)), ('setPosition', (1, 28)),
('rawtext', '<li/></ol></dd></dl>'), ('rawtext', '<li></li></ol></dd></dl>'),
]) ])
def check_code_implied_table_closings(self): def check_code_implied_table_closings(self):
self._run_check("""<p>text <table><tr><th>head <tr><td>cell """ self._run_check("""<p>text <table><tr><th>head\t<tr><td>cell\t"""
"""<table><tr><td>cell <tr>""", [ """<table><tr><td>cell \n \t \n<tr>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<p>text</p> '), ('rawtext', '<p>text</p> '),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
...@@ -75,19 +76,19 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -75,19 +76,19 @@ class HTMLTALParserTestCases(unittest.TestCase):
('setPosition', (1, 15)), ('setPosition', (1, 15)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 19)), ('setPosition', (1, 19)),
('rawtext', '<th>head</th></tr> '), ('rawtext', '<th>head</th></tr>\t'),
('setPosition', (1, 28)), ('setPosition', (1, 28)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 32)), ('setPosition', (1, 32)),
('rawtext', '<td>cell '), ('rawtext', '<td>cell\t'),
('setPosition', (1, 41)), ('setPosition', (1, 41)),
('rawtext', '<table>'), ('rawtext', '<table>'),
('setPosition', (1, 48)), ('setPosition', (1, 48)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 52)), ('setPosition', (1, 52)),
('rawtext', '<td>cell</td></tr> '), ('rawtext', '<td>cell</td></tr> \n \t \n'),
('setPosition', (1, 61)), ('setPosition', (3, 0)),
('rawtext', '<tr/></table></td></tr></table>'), ('rawtext', '<tr></tr></table></td></tr></table>'),
]) ])
self._run_check("""<table><tr><td>cell """ self._run_check("""<table><tr><td>cell """
"""<table><tr><td>cell </table></table>""", [ """<table><tr><td>cell </table></table>""", [
...@@ -113,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -113,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self): def check_code_attr_syntax(self):
output = [ output = [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="v" c="v" d="v" e="e"/>'), ('rawtext', '<a b="v" c="v" d="v" e="e"></a>'),
] ]
self._run_check("""<a b='v' c="v" d=v e>""", output) self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output) self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
...@@ -125,41 +126,49 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -125,41 +126,49 @@ class HTMLTALParserTestCases(unittest.TestCase):
"""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", """<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
[('setPosition', (1, 0)), [('setPosition', (1, 0)),
('rawtext', ('rawtext',
'<a b="xxx\012\011xxx" c="yyy\011\012yyy" d="\011xyz\012"/>')]) '<a b="xxx\n\txxx" c="yyy\t\nyyy" d="\txyz\n"></a>')])
self._run_check("""<a b='' c="" d=>""", [ self._run_check("""<a b='' c="" d=>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="" c="" d=""/>'), ('rawtext', '<a b="" c="" d=""></a>'),
]) ])
def check_code_attr_entity_replacement(self): def check_code_attr_entity_replacement(self):
# we expect entities *not* to be replaced by HTLMParser! # we expect entities *not* to be replaced by HTLMParser!
self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [ self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&amp;&gt;&lt;&quot;\'"/>'), ('rawtext', '<a b="&amp;&gt;&lt;&quot;\'"></a>'),
]) ])
self._run_check("""<a b='\"'>""", [ self._run_check("""<a b='\"'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', "<a b='\"'/>"), ('rawtext', "<a b='\"'></a>"),
]) ])
self._run_check("""<a b='&'>""", [ self._run_check("""<a b='&'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&amp;"/>'), ('rawtext', '<a b="&amp;"></a>'),
]) ])
self._run_check("""<a b='<'>""", [ self._run_check("""<a b='<'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&lt;"/>'), ('rawtext', '<a b="&lt;"></a>'),
]) ])
def check_code_attr_funky_names(self): def check_code_attr_funky_names(self):
self._run_check("""<a a.b='v' c:d=v e-f=v>""", [ self._run_check("""<a a.b='v' c:d=v e-f=v>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a a.b="v" c:d="v" e-f="v"/>'), ('rawtext', '<a a.b="v" c:d="v" e-f="v"></a>'),
])
def check_code_pcdata_entityref(self):
self._run_check("""&nbsp;""", [
('rawtext', '&nbsp;'),
]) ])
## def check_code_pcdata_entityref(self): def check_code_short_endtags(self):
## self._get_check("""&nbsp;""", [ self._run_check("""<html><img/></html>""", [
## ('rawtext', '&nbsp;'), ('setPosition', (1, 0)),
## ]) ('rawtext', '<html>'),
('setPosition', (1, 6)),
('rawtext', '<img/></html>'),
])
def test_suite(): def test_suite():
......
...@@ -11,12 +11,13 @@ from TAL import HTMLTALParser ...@@ -11,12 +11,13 @@ from TAL import HTMLTALParser
class HTMLTALParserTestCases(unittest.TestCase): class HTMLTALParserTestCases(unittest.TestCase):
def _run_check(self, source, program, macros={}): def _run_check(self, source, program, macros={}):
parser = HTMLTALParser.HTMLTALParser() parser = HTMLTALParser.HTMLTALParser()
parser.parseString(source) parser.parseString(source)
got_program, got_macros = parser.getCode() got_program, got_macros = parser.getCode()
assert got_program == program, got_program self.assert_(got_program == program, got_program)
assert got_macros == macros, got_macros self.assert_(got_macros == macros, got_macros)
def _get_check(self, source, xxx=None): def _get_check(self, source, xxx=None):
parser = HTMLTALParser.HTMLTALParser() parser = HTMLTALParser.HTMLTALParser()
...@@ -40,34 +41,34 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -40,34 +41,34 @@ class HTMLTALParserTestCases(unittest.TestCase):
('setPosition', (1, 4)), ('setPosition', (1, 4)),
('rawtext', '<li>'), ('rawtext', '<li>'),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
('rawtext', '<p/>'), ('rawtext', '<p></p>'),
('setPosition', (1, 11)), ('setPosition', (1, 11)),
('rawtext', '<p/></li>'), ('rawtext', '<p></p></li>'),
('setPosition', (1, 14)), ('setPosition', (1, 14)),
('rawtext', '<li/></ul>'), ('rawtext', '<li></li></ul>'),
]) ])
self._run_check("""<dl><dt><dt><dd><dd><ol><li><li></ol></dl>""", [ self._run_check("""<dl><dt><dt><dd><dd><ol><li><li></ol></dl>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<dl>'), ('rawtext', '<dl>'),
('setPosition', (1, 4)), ('setPosition', (1, 4)),
('rawtext', '<dt/>'), ('rawtext', '<dt></dt>'),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
('rawtext', '<dt/>'), ('rawtext', '<dt></dt>'),
('setPosition', (1, 12)), ('setPosition', (1, 12)),
('rawtext', '<dd/>'), ('rawtext', '<dd></dd>'),
('setPosition', (1, 16)), ('setPosition', (1, 16)),
('rawtext', '<dd>'), ('rawtext', '<dd>'),
('setPosition', (1, 20)), ('setPosition', (1, 20)),
('rawtext', '<ol>'), ('rawtext', '<ol>'),
('setPosition', (1, 24)), ('setPosition', (1, 24)),
('rawtext', '<li/>'), ('rawtext', '<li></li>'),
('setPosition', (1, 28)), ('setPosition', (1, 28)),
('rawtext', '<li/></ol></dd></dl>'), ('rawtext', '<li></li></ol></dd></dl>'),
]) ])
def check_code_implied_table_closings(self): def check_code_implied_table_closings(self):
self._run_check("""<p>text <table><tr><th>head <tr><td>cell """ self._run_check("""<p>text <table><tr><th>head\t<tr><td>cell\t"""
"""<table><tr><td>cell <tr>""", [ """<table><tr><td>cell \n \t \n<tr>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<p>text</p> '), ('rawtext', '<p>text</p> '),
('setPosition', (1, 8)), ('setPosition', (1, 8)),
...@@ -75,19 +76,19 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -75,19 +76,19 @@ class HTMLTALParserTestCases(unittest.TestCase):
('setPosition', (1, 15)), ('setPosition', (1, 15)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 19)), ('setPosition', (1, 19)),
('rawtext', '<th>head</th></tr> '), ('rawtext', '<th>head</th></tr>\t'),
('setPosition', (1, 28)), ('setPosition', (1, 28)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 32)), ('setPosition', (1, 32)),
('rawtext', '<td>cell '), ('rawtext', '<td>cell\t'),
('setPosition', (1, 41)), ('setPosition', (1, 41)),
('rawtext', '<table>'), ('rawtext', '<table>'),
('setPosition', (1, 48)), ('setPosition', (1, 48)),
('rawtext', '<tr>'), ('rawtext', '<tr>'),
('setPosition', (1, 52)), ('setPosition', (1, 52)),
('rawtext', '<td>cell</td></tr> '), ('rawtext', '<td>cell</td></tr> \n \t \n'),
('setPosition', (1, 61)), ('setPosition', (3, 0)),
('rawtext', '<tr/></table></td></tr></table>'), ('rawtext', '<tr></tr></table></td></tr></table>'),
]) ])
self._run_check("""<table><tr><td>cell """ self._run_check("""<table><tr><td>cell """
"""<table><tr><td>cell </table></table>""", [ """<table><tr><td>cell </table></table>""", [
...@@ -113,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -113,7 +114,7 @@ class HTMLTALParserTestCases(unittest.TestCase):
def check_code_attr_syntax(self): def check_code_attr_syntax(self):
output = [ output = [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="v" c="v" d="v" e="e"/>'), ('rawtext', '<a b="v" c="v" d="v" e="e"></a>'),
] ]
self._run_check("""<a b='v' c="v" d=v e>""", output) self._run_check("""<a b='v' c="v" d=v e>""", output)
self._run_check("""<a b = 'v' c = "v" d = v e>""", output) self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
...@@ -125,41 +126,49 @@ class HTMLTALParserTestCases(unittest.TestCase): ...@@ -125,41 +126,49 @@ class HTMLTALParserTestCases(unittest.TestCase):
"""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", """<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
[('setPosition', (1, 0)), [('setPosition', (1, 0)),
('rawtext', ('rawtext',
'<a b="xxx\012\011xxx" c="yyy\011\012yyy" d="\011xyz\012"/>')]) '<a b="xxx\n\txxx" c="yyy\t\nyyy" d="\txyz\n"></a>')])
self._run_check("""<a b='' c="" d=>""", [ self._run_check("""<a b='' c="" d=>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="" c="" d=""/>'), ('rawtext', '<a b="" c="" d=""></a>'),
]) ])
def check_code_attr_entity_replacement(self): def check_code_attr_entity_replacement(self):
# we expect entities *not* to be replaced by HTLMParser! # we expect entities *not* to be replaced by HTLMParser!
self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [ self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&amp;&gt;&lt;&quot;\'"/>'), ('rawtext', '<a b="&amp;&gt;&lt;&quot;\'"></a>'),
]) ])
self._run_check("""<a b='\"'>""", [ self._run_check("""<a b='\"'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', "<a b='\"'/>"), ('rawtext', "<a b='\"'></a>"),
]) ])
self._run_check("""<a b='&'>""", [ self._run_check("""<a b='&'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&amp;"/>'), ('rawtext', '<a b="&amp;"></a>'),
]) ])
self._run_check("""<a b='<'>""", [ self._run_check("""<a b='<'>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a b="&lt;"/>'), ('rawtext', '<a b="&lt;"></a>'),
]) ])
def check_code_attr_funky_names(self): def check_code_attr_funky_names(self):
self._run_check("""<a a.b='v' c:d=v e-f=v>""", [ self._run_check("""<a a.b='v' c:d=v e-f=v>""", [
('setPosition', (1, 0)), ('setPosition', (1, 0)),
('rawtext', '<a a.b="v" c:d="v" e-f="v"/>'), ('rawtext', '<a a.b="v" c:d="v" e-f="v"></a>'),
])
def check_code_pcdata_entityref(self):
self._run_check("""&nbsp;""", [
('rawtext', '&nbsp;'),
]) ])
## def check_code_pcdata_entityref(self): def check_code_short_endtags(self):
## self._get_check("""&nbsp;""", [ self._run_check("""<html><img/></html>""", [
## ('rawtext', '&nbsp;'), ('setPosition', (1, 0)),
## ]) ('rawtext', '<html>'),
('setPosition', (1, 6)),
('rawtext', '<img/></html>'),
])
def test_suite(): def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment