# -*- coding: UTF-8 -*- """ test of html generation """ from py.__.apigen.source.html import prepare_line, create_html, HTMLDocument, \ get_module_encoding from py.__.apigen.source.browser import parse_path from py.__.apigen.source.color import Tokenizer, PythonSchema 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('source view') > -1 assert py.std.re.search('', rendered) def test_body(self): doc = _HTMLDocument() body = doc.create_body() assert unicode(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 test_add_row(self): doc = HTMLDocument('ascii') 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]) == '1' assert unicode(tbody[0][1]) == ('' '' '""" ' 'this is a foo implementation ' '"""' '') assert unicode(tbody[1][1]) == ' ' assert unicode(tbody[2][1]) == ('' 'class' ' ' 'Foo:') assert unicode(tbody[3][1]) == (' ' 'pass' '') def test_unicode(self): doc = HTMLDocument('ascii') h = unicode(doc) print h assert py.std.re.match(r'\s*\s*[^<]+' '.*\w*$', h, py.std.re.S) def prepare_line_helper(line, tokenizer=None, encoding='ascii'): if tokenizer is None: tokenizer = Tokenizer(PythonSchema) l = prepare_line(line, tokenizer, encoding) return ''.join([unicode(i) for i in l]) def test_prepare_line_basic(): result = prepare_line_helper(['see if this works']) assert result == 'see if this works' result = prepare_line_helper(['see if this ', html.a('works', name='works'),' too']) assert result == ('see if this ' 'works too') result = prepare_line_helper(['see if something else works']) assert result == ('see if something ' 'else works') result = prepare_line_helper(['see if something ', html.a('else', name='else'), ' works too']) assert result == ('see if something ' 'else works too') def test_prepare_line_strings(): result = prepare_line_helper(['foo = "bar"']) assert result == 'foo = "bar"' result = prepare_line_helper(['"spam"']) assert result == '"spam"' def test_prepare_line_multiline_strings(): # test multiline strings t = Tokenizer(PythonSchema) result = prepare_line_helper(['"""start of multiline'], t) assert result == ('"""start of ' 'multiline') result = prepare_line_helper(['see if it doesn\'t touch this'], t) assert result == ('see if it doesn't touch ' 'this') result = prepare_line_helper(['"""'], t) assert result == '"""' result = prepare_line_helper(['see if it colours this again'], t) assert result == ('see if it colours ' 'this again') def test_prepare_line_nonascii(): result = prepare_line_helper(['"föö"'], encoding='UTF-8') assert (result == unicode('"föö"', 'UTF-8')) def test_get_encoding_ascii(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('ascii.py') fpath.write(str(py.code.Source("""\ def foo(): return 'foo' """))) # XXX I think the specs say we have to assume latin-1 here... assert get_module_encoding(fpath.strpath) == 'ISO-8859-1' def test_get_encoding_for_real(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('utf-8.py') fpath.write(str(py.code.Source("""\ #!/usr/bin/env python # -*- coding: UTF-8 -*- def foo(): return 'föö' """))) assert get_module_encoding(fpath.strpath) == 'UTF-8' def test_get_encoding_matching_pattern_elsewhere(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('matching_pattern.py') fpath.write(str(py.code.Source("""\ #!/usr/bin/env python def foo(coding=None): pass """))) assert get_module_encoding(fpath.strpath) == 'ISO-8859-1'