def test_do_not_show_marker(self): """Test marker is not shown when no marker is specified.""" import itertools stop = 2 it_1 = list(tools.islice_with_ellipsis(self.it, stop, marker=None)) it_2 = list(itertools.islice(self.it, stop)) self.assertEqual(it_1, it_2) # same behavior as islice().
def display_references(self): """ Display pages which links the current page, sorted per namespace. Number of pages to display per namespace is provided by: - self.getOption('isorphan') """ refs = self.current_page.ref_table if refs: total = sum(len(v) for v in refs.values()) pywikibot.warning('There are %d pages who link to %s.' % (total, self.current_page)) else: return show_n_pages = self.getOption('isorphan') width = len(max((ns.canonical_prefix() for ns in refs), key=len)) for ns in sorted(refs): n_pages_in_ns = len(refs[ns]) plural = '' if n_pages_in_ns == 1 else 's' ns_name = ns.canonical_prefix() if ns != ns.MAIN else 'Main:' ns_id = '[{0}]'.format(ns.id) pywikibot.output( ' {0!s:<{width}} {1:>6} {2:>10} page{pl}'.format( ns_name, ns_id, n_pages_in_ns, width=width, pl=plural)) if show_n_pages: # do not show marker if 0 pages are requested. for page in islice_with_ellipsis(refs[ns], show_n_pages): pywikibot.output(' {0!s}'.format(page.title()))
def test_do_not_show_marker_when_get_all(self): """Test marker is not shown when all elements are retrieved.""" stop = None it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertEqual(len(it), len(self.it)) self.assertEqual(it, self.it) self.assertNotEqual(it[-1], "…")
def test_do_not_show_marker_when_get_all(self): """Test marker is not shown when all elements are retrieved.""" stop = None it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertLength(it, len(self.it)) self.assertEqual(it, self.it) self.assertNotEqual(it[-1], '…')
def display_references(self) -> None: """ Display pages that link to the current page, sorted per namespace. Number of pages to display per namespace is provided by: - self.getOption('isorphan') """ refs = self.current_page.ref_table if not refs: return total = sum(len(v) for v in refs.values()) if total > 1: pywikibot.warning('There are {} pages that link to {}.'.format( total, self.current_page)) else: pywikibot.warning('There is a page that links to {}.'.format( self.current_page)) show_n_pages = self.getOption('isorphan') width = len(max((ns.canonical_prefix() for ns in refs), key=len)) for ns in sorted(refs): n_pages_in_ns = len(refs[ns]) plural = '' if n_pages_in_ns == 1 else 's' ns_name = ns.canonical_prefix() if ns != ns.MAIN else 'Main:' ns_id = '[{0}]'.format(ns.id) pywikibot.output( ' {0!s:<{width}} {1:>6} {2:>10} page{pl}'.format( ns_name, ns_id, n_pages_in_ns, width=width, pl=plural)) if show_n_pages: # do not show marker if 0 pages are requested. for page in islice_with_ellipsis(refs[ns], show_n_pages): pywikibot.output(' {0!s}'.format(page.title()))
def test_show_default_marker(self): """Test marker is shown without kwargs.""" stop = 2 it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertLength(it, stop + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[:stop]) self.assertEqual(it[-1], '…')
def test_show_default_marker(self): """Test marker is shown without kwargs.""" stop = 2 it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertEqual(len(it), stop + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[:stop]) self.assertEqual(it[-1], "…")
def test_show_marker_with_start_stop(self): """Test marker is shown with start and stop without kwargs.""" start = 1 stop = 3 it = list(tools.islice_with_ellipsis(self.it, start, stop)) self.assertLength(it, stop - start + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[start:stop]) self.assertEqual(it[-1], '…')
def test_show_custom_marker(self): """Test correct marker is shown with kwargs..""" stop = 2 it = list(tools.islice_with_ellipsis(self.it, stop, marker='new')) self.assertLength(it, stop + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[:stop]) self.assertNotEqual(it[-1], '…') self.assertEqual(it[-1], 'new')
def test_show_marker_with_start_stop(self): """Test marker is shown with start and stop without kwargs.""" start = 1 stop = 3 it = list(tools.islice_with_ellipsis(self.it, start, stop)) self.assertEqual(len(it), stop - start + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[start:stop]) self.assertEqual(it[-1], "…")
def test_show_custom_marker(self): """Test correct marker is shown with kwargs..""" stop = 2 it = list(tools.islice_with_ellipsis(self.it, stop, marker="new")) self.assertEqual(len(it), stop + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[:stop]) self.assertNotEqual(it[-1], "…") self.assertEqual(it[-1], "new")
def test_show_custom_marker_with_start_stop(self): """Test marker is shown with start and stop with kwargs.""" start = 1 stop = 3 it = list(tools.islice_with_ellipsis(self.it, start, stop, marker='new')) self.assertEqual(len(it), stop - start + 1) # +1 to consider marker. self.assertEqual(it[:-1], self.it[start:stop]) self.assertNotEqual(it[-1], '…') self.assertEqual(it[-1], 'new')
def test_do_not_show_marker_with_stop_zero(self): """Test marker is shown with stop for empty iterable.""" stop = 0 it = list(tools.islice_with_ellipsis(self.it_null, stop)) self.assertLength(it, stop)
def test_show_marker_with_stop_zero(self): """Test marker is shown with stop for non empty iterable.""" stop = 0 it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertLength(it, stop + 1) # +1 to consider marker. self.assertEqual(it[-1], '…')
def test_accept_only_keyword_marker(self): """Test that the only kwargs accepted is 'marker'.""" self.assertRaisesRegex(TypeError, "'generator' object is not callable", tools.islice_with_ellipsis(self.it, 1, t=''))
def test_accept_only_keyword_marker(self): """Test that the only kwargs accepted is 'marker'.""" with self.assertRaisesRegex( TypeError, r'islice_with_ellipsis\(\) got an ' "unexpected keyword argument 't'"): tools.islice_with_ellipsis(self.it, 1, t='')
def test_do_not_show_marker_with_stop_zero(self): """Test marker is shown with stop for empty iterable.""" stop = 0 it = list(tools.islice_with_ellipsis(self.it_null, stop)) self.assertEqual(len(it), stop)
def test_show_marker_with_stop_zero(self): """Test marker is shown with stop for non empty iterable.""" stop = 0 it = list(tools.islice_with_ellipsis(self.it, stop)) self.assertEqual(len(it), stop + 1) # +1 to consider marker. self.assertEqual(it[-1], "…")
def test_accept_only_keyword_marker(self): """Test that the only kwargs accepted is 'marker'.""" GENERATOR_NOT_CALLABLE = "'generator' object is not callable" self.assertRaisesRegex(TypeError, GENERATOR_NOT_CALLABLE, tools.islice_with_ellipsis(self.it, 1, t=''))
def test_accept_only_keyword_marker(self): """Test that the only kwargs accepted is 'marker'.""" self.assertRaises(TypeError, tools.islice_with_ellipsis(self.it, 1, t=""))
def test_accept_only_keyword_marker(self): """Test that the only kwargs accepted is 'marker'.""" self.assertRaises(TypeError, tools.islice_with_ellipsis(self.it, 1, t=''))