1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
import sys
import unittest
from osrf_pycommon.cli_utils import verb_pattern
called = None
class TestCliUtilsVerbPattern(unittest.TestCase):
def test_call_prepare_arguments(self):
global called
cpa = verb_pattern.call_prepare_arguments
# Try with basic, one parameter
called = False
def fake_prepare_arguments(parser):
global called
called = True
if called:
pass
return parser
r = cpa(fake_prepare_arguments, None)
self.assertTrue(called)
self.assertIsNone(r)
# Try with args
called = False
def fake_prepare_arguments(parser, args):
global called
called = True
if called:
pass
return parser
r = cpa(fake_prepare_arguments, None)
self.assertTrue(called)
self.assertIsNone(r)
# Try with self
called = False
class Foo:
def fake_prepare_arguments(self, parser, args):
global called
called = True
if called:
pass
return parser
f = Foo()
r = cpa(f.fake_prepare_arguments, None)
self.assertTrue(called)
self.assertIsNone(r)
# Try with more than needed
called = False
class Foo:
def fake_prepare_arguments(self, parser, args, extra):
global called
called = True
if called:
pass
return parser
f = Foo()
# Remove the following if condition and keep else condition once
# Xenial is dropped
if sys.version_info[0] < 3:
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
r = cpa(f.fake_prepare_arguments, None)
else:
with self.assertRaisesRegex(ValueError, 'one or two parameters'):
r = cpa(f.fake_prepare_arguments, None)
# Try with less than needed
called = False
class Foo:
def fake_prepare_arguments(self):
global called
called = True
if called:
pass
return 'Should not get here'
f = Foo()
# Remove the following if condition and keep else condition once
# Xenial is dropped
if sys.version_info[0] < 3:
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
r = cpa(f.fake_prepare_arguments, None)
else:
with self.assertRaisesRegex(ValueError, 'one or two parameters'):
r = cpa(f.fake_prepare_arguments, None)
# Try with additional optional argument
called = False
class Foo:
def fake_prepare_arguments(self, parser, args, optional=None):
global called
called = True
if called:
pass
return parser
f = Foo()
r = cpa(f.fake_prepare_arguments, None)
self.assertTrue(called)
self.assertIsNone(r)
def test_split_arguments_by_verb(self):
args = ['--cmd-arg1', 'verb', '--verb-arg1', '--verb-arg2']
expected = ('verb', ['--cmd-arg1'], ['--verb-arg1', '--verb-arg2'])
self.assertEqual(verb_pattern.split_arguments_by_verb(args), expected)
args = ['verb', '--verb-arg1', '--verb-arg2']
expected = ('verb', [], ['--verb-arg1', '--verb-arg2'])
self.assertEqual(verb_pattern.split_arguments_by_verb(args), expected)
args = ['--cmd-arg1', 'verb']
expected = ('verb', ['--cmd-arg1'], [])
self.assertEqual(verb_pattern.split_arguments_by_verb(args), expected)
args = ['verb']
expected = ('verb', [], [])
self.assertEqual(verb_pattern.split_arguments_by_verb(args), expected)
args = ['--cmd-arg1']
expected = (None, ['--cmd-arg1'], [])
self.assertEqual(verb_pattern.split_arguments_by_verb(args), expected)
|