Revision 46d130c9 kamaki/cli/utils/test.py
b/kamaki/cli/utils/test.py | ||
---|---|---|
201 | 201 |
call_counter = len(PR.mock_calls) |
202 | 202 |
self.assertEqual(sorted(real_calls), sorted(exp_calls)) |
203 | 203 |
|
204 |
@patch('__builtin__.raw_input') |
|
205 |
def test_page_hold(self, RI): |
|
206 |
from kamaki.cli.utils import page_hold |
|
207 |
ri_counter = 0 |
|
208 |
for args, expected in ( |
|
209 |
((0, 0, 0), False), |
|
210 |
((1, 3, 10), True), |
|
211 |
((3, 3, 10), True), |
|
212 |
((5, 3, 10), True), |
|
213 |
((6, 3, 10), True), |
|
214 |
((10, 3, 10), False), |
|
215 |
((11, 3, 10), False)): |
|
216 |
self.assertEqual(page_hold(*args), expected) |
|
217 |
index, limit, maxlen = args |
|
218 |
if index and index < maxlen and index % limit == 0: |
|
219 |
self.assertEqual(ri_counter + 1, len(RI.mock_calls)) |
|
220 |
self.assertEqual(RI.mock_calls[-1], call( |
|
221 |
'(%s listed - %s more - "enter" to continue)' % ( |
|
222 |
index, maxlen - index))) |
|
223 |
else: |
|
224 |
self.assertEqual(ri_counter, len(RI.mock_calls)) |
|
225 |
ri_counter = len(RI.mock_calls) |
|
226 |
|
|
227 |
@patch('kamaki.cli.utils._print') |
|
228 |
@patch('kamaki.cli.utils._write') |
|
229 |
@patch('kamaki.cli.utils.print_dict') |
|
230 |
@patch('kamaki.cli.utils.print_list') |
|
231 |
@patch('kamaki.cli.utils.page_hold') |
|
232 |
@patch('kamaki.cli.utils.bold', return_value='bold') |
|
233 |
def test_print_items(self, bold, PH, PL, PD, WR, PR): |
|
234 |
from kamaki.cli.utils import print_items, INDENT_TAB |
|
235 |
for args in product( |
|
236 |
( |
|
237 |
42, None, 'simple outputs', |
|
238 |
[1, 2, 3], {1: 1, 2: 2}, (3, 4), |
|
239 |
({'k': 1, 'id': 2}, [5, 6, 7], (8, 9), '10')), |
|
240 |
(('id', 'name'), ('something', 2), ('lala', )), |
|
241 |
(False, True), |
|
242 |
(False, True), |
|
243 |
(0, 1, 2, 10)): |
|
244 |
items, title, with_enumeration, with_redundancy, page_size = args |
|
245 |
wr_counter, pr_counter = len(WR.mock_calls), len(PR.mock_calls) |
|
246 |
pl_counter, pd_counter = len(PL.mock_calls), len(PD.mock_calls) |
|
247 |
bold_counter, ph_counter = len(bold.mock_calls), len(PH.mock_calls) |
|
248 |
print_items(*args) |
|
249 |
if not (isinstance(items, dict) or isinstance( |
|
250 |
items, list) or isinstance(items, tuple)): |
|
251 |
self.assertEqual(PR.mock_calls[-1], call( |
|
252 |
'%s' % items if items is not None else '')) |
|
253 |
else: |
|
254 |
for i, item in enumerate(items): |
|
255 |
if with_enumeration: |
|
256 |
self.assertEqual( |
|
257 |
WR.mock_calls[wr_counter], |
|
258 |
call('%s. ' % (i + 1))) |
|
259 |
wr_counter += 1 |
|
260 |
if isinstance(item, dict): |
|
261 |
title = sorted(set(title).intersection(item)) |
|
262 |
pick = item.get if with_redundancy else item.pop |
|
263 |
header = ' '.join('%s' % pick(key) for key in title) |
|
264 |
self.assertEqual( |
|
265 |
bold.mock_calls[bold_counter], call(header)) |
|
266 |
self.assertEqual( |
|
267 |
PR.mock_calls[pr_counter], call('bold')) |
|
268 |
self.assertEqual( |
|
269 |
PD.mock_calls[pd_counter], |
|
270 |
call(item, indent=INDENT_TAB)) |
|
271 |
pr_counter += 1 |
|
272 |
pd_counter += 1 |
|
273 |
bold_counter += 1 |
|
274 |
elif isinstance(item, list) or isinstance(item, tuple): |
|
275 |
self.assertEqual( |
|
276 |
PL.mock_calls[pl_counter], |
|
277 |
call(item, indent=INDENT_TAB)) |
|
278 |
pl_counter += 1 |
|
279 |
else: |
|
280 |
self.assertEqual( |
|
281 |
PR.mock_calls[pr_counter], call(' %s' % item)) |
|
282 |
pr_counter += 1 |
|
283 |
page_size = page_size if page_size > 0 else len(items) |
|
284 |
self.assertEqual( |
|
285 |
PH.mock_calls[ph_counter], |
|
286 |
call(i + 1, page_size, len(items))) |
|
287 |
ph_counter += 1 |
|
288 |
|
|
204 | 289 |
|
205 | 290 |
if __name__ == '__main__': |
206 | 291 |
from sys import argv |
Also available in: Unified diff