368 |
368 |
finish.assert_called_once()
|
369 |
369 |
|
370 |
370 |
|
|
371 |
class ArgumentParseManager(TestCase):
|
|
372 |
|
|
373 |
@patch('kamaki.cli.argument.ArgumentParseManager.parse')
|
|
374 |
@patch('kamaki.cli.argument.ArgumentParseManager.update_parser')
|
|
375 |
def test___init__(self, parse, update_parser):
|
|
376 |
for arguments in (None, {'k1': 'v1', 'k2': 'v2'}):
|
|
377 |
apm = argument.ArgumentParseManager('exe', arguments)
|
|
378 |
self.assertTrue(isinstance(apm, argument.ArgumentParseManager))
|
|
379 |
|
|
380 |
self.assertTrue(isinstance(apm.parser, argument.ArgumentParser))
|
|
381 |
self.assertFalse(apm.parser.add_help)
|
|
382 |
self.assertEqual(
|
|
383 |
apm.parser.formatter_class,
|
|
384 |
argument.RawDescriptionHelpFormatter)
|
|
385 |
|
|
386 |
self.assertEqual(
|
|
387 |
apm.syntax, 'exe <cmd_group> [<cmd_subbroup> ...] <cmd>')
|
|
388 |
assert_dicts_are_equal(
|
|
389 |
self, apm.arguments,
|
|
390 |
arguments or argument._arguments)
|
|
391 |
self.assertFalse(apm._parser_modified)
|
|
392 |
self.assertEqual(apm._parsed, None)
|
|
393 |
self.assertEqual(apm._unparsed, None)
|
|
394 |
self.assertEqual(parse.mock_calls[-1], call())
|
|
395 |
if arguments:
|
|
396 |
update_parser.assert_called_once()
|
|
397 |
|
|
398 |
def test_syntax(self):
|
|
399 |
apm = argument.ArgumentParseManager('exe')
|
|
400 |
self.assertEqual(
|
|
401 |
apm.syntax, 'exe <cmd_group> [<cmd_subbroup> ...] <cmd>')
|
|
402 |
apm.syntax = 'some syntax'
|
|
403 |
self.assertEqual(apm.syntax, 'some syntax')
|
|
404 |
|
|
405 |
@patch('kamaki.cli.argument.ArgumentParseManager.update_parser')
|
|
406 |
def test_arguments(self, update_parser):
|
|
407 |
apm = argument.ArgumentParseManager('exe')
|
|
408 |
assert_dicts_are_equal(self, apm.arguments, argument._arguments)
|
|
409 |
update_parser.assert_called_once()
|
|
410 |
exp = {'k1': 'v1', 'k2': 'v2'}
|
|
411 |
apm.arguments = exp
|
|
412 |
assert_dicts_are_equal(self, apm.arguments, exp)
|
|
413 |
self.assertEqual(update_parser.mock_calls[-1], call())
|
|
414 |
try:
|
|
415 |
apm.arguments = None
|
|
416 |
except Exception as e:
|
|
417 |
self.assertTrue(isinstance(e, AssertionError))
|
|
418 |
|
|
419 |
@patch('kamaki.cli.argument.ArgumentParseManager.parse')
|
|
420 |
def test_parsed(self, parse):
|
|
421 |
apm = argument.ArgumentParseManager('exe')
|
|
422 |
self.assertEqual(apm.parsed, None)
|
|
423 |
exp = 'you have been parsed'
|
|
424 |
apm._parsed = exp
|
|
425 |
self.assertEqual(apm.parsed, exp)
|
|
426 |
apm._parser_modified = True
|
|
427 |
apm._parsed = exp + ' v2'
|
|
428 |
self.assertEqual(apm.parsed, exp + ' v2')
|
|
429 |
self.assertEqual(parse.mock_calls, [call(), call()])
|
|
430 |
|
|
431 |
@patch('kamaki.cli.argument.ArgumentParseManager.parse')
|
|
432 |
def test_unparsed(self, parse):
|
|
433 |
apm = argument.ArgumentParseManager('exe')
|
|
434 |
self.assertEqual(apm.unparsed, None)
|
|
435 |
exp = 'you have been unparsed'
|
|
436 |
apm._unparsed = exp
|
|
437 |
self.assertEqual(apm.unparsed, exp)
|
|
438 |
apm._parser_modified = True
|
|
439 |
apm._unparsed = exp + ' v2'
|
|
440 |
self.assertEqual(apm.unparsed, exp + ' v2')
|
|
441 |
self.assertEqual(parse.mock_calls, [call(), call()])
|
|
442 |
|
|
443 |
@patch('kamaki.cli.argument.Argument.update_parser')
|
|
444 |
def test_update_parser(self, update_parser):
|
|
445 |
apm = argument.ArgumentParseManager('exe')
|
|
446 |
body_count = len(update_parser.mock_calls)
|
|
447 |
exp = len(argument._arguments)
|
|
448 |
self.assertEqual(body_count, exp)
|
|
449 |
apm.update_parser()
|
|
450 |
exp = len(apm.arguments) + body_count
|
|
451 |
body_count = len(update_parser.mock_calls)
|
|
452 |
self.assertEqual(body_count, exp)
|
|
453 |
expd = dict(
|
|
454 |
k1=argument.Argument(0, parsed_name='-a'),
|
|
455 |
k2=argument.Argument(0, parsed_name='-b'))
|
|
456 |
apm.update_parser(expd)
|
|
457 |
body_count = len(update_parser.mock_calls)
|
|
458 |
self.assertEqual(body_count, exp + 2)
|
|
459 |
|
371 |
460 |
if __name__ == '__main__':
|
372 |
461 |
from sys import argv
|
373 |
462 |
from kamaki.cli.test import runTestCase
|
... | ... | |
381 |
470 |
runTestCase(VersionArgument, 'VersionArgument', argv[1:])
|
382 |
471 |
runTestCase(KeyValueArgument, 'KeyValueArgument', argv[1:])
|
383 |
472 |
runTestCase(ProgressBarArgument, 'ProgressBarArgument', argv[1:])
|
|
473 |
runTestCase(ArgumentParseManager, 'ArgumentParseManager', argv[1:])
|