aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--qtip/reporter/filters.py11
-rw-r--r--tests/unit/reporter/filters_test.py14
2 files changed, 20 insertions, 5 deletions
diff --git a/qtip/reporter/filters.py b/qtip/reporter/filters.py
index c0c379df..52b34bc6 100644
--- a/qtip/reporter/filters.py
+++ b/qtip/reporter/filters.py
@@ -8,7 +8,16 @@
##############################################################################
-def justify(pair, width=80, padding_with='.'):
+def _justify_pair(pair, width=80, padding_with='.'):
"""align first element along the left margin, second along the right, padding spaces"""
n = width - len(pair[0])
return '{key}{value:{c}>{n}}'.format(key=pair[0], value=pair[1], c=padding_with, n=n)
+
+
+def justify(content, width=80, padding_with='.'):
+ if isinstance(content, list):
+ return '\n'.join([justify(item, width, padding_with) for item in content])
+ elif isinstance(content, dict):
+ return '\n'.join([justify(item, width, padding_with) for item in content.items()])
+ else:
+ return _justify_pair(content, width, padding_with)
diff --git a/tests/unit/reporter/filters_test.py b/tests/unit/reporter/filters_test.py
index 2ced9304..ab96e554 100644
--- a/tests/unit/reporter/filters_test.py
+++ b/tests/unit/reporter/filters_test.py
@@ -7,13 +7,19 @@
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
+from jinja2 import Environment
+import pytest
from qtip.reporter import filters
-from jinja2 import Environment
-def test_justify():
+@pytest.mark.parametrize('template, content, output', [
+ ('{{ content|justify(width=6) }}', [('k1', 'v1'), ('k2', 'v2')], 'k1..v1\nk2..v2'),
+ ('{{ content|justify(width=6) }}', ('k1', 'v1'), 'k1..v1'),
+ ('{{ content|justify(width=6) }}', {'k1': 'v1'}, 'k1..v1')
+])
+def test_justify(template, content, output):
env = Environment()
env.filters['justify'] = filters.justify
- template = env.from_string('{{ kvpair|justify(width=10) }}')
- assert template.render(kvpair=('key', 'value')) == 'key..value'
+ template = env.from_string(template)
+ assert template.render(content=content) == output