コード例 #1
0
    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',
            '_': []
        }
コード例 #2
0
ファイル: testParameters.py プロジェクト: afcarl/PyPPL
    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, '_': []}