test_ok2/py/apigen/source/testing/test_html.py

150 lines
5.5 KiB
Python

""" test of html generation
"""
from py.__.apigen.source.html import create_html, HTMLDocument
from py.__.apigen.source.browser import parse_path
from py.xml import html
import py
import os
def create_html_and_show(path):
mod = parse_path(path)
html = create_html(mod)
testfile = py.test.ensuretemp("htmloutput").ensure("test.html")
testfile.write(unicode(html))
return testfile
def test_basic():
tmp = py.test.ensuretemp("sourcehtml")
inp = tmp.ensure("one.py")
inp.write(py.code.Source("""
def func_one():
pass
def func_two(x, y):
x = 1
y = 2
return x + y
class B:
pass
class A(B):
def meth1(self):
pass
def meth2(self):
pass
"""))
testfile = create_html_and_show(inp)
data = testfile.open().read()
assert data.find('<a href="#func_one"') != -1
assert data.find('<a href="#func_two"') != -1
assert data.find('<a href="#B"') != -1
assert data.find('<a href="#A"') != -1
assert data.find('<a href="#A.meth1"') != -1
class _HTMLDocument(HTMLDocument):
def __init__(self):
pass
class TestHTMLDocument(object):
def test_head(self):
doc = _HTMLDocument()
head = doc.create_head()
assert isinstance(head, html.head)
rendered = unicode(head)
assert rendered.find('<title>source view</title>') > -1
assert py.std.re.search('<style type="text/css">[^<]+</style>',
rendered)
def test_body(self):
doc = _HTMLDocument()
body = doc.create_body()
assert unicode(body) == '<body></body>'
def test_table(self):
doc = _HTMLDocument()
table, tbody = doc.create_table()
assert isinstance(table, html.table)
assert isinstance(tbody, html.tbody)
assert tbody == table[0]
def prepare_line(self, line, doc=None):
if doc is None:
doc = HTMLDocument()
l = doc.prepare_line(line)
return ''.join([unicode(i) for i in l])
def test_prepare_line_basic(self):
result = self.prepare_line(['see if this works'])
assert result == 'see <span class="keyword">if</span> this works'
result = self.prepare_line(['see if this ',
html.a('works', name='works'),' too'])
assert result == ('see <span class="keyword">if</span> this '
'<a name="works">works</a> too')
result = self.prepare_line(['see if something else works'])
assert result == ('see <span class="keyword">if</span> something '
'<span class="keyword">else</span> works')
result = self.prepare_line(['see if something ',
html.a('else', name='else'), ' works too'])
assert result == ('see <span class="keyword">if</span> something '
'<a name="else">else</a> works too')
def test_prepare_line_strings(self):
result = self.prepare_line(['foo = "bar"'])
assert result == 'foo = <span class="string">&quot;bar&quot;</span>'
result = self.prepare_line(['"spam"'])
assert result == '<span class="string">&quot;spam&quot;</span>'
# test multiline strings
doc = HTMLDocument()
result = self.prepare_line(['"""start of multiline'], doc)
assert result == ('<span class="string">&quot;&quot;&quot;start of '
'multiline</span>')
# doc should now be in 'string mode'
result = self.prepare_line(['see if it doesn\'t touch this'], doc)
assert result == ('<span class="string">see if it doesn&apos;t touch '
'this</span>')
result = self.prepare_line(['"""'], doc)
assert result == '<span class="string">&quot;&quot;&quot;</span>'
result = self.prepare_line(['see if it colours this again'], doc)
assert result == ('see <span class="keyword">if</span> it colours '
'this again')
def test_add_row(self):
doc = HTMLDocument()
doc.add_row(1, ['""" this is a foo implementation """'])
doc.add_row(2, [''])
doc.add_row(3, ['class ', html.a('Foo', name='Foo'), ':'])
doc.add_row(4, [' pass'])
tbody = doc.tbody
assert len(tbody) == 4
assert unicode(tbody[0][0]) == '<td class="lineno">1</td>'
assert unicode(tbody[0][1]) == ('<td class="code">'
'<span class="string">'
'&quot;&quot;&quot; '
'this is a foo implementation '
'&quot;&quot;&quot;'
'</span></td>')
assert unicode(tbody[1][1]) == '<td class="code">&#xa0;</td>'
assert unicode(tbody[2][1]) == ('<td class="code">'
'<span class="alt_keyword">class'
'</span> '
'<a name="Foo">Foo</a>:</td>')
assert unicode(tbody[3][1]) == ('<td class="code"> '
'<span class="alt_keyword">pass'
'</span></td>')
def test_unicode(self):
doc = HTMLDocument()
h = unicode(doc)
print h
assert py.std.re.match(r'<html>\s*<head>\s*<title>[^<]+</title>'
'.*</body>\w*</html>$', h, py.std.re.S)