示例#1
0
    def _test(self, outer, inner, args, kwargs, expected, expected_get,
              exp_src, exp_src_get):
        outer_f = support.f(outer, name='outer')
        inner_f = support.f(inner, name='inner')
        forw = specifiers.forwards_to_function(inner_f, *args,
                                               **kwargs)(outer_f)

        sig = specifiers.signature(forw)
        self.assertSigsEqual(sig, support.s(expected))

        self.assertSourcesEqual(
            sig.sources, {
                'outer': exp_src[0],
                'inner': exp_src[1],
                '+depths': ['outer', 'inner']
            })

        sig_get = specifiers.signature(_util.safe_get(forw, object(), object))
        self.assertSigsEqual(sig_get, support.s(expected_get))

        self.assertSourcesEqual(
            sig_get.sources, {
                'outer': exp_src_get[0],
                'inner': exp_src_get[1],
                '+depths': ['outer', 'inner']
            })
示例#2
0
 def test_success(self):
     self.assertSigsEqual(s('a, b:1'),
                          signature(modifiers.annotate(b=1)(f('a, b'))))
     self.assertSigsEqual(
         s('a:1, b:2'), signature(modifiers.annotate(a=1, b=2)(f('a, b'))))
     self.assertSigsEqual(s('a:1, b', 2),
                          signature(modifiers.annotate(2, a=1)(f('a, b'))))
示例#3
0
    def test_preserve_annotations(self):
        func = f('self, a:2, b, c:3', 4)

        tr = modifiers._PokTranslator(func, kwoargs=('a', 'b'))
        self.assertSigsEqual(s('self, c:3, *, a:2, b', 4), signature(tr))
        self.assertSigsEqual(s('c:3, *, a:2, b', 4),
                             signature(safe_get(tr, object(), object)))
示例#4
0
    def _test(self,
              result,
              exp_src,
              signatures,
              use_varargs=True,
              use_varkwargs=True):
        assert len(signatures) >= 2
        sigs = [
            s(sig_str, name='_' + str(i))
            for i, sig_str in enumerate(signatures, 1)
        ]
        exp_src.setdefault('+depths',
                           ['_' + str(i + 1) for i in range(len(signatures))])

        sig = embed(*sigs,
                    use_varargs=use_varargs,
                    use_varkwargs=use_varkwargs)
        exp_sig = s(result)
        self.assertSigsEqual(sig, exp_sig)
        self.assertSourcesEqual(sig.sources, exp_src)

        sigs = [self.downgrade_sig(sig) for sig in sigs]
        self.assertSigsEqual(
            embed(*sigs, use_varargs=use_varargs, use_varkwargs=use_varkwargs),
            exp_sig)
示例#5
0
    def _test(self, exp_sig, exp_src, outer, inner,
                    num_args=0, named_args=(),
                    hide_args=False, hide_kwargs=False,
                    use_varargs=True, use_varkwargs=True,
                    partial=False):
        outer_sig = s(outer, name='o')
        inner_sig = s(inner, name='i')

        sig = forwards(
                    outer_sig, inner_sig,
                    num_args, *named_args,
                    hide_args=hide_args, hide_kwargs=hide_kwargs,
                    use_varargs=use_varargs, use_varkwargs=use_varkwargs,
                    partial=partial)
        self.assertSigsEqual(sig, s(exp_sig))
        self.assertSourcesEqual(sig.sources, {
                'o': exp_src[0], 'i': exp_src[1],
                '+depths': ['o', 'i']})

        outer_sig = self.downgrade_sig(outer_sig)
        inner_sig = self.downgrade_sig(inner_sig)
        sig = forwards(
            outer_sig, inner_sig,
            num_args, *named_args,
            hide_args=hide_args, hide_kwargs=hide_kwargs,
            use_varargs=use_varargs, use_varkwargs=use_varkwargs,
            partial=partial)
        self.assertSigsEqual(sig, s(exp_sig))
 def test_all(self):
     self.assertSigsEqual(
         s('one, two, /, three, four, *five, six, seven, **eight'),
         apply_params(
             s(''), [p('one, /'), p('two, /')],
             [p('three'), p('four')], p('*five'),
             OrderedDict((('six', p('*, six')), ('seven', p('*, seven')))),
             p('**eight')))
示例#7
0
def forwards_tests(self, outer, inner, args, kwargs, expected, expected_get):
    outer_f = support.f(outer)
    inner_f = support.f(inner)
    forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f)

    self.assertSigsEqual(specifiers.signature(forw), support.s(expected))

    self.assertSigsEqual(
        specifiers.signature(_util.safe_get(forw, object(), object)),
        support.s(expected_get))
示例#8
0
 def test_use_twice(self):
     annotator = modifiers.annotate(a=1)
     self.assertSigsEqual(
         s('a:1, b'),
         signature(annotator(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1'),
         signature(annotator(f('a')))
         )
示例#9
0
def poktranslator_test(self, expected_sig_str, orig_sig_str, posoargs,
                       kwoargs):
    expected_sig = s(expected_sig_str)
    orig_sig = s(orig_sig_str)
    for exp, orig in defaults_variations(expected_sig, orig_sig):
        for exp, orig in stars_variations(exp, orig):
            func = modifiers._PokTranslator(func_from_sig(orig), posoargs,
                                            kwoargs)
            self.assertSigsEqual(exp, signature(func))
            test_func_sig_coherent(func)
            repr(func)  # must not cause an error
示例#10
0
 def test_pok_interact(self):
     pok = f('self, a, *, b')
     annotated = modifiers.annotate(a=1, b=2)(pok)
     self.assertSigsEqual(
         s('self, a:1, *, b:2'),
         signature(annotated)
         )
     self.assertSigsEqual(
         s('a:1, *, b:2'),
         signature(safe_get(annotated, object(), object))
         )
示例#11
0
def poktranslator_test(self, expected_sig_str, orig_sig_str,
                        posoargs, kwoargs):
    expected_sig = s(expected_sig_str)
    orig_sig = s(orig_sig_str)
    for exp, orig in defaults_variations(expected_sig, orig_sig):
        for exp, orig in stars_variations(exp, orig):
            func = modifiers._PokTranslator(
                func_from_sig(orig), posoargs, kwoargs)
            self.assertSigsEqual(exp, signature(func))
            test_func_sig_coherent(func)
            repr(func) # must not cause an error
示例#12
0
    def test_preserve_annotations(self):
        func = f('self, a:2, b, c:3', 4)

        tr = modifiers._PokTranslator(func, kwoargs=('a', 'b'))
        self.assertSigsEqual(
            s('self, c:3, *, a:2, b', 4),
            signature(tr)
            )
        self.assertSigsEqual(
            s('c:3, *, a:2, b', 4),
            signature(safe_get(tr, object(), object))
            )
示例#13
0
 def test_success(self):
     self.assertSigsEqual(
         s('a, b:1'),
         signature(modifiers.annotate(b=1)(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1, b:2'),
         signature(modifiers.annotate(a=1, b=2)(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1, b', 2),
         signature(modifiers.annotate(2, a=1)(f('a, b')))
         )
示例#14
0
def mask_tests(self,
               expected_str,
               sig_str,
               num_args=0,
               named_args=(),
               hide_varargs=False,
               hide_varkwargs=False):
    self.assertSigsEqual(
        support.s(expected_str),
        signatures.mask(support.s(sig_str),
                        num_args,
                        *named_args,
                        hide_varargs=hide_varargs,
                        hide_varkwargs=hide_varkwargs))
示例#15
0
    def test_sigs_equal(self):
        self.assertSigsEqual(s('one'), s('one'))
        self.assertSigsEqual(s('*, one'), s('*, one'))

        with self.assertRaises(AssertionError):
            self.assertSigsEqual(s('one'), s('two'))
        with self.assertRaises(AssertionError):
            self.assertSigsEqual(s('one'), s('*, one'))
示例#16
0
    def test_sigs_equal(self):
        self.assertSigsEqual(s('one'), s('one'))
        self.assertSigsEqual(s('*, one'), s('*, one'))

        with self.assertRaises(AssertionError):
            self.assertSigsEqual(s('one'), s('two'))
        with self.assertRaises(AssertionError):
            self.assertSigsEqual(s('one'), s('*, one'))
示例#17
0
def signaturetests(self, sig_str, str_rep, args, posargs, kwargs):
    sig = support.s(sig_str, locals={'P': parser.Parameter})
    csig = parser.CliSignature.from_signature(sig)
    ba = read_arguments(csig, args)
    self.assertEqual(str(csig), str_rep)
    self.assertEqual(ba.args, posargs)
    self.assertEqual(ba.kwargs, kwargs)
示例#18
0
def annotated_sigtests(self, sig_info, in_args, args, kwargs):
    sig_str, annotation, str_rep = sig_info
    sig = support.s(sig_str, locals={'a': annotation})
    csig = parser.CliSignature.from_signature(sig)
    ba = util.read_arguments(csig, in_args)
    self.assertEqual(ba.args, args)
    self.assertEqual(ba.kwargs, kwargs)
示例#19
0
    def test_pconverter(self):
        class CustExc(Exception):
            pass

        @parser.parameter_converter
        def converter(param, annotations):
            raise CustExc

        @parser.parameter_converter
        def noop_converter(param, annotations):
            raise NotImplementedError

        sigs = [support.s("o: c", locals={"c": converter}), support.s("*, o: a", locals={"a": ("abc", converter)})]
        for sig in sigs:
            sparam = list(sig.parameters.values())[0]
            self.assertRaises(CustExc, parser.CliSignature.convert_parameter, sparam)
示例#20
0
 def test_pos(self):
     sig = s('a, b, /')
     ret = sort_params(sig)
     self.assertEqual(len(ret[0]), 2)
     self.assertEqual(ret[0][0].name, 'a')
     self.assertEqual(ret[0][1].name, 'b')
     self.assertEqual(ret[1:], ([], None, {}, None))
示例#21
0
    def test_vconverter(self):
        @parser.value_converter
        def converter(value):
            raise NotImplementedError

        sig = support.s("*, par: conv", locals={"conv": converter})
        self._do_test(sig, parser.OptionParameter, "--par=CONVERTER", {"conv": converter})
示例#22
0
def signaturetests(self, sig_str, str_rep, args, posargs, kwargs):
    sig = support.s(sig_str, locals={'P': parser.Parameter})
    csig = parser.CliSignature.from_signature(sig)
    ba = csig.read_arguments(args)
    self.assertEqual(str(csig), str_rep)
    self.assertEqual(ba.args, posargs)
    self.assertEqual(ba.kwargs, kwargs)
示例#23
0
    def test_bad_default_good_conv(self):
        class UnknownDefault(object):
            pass

        deft = UnknownDefault()
        sig = support.s("*, par:str=default", locals={"default": deft})
        self._do_test(sig, parser.OptionParameter, "[--par=STR]", {"conv": parser.identity, "default": deft})
示例#24
0
    def test_forger_sig(self):
        @specifiers.forger_function
        def forger(p1, p2, p3, obj):
            raise NotImplementedError

        self.assertSigsEqual(support.s('p1, p2, p3, *, emulate=None'),
                             specifiers.signature(forger))
示例#25
0
 def _test(self, func, expected, expected_src):
     sig = specifiers.signature(func)
     self.assertSigsEqual(sig, support.s(expected))
     self.assertSourcesEqual(sig.sources, expected_src, func)
     with self.assertRaises(AssertionError):
         support.test_func_sig_coherent(
             func, check_return=False, check_invalid=False)
示例#26
0
 def test_directly_applied(self):
     def forger(obj):
         return support.s('abc')
     def forged():
         raise NotImplementedError
     specifiers.set_signature_forger(forged, forger)
     self.assertSigsEqual(support.s('abc'), specifiers.signature(forged))
示例#27
0
 def test_converter_ignore(self):
     @parser.parameter_converter
     def conv(param, annotations):
         return parser.Parameter.IGNORE
     sig = support.s('one:conv', locals={'conv': conv})
     csig = parser.CliSignature.from_signature(sig)
     self.assertEqual(str(csig), '')
示例#28
0
def badparam(self, sig_str, locals=None):
    if locals is None:
        locals = {}
    sig = support.s(sig_str, pre='from clize import Parameter', locals=locals)
    params = list(sig.parameters.values())
    self.assertRaises(ValueError, parser.CliSignature.convert_parameter,
                      params[0])
示例#29
0
def badparam(self, sig_str, locals=None):
    if locals is None:
        locals = {}
    sig = support.s(sig_str, pre='from clize import Parameter', locals=locals)
    params = list(sig.parameters.values())
    self.assertRaises(
        ValueError, parser.CliSignature.convert_parameter, params[0])
示例#30
0
 def test_forger_sig(self):
     @specifiers.forger_function
     def forger(p1, p2, p3, obj):
         raise NotImplementedError
     self.assertSigsEqual(
         support.s('p1, p2, p3, *, emulate=None'),
         specifiers.signature(forger))
示例#31
0
 def _test(self, obj, exp_sig, exp_src=None):
     sig = signature(obj)
     self.assertSigsEqual(sig, s(exp_sig))
     if exp_src is None:
         exp_src = dict((p, [obj.func]) for p in sig.parameters)
         exp_src['+depths'] = {obj: 0, obj.func: 1}
     self.assertEqual(sig.sources, exp_src)
示例#32
0
 def test_vconverter_convert_value(self):
     @parser.value_converter(convert_default=True)
     def conv(arg):
         return 'c{}c'.format(arg)
     sig = support.s('*, par:conv="default"', locals={'conv': conv})
     self._do_test(sig, '[--par=CONV]', ('--par=A',), [], {'par': 'cAc'})
     self._do_test(sig, '[--par=CONV]', ('--par', 'A',), [], {'par': 'cAc'})
示例#33
0
 def _test(self, sig_str, locals, exp_msg):
     sig = support.s(sig_str, pre='from clize import Parameter', locals=locals)
     params = list(sig.parameters.values())
     with self.assertRaises(ValueError) as ar:
         parser.CliSignature.convert_parameter(params[0])
     if exp_msg is not None:
         self.assertEqual(exp_msg, str(ar.exception))
示例#34
0
 def test_converter_ignore(self):
     @parser.parameter_converter
     def conv(param, annotations):
         return parser.Parameter.IGNORE
     sig = support.s('one:conv', locals={'conv': conv})
     csig = parser.CliSignature.from_signature(sig)
     self.assertEqual(str(csig), '')
示例#35
0
 def test_order_kwo(self):
     sig = s('*, one, two, three, four, five, six, seven')
     ret = sort_params(sig)
     self.assertEqual(ret[:3], ([], [], None))
     self.assertEqual(list(
         ret[3]), ['one', 'two', 'three', 'four', 'five', 'six', 'seven'])
     self.assertEqual(ret[4], None)
示例#36
0
 def test_posparam_set_value_parameter_not_present(self):
     param = parser.PositionalParameter(argument_name='two', display_name='two')
     sig = support.s('one, two')
     csig = parser.CliSignature.from_signature(sig)
     ba = parser.CliBoundArguments(csig, [], 'func', args=['one', 'two'])
     with self.assertRaises(ValueError):
         param.set_value(ba, 'inserted')
示例#37
0
 def test_posparam_set_value_after_default(self):
     param = parser.PositionalParameter(argument_name='two', display_name='two', default="two")
     sig = support.s('one="one", two:par="two"', locals={'par': param})
     csig = parser.CliSignature.from_signature(sig)
     ba = parser.CliBoundArguments(csig, [], 'func', args=[])
     param.set_value(ba, 'inserted')
     self.assertEqual(ba.args, ['one', 'inserted'])
示例#38
0
def extraparamstests(self, sig_str, extra, args, posargs, kwargs, func):
    sig = support.s(sig_str)
    csig = parser.CliSignature.from_signature(sig, extra=extra)
    ba = csig.read_arguments(args)
    self.assertEqual(ba.args, posargs)
    self.assertEqual(ba.kwargs, kwargs)
    self.assertEqual(ba.func, func)
示例#39
0
 def test_posparam_set_value_after_missing(self):
     param = parser.PositionalParameter(argument_name='two', display_name='two')
     sig = support.s('one, two:par', locals={'par': param})
     csig = parser.CliSignature.from_signature(sig)
     ba = parser.CliBoundArguments(csig, [], 'func', args=[])
     with self.assertRaises(ValueError):
         param.set_value(ba, 'inserted')
示例#40
0
 def _test(self, func, expected, sources, incoherent=False):
     sig = specifiers.signature(func)
     self.assertSigsEqual(sig, support.s(expected))
     self.assertSourcesEqual(sig.sources, sources, func)
     if not incoherent:
         support.test_func_sig_coherent(
             func, check_return=False, check_invalid=False)
示例#41
0
文件: test_parser.py 项目: kouk/clize
 def _test(self, sig_str, extra, args, posargs, kwargs, func):
     sig = support.s(sig_str)
     csig = parser.CliSignature.from_signature(sig, extra=extra)
     ba = self.read_arguments(csig, args)
     self.assertEqual(ba.args, posargs)
     self.assertEqual(ba.kwargs, kwargs)
     self.assertEqual(ba.func, func)
示例#42
0
 def _test(self, sig_str, num_args, named_args=(),
           hide_varargs=False, hide_varkwargs=False):
     sig = support.s(sig_str)
     self.assertRaises(
         ValueError, signatures.mask,
         sig, num_args, *named_args,
         hide_varargs=hide_varargs, hide_varkwargs=hide_varkwargs)
示例#43
0
def annotated_sigtests(self, sig_info, in_args, args, kwargs):
    sig_str, annotation, str_rep = sig_info
    sig = support.s(sig_str, locals={'a': annotation})
    csig = parser.CliSignature.from_signature(sig)
    ba = util.read_arguments(csig, in_args)
    self.assertEqual(ba.args, args)
    self.assertEqual(ba.kwargs, kwargs)
示例#44
0
文件: test_parser.py 项目: kouk/clize
    def test_vconverter_convert_default(self):
        @parser.value_converter(convert_default=True)
        def conv(arg):
            return 'converted'

        sig = support.s('*, par:conv="default"', locals={'conv': conv})
        self._do_test(sig, '[--par=CONV]', (), [], {'par': 'converted'})
    def test_decorator_wraps(self):
        def decorator(function):
            @wraps(function)
            @modifiers.autokwoargs
            def _decorated(a, b=2, *args, **kwargs):
                function(1, *args, **kwargs)

            return _decorated

        func = decorator(_wrapped)
        sig = specifiers.signature(func)
        self.assertSigsEqual(sig, support.s('a, y, *, b=2, z'))
        self.assertEqual(
            sig.sources, {
                '+depths': {
                    func: 0,
                    _wrapped: 1
                },
                'a': [func],
                'b': [func],
                'y': [_wrapped],
                'z': [_wrapped]
            })
        support.test_func_sig_coherent(func,
                                       check_return=False,
                                       check_invalid=False)
示例#46
0
文件: test_parser.py 项目: kouk/clize
 def test_alias_multi(self):
     sig = support.s('*, one: a', locals={'a': ('a', 'b', 'abc')})
     param = list(sig.parameters.values())[0]
     cparam = parser.CliSignature.convert_parameter(param)
     self.assertEqual(type(cparam), parser.OptionParameter)
     self.assertEqual(str(cparam), '-a STR')
     self.assertEqual(cparam.display_name, '--one')
     self.assertEqual(cparam.aliases, ['--one', '-a', '-b', '--abc'])
示例#47
0
 def test_deco(self):
     @specifiers.forger_function
     def forger(obj):
         return support.s('abc')
     @forger()
     def forged():
         raise NotImplementedError
     self.assertSigsEqual(support.s('abc'), specifiers.signature(forged))
 def _test(self, func, expected, sources, incoherent=False):
     sig = specifiers.signature(func)
     self.assertSigsEqual(sig, support.s(expected))
     self.assertSourcesEqual(sig.sources, sources, func)
     if not incoherent:
         support.test_func_sig_coherent(func,
                                        check_return=False,
                                        check_invalid=False)
示例#49
0
 def test_orig_sig(self):
     @specifiers.forger_function
     def forger(obj):
         return None
     @forger()
     def forged(alpha):
         raise NotImplementedError
     self.assertSigsEqual(support.s('alpha'), specifiers.signature(forged))
示例#50
0
 def test_alias_multi(self):
     sig = support.s("*, one: a", locals={"a": ("a", "b", "abc")})
     param = list(sig.parameters.values())[0]
     cparam = parser.CliSignature.convert_parameter(param)
     self.assertEqual(type(cparam), parser.OptionParameter)
     self.assertEqual(str(cparam), "-a STR")
     self.assertEqual(cparam.display_name, "--one")
     self.assertEqual(cparam.aliases, ["--one", "-a", "-b", "--abc"])
示例#51
0
def sigerrortests(self, sig_str, args, exc_typ, message):
    sig = support.s(sig_str)
    csig = parser.CliSignature.from_signature(sig)
    self.assertRaises(exc_typ, read_arguments, csig, args)
    try:
        read_arguments(csig, args)
    except exc_typ as e:
        self.assertEqual('Error: ' + message, str(e))
示例#52
0
 def test_pn_pos_errinnext_context(self):
     sig_str, annotation, str_rep = RepTests.pn_pos_nextpicky
     sig = support.s(sig_str, locals={'a': annotation})
     csig = parser.CliSignature.from_signature(sig)
     try:
         util.read_arguments(csig, ('bad',))
     except errors.BadArgumentFormat as exc:
         self.assertEqual(exc.param.display_name, 'other')
示例#53
0
 def test_alias_multi(self):
     sig = support.s('*, one: a', locals={'a': ('a', 'b', 'abc')})
     param = list(sig.parameters.values())[0]
     cparam = parser.CliSignature.convert_parameter(param)
     self.assertEqual(type(cparam), parser.OptionParameter)
     self.assertEqual(str(cparam), '-a STR')
     self.assertEqual(cparam.display_name, '--one')
     self.assertEqual(cparam.aliases, ['--one', '-a', '-b', '--abc'])
示例#54
0
def sigerrortests(self, sig_str, args, exc_typ, message):
    sig = support.s(sig_str)
    csig = parser.CliSignature.from_signature(sig)
    self.assertRaises(exc_typ, read_arguments, csig, args)
    try:
        read_arguments(csig, args)
    except exc_typ as e:
        self.assertEqual('Error: ' + message, str(e))
示例#55
0
 def test_vconverter(self):
     @parser.value_converter
     def converter(value):
         raise NotImplementedError
     sig = support.s('*, par: conv', locals={'conv': converter})
     self._do_test(sig, parser.OptionParameter, '--par=CONVERTER', {
         'conv': converter,
         })
示例#56
0
    def _test(self, result, exp_src, signatures,
              use_varargs=True, use_varkwargs=True):
        assert len(signatures) >= 2
        sigs = [s(sig_str, name='_' + str(i))
                for i, sig_str in enumerate(signatures, 1)]
        exp_src.setdefault(
            '+depths', ['_' + str(i+1) for i in range(len(signatures))])

        sig = embed(*sigs, use_varargs=use_varargs, use_varkwargs=use_varkwargs)
        exp_sig = s(result)
        self.assertSigsEqual(sig, exp_sig)
        self.assertSourcesEqual(sig.sources, exp_src)

        sigs = [self.downgrade_sig(sig) for sig in sigs]
        self.assertSigsEqual(
            embed(*sigs, use_varargs=use_varargs, use_varkwargs=use_varkwargs),
            exp_sig)
示例#57
0
 def test_bad_default_good_conv(self):
     class UnknownDefault(object):
         pass
     deft = UnknownDefault()
     sig = support.s('*, par:str=default', locals={'default': deft})
     self._do_test(sig, parser.OptionParameter, '[--par=STR]', {
         'conv': parser.identity,
         'default': deft,
     })
示例#58
0
    def test_converter(self):
        class CustExc(Exception):
            pass
        @parser.parameter_converter
        def converter(param, annotations):
            raise CustExc
        @parser.parameter_converter
        def noop_converter(param, annotations):
            pass

        sigs = [
            support.s('o: c', locals={'c': converter, 'n': noop_converter}),
            support.s('*, o: a',
                      locals={'a': ("abc", converter, noop_converter)})
            ]
        for sig in sigs:
            sparam = list(sig.parameters.values())[0]
            self.assertRaises(CustExc, parser.Parameter.from_parameter, sparam)