Add support for render a justified output from list and dict 59/33659/2
authorYujun Zhang <zhang.yujunz@zte.com.cn>
Sat, 22 Apr 2017 06:15:13 +0000 (14:15 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Mon, 24 Apr 2017 00:51:48 +0000 (08:51 +0800)
Change-Id: I4411e62b3d1a067cfa8ae1296cf521877aedb830
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
qtip/reporter/filters.py
tests/unit/reporter/filters_test.py

index c0c379d..52b34bc 100644 (file)
@@ -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)
index 2ced930..ab96e55 100644 (file)
@@ -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