Exemple #1
0
    def test_validation(self):
        # No exceptions should be thrown
        compat_args = OrderedDict()
        compat_args["foo"] = 1
        compat_args["bar"] = None
        compat_args["baz"] = -2
        kwargs = {"baz": -2}
        args = (1, None)

        min_fname_arg_count = 2
        validate_args_and_kwargs(self.fname, args, kwargs, min_fname_arg_count, compat_args)
Exemple #2
0
    def test_validation(self):
        # No exceptions should be thrown
        compat_args = OrderedDict()
        compat_args['foo'] = 1
        compat_args['bar'] = None
        compat_args['baz'] = -2
        kwargs = {'baz': -2}
        args = (1, None)

        min_fname_arg_count = 2
        validate_args_and_kwargs(self.fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Exemple #3
0
    def test_duplicate_argument(self):
        min_fname_arg_count = 2
        compat_args = OrderedDict()
        compat_args["foo"] = None
        compat_args["bar"] = None
        compat_args["baz"] = None
        kwargs = {"foo": None, "bar": None}
        args = (None,)  # duplicate value for 'foo'

        msg = r"{fname}\(\) got multiple values for keyword " r"argument '{arg}'".format(fname=self.fname, arg="foo")

        with tm.assertRaisesRegexp(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs, min_fname_arg_count, compat_args)
Exemple #4
0
    def test_invalid_total_length_max_length_multiple(self):
        compat_args = ("foo", "bar", "baz")
        kwargs = {"foo": "FOO", "bar": "BAR"}
        args = ("FoO", "BaZ")

        min_fname_arg_count = 2
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = r"{fname}\(\) takes at most {max_length} " r"arguments \({actual_length} given\)".format(
            fname=self.fname, max_length=max_length, actual_length=actual_length
        )

        with tm.assertRaisesRegexp(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs, min_fname_arg_count, compat_args)
Exemple #5
0
    def test_duplicate_argument(self):
        min_fname_arg_count = 2
        compat_args = OrderedDict()
        compat_args['foo'] = None
        compat_args['bar'] = None
        compat_args['baz'] = None
        kwargs = {'foo': None, 'bar': None}
        args = (None, )  # duplicate value for 'foo'

        msg = (r"{fname}\(\) got multiple values for keyword "
               r"argument '{arg}'".format(fname=self.fname, arg='foo'))

        with tm.assertRaisesRegexp(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)
Exemple #6
0
    def test_invalid_total_length_max_length_multiple(self):
        compat_args = ('foo', 'bar', 'baz')
        kwargs = {'foo': 'FOO', 'bar': 'BAR'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 2
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"arguments \({actual_length} given\)".format(
                   fname=self.fname,
                   max_length=max_length,
                   actual_length=actual_length))

        with tm.assertRaisesRegexp(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)
Exemple #7
0
    def test_invalid_total_length_max_length_multiple(self):
        compat_args = ('foo', 'bar', 'baz')
        kwargs = {'foo': 'FOO', 'bar': 'BAR'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 2
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"arguments \({actual_length} given\)"
               .format(fname=self.fname, max_length=max_length,
                       actual_length=actual_length))

        with tm.assertRaisesRegexp(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count,
                                     compat_args)
Exemple #8
0
    def __call__(self, args, kwargs, fname=None,
                 max_fname_arg_count=None, method=None):
        fname = self.fname if fname is None else fname
        max_fname_arg_count = (self.max_fname_arg_count if
                               max_fname_arg_count is None
                               else max_fname_arg_count)
        method = self.method if method is None else method

        if method == 'args':
            validate_args(fname, args, max_fname_arg_count, self.defaults)
        elif method == 'kwargs':
            validate_kwargs(fname, kwargs, self.defaults)
        elif method == 'both':
            validate_args_and_kwargs(fname, args, kwargs,
                                     max_fname_arg_count,
                                     self.defaults)
        else:
            raise ValueError("invalid validation method "
                             "'{method}'".format(method=method))