def test_print_items(self, bold, PL, PD): from kamaki.cli.utils import print_items, INDENT_TAB for args in product( ( 42, None, 'simple outputs', [1, 2, 3], {1: 1, 2: 2}, (3, 4), ({'k': 1, 'id': 2}, [5, 6, 7], (8, 9), '10')), (('id', 'name'), ('something', 2), ('lala', )), (False, True), (False, True)): items, title, with_enumeration, with_redundancy = args pl_counter, pd_counter = len(PL.mock_calls), len(PD.mock_calls) bold_counter, out_counter = len(bold.mock_calls), 0 out = StringIO() print_items(*args, out=out) out.seek(0) if not (isinstance(items, dict) or isinstance( items, list) or isinstance(items, tuple)): if items: self.assertEqual(out.getvalue(), '%s\n' % items) else: for i, item in enumerate(items): if with_enumeration: exp_str = '%s. ' % (i + 1) self.assertEqual(out.read(len(exp_str)), exp_str) if isinstance(item, dict): title = sorted(set(title).intersection(item)) pick = item.get if with_redundancy else item.pop header = ' '.join('%s' % pick(key) for key in title) if header: self.assertEqual( bold.mock_calls[bold_counter], call(header)) self.assertEqual(out.read(5), 'bold\n') bold_counter += 1 else: out.read(1) self.assertEqual( PD.mock_calls[pd_counter], call(item, indent=INDENT_TAB, out=out)) pd_counter += 1 elif isinstance(item, list) or isinstance(item, tuple): self.assertEqual( PL.mock_calls[pl_counter], call(item, indent=INDENT_TAB, out=out)) pl_counter += 1 else: exp_str = u' %s\n' % item self.assertEqual(out.read(len(exp_str)), exp_str)
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_items(self, *args, **kwargs): kwargs.setdefault('out', self) return print_items(*args, **kwargs)
def _optional_output(self, r): if self['json_output']: print_json(r, out=self._out) elif self['with_output']: print_items([r] if isinstance(r, dict) else r, out=self._out)