def test_print_list(self): from kamaki.cli.utils import print_list, INDENT_TAB out = StringIO() self.assertRaises(AssertionError, print_list, 'non-list non-tuple') self.assertRaises(AssertionError, print_list, {}, indent=-10) for args in product( ( ['v1', ], ('v2', 'v3'), [1, '2', 'v3'], ({'k1': 'v1'}, 2, 'v3'), [(1, 2), 'v2', [(3, 4), {'k3': [5, 6], 'k4': 7}]]), (tuple(), ('v1', ), ('v1', 1), ('v1', 'k3')), (0, 1, 2, 9), (False, True), (False, True)): l, exclude, indent, with_enumeration, recursive_enumeration = args with patch('kamaki.cli.utils.print_dict') as PD: with patch('kamaki.cli.utils.print_list') as PL: pd_calls, pl_calls = 0, 0 print_list(*args, out=out) exp_calls = '' for i, v in enumerate(l): str_v = u' ' * indent str_v += u'%s.' % (i + 1) if with_enumeration else u'' if isinstance(v, dict): if with_enumeration: exp_calls += str_v + '\n' elif i and i < len(l): exp_calls += u'\n' self.assertEqual( PD.mock_calls[pd_calls], call( v, exclude, indent + ( INDENT_TAB if with_enumeration else 0), recursive_enumeration, recursive_enumeration, out)) pd_calls += 1 elif isinstance(v, list) or isinstance(v, tuple): if with_enumeration: exp_calls += str_v + '\n' elif i and i < len(l): exp_calls += u'\n' self.assertEqual( PL.mock_calls[pl_calls], call( v, exclude, indent + INDENT_TAB, recursive_enumeration, recursive_enumeration, out)) pl_calls += 1 elif ('%s' % v) in exclude: continue else: exp_calls += u'%s%s\n' % (str_v, v) self.assertEqual(out.getvalue(), exp_calls) out = StringIO()
def _run(self): from kamaki.cli.utils import print_dict, print_list, print_items self.writeln('Test simple dict:\n- - -') print_dict(self.d1) self.writeln('- - -\n') self.writeln('\nTest simple list:\n- - -') print_list(self.l1) self.writeln('- - -\n') self.writeln('\nTest 2-level dict:\n- - -') print_dict(self.d2) self.writeln('- - -\n') self.writeln('\nTest non-trivial list:\n- - -') print_list(self.l2) self.writeln('- - -') self.writeln('\nTest extreme dict:\n- - -') print_dict(self.d3) self.writeln('- - -\n') self.writeln('Test simple enumerated dict:\n- - -') print_dict(self.d1, with_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest simple enumerated list:\n- - -') print_list(self.l1, with_enumeration=True) self.writeln('- - -\n') self.writeln('Test non-trivial deep-enumerated dict:\n- - -') print_dict(self.d2, with_enumeration=True, recursive_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest non-trivial enumerated list:\n- - -') print_list(self.l2, with_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest print_items with id:\n- - -') print_items([ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}]) self.writeln('- - -') self.writeln('\nTest print_items with id and enumeration:\n- - -') print_items( [ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}], with_enumeration=True) self.writeln('- - -') self.writeln('\nTest print_items with id, title, redundancy:\n- - -') print_items( [ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}], title=('id', 'title'), with_redundancy=True) self.writeln('- - -') self.writeln('\nTest print_items with lists- - -') print_items([['i00', 'i01', 'i02'], [self.l2, 'i11', self.d1], 3]) self.writeln('- - -')
def print_list(self, *args, **kwargs): kwargs.setdefault('out', self._out) return print_list(*args, **kwargs)