(self)
| 152 | msgfmt.MESSAGES.clear() |
| 153 | |
| 154 | def test_strings(self): |
| 155 | # Test that the PO parser correctly handles and unescape |
| 156 | # strings in the PO file. |
| 157 | # The PO file format allows for a variety of escape sequences, |
| 158 | # octal and hex escapes. |
| 159 | valid_strings = ( |
| 160 | # empty strings |
| 161 | ('""', ''), |
| 162 | ('"" "" ""', ''), |
| 163 | # allowed escape sequences |
| 164 | (r'"\\"', '\\'), |
| 165 | (r'"\""', '"'), |
| 166 | (r'"\t"', '\t'), |
| 167 | (r'"\n"', '\n'), |
| 168 | (r'"\r"', '\r'), |
| 169 | (r'"\f"', '\f'), |
| 170 | (r'"\a"', '\a'), |
| 171 | (r'"\b"', '\b'), |
| 172 | (r'"\v"', '\v'), |
| 173 | # non-empty strings |
| 174 | ('"foo"', 'foo'), |
| 175 | ('"foo" "bar"', 'foobar'), |
| 176 | ('"foo""bar"', 'foobar'), |
| 177 | ('"" "foo" ""', 'foo'), |
| 178 | # newlines and tabs |
| 179 | (r'"foo\nbar"', 'foo\nbar'), |
| 180 | (r'"foo\n" "bar"', 'foo\nbar'), |
| 181 | (r'"foo\tbar"', 'foo\tbar'), |
| 182 | (r'"foo\t" "bar"', 'foo\tbar'), |
| 183 | # escaped quotes |
| 184 | (r'"foo\"bar"', 'foo"bar'), |
| 185 | (r'"foo\"" "bar"', 'foo"bar'), |
| 186 | (r'"foo\\" "bar"', 'foo\\bar'), |
| 187 | # octal escapes |
| 188 | (r'"\120\171\164\150\157\156"', 'Python'), |
| 189 | (r'"\120\171\164" "\150\157\156"', 'Python'), |
| 190 | (r'"\"\120\171\164" "\150\157\156\""', '"Python"'), |
| 191 | # hex escapes |
| 192 | (r'"\x50\x79\x74\x68\x6f\x6e"', 'Python'), |
| 193 | (r'"\x50\x79\x74" "\x68\x6f\x6e"', 'Python'), |
| 194 | (r'"\"\x50\x79\x74" "\x68\x6f\x6e\""', '"Python"'), |
| 195 | ) |
| 196 | |
| 197 | with temp_cwd(): |
| 198 | for po_string, expected in valid_strings: |
| 199 | with self.subTest(po_string=po_string): |
| 200 | # Construct a PO file with a single entry, |
| 201 | # compile it, read it into a catalog and |
| 202 | # check the result. |
| 203 | po = f'msgid {po_string}\nmsgstr "translation"' |
| 204 | Path('messages.po').write_text(po) |
| 205 | # Reset the global MESSAGES dictionary |
| 206 | msgfmt.MESSAGES.clear() |
| 207 | msgfmt.make('messages.po', 'messages.mo') |
| 208 | |
| 209 | with open('messages.mo', 'rb') as f: |
| 210 | actual = GNUTranslations(f) |
| 211 |
nothing calls this directly
no test coverage detected