diff --git a/app/__init__.py b/app/__init__.py index 1c95a7f75..db4a51d31 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -347,24 +347,29 @@ def format_notification_status_as_url(status): def formatted_list( - items, - conjunction='and', - before_each='‘', - after_each='’', - separator=', ', - prefix='', - prefix_plural='' + items, + conjunction='and', + before_each='‘', + after_each='’', + separator=', ', + prefix='', + prefix_plural='' ): + if prefix: + prefix += ' ' + if prefix_plural: + prefix_plural += ' ' + items = list(items) if len(items) == 1: - return '{prefix} {before_each}{items[0]}{after_each}'.format(**locals()) + return '{prefix}{before_each}{items[0]}{after_each}'.format(**locals()) elif items: formatted_items = ['{}{}{}'.format(before_each, item, after_each) for item in items] first_items = separator.join(formatted_items[:-1]) last_item = formatted_items[-1] return ( - '{prefix_plural} {first_items} {conjunction} {last_item}' + '{prefix_plural}{first_items} {conjunction} {last_item}' ).format(**locals()) diff --git a/app/templates/views/check.html b/app/templates/views/check.html index ef0788923..b8880fe30 100644 --- a/app/templates/views/check.html +++ b/app/templates/views/check.html @@ -47,8 +47,8 @@

Your file has {{ recipients.column_headers | formatted_list( - prefix='one column, called', - prefix_plural='columns called' + prefix='one column, called ', + prefix_plural='columns called ' ) }}.

{{ skip_to_file_contents() }} @@ -65,15 +65,15 @@

Your file has {{ recipients.column_headers | formatted_list( - prefix='one column, called', - prefix_plural='columns called' + prefix='one column, called ', + prefix_plural='columns called ' ) }}.

It doesn’t have {{ recipients.column_headers | formatted_list( conjunction='or', - prefix='a column called', - prefix_plural='columns called' + prefix='a column called ', + prefix_plural='columns called ' ) }}.

{{ skip_to_file_contents() }} diff --git a/tests/app/test_jinja_filters.py b/tests/app/test_jinja_filters.py new file mode 100644 index 000000000..2d24fc00e --- /dev/null +++ b/tests/app/test_jinja_filters.py @@ -0,0 +1,16 @@ +import pytest + +from app import formatted_list + + +@pytest.mark.parametrize('items, kwargs, expected_output', [ + ([1], {}, '‘1’'), + ([1, 2], {}, '‘1’ and ‘2’'), + ([1, 2, 3], {}, '‘1’, ‘2’ and ‘3’'), + ([1, 2, 3], {'prefix': 'foo', 'prefix_plural': 'bar'}, 'bar ‘1’, ‘2’ and ‘3’'), + ([1], {'prefix': 'foo', 'prefix_plural': 'bar'}, 'foo ‘1’'), + ([1, 2, 3], {'before_each': 'a', 'after_each': 'b'}, 'a1b, a2b and a3b'), + ([1, 2, 3], {'conjunction': 'foo'}, '‘1’, ‘2’ foo ‘3’'), +]) +def test_formatted_list(items, kwargs, expected_output): + assert formatted_list(items, **kwargs) == expected_output