summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-09-12 17:36:33 +0000
committersteven.bethard <devnull@localhost>2009-09-12 17:36:33 +0000
commitff7fc5019350fdddede1961aab7a6892e0cd01c2 (patch)
treeef2524615195933491f93b4564ff1255053a77cb
parentadfb02cfbbd3205b5f86a475ac4f9080337eb1c5 (diff)
downloadargparse-ff7fc5019350fdddede1961aab7a6892e0cd01c2.tar.gz
Extra tests from 4kir4.1i.
-rw-r--r--test/test_argparse.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/test/test_argparse.py b/test/test_argparse.py
index 6939380..57de0c4 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -1819,24 +1819,67 @@ class TestParentParsers(TestCase):
self.z_parent = ErrorRaisingArgumentParser(add_help=False)
self.z_parent.add_argument('z')
+ # parents with mutually exclusive groups
+ self.ab_mutex_parent = ErrorRaisingArgumentParser(add_help=False)
+ group = self.ab_mutex_parent.add_mutually_exclusive_group()
+ group.add_argument('-a', action='store_true')
+ group.add_argument('-b', action='store_true')
+
def test_single_parent(self):
parser = ErrorRaisingArgumentParser(
parents=[self.wxyz_parent])
self.assertEqual(parser.parse_args('-y 1 2 --w 3'.split()),
NS(w='3', y='1', z='2'))
+ def test_single_parent_mutex(self):
+ self._test_mutex_ab(self.ab_mutex_parent.parse_args)
+ self._test_mutex_ab(
+ ErrorRaisingArgumentParser(parents=[self.ab_mutex_parent]).parse_args)
+
+ def test_single_granparent_mutex(self):
+ parser = ErrorRaisingArgumentParser(
+ add_help=False, parents=[self.ab_mutex_parent])
+ self._test_mutex_ab(
+ ErrorRaisingArgumentParser(parents=[parser]).parse_args)
+
+ def _test_mutex_ab(self, parse_args):
+ self.assertEqual(parse_args([]), NS(a=False, b=False))
+ self.assertEqual(parse_args(['-a']), NS(a=True, b=False))
+ self.assertEqual(parse_args(['-b']), NS(a=False, b=True))
+ self.assertArgumentParserError(parse_args, ['-a', '-b'])
+ self.assertArgumentParserError(parse_args, ['-b', '-a'])
+ self.assertArgumentParserError(parse_args, ['-c'])
+ self.assertArgumentParserError(parse_args, ['-a', '-c'])
+ self.assertArgumentParserError(parse_args, ['-b', '-c'])
+
def test_multiple_parents(self):
parser = ErrorRaisingArgumentParser(
parents=[self.abcd_parent, self.wxyz_parent])
self.assertEqual(parser.parse_args('--d 1 --w 2 3 4'.split()),
NS(a='3', b=None, d='1', w='2', y=None, z='4'))
+ def test_multiple_parents_mutex(self):
+ parser = ErrorRaisingArgumentParser(
+ parents=[self.ab_mutex_parent, self.wxyz_parent])
+ self.assertEqual(parser.parse_args('-a --w 2 3'.split()),
+ NS(a=True, b=False, w='2', y=None, z='3'))
+ self.assertArgumentParserError(
+ parser.parse_args, '-a --w 2 3 -b'.split())
+ self.assertArgumentParserError(
+ parser.parse_args, '-a -b --w 2 3'.split())
+
def test_conflicting_parents(self):
self.assertRaises(
argparse.ArgumentError,
argparse.ArgumentParser,
parents=[self.w_parent, self.wxyz_parent])
+ def test_conflicting_parents_mutex(self):
+ self.assertRaises(
+ argparse.ArgumentError,
+ argparse.ArgumentParser,
+ parents=[self.abcd_parent, self.ab_mutex_parent])
+
def test_same_argument_name_parents(self):
parser = ErrorRaisingArgumentParser(
parents=[self.wxyz_parent, self.z_parent])
@@ -1852,6 +1895,23 @@ class TestParentParsers(TestCase):
self.assertEqual(parser.parse_args('bar -b 1 --d 2 3 4'.split()),
NS(a='3', b='1', d='2', e='4'))
+ def test_subparser_parents_mutex(self):
+ parser = ErrorRaisingArgumentParser()
+ subparsers = parser.add_subparsers()
+ abc_parser = subparsers.add_parser(
+ 'foo', parents=[self.ab_mutex_parent])
+ c_group = abc_parser.add_argument_group('c_group')
+ c_group.add_argument('c')
+ wxyzabe_parser = subparsers.add_parser(
+ 'bar', parents=[self.wxyz_parent, self.ab_mutex_parent])
+ wxyzabe_parser.add_argument('e')
+ self.assertEqual(parser.parse_args('foo -a 4'.split()),
+ NS(a=True, b=False, c='4'))
+ self.assertEqual(parser.parse_args('bar -b --w 2 3 4'.split()),
+ NS(a=False, b=True, w='2', y=None, z='3', e='4'))
+ self.assertArgumentParserError(parser.parse_args, 'foo -a -b 4'.split())
+ self.assertArgumentParserError(parser.parse_args, 'bar -b -a 4'.split())
+
def test_parent_help(self):
parser = ErrorRaisingArgumentParser(
parents=[self.abcd_parent, self.wxyz_parent])
@@ -2250,6 +2310,40 @@ class TestMutuallyExclusiveOptionalsAndPositionalsMixed(MEMixin, TestCase):
-c c help
'''
+# =================================================
+# Mutually exclusive group in parent parser tests
+# =================================================
+
+class MEPBase(object):
+ def get_parser(self, required=None):
+ parent = super(MEPBase, self).get_parser(required=required)
+ parser = ErrorRaisingArgumentParser(
+ prog=parent.prog, add_help=False, parents=[parent])
+ return parser
+
+class TestMutuallyExclusiveGroupErrorsParent(
+ MEPBase, TestMutuallyExclusiveGroupErrors): pass
+
+class TestMutuallyExclusiveSimpleParent(
+ MEPBase, TestMutuallyExclusiveSimple): pass
+
+class TestMutuallyExclusiveLongParent(
+ MEPBase, TestMutuallyExclusiveLong): pass
+
+class TestMutuallyExclusiveFirstSuppressedParent(
+ MEPBase, TestMutuallyExclusiveFirstSuppressed): pass
+
+class TestMutuallyExclusiveManySuppressedParent(
+ MEPBase, TestMutuallyExclusiveManySuppressed): pass
+
+class TestMutuallyExclusiveOptionalAndPositionalParent(
+ MEPBase, TestMutuallyExclusiveOptionalAndPositional): pass
+
+class TestMutuallyExclusiveOptionalsMixedParent(
+ MEPBase, TestMutuallyExclusiveOptionalsMixed): pass
+
+class TestMutuallyExclusiveOptionalsAndPositionalsMixedParent(
+ MEPBase, TestMutuallyExclusiveOptionalsAndPositionalsMixed): pass
# =================
# Set default tests