#!/usr/bin/env python from translate.convert import html2po from translate.convert import po2html from translate.convert import test_convert from translate.misc import wStringIO from translate.storage import po from translate.storage import html class TestHTML2PO: def html2po(self, markup): """Helper to convert html to po without a file.""" inputfile = wStringIO.StringIO(markup) convertor = html2po.html2po() outputpo = convertor.convertfile(inputfile, "test", False, False) return outputpo def po2html(self, posource, htmltemplate): """Helper to convert po to html without a file.""" inputfile = wStringIO.StringIO(posource) outputfile = wStringIO.StringIO() templatefile = wStringIO.StringIO(htmltemplate) assert po2html.converthtml(inputfile, outputfile, templatefile) return outputfile.getvalue() def countunits(self, pofile, expected): """helper to check that we got the expected number of messages""" actual = len(pofile.units) if actual > 0: if pofile.units[0].isheader(): actual = actual - 1 print pofile assert actual == expected def compareunit(self, pofile, unitnumber, expected): """helper to validate a PO message""" if not pofile.units[0].isheader(): unitnumber = unitnumber - 1 print 'unit source: ' + str(pofile.units[unitnumber].source) + '|' print 'expected: ' + expected.encode('utf-8') + '|' assert unicode(pofile.units[unitnumber].source) == unicode(expected) def check_single(self, markup, itemtext): """checks that converting this markup produces a single element with value itemtext""" pofile = self.html2po(markup) self.countunits(pofile, 1) self.compareunit(pofile, 1, itemtext) def check_null(self, markup): """checks that converting this markup produces no elements""" pofile = self.html2po(markup) self.countunits(pofile, 0) def test_htmllang(self): """test to ensure that we no longer use the lang attribure""" markup = '''
tag""" self.check_single("
A paragraph.
", "A paragraph.") markup = "First line.
Second line.
tag.""" htmltext = '''
A paragraph is a section in a piece of writing, usually highlighting a particular point or topic. It always begins on a new line and usually with indentation, and it consists of at least one sentence.
''' self.check_single(htmltext, "A paragraph is a section in a piece of writing, usually highlighting a particular point or topic. It always begins on a new line and usually with indentation, and it consists of at least one sentence.") markup = "First\nline.
Second\nline.
A paragraph with hyperlink.
', 'A paragraph with hyperlink.') def test_tag_a_with_linebreak(self): """Test that we can extract the tag with newlines in it.""" htmltext = '''A paragraph with hyperlink and newlines.
''' self.check_single(htmltext, 'A paragraph with hyperlink and newlines.') def test_tag_img(self): """Test that we can extract the alt attribute from the
''', "A picture")
def test_img_empty(self):
"""Test that we can extract the alt attribute from the
'''
self.check_null(htmlsource)
def test_tag_table_summary(self):
"""Test that we can extract the summary attribute."""
self.check_single( '''| Heading One | Heading Two |
|---|---|
| One | Two |
| Heading One | Heading Two |
|---|---|
| Foot One | Foot Two |
| One | Two |
![]() |
You are a Spanish sentence.
", "You are a Spanish sentence.") def test_ul(self): """Test to see if the list itemDuplicate
Duplicate
" pofile = self.html2po(markup) self.countunits(pofile, 2) # FIXME change this so that we check that the KDE comment is correctly added self.compareunit(pofile, 1, "Duplicate") self.compareunit(pofile, 2, "Duplicate") def wtest_multiline_reflow(self): """check that we reflow multiline content to make it more readable for translators""" self.check_single('''Extract this
And thisThe rapid expansion of telecommunications infrastructure in recent\r years has helped to bridge the digital divide to a limited extent.
\r \r \r ''' self.check_single(htmlsource, 'The rapid expansion of telecommunications infrastructure in recent years has helped to bridge the digital divide to a limited extent.') def test_encoding_latin1(self): """Convert HTML input in iso-8859-1 correctly to unicode.""" htmlsource = '''We aim to please \x96 will you aim too, please?
South Africa\x92s language diversity can be challenging.
''' pofile = self.html2po(htmlsource) self.countunits(pofile, 4) self.compareunit(pofile, 3, u'We aim to please \x96 will you aim too, please?') self.compareunit(pofile, 4, u'South Africa\x92s language diversity can be challenging.') def test_strip_html(self): """Ensure that unnecessary html is stripped from the resulting unit.""" htmlsource = '''
|