1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4import codecs 5import os 6import subprocess 7import sys 8import tempfile 9import unittest 10 11sys.path.insert(1, os.path.abspath('.')) 12 13import polib 14 15from polib import u 16 17class TestFunctions(unittest.TestCase): 18 19 def test_pofile_and_mofile1(self): 20 """ 21 Test bad usage of pofile/mofile. 22 """ 23 data = u('''# test for pofile/mofile with string buffer 24msgid "" 25msgstr "" 26"Project-Id-Version: django\n" 27 28msgid "foo" 29msgstr "bar" 30''') 31 po = polib.pofile(data) 32 self.assertTrue(isinstance(po, polib.POFile)) 33 self.assertEqual(po.encoding, 'utf-8') 34 self.assertEqual(po[0].msgstr, u("bar")) 35 36 def test_indented_pofile(self): 37 """ 38 Test that an indented pofile returns a POFile instance. 39 """ 40 po = polib.pofile('tests/test_indented.po') 41 self.assertTrue(isinstance(po, polib.POFile)) 42 43 def test_pofile_and_mofile2(self): 44 """ 45 Test that the pofile function returns a POFile instance. 46 """ 47 po = polib.pofile('tests/test_utf8.po') 48 self.assertTrue(isinstance(po, polib.POFile)) 49 50 def test_pofile_and_mofile3(self): 51 """ 52 Test that the mofile function returns a MOFile instance. 53 """ 54 mo = polib.mofile('tests/test_utf8.mo') 55 self.assertTrue(isinstance(mo, polib.MOFile)) 56 57 def test_pofile_and_mofile4(self): 58 """ 59 Test that check_for_duplicates is passed to the instance. 60 """ 61 po = polib.pofile('tests/test_iso-8859-15.po', check_for_duplicates=True, 62 autodetect_encoding=False, encoding='iso-8859-15') 63 self.assertTrue(po.check_for_duplicates == True) 64 65 def test_pofile_and_mofile5(self): 66 """ 67 Test that detect_encoding works as expected. 68 """ 69 po = polib.pofile('tests/test_iso-8859-15.po') 70 self.assertTrue(po.encoding == 'ISO_8859-15') 71 72 def test_pofile_and_mofile6(self): 73 """ 74 Test that encoding is default_encoding when detect_encoding is False. 75 """ 76 po = polib.pofile('tests/test_noencoding.po') 77 self.assertTrue(po.encoding == 'utf-8') 78 79 def test_pofile_and_mofile7(self): 80 """ 81 Test that encoding is ok when encoding is explicitly given. 82 """ 83 po = polib.pofile('tests/test_iso-8859-15.po', encoding='iso-8859-15') 84 self.assertTrue(po.encoding == 'iso-8859-15') 85 86 def test_pofile_and_mofile8(self): 87 """ 88 Test that weird occurrences are correctly parsed. 89 """ 90 po = polib.pofile('tests/test_weird_occurrences.po') 91 self.assertEqual(len(po), 47) 92 93 def test_pofile_and_mofile9(self): 94 """ 95 Test that obsolete previous msgid are ignored 96 """ 97 po = polib.pofile('tests/test_obsolete_previousmsgid.po') 98 self.assertTrue(isinstance(po, polib.POFile)) 99 100 def test_previous_msgid_1(self): 101 """ 102 Test previous msgid multiline. 103 """ 104 po = polib.pofile('tests/test_previous_msgid.po') 105 expected = "\nPartition table entries are not in disk order\n" 106 self.assertEqual( 107 po[0].previous_msgid, 108 expected 109 ) 110 111 def test_previous_msgid_2(self): 112 """ 113 Test previous msgid single line. 114 """ 115 po = polib.pofile('tests/test_previous_msgid.po') 116 expected = "Partition table entries are not in disk order2\n" 117 self.assertEqual( 118 po[1].previous_msgid, 119 expected 120 ) 121 122 def test_previous_msgctxt_1(self): 123 """ 124 Test previous msgctxt multiline. 125 """ 126 po = polib.pofile('tests/test_previous_msgid.po') 127 expected = "\nSome message context" 128 self.assertEqual( 129 po[0].previous_msgctxt, 130 expected 131 ) 132 133 def test_previous_msgctxt_2(self): 134 """ 135 Test previous msgctxt single line. 136 """ 137 po = polib.pofile('tests/test_previous_msgid.po') 138 expected = "Some message context" 139 self.assertEqual( 140 po[1].previous_msgctxt, 141 expected 142 ) 143 144 def test_unescaped_double_quote1(self): 145 """ 146 Test that polib reports an error when unescaped double quote is found. 147 """ 148 data = r''' 149msgid "Some msgid with \"double\" quotes" 150msgid "Some msgstr with "double\" quotes" 151''' 152 try: 153 po = polib.pofile(data) 154 self.fail("Unescaped quote not detected") 155 except IOError: 156 exc = sys.exc_info()[1] 157 msg = 'Syntax error in po file (line 3): unescaped double quote found' 158 self.assertEqual(str(exc), msg) 159 160 def test_unescaped_double_quote2(self): 161 """ 162 Test that polib reports an error when unescaped double quote is found. 163 """ 164 data = r''' 165msgid "Some msgid with \"double\" quotes" 166msgstr "" 167"Some msgstr with "double\" quotes" 168''' 169 try: 170 po = polib.pofile(data) 171 self.fail("Unescaped quote not detected") 172 except IOError: 173 exc = sys.exc_info()[1] 174 msg = 'Syntax error in po file (line 4): unescaped double quote found' 175 self.assertEqual(str(exc), msg) 176 177 def test_unescaped_double_quote3(self): 178 """ 179 Test that polib reports an error when unescaped double quote is found at the beginning of the string. 180 """ 181 data = r''' 182msgid "Some msgid with \"double\" quotes" 183msgid ""Some msgstr with double\" quotes" 184''' 185 try: 186 po = polib.pofile(data) 187 self.fail("Unescaped quote not detected") 188 except IOError: 189 exc = sys.exc_info()[1] 190 msg = 'Syntax error in po file (line 3): unescaped double quote found' 191 self.assertEqual(str(exc), msg) 192 193 def test_unescaped_double_quote4(self): 194 """ 195 Test that polib reports an error when unescaped double quote is found at the beginning of the string. 196 """ 197 data = r''' 198msgid "Some msgid with \"double\" quotes" 199msgstr "" 200""Some msgstr with double\" quotes" 201''' 202 try: 203 po = polib.pofile(data) 204 self.fail("Unescaped quote not detected") 205 except IOError: 206 exc = sys.exc_info()[1] 207 msg = 'Syntax error in po file (line 4): unescaped double quote found' 208 self.assertEqual(str(exc), msg) 209 210 def test_detect_encoding1(self): 211 """ 212 Test that given encoding is returned when file has no encoding defined. 213 """ 214 self.assertEqual(polib.detect_encoding('tests/test_noencoding.po'), 'utf-8') 215 216 def test_detect_encoding2(self): 217 """ 218 Test with a .pot file. 219 """ 220 self.assertEqual(polib.detect_encoding('tests/test_merge.pot'), 'utf-8') 221 222 def test_detect_encoding3(self): 223 """ 224 Test with an utf8 .po file. 225 """ 226 self.assertEqual(polib.detect_encoding('tests/test_utf8.po'), 'UTF-8') 227 228 def test_detect_encoding4(self): 229 """ 230 Test with utf8 data (no file). 231 """ 232 if polib.PY3: 233 f = open('tests/test_utf8.po', 'rb') 234 data = str(f.read(), 'utf-8') 235 else: 236 f = open('tests/test_utf8.po', 'r') 237 data = f.read() 238 try: 239 self.assertEqual(polib.detect_encoding(data), 'UTF-8') 240 finally: 241 f.close() 242 243 def test_detect_encoding5(self): 244 """ 245 Test with utf8 .mo file. 246 """ 247 self.assertEqual(polib.detect_encoding('tests/test_utf8.mo', True), 'UTF-8') 248 249 def test_detect_encoding6(self): 250 """ 251 Test with iso-8859-15 .po file. 252 """ 253 self.assertEqual(polib.detect_encoding('tests/test_iso-8859-15.po'), 'ISO_8859-15') 254 255 def test_detect_encoding7(self): 256 """ 257 Test with iso-8859-15 .mo file. 258 """ 259 self.assertEqual(polib.detect_encoding('tests/test_iso-8859-15.mo', True), 'ISO_8859-15') 260 261 def test_escape(self): 262 """ 263 Tests the escape function. 264 """ 265 self.assertEqual( 266 polib.escape('\\t and \\n and \\r and " and \\ and \\\\'), 267 '\\\\t and \\\\n and \\\\r and \\" and \\\\ and \\\\\\\\' 268 ) 269 270 def test_unescape(self): 271 """ 272 Tests the unescape function. 273 """ 274 self.assertEqual( 275 polib.unescape('\\\\t and \\\\n and \\\\r and \\\\" and \\\\\\\\'), 276 '\\t and \\n and \\r and \\" and \\\\' 277 ) 278 279 def test_pofile_with_subclass(self): 280 """ 281 Test that the pofile function correctly returns an instance of the 282 passed in class 283 """ 284 class CustomPOFile(polib.POFile): 285 pass 286 287 pofile = polib.pofile('tests/test_indented.po', klass=CustomPOFile) 288 self.assertEqual(pofile.__class__, CustomPOFile) 289 290 def test_mofile_with_subclass(self): 291 """ 292 Test that the mofile function correctly returns an instance of the 293 passed in class 294 """ 295 class CustomMOFile(polib.MOFile): 296 pass 297 298 mofile = polib.mofile('tests/test_utf8.mo', klass=CustomMOFile) 299 self.assertEqual(mofile.__class__, CustomMOFile) 300 301 def test_empty(self): 302 po = polib.pofile('') 303 self.assertEqual(po.__unicode__(), '#\nmsgid ""\nmsgstr ""\n') 304 305 def test_linenum_1(self): 306 po = polib.pofile('tests/test_utf8.po') 307 self.assertEqual(po[0].linenum, 17) 308 309 def test_linenum_2(self): 310 po = polib.pofile('tests/test_utf8.po') 311 self.assertEqual(po.find('XML text').linenum, 1798) 312 313 def test_linenum_3(self): 314 po = polib.pofile('tests/test_utf8.po') 315 self.assertEqual(po[-1].linenum, 3477) 316 317 def test_windows_path_occurrences(self): 318 po = polib.pofile('tests/test_weird_occurrences.po') 319 self.assertEqual(po[0].occurrences, [('C:\\foo\\bar.py', '12')]) 320 321 322class TestBaseFile(unittest.TestCase): 323 """ 324 Tests for the _BaseFile class. 325 """ 326 327 def test_append1(self): 328 pofile = polib.pofile('tests/test_pofile_helpers.po') 329 entry = polib.POEntry(msgid="Foo", msgstr="Bar", msgctxt="Some context") 330 pofile.append(entry) 331 self.assertTrue(entry in pofile) 332 333 def test_append2(self): 334 def add_duplicate(): 335 pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True) 336 pofile.append(polib.POEntry(msgid="and")) 337 self.assertRaises(ValueError, add_duplicate) 338 339 def test_append3(self): 340 def add_duplicate(): 341 pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True) 342 pofile.append(polib.POEntry(msgid="and", msgctxt="some context")) 343 self.assertRaises(ValueError, add_duplicate) 344 345 def test_append4(self): 346 pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True) 347 entry = polib.POEntry(msgid="and", msgctxt="some different context") 348 pofile.append(entry) 349 self.assertTrue(entry in pofile) 350 351 def test_insert1(self): 352 pofile = polib.pofile('tests/test_pofile_helpers.po') 353 entry = polib.POEntry(msgid="Foo", msgstr="Bar", msgctxt="Some context") 354 pofile.insert(0, entry) 355 self.assertEqual(pofile[0], entry) 356 357 def test_insert2(self): 358 def add_duplicate(): 359 pofile = polib.pofile('tests/test_pofile_helpers.po', check_for_duplicates=True) 360 pofile.insert(0, polib.POEntry(msgid="and", msgstr="y")) 361 self.assertRaises(ValueError, add_duplicate) 362 363 def test_metadata_as_entry(self): 364 pofile = polib.pofile('tests/test_fuzzy_header.po') 365 f = open('tests/test_fuzzy_header.po') 366 lines = f.readlines()[2:] 367 f.close() 368 self.assertEqual(pofile.metadata_as_entry().__unicode__(), "".join(lines)) 369 370 def test_find1(self): 371 pofile = polib.pofile('tests/test_pofile_helpers.po') 372 entry = pofile.find('and') 373 self.assertEqual(entry.msgstr, u('y')) 374 375 def test_find2(self): 376 pofile = polib.pofile('tests/test_pofile_helpers.po') 377 entry = pofile.find('pacote', by="msgstr") 378 self.assertEqual(entry, None) 379 380 def test_find3(self): 381 pofile = polib.pofile('tests/test_pofile_helpers.po') 382 entry = pofile.find('package', include_obsolete_entries=True) 383 self.assertEqual(entry.msgstr, u('pacote')) 384 385 def test_find4(self): 386 pofile = polib.pofile('tests/test_utf8.po') 387 entry1 = pofile.find('test context', msgctxt='@context1') 388 entry2 = pofile.find('test context', msgctxt='@context2') 389 self.assertEqual(entry1.msgstr, u('test context 1')) 390 self.assertEqual(entry2.msgstr, u('test context 2')) 391 392 def test_find5(self): 393 pofile = polib.pofile('tests/test_msgctxt.po') 394 entry1 = pofile.find('some string') 395 entry2 = pofile.find('some string', msgctxt='Some message context') 396 self.assertEqual(entry1.msgstr, u('une cha\u00eene sans contexte')) 397 self.assertEqual(entry2.msgstr, u('une cha\u00eene avec contexte')) 398 399 def test_save1(self): 400 pofile = polib.POFile() 401 self.assertRaises(IOError, pofile.save) 402 403 def test_save2(self): 404 fd, tmpfile = tempfile.mkstemp() 405 os.close(fd) 406 try: 407 pofile = polib.POFile() 408 pofile.save(tmpfile) 409 pofile.save() 410 self.assertTrue(os.path.isfile(tmpfile)) 411 finally: 412 os.remove(tmpfile) 413 414 def test_ordered_metadata(self): 415 pofile = polib.pofile('tests/test_fuzzy_header.po') 416 f = open('tests/test_fuzzy_header.po') 417 lines = f.readlines()[2:] 418 f.close() 419 mdata = [ 420 ('Project-Id-Version', u('PACKAGE VERSION')), 421 ('Report-Msgid-Bugs-To', u('')), 422 ('POT-Creation-Date', u('2010-02-08 16:57+0100')), 423 ('PO-Revision-Date', u('YEAR-MO-DA HO:MI+ZONE')), 424 ('Last-Translator', u('FULL NAME <EMAIL@ADDRESS>')), 425 ('Language-Team', u('LANGUAGE <LL@li.org>')), 426 ('MIME-Version', u('1.0')), 427 ('Content-Type', u('text/plain; charset=UTF-8')), 428 ('Content-Transfer-Encoding', u('8bit')), 429 ('X-Poedit-SearchPath-1', u('Foo')), 430 ('X-Poedit-SearchPath-2', u('Bar')), 431 ('X-Poedit-SearchPath-10', u('Baz')), 432 ] 433 self.assertEqual(pofile.ordered_metadata(), mdata) 434 435 def test_unicode1(self): 436 pofile = polib.pofile('tests/test_merge_after.po') 437 f = codecs.open('tests/test_merge_after.po', encoding='utf8') 438 expected = f.read() 439 f.close() 440 self.assertEqual(pofile.__unicode__(), expected) 441 442 def test_unicode2(self): 443 pofile = polib.pofile('tests/test_iso-8859-15.po') 444 f = codecs.open('tests/test_iso-8859-15.po', encoding='iso-8859-15') 445 expected = f.read() 446 f.close() 447 self.assertEqual(pofile.__unicode__(), expected) 448 449 def test_str(self): 450 pofile = polib.pofile('tests/test_iso-8859-15.po') 451 if polib.PY3: 452 f = codecs.open('tests/test_iso-8859-15.po', encoding='iso-8859-15') 453 else: 454 f = open('tests/test_iso-8859-15.po') 455 expected = f.read() 456 f.close() 457 self.assertEqual(str(pofile), expected) 458 459 def test_wrapping(self): 460 pofile = polib.pofile('tests/test_wrap.po', wrapwidth=50) 461 expected = r'''# test wrapping 462msgid "" 463msgstr "" 464 465msgid "This line will not be wrapped" 466msgstr "" 467 468msgid "" 469"Some line that contain special characters \" and" 470" that \t is very, very, very long...: %s \n" 471msgstr "" 472 473msgid "" 474"Some line that contain special characters " 475"\"foobar\" and that contains whitespace at the " 476"end " 477msgstr "" 478''' 479 self.assertEqual(str(pofile), expected) 480 481 def test_sort(self): 482 a1 = polib.POEntry(msgid='a1', occurrences=[('b.py', 1), ('b.py', 3)]) 483 a2 = polib.POEntry(msgid='a2') 484 a3 = polib.POEntry(msgid='a1', occurrences=[('b.py', 1), ('b.py', 3)], obsolete=True) 485 b1 = polib.POEntry(msgid='b1', occurrences=[('b.py', 1), ('b.py', 3)]) 486 b2 = polib.POEntry(msgid='b2', occurrences=[('d.py', 3), ('b.py', 1)]) 487 c1 = polib.POEntry(msgid='c1', occurrences=[('a.py', 1), ('b.py', 1)]) 488 c2 = polib.POEntry(msgid='c2', occurrences=[('a.py', 1), ('a.py', 3)]) 489 pofile = polib.POFile() 490 pofile.append(b1) 491 pofile.append(a3) 492 pofile.append(a2) 493 pofile.append(a1) 494 pofile.append(b2) 495 pofile.append(c1) 496 pofile.append(c2) 497 pofile.sort() 498 expected = u('''# 499msgid "" 500msgstr "" 501 502msgid "a2" 503msgstr "" 504 505#: a.py:1 a.py:3 506msgid "c2" 507msgstr "" 508 509#: a.py:1 b.py:1 510msgid "c1" 511msgstr "" 512 513#: b.py:1 b.py:3 514msgid "a1" 515msgstr "" 516 517#: b.py:1 b.py:3 518msgid "b1" 519msgstr "" 520 521#: d.py:3 b.py:1 522msgid "b2" 523msgstr "" 524 525#~ msgid "a1" 526#~ msgstr "" 527''') 528 self.assertEqual(pofile.__unicode__(), expected) 529 530 def test_trailing_comment(self): 531 pofile = polib.pofile('tests/test_trailing_comment.po') 532 expected = r'''# 533msgid "" 534msgstr "Content-Type: text/plain; charset=UTF-8\n" 535 536msgid "foo" 537msgstr "oof" 538''' 539 self.assertEqual(str(pofile), expected) 540 541class TestPoFile(unittest.TestCase): 542 """ 543 Tests for PoFile class. 544 """ 545 546 def test_save_as_mofile(self): 547 """ 548 Test for the POFile.save_as_mofile() method. 549 """ 550 import distutils.spawn 551 msgfmt = distutils.spawn.find_executable('msgfmt') 552 if msgfmt is None: 553 try: 554 return unittest.skip('msgfmt is not installed') 555 except AttributeError: 556 return 557 reffiles = ['tests/test_utf8.po', 'tests/test_iso-8859-15.po'] 558 encodings = ['utf-8', 'iso-8859-15'] 559 for reffile, encoding in zip(reffiles, encodings): 560 fd, tmpfile1 = tempfile.mkstemp() 561 os.close(fd) 562 fd, tmpfile2 = tempfile.mkstemp() 563 os.close(fd) 564 po = polib.pofile(reffile, autodetect_encoding=False, encoding=encoding) 565 po.save_as_mofile(tmpfile1) 566 subprocess.call([msgfmt, '--no-hash', '-o', tmpfile2, reffile]) 567 try: 568 f = open(tmpfile1, 'rb') 569 s1 = f.read() 570 f.close() 571 f = open(tmpfile2, 'rb') 572 s2 = f.read() 573 f.close() 574 self.assertEqual(s1, s2) 575 finally: 576 os.remove(tmpfile1) 577 os.remove(tmpfile2) 578 579 def test_merge(self): 580 refpot = polib.pofile('tests/test_merge.pot') 581 po = polib.pofile('tests/test_merge_before.po') 582 po.merge(refpot) 583 expected_po = polib.pofile('tests/test_merge_after.po') 584 self.assertEqual(po, expected_po) 585 586 def test_percent_translated(self): 587 po = polib.pofile('tests/test_pofile_helpers.po') 588 self.assertEqual(po.percent_translated(), 53) 589 po = polib.POFile() 590 self.assertEqual(po.percent_translated(), 100) 591 592 def test_translated_entries(self): 593 po = polib.pofile('tests/test_pofile_helpers.po') 594 self.assertEqual(len(po.translated_entries()), 7) 595 596 def test_untranslated_entries(self): 597 po = polib.pofile('tests/test_pofile_helpers.po') 598 self.assertEqual(len(po.untranslated_entries()), 4) 599 600 def test_fuzzy_entries(self): 601 po = polib.pofile('tests/test_pofile_helpers.po') 602 self.assertEqual(len(po.fuzzy_entries()), 2) 603 604 def test_obsolete_entries(self): 605 po = polib.pofile('tests/test_pofile_helpers.po') 606 self.assertEqual(len(po.obsolete_entries()), 4) 607 608 def test_unusual_metadata_location(self): 609 po = polib.pofile('tests/test_unusual_metadata_location.po') 610 self.assertNotEqual(po.metadata, {}) 611 self.assertEqual(po.metadata['Content-Type'], 'text/plain; charset=UTF-8') 612 613 def test_comment_starting_with_two_hashes(self): 614 po = polib.pofile('tests/test_utf8.po') 615 e = po.find("Some comment starting with two '#'", by='tcomment') 616 self.assertTrue(isinstance(e, polib.POEntry)) 617 618 def test_word_garbage(self): 619 po = polib.pofile('tests/test_word_garbage.po') 620 e = po.find("Whatever", by='msgid') 621 self.assertTrue(isinstance(e, polib.POEntry)) 622 623class TestMoFile(unittest.TestCase): 624 """ 625 Tests for MoFile class. 626 """ 627 def test_dummy_methods(self): 628 """ 629 This is stupid and just here for code coverage. 630 """ 631 mo = polib.MOFile() 632 self.assertEqual(mo.percent_translated(), 100) 633 self.assertEqual(mo.translated_entries(), mo) 634 self.assertEqual(mo.untranslated_entries(), []) 635 self.assertEqual(mo.fuzzy_entries(), []) 636 self.assertEqual(mo.obsolete_entries(), []) 637 638 def test_save_as_pofile(self): 639 """ 640 Test for the MOFile.save_as_pofile() method. 641 """ 642 fd, tmpfile = tempfile.mkstemp() 643 os.close(fd) 644 mo = polib.mofile('tests/test_utf8.mo', wrapwidth=78) 645 mo.save_as_pofile(tmpfile) 646 try: 647 if polib.PY3: 648 f = open(tmpfile, encoding='utf-8') 649 else: 650 f = open(tmpfile) 651 s1 = f.read() 652 f.close() 653 if polib.PY3: 654 f = open('tests/test_save_as_pofile.po', encoding='utf-8') 655 else: 656 f = open('tests/test_save_as_pofile.po') 657 s2 = f.read() 658 f.close() 659 self.assertEqual(s1, s2) 660 finally: 661 os.remove(tmpfile) 662 663 def test_msgctxt(self): 664 #import pdb; pdb.set_trace() 665 mo = polib.mofile('tests/test_msgctxt.mo') 666 expected = u('''msgid "" 667msgstr "Content-Type: text/plain; charset=UTF-8\u005cn" 668 669msgctxt "Some message context" 670msgid "some string" 671msgstr "une cha\u00eene avec contexte" 672 673msgctxt "Some other message context" 674msgid "singular" 675msgid_plural "plural" 676msgstr[0] "singulier" 677msgstr[1] "pluriel" 678 679msgid "some string" 680msgstr "une cha\u00eene sans contexte" 681''') 682 self.assertEqual(mo.__unicode__(), expected) 683 684 def test_invalid_version(self): 685 self.assertRaises(IOError, polib.mofile, 'tests/test_invalid_version.mo') 686 polib.mofile('tests/test_version_1.1.mo') 687 688 def test_no_header(self): 689 mo = polib.mofile('tests/test_no_header.mo') 690 expected = u('''msgid "" 691msgstr "" 692 693msgid "bar" 694msgstr "rab" 695 696msgid "foo" 697msgstr "oof" 698''') 699 self.assertEqual(mo.__unicode__(), expected) 700 701 702class TestTextWrap(unittest.TestCase): 703 704 def test_wrap1(self): 705 text = ' Some line that is longer than fifteen characters (whitespace will not be preserved) ' 706 ret = polib.TextWrapper(width=15).wrap(text) 707 expected = [ 708 ' Some line', 'that is longer', 'than fifteen', 'characters', 709 '(whitespace', 'will not be', 'preserved)' 710 ] 711 self.assertEqual(ret, expected) 712 713 def test_wrap2(self): 714 text = ' Some line that is longer than fifteen characters (whitespace will be preserved) ' 715 ret = polib.TextWrapper(width=15, drop_whitespace=False).wrap(text) 716 expected = [ 717 ' Some line ', 'that is longer ', 'than fifteen ', 'characters ', 718 '(whitespace ', 'will be ', 'preserved) ' 719 ] 720 self.assertEqual(ret, expected) 721 722if __name__ == '__main__': 723 unittest.main() 724