def dataProvider_testSetGetAttr(self): ps = Parameters() #yield ps, '_props', None, ParameterNameError yield ps, 'a', 1 ps1 = Parameters() ps1.a = 'a' yield ps1, 'a', 'a'
def dataProvider_testShouldPrintHelp(self): ps = Parameters() ps('hopts', '-h, ') yield ps, [], True yield ps, ['-h'], True ps1 = Parameters() ps1('hopts', '', excl=True) yield ps1, [], False yield ps1, ['-h'], True
def testLoadFile(self, cfgfile, show, params, exception=None, msg=None): ps = Parameters() if exception: self.assertRaisesRegex(exception, msg, ps.loadFile, dictVar, cfgfile) else: ps.loadFile(cfgfile, show) for param in params: p = getattr(ps, param.name) self.assertDictEqual(param._props, p._props)
def dataProvider_testShouldPrintHelp(self): ps = Parameters() ps('hopts', '-h') yield ps, [], True yield ps, ['-h'], True ps1 = Parameters() ps1('hopts', ['--help']) yield ps1, [], True yield ps1, ['-h'], False ps2 = Parameters() ps2._hbald = False yield ps2, [], False
def dataProvider_testCall(self): ps = Parameters() yield ps, 'prefix', '', '', ParametersParseError yield ps, 'prefix', 'a', 'a' yield ps, 'prefix', '-', '-' yield ps, 'prefix', '--', '--' # 4, hopts yield ps, 'hopts', '', [''] yield ps, 'hopts', 'a', ['a'] yield ps, 'hopts', '-', ['-'] yield ps, 'hopts', ' --, -h', ['--', '-h'] yield ps, 'hopts', ['--help', '?'], ['--help', '?'] # cannot be tested solely yield ps, 'hopts', '?', ['?'] # 10, usage yield ps, 'usage', '', [''] yield ps, 'usage', 'a', ['a'] yield ps, 'usage', 'a\nb', ['a\nb'] yield ps, 'usage', ' a \n\n b \n', [' a \n\n b \n'] # 14, desc yield ps, 'desc', '', [''] yield ps, 'desc', 'a', ['a'] yield ps, 'desc', 'a\nb', ['a\nb'] yield ps, 'desc', ' a \n\n b \n', [' a \n\n b \n']
def dataProvider_testParseName(self): ps = Parameters() ps('prefix', '---') yield ps, '-a', None, 'auto', 'auto', None yield ps, '----a', None, 'auto', 'auto', None yield ps, '---a', 'a', 'auto', 'auto', None yield ps, '---a:i', 'a', 'int', 'auto', None yield ps, '---a:int', 'a', 'int', 'auto', None yield ps, '---a:s', 'a', 'str', 'auto', None yield ps, '---a:str', 'a', 'str', 'auto', None yield ps, '---a:b', 'a', 'bool', 'auto', None yield ps, '---a:bool', 'a', 'bool', 'auto', None yield ps, '---a:f', 'a', 'float', 'auto', None yield ps, '---a:float', 'a', 'float', 'auto', None yield ps, '---a:l', 'a', 'list', 'auto', None yield ps, '---a:list', 'a', 'list', 'auto', None yield ps, '---a:l:s', 'a', 'list', 'str', None yield ps, '---a:list:s', 'a', 'list', 'str', None yield ps, '---a:list:str', 'a', 'list', 'str', None yield ps, '---a:l:i', 'a', 'list', 'int', None yield ps, '---a:list:i', 'a', 'list', 'int', None yield ps, '---a:list:int', 'a', 'list', 'int', None yield ps, '---a:l:f', 'a', 'list', 'float', None yield ps, '---a:list:f', 'a', 'list', 'float', None yield ps, '---a:list:float', 'a', 'list', 'float', None yield ps, '---a:l:b', 'a', 'list', 'bool', None yield ps, '---a:list:b', 'a', 'list', 'bool', None yield ps, '---a:list:bool', 'a', 'list', 'bool', None
def dataProvider_testCall(self): ps = Parameters() yield ps, 'prefix', '', '', None, ParametersParseError yield ps, 'prefix', 'a', 'a' yield ps, 'prefix', '-', '-' yield ps, 'prefix', '--', '--' # 4, hopts yield ps, 'hopts', '', [''] yield ps, 'hopts', 'a', ['a'] yield ps, 'hopts', '-', ['-'] yield ps, 'hopts', ' --, -h', ['--', '-h'] yield ps, 'hopts', ['--help', '?'], ['--help', '?'] # cannot be tested solely yield ps, 'hopts', '?', ['--help'], True # 9, usage yield ps, 'usage', '', [] yield ps, 'usage', 'a', ['a'] yield ps, 'usage', 'a\nb', ['a', 'b'] yield ps, 'usage', ' a \n\n b \n', ['a', '', 'b'] # 13, example yield ps, 'example', '', [] yield ps, 'example', 'a', ['a'] yield ps, 'example', 'a\nb', ['a', 'b'] yield ps, 'example', ' a \n\n b \n', ['a', '', 'b'] # 17, desc yield ps, 'desc', '', [] yield ps, 'desc', 'a', ['a'] yield ps, 'desc', 'a\nb', ['a', 'b'] yield ps, 'desc', ' a \n\n b \n', ['a', '', 'b'] yield ps, 'Unknown', '', '', None, AttributeError
def dataProvider_testToDict(self): ps = Parameters() ps.a = 1 ps.b = 2 yield ps, {'a': 1, 'b': 2} ps2 = Parameters() yield ps2, {} ps3 = Parameters() ps3.x = True ps3.y = [] yield ps3, {'x': True, 'y': []}
def dataProvider_testPutValue(self): ps = Parameters() yield ps, 'noSuchArgname', None, None, None, 'auto' ps.a.type = 'list' yield ps, 'a', 'auto', 1, 1, 'auto' yield ps, 'a', 'auto', '2', 2, 'auto' yield ps, 'a', 'auto', '', '', 'auto' yield ps, 'a', 'list', 3, ['3'], 'str' ps.b.type = 'bool' yield ps, 'b', 'auto', 'F', False, 'auto' ps.c.type = 'list' yield ps, 'c', 'list', 1, [[1]], 'one' yield ps, 'd', 'auto', '1', 1, 'auto', True ps.e.type = 'list' ps.e = [1, 2, 3] yield ps, 'e', 'list', '4', [1, 2, 3, 4], 'auto', True ps.f = [1, 2, 3] yield ps, 'f', 'list', '4', [], None, True
def testInit(self): ps = Parameters() self.assertIsInstance(ps, Parameters) self.assertEqual(ps._props['usage'], []) self.assertEqual(ps._props['example'], []) self.assertEqual(ps._props['desc'], []) self.assertListEqual(ps._props['hopts'], ['-h', '--help', '-H', '-?', '']) self.assertEqual(ps._props['prefix'], '-') self.assertDictEqual(ps._params, {})
def dataProvider_testPutValue(self): ps = Parameters() yield ps, 'noSuchArgname', None, None, None, False ps.a.type = 'list' yield ps, 'a', 'auto', 1, [1], True # if test solely, outval should be [2] yield ps, 'a', 'auto', '2', [1, 2], True yield ps, 'a', 'list:str', 3, [1, 2, '3'], True ps.b.type = 'bool' yield ps, 'b', 'auto', 'F', False, False ps.c.type = 'list' yield ps, 'c', 'list:one', 1, [[1]], True
def testInit(self): ps = Parameters() self.assertIsInstance(ps, Parameters) self.assertEqual(ps._props['usage'], []) self.assertEqual(ps._props['desc'], []) self.assertListEqual(ps._props['hopts'], ['-h', '--help', '-H']) self.assertEqual(ps._props['prefix'], '-') self.assertIsInstance(ps.__dict__['_assembler'], HelpAssembler) self.assertEqual(ps._assembler.theme, HelpAssembler.THEMES['default']) self.assertDictEqual(ps._params, {}) self.assertTrue(ps == ps) self.assertFalse(ps != ps)
def testLoadDict(self, dictVar, show, exception=None, msg=None): ps = Parameters() if exception: self.assertRaisesRegex(exception, msg, ps.loadDict, dictVar, show) else: ps.loadDict(dictVar, show) for dk, dv in dictVar.items(): if '.' in dk: pn, pa = dk.split('.', 2) p = getattr(ps, pn) self.assertIsInstance(p, Parameter) self.assertEqual(p.name, pn) if pa == 'desc': self.assertEqual(getattr(p, pa)[0], dv) else: self.assertEqual(getattr(p, pa), dv) else: p = getattr(ps, dk) self.assertIsInstance(p, Parameter) self.assertEqual(p.name, dk) self.assertEqual(p.value, dv) self.assertEqual(p.show, show)
def dataProvider_testGetType(self): ps = Parameters() yield ps, 'noSuchArgname', None, False, 'WARNING: Unknown option' ps.a.type = 'int' yield ps, 'a', 'auto', 'int' yield ps, 'a', 'float', 'float', 'WARNING: Decleared type "int" ignored' ps.b.type = 'list' yield ps, 'b', 'auto', 'list:auto' ps.c yield ps, 'c', None, 'auto'
def testRepr(self): ps = Parameters() ps.a ps.b self.assertIn('<Parameters(a:None,b:None) @ ', repr(ps)) self.assertIn('>', repr(ps))
def dataProvider_testHelp(self): ps = Parameters() yield ps, [ 'USAGE:', ' testParameters.py', '', 'OPTIONAL OPTIONS:', ' -h, --help, -H - Print this help information', '' ] ps1 = Parameters() ps1('hopts', '-h') yield ps1, [ 'USAGE:', ' testParameters.py', '', 'OPTIONAL OPTIONS:', ' -h - Print this help information', '' ] ps2 = Parameters() ps2('prefix', '--param-') ps2.a yield ps2, [ 'USAGE:', ' testParameters.py [OPTIONS]', '', 'OPTIONAL OPTIONS:', ' --param-a - Default: None', ' -h, --help, -H - Print this help information', '' ] ps3 = Parameters() ps3.e = False ps3.e.type = 'bool' ps3._.required = True ps3._.desc = 'positional options' yield ps3, [ 'USAGE:', ' testParameters.py [OPTIONS] POSITIONAL', '', 'REQUIRED OPTIONS:', ' POSITIONAL - positional options', '', 'OPTIONAL OPTIONS:', ' -e (BOOL) - Default: False', ' -h, --help, -H - Print this help information', '' ] ps4 = Parameters() ps4('prefix', '--param-') ps4.ef.required = True ps4.ef.type = 'str' ps4.ef.desc = 'This is a description of option ef. \n Option ef is required.' ps4.f = [] ps4.f.type = 'list' ps4.f.desc = 'This is a description of option f. \n Option f is not required.' ps4.g = ps4.f # alias ps4._.required = False ps4._.desc = 'positional options' ps4( 'usage', '{prog} User-defined usages\n{prog} User-defined another usage'. split('\n')) ps4('desc', 'This program is doing: \n* 1. blahblah\n* 2. lalala'.split('\n')) ps4._helpx = lambda items: items.update({'END': ['Bye!']}) or items yield ps4, [ 'DESCRIPTION:', ' This program is doing:', ' * 1. blahblah', ' * 2. lalala', '', 'USAGE:', ' testParameters.py User-defined usages', ' testParameters.py User-defined another usage', '', 'REQUIRED OPTIONS:', ' --param-ef <STR> - This is a description of option ef.', ' Option ef is required.', '', 'OPTIONAL OPTIONS:', ' --param-f, --param-g <LIST> - This is a description of option f.', ' Option f is not required.', ' Default: []', ' POSITIONAL - positional options', ' Default: None', ' -h, --help, -H - Print this help information', '', 'END:', ' Bye!', '' ] # show = False, description ps5 = Parameters() ps5.g = '' ps5.g.show = False yield ps5, [ 'Error: This is an error!', 'USAGE:', ' testParameters.py', '', 'OPTIONAL OPTIONS:', ' -h, --help, -H - Print this help information', '' ], 'This is an error!'
def dataProvider_testParse(self): ps = Parameters() yield ps, [], {}, 'USAGE', SystemExit ps1 = Parameters() ps1('hopts', '-h') yield ps1, ['a', 'b', '-h'], {}, 'USAGE', SystemExit, None ps2 = Parameters() ps2('prefix', '--param-') ps2.a yield ps2, ['--param-a=b'], {'a': 'b', '_': []} yield ps2, ['--param-d'], { 'a': 'b', '_': [] }, 'Warning: No such option: --param-d' ps3 = Parameters() ps3('prefix', '--param-') ps3.e = True ps3.e.type = 'bool' yield ps3, ['--param-e=False'], {'e': False, '_': []} # 5 yield ps3, ['--param-e'], {'e': True, '_': []} yield ps3, ['--param-e', 'Yes'], {'e': True, '_': []} yield ps3, ['--param-e', 't'], {'e': True, '_': []} yield ps3, ['--param-e', 'true'], {'e': True, '_': []} yield ps3, ['--param-e', 'y'], {'e': True, '_': []} # 10 yield ps3, ['--param-e', '1'], {'e': True, '_': []} yield ps3, ['--param-e', 'on'], {'e': True, '_': []} yield ps3, ['--param-e', 'f'], {'e': False, '_': []} yield ps3, ['--param-e', 'false'], {'e': False, '_': []} yield ps3, ['--param-e', 'no'], {'e': False, '_': []} # 15 yield ps3, ['--param-e', 'n'], {'e': False, '_': []} yield ps3, ['--param-e', '0'], {'e': False, '_': []} yield ps3, ['--param-e', 'off'], {'e': False, '_': []} yield ps3, ['--param-e', 'a'], { 'e': True, '_': [] }, None, ParameterTypeError, "Unable to coerce value 'a' to type: 'bool'" ps4 = Parameters() ps4('prefix', '--param-') ps4.f = [] ps4.f.type = 'list:str' yield ps4, ['--param-f=1'], {'f': ['1'], '_': []} # 20 yield ps4, ['--param-f=1', '2', '3'], { 'f': ['1', '1', '2', '3'], '_': [] } ps5 = Parameters() ps5('prefix', '--param-') ps5.g = '' yield ps5, ['--param-g'], {'g': '', '_': []}, '' yield ps5, ['--param-g', 'a', 'b'], {'g': 'a', '_': ['b']} ps6 = Parameters() ps6('prefix', '--param-') ps6('hbald', False) ps6.h.required = True # 23 yield ps6, [], {}, 'Error: Option --param-h is required.', SystemExit ps7 = Parameters() ps7('prefix', '--param-') ps7.i = 1 yield ps7, ['--param-i=a' ], {}, None, ParameterTypeError, 'Unable to coerce' # mixed ps8 = Parameters() ps8('prefix', '--param-') ps8.a.type = 'str' ps8.b.type = 'str' ps8.c # 25 yield ps8, ['--param-a=1', '--param-b', '2', '--param-c="3"'], { 'a': '1', 'b': '2', 'c': '"3"', '_': [] } ps9 = Parameters() ps9('prefix', '--param-') ps9.a = [] ps9.a.type = 'list:str' ps9.b = [] ps9.c = [] yield ps9, ['--param-a=1', '2', '--param-b', 'a', '--param-c'], { 'a': ['1', '2'], 'b': ['a'], 'c': [], '_': [] } ps10 = Parameters() ps10('prefix', '--param-') ps10.a = False ps10.b = False ps10.c = False yield ps10, ['--param-a', '--param-b', '1', '--param-c=yes'], { 'a': True, 'b': True, 'c': True, '_': [] } ps11 = Parameters() ps11('prefix', '--param-') ps11.a ps11.b = 'a' ps11.c = 1 ps11.d = False ps11.e = [] yield ps11, ['--param-d'], { 'a': None, 'b': 'a', 'c': 1, 'd': True, 'e': [], '_': [] } yield ps11, [ 'a', '--param-d', 'no', 'b', '--param-c=100', '--param-e:l:s', '-1', '-2' ], { 'a': None, 'b': 'a', 'c': 100, 'd': False, 'e': ['-1', '-2'], '_': ['a'] }, 'Warning: Unexpected value(s): b.' # 30 ps12 = Parameters() ps12.a ps12.b yield ps12, ['-a', '-b=1'], {'a': True, 'b': 1, '_': []} ps13 = Parameters() ps13.a.type = list yield ps13, ['-a', '1', '-a', '-a', '2', '3'], {'a': [2, 3], '_': []} # 32: test callback ps14 = Parameters() ps14.a.type = str def ps14_a_callback(a): a.value = 'arg:' + a.value ps14.a.callback = ps14_a_callback yield ps14, ['-a', '1'], {'a': 'arg:1', '_': []} ps15 = Parameters() ps15.infile.type = str def ps15_infile_callback(infile): return path.isfile(infile.value) or '"infile" does not exist.' ps15.infile.callback = ps15_infile_callback yield ps15, ['-infile', '__no_such_file__'], { 'a': '__no_such_file__', '_': [] }, '"infile" does not exist.', SystemExit ps16 = Parameters() ps16.d.type = str ps16.f.type = str ps16.f.callback = lambda f, ps: f.setValue( path.join(ps.d.value, f.value)) yield ps16, ['-d', 'dir', '-f', 'a.txt'], { 'd': 'dir', 'f': 'dir/a.txt', '_': [] }
def testCoerceValue(self, value, t='auto', outval=None, exception=None): if exception: self.assertRaises(exception, Parameters._coerceValue, value, t) else: outval = outval is None and value or outval self.assertEqual(Parameters._coerceValue(value, t), outval)
def dataProvider_testParse(self): ps = Parameters() yield ps, [], {}, 'USAGE', SystemExit ps1 = Parameters() ps1('hopts', '-h') yield ps1, ['a', 'b', '-h'], {}, 'USAGE', SystemExit, None ps2 = Parameters() ps2('prefix', '--param-') ps2.a yield ps2, ['--param-a=b'], {'a': 'b', '_': []} yield ps2, ['--param-d'], { 'a': 'b', '_': [] }, 'WARNING: Unknown option' ps3 = Parameters() ps3('prefix', '--param-') ps3.e = True ps3.e.type = 'bool' yield ps3, ['--param-e=False'], {'e': False, '_': []} # 5 yield ps3, ['--param-e'], {'e': True, '_': []} yield ps3, ['--param-e', 'Yes'], {'e': True, '_': []} yield ps3, ['--param-e', 't'], {'e': True, '_': []} yield ps3, ['--param-e', 'true'], {'e': True, '_': []} yield ps3, ['--param-e', 'y'], {'e': True, '_': []} # 10 yield ps3, ['--param-e', '1'], {'e': True, '_': []} yield ps3, ['--param-e', 'on'], {'e': True, '_': []} yield ps3, ['--param-e', 'f'], {'e': False, '_': []} yield ps3, ['--param-e', 'false'], {'e': False, '_': []} yield ps3, ['--param-e', 'no'], {'e': False, '_': []} # 15 yield ps3, ['--param-e', 'n'], {'e': False, '_': []} yield ps3, ['--param-e', '0'], {'e': False, '_': []} yield ps3, ['--param-e', 'off'], {'e': False, '_': []} yield ps3, ['--param-e', 'a'], { 'e': True, '_': [] }, 'WARNING: Unknown bool value, use True instead of \'a\'' ps4 = Parameters() ps4('prefix', '--param-') ps4.f = [] ps4.f.type = 'list:str' yield ps4, ['--param-f=1'], {'f': ['1'], '_': []} # 20 yield ps4, ['--param-f=1', '2', '3'], { 'f': ['1', '1', '2', '3'], '_': [] } ps5 = Parameters() ps5('prefix', '--param-') ps5.g = '' yield ps5, ['--param-g'], { 'g': True, '_': [] }, 'WARNING: Decleared type "str" ignored, use "bool" instead for option --param-g.' yield ps5, ['--param-g', 'a', 'b'], {'g': 'a', '_': ['b']} ps6 = Parameters() ps6('prefix', '--param-') ps6('hopts', '-?') ps6.h.required = True yield ps6, [], {}, 'ERROR: Option --param-h is required.', SystemExit ps7 = Parameters() ps7('prefix', '--param-') ps7.i = 1 yield ps7, ['--param-i=a' ], {}, None, ParameterTypeError, 'Unable to coerce' # mixed ps8 = Parameters() ps8('prefix', '--param-') ps8.a.type = 'str' ps8.b.type = 'str' ps8.c # 25 yield ps8, ['--param-a=1', '--param-b', '2', '--param-c="3"'], { 'a': '1', 'b': '2', 'c': '"3"', '_': [] } ps9 = Parameters() ps9('prefix', '--param-') ps9.a = [] ps9.a.type = 'list:str' ps9.b = [] ps9.c = [] yield ps9, ['--param-a=1', '2', '--param-b', 'a', '--param-c'], { 'a': ['1', '2'], 'b': ['a'], 'c': [], '_': [] } ps10 = Parameters() ps10('prefix', '--param-') ps10.a = False ps10.b = False ps10.c = False yield ps10, ['--param-a', '--param-b', '1', '--param-c=yes'], { 'a': True, 'b': True, 'c': True, '_': [] } ps11 = Parameters() ps11('prefix', '--param-') ps11.a ps11.b = 'a' ps11.c = 1 ps11.d = False ps11.e = [] yield ps11, ['--param-d'], { 'a': None, 'b': 'a', 'c': 1, 'd': True, 'e': [], '_': [] } yield ps11, [ 'a', '--param-d', 'no', 'b', '--param-c=100', '--param-e:l:s', '-1', '-2' ], { 'a': None, 'b': 'a', 'c': 100, 'd': False, 'e': ['-1', '-2'], '_': ['a', 'b'] }, 'WARNING: Decleared type "list" ignored, use "list:str" instead for option --param-e.' ps12 = Parameters() ps12.a ps12.b yield ps12, ['-a', '-b=1'], {'a': True, 'b': 1, '_': []}
def dataProvider_testHelp(self): ps = Parameters() yield ps, [ 'USAGE:', ' progname', '', 'OPTIONAL OPTIONS:', ' -h, --help, -H, -? - Print this help information.', '' ] ps1 = Parameters() ps1('hopts', '-h') yield ps1, [ 'USAGE:', ' progname', '', 'OPTIONAL OPTIONS:', ' -h - Print this help information.', '' ] ps2 = Parameters() ps2('prefix', '--param-') ps2.a yield ps2, [ 'USAGE:', ' progname [OPTIONS]', '', 'OPTIONAL OPTIONS:', ' --param-a - DEFAULT: None', ' -h, --help, -H, -? - Print this help information.', '' ] ps3 = Parameters() ps3.e = False ps3.e.type = 'bool' ps3._.required = True ps3._.desc = 'positional options' yield ps3, [ 'USAGE:', ' progname [OPTIONS] <POSITIONAL>', '', 'REQUIRED OPTIONS:', ' <POSITIONAL> - positional options', '', 'OPTIONAL OPTIONS:', ' -e (bool) - DEFAULT: False', ' -h, --help, -H, -? - Print this help information.', '' ] ps4 = Parameters() ps4('prefix', '--param-') ps4.ef.required = True ps4.ef.type = 'str' ps4.ef.desc = 'This is a description of option ef. \n Option ef is required.' ps4.f = [] ps4.f.type = 'list' ps4.f.desc = 'This is a description of option f. \n Option f is not required.' ps4._.required = True ps4._.desc = 'positional options' ps4('usage', '{prog} User-defined usages\n{prog} User-defined another usage') ps4('desc', 'This program is doing: \n* 1. blahblah\n* 2. lalala') ps4('example', '{prog} --param-f abc\n {prog} --param-f 22') yield ps4, [ 'DESCRIPTION:', ' This program is doing:', ' * 1. blahblah', ' * 2. lalala', '', 'USAGE:', ' progname User-defined usages', ' progname User-defined another usage', '', 'EXAMPLE:', ' progname --param-f abc', ' progname --param-f 22', '', 'REQUIRED OPTIONS:', ' --param-ef <str> - This is a description of option ef. ', ' Option ef is required.', ' <POSITIONAL> - positional options', '', 'OPTIONAL OPTIONS:', ' --param-f <list> - This is a description of option f. ', ' Option f is not required.', ' DEFAULT: []', ' -h, --help, -H, -? - Print this help information.', '' ] # show = False, description ps5 = Parameters() ps5.g = '' ps5.g.show = False yield ps5, [ 'USAGE:', ' progname', '', 'OPTIONAL OPTIONS:', ' -h, --help, -H, -? - Print this help information.', '' ]
def testSetTheme(self, theme): ps = Parameters() ps._setTheme(theme) self.assertEqual(ps._assembler.theme, HelpAssembler.THEMES.get(str(theme), theme))