#!/usr/bin/env python from translate.storage import dtd from translate.storage import test_monolingual from translate.misc import wStringIO from py import test def test_roundtrip_quoting(): specials = ['Fish & chips', 'five < six', 'six > five', 'Use ', 'Use ' 'A "solution"', "skop 'n bal", '"""', "'''", '\n', '\t', '\r', 'Escape at end \\', '\\n', '\\t', '\\r', '\\"', '\r\n', '\\r\\n', '\\'] for special in specials: quoted_special = dtd.quotefordtd(special) unquoted_special = dtd.unquotefromdtd(quoted_special) print "special: %r\nquoted: %r\nunquoted: %r\n" % (special, quoted_special, unquoted_special) assert special == unquoted_special class TestDTDUnit(test_monolingual.TestMonolingualUnit): UnitClass = dtd.dtdunit def test_markreview(self): assert test.raises(NotImplementedError, self.unit.markreviewneeded) class TestDTD(test_monolingual.TestMonolingualStore): StoreClass = dtd.dtdfile def dtdparse(self, dtdsource): """helper that parses dtd source without requiring files""" dummyfile = wStringIO.StringIO(dtdsource) dtdfile = dtd.dtdfile(dummyfile) return dtdfile def dtdregen(self, dtdsource): """helper that converts dtd source to dtdfile object and back""" return str(self.dtdparse(dtdsource)) def test_simpleentity(self): """checks that a simple dtd entity definition is parsed correctly""" dtdsource = '\n' dtdfile = self.dtdparse(dtdsource) assert len(dtdfile.units) == 1 dtdunit = dtdfile.units[0] assert dtdunit.entity == "test.me" assert dtdunit.definition == '"bananas for sale"' def test_blanklines(self): """checks that blank lines don't break the parsing or regeneration""" dtdsource = '\n\n' dtdregen = self.dtdregen(dtdsource) assert dtdsource == dtdregen def test_simpleentity_source(self): """checks that a simple dtd entity definition can be regenerated as source""" dtdsource = '\n' dtdregen = self.dtdregen(dtdsource) assert dtdsource == dtdregen def test_hashcomment_source(self): """checks that a #expand comment is retained in the source""" dtdsource = '#expand \n' dtdregen = self.dtdregen(dtdsource) assert dtdsource == dtdregen def test_commentclosing(self): """tests that comment closes with trailing space aren't duplicated""" dtdsource = ' \n\n' dtdregen = self.dtdregen(dtdsource) assert dtdsource == dtdregen def test_commententity(self): """check that we don't process messages in : bug 102""" dtdsource = '''''' dtdfile = self.dtdparse(dtdsource) assert len(dtdfile.units) == 1 dtdunit = dtdfile.units[0] print dtdunit assert dtdunit.isnull() def test_newlines_in_entity(self): """tests that we can handle newlines in the entity itself""" dtdsource = '''