fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / test_dtd.py
blobb96a6e7f1f46ca5d589b2332059482922d36efb7
1 #!/usr/bin/env python
3 from translate.storage import dtd
4 from translate.storage import test_monolingual
5 from translate.misc import wStringIO
7 def test_roundtrip_quoting():
8 specials = ['Fish & chips', 'five < six', 'six > five',
9 'Use &nbsp;', 'Use &amp;nbsp;'
10 'A "solution"', "skop 'n bal", '"""', "'''",
11 '\n', '\t', '\r',
12 'Escape at end \\',
13 '\\n', '\\t', '\\r', '\\"', '\r\n', '\\r\\n', '\\']
14 for special in specials:
15 quoted_special = dtd.quotefordtd(special)
16 unquoted_special = dtd.unquotefromdtd(quoted_special)
17 print "special: %r\nquoted: %r\nunquoted: %r\n" % (special, quoted_special, unquoted_special)
18 assert special == unquoted_special
20 class TestDTDUnit(test_monolingual.TestMonolingualUnit):
21 UnitClass = dtd.dtdunit
24 class TestDTD(test_monolingual.TestMonolingualStore):
25 StoreClass = dtd.dtdfile
26 def dtdparse(self, dtdsource):
27 """helper that parses dtd source without requiring files"""
28 dummyfile = wStringIO.StringIO(dtdsource)
29 dtdfile = dtd.dtdfile(dummyfile)
30 return dtdfile
32 def dtdregen(self, dtdsource):
33 """helper that converts dtd source to dtdfile object and back"""
34 return str(self.dtdparse(dtdsource))
36 def test_simpleentity(self):
37 """checks that a simple dtd entity definition is parsed correctly"""
38 dtdsource = '<!ENTITY test.me "bananas for sale">\n'
39 dtdfile = self.dtdparse(dtdsource)
40 assert len(dtdfile.units) == 1
41 dtdunit = dtdfile.units[0]
42 assert dtdunit.entity == "test.me"
43 assert dtdunit.definition == '"bananas for sale"'
45 def test_blanklines(self):
46 """checks that blank lines don't break the parsing or regeneration"""
47 dtdsource = '<!ENTITY test.me "bananas for sale">\n\n'
48 dtdregen = self.dtdregen(dtdsource)
49 assert dtdsource == dtdregen
51 def test_simpleentity_source(self):
52 """checks that a simple dtd entity definition can be regenerated as source"""
53 dtdsource = '<!ENTITY test.me "bananas for sale">\n'
54 dtdregen = self.dtdregen(dtdsource)
55 assert dtdsource == dtdregen
57 def test_hashcomment_source(self):
58 """checks that a #expand comment is retained in the source"""
59 dtdsource = '#expand <!ENTITY lang.version "__MOZILLA_LOCALE_VERSION__">\n'
60 dtdregen = self.dtdregen(dtdsource)
61 assert dtdsource == dtdregen
63 def test_commentclosing(self):
64 """tests that comment closes with trailing space aren't duplicated"""
65 dtdsource = '<!-- little comment --> \n<!ENTITY pane.title "Notifications">\n'
66 dtdregen = self.dtdregen(dtdsource)
67 assert dtdsource == dtdregen
69 def test_commententity(self):
70 """check that we don't process messages in <!-- comments -->: bug 102"""
71 dtdsource = '''<!-- commenting out until bug 38906 is fixed
72 <!ENTITY messagesHeader.label "Messages"> -->'''
73 dtdfile = self.dtdparse(dtdsource)
74 assert len(dtdfile.units) == 1
75 dtdunit = dtdfile.units[0]
76 print dtdunit
77 assert dtdunit.isnull()
79 def test_newlines_in_entity(self):
80 """tests that we can handle newlines in the entity itself"""
81 dtdsource = '''<!ENTITY fileNotFound.longDesc "
82 <ul>
83 <li>Check the file name for capitalisation or other typing errors.</li>
84 <li>Check to see if the file was moved, renamed or deleted.</li>
85 </ul>
87 '''
88 dtdregen = self.dtdregen(dtdsource)
89 print dtdregen
90 print dtdsource
91 assert dtdsource == dtdregen
93 def test_conflate_comments(self):
94 """Tests that comments don't run onto the same line"""
95 dtdsource = '<!-- test comments -->\n<!-- getting conflated -->\n<!ENTITY sample.txt "hello">\n'
96 dtdregen = self.dtdregen(dtdsource)
97 print dtdsource
98 print dtdregen
99 assert dtdsource == dtdregen
101 def test_localisation_notes(self):
102 """test to ensure that we retain the localisation note correctly"""
103 dtdsource = '''<!--LOCALIZATION NOTE (publishFtp.label): Edit box appears beside this label -->
104 <!ENTITY publishFtp.label "If publishing to a FTP site, enter the HTTP address to browse to:">
106 dtdregen = self.dtdregen(dtdsource)
107 assert dtdsource == dtdregen
109 def test_entitityreference_in_source(self):
110 """checks that an &entity; in the source is retained"""
111 dtdsource = '<!ENTITY % realBrandDTD SYSTEM "chrome://branding/locale/brand.dtd">\n%realBrandDTD;\n'
112 dtdregen = self.dtdregen(dtdsource)
113 print dtdsource
114 print dtdregen
115 assert dtdsource == dtdregen
117 def wtest_comment_following(self):
118 """check that comments that appear after and entity are not pushed onto another line"""
119 dtdsource = '<!ENTITY textZoomEnlargeCmd.commandkey2 "="> <!-- + is above this key on many keyboards -->'
120 dtdregen = self.dtdregen(dtdsource)
121 assert dtdsource == dtdregen
123 def test_comment_newline_space_closing(self):
124 """check that comments that are closed by a newline then space then --> don't break the following entries"""
125 dtdsource = '<!-- Comment\n -->\n<!ENTITY searchFocus.commandkey "k">\n'
126 dtdregen = self.dtdregen(dtdsource)
127 assert dtdsource == dtdregen
129 def test_invalid_quoting(self):
130 """checks that invalid quoting doesn't work - quotes can't be reopened"""
131 # TODO: we should rather raise an error
132 dtdsource = '<!ENTITY test.me "bananas for sale""room">\n'
133 assert dtd.unquotefromdtd(dtdsource[dtdsource.find('"'):]) == 'bananas for sale'
134 dtdfile = self.dtdparse(dtdsource)
135 assert len(dtdfile.units) == 1
136 dtdunit = dtdfile.units[0]
137 assert dtdunit.definition == '"bananas for sale"'
138 assert str(dtdfile) == '<!ENTITY test.me "bananas for sale">\n'
140 def test_missing_quotes(self):
141 """test that we fail graacefully when a message without quotes is found (bug #161)"""
142 dtdsource = '<!ENTITY bad no quotes">\n<!ENTITY good "correct quotes">\n'
143 dtdfile = self.dtdparse(dtdsource)
144 # Check that we raise a correct warning
145 assert len(dtdfile.units) == 1