Part of lp.services.tests
|Function||make_test_tarball_1||Generate a test tarball that looks something like a source tarball which|
|Function||make_test_tarball_2||Generate a test tarball string that has some interesting files in a common|
|Function||test_shortlist_returns_all_elements||Override the warning function since by default all warnings raises an|
|Function||test_english_list||The english_list function takes a list of strings and concatenates them|
|Class||TruncateTextTest||No class docstring; 4/4 methods documented|
Generate a test tarball that looks something like a source tarball which has exactly one directory called 'po' which is interesting (i.e. contains some files which look like POT/PO files).
>>> tarball = make_test_tarball_1()
Check it looks vaguely sensible.
>>> names = tarball.getnames() >>> 'uberfrob-0.1/po/cy.po' in names True
Generate a test tarball string that has some interesting files in a common prefix.
>>> tarball = make_test_tarball_2()
Check the expected files are in the archive.
>>> tarball.getnames() ['test', 'test/cy.po', 'test/es.po', 'test/test.pot']
Check the contents.
>>> f = tarball.extractfile('test/cy.po') >>> f.readline() '# Test PO file.\n'
Override the warning function since by default all warnings raises an exception and we can't test the return value of the function.
>>> import warnings
>>> def warn(message, category=None, stacklevel=2): ... if category is None: ... category = 'UserWarning' ... else: ... category = category.__class__.__name__ ... print "%s: %s" % (category, message)
>>> old_warn = warnings.warn >>> warnings.warn = warn
Show that shortlist doesn't crop the results when a warning is printed.
>>> from lp.services.helpers import shortlist >>> shortlist(list(range(10)), longest_expected=5) #doctest: +ELLIPSIS UserWarning: shortlist() should not... [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> shortlist(iter(range(10)), longest_expected=5) #doctest: +ELLIPSIS UserWarning: shortlist() should not... [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Reset our monkey patch.
>>> warnings.warn = old_warn
The english_list function takes a list of strings and concatenates them in a form suitable for inclusion in an English sentence. For lists of 3 or more elements it follows the advice given in The Elements of Style, chapter I, section 2.
>>> from lp.services.helpers import english_list
By default, it joins the last two elements in the list with 'and', and joins the rest of the list with ','. It also adds whitespace around these delimiters as appropriate.
>>> english_list() ''>>> english_list(['Fred']) 'Fred'>>> english_list(['Fred', 'Bob']) 'Fred and Bob'>>> english_list(['Fred', 'Bob', 'Harold']) 'Fred, Bob, and Harold'
It accepts any iterable that yields strings:
>>> english_list('12345') '1, 2, 3, 4, and 5'>>> english_list(str(i) for i in range(5)) '0, 1, 2, 3, and 4'
It does not convert non-string elements:
>>> english_list(range(3)) Traceback (most recent call last): ... TypeError: sequence item 0: expected string, int found
The conjunction can be changed:
>>> english_list('123', 'or') '1, 2, or 3'