<commit_before>import unittest
import sys
import inspect

from robot.running.handlers import _PythonHandler, _JavaHandler, DynamicHandler
from robot import utils
from robot.utils.asserts import *
from robot.running.testlibraries import TestLibrary
from robot.running.dynamicmethods import GetKeywordArguments, GetKeywordDocumentation
from robot.errors import DataError

from classes import NameLibrary, DocLibrary, ArgInfoLibrary
from ArgumentsPython import ArgumentsPython
if utils.is_jython:
    import ArgumentsJava


def _get_handler_methods(lib):
    attrs = [getattr(lib, a) for a in dir(lib) if not a.startswith('_')]
    return [a for a in attrs if inspect.ismethod(a)]

def _get_java_handler_methods(lib):
    # This hack assumes that all java handlers used start with 'a_' -- easier
    # than excluding 'equals' etc. otherwise
    return [a for a in _get_handler_methods(lib) if a.__name__.startswith('a_') ]


class LibraryMock:
    def __init__(self, name='MyLibrary', scope='GLOBAL'):
        self.name = self.orig_name = name
        self.scope = scope


class TestPythonHandler(unittest.TestCase):

    def test_name(self):
        for method in _get_handler_methods(NameLibrary()):
            handler = _PythonHandler(LibraryMock('mylib'), method.__name__, method)
            assert_equals(handler.name, method.__doc__)
            assert_equals(handler.longname, 'mylib.'+method.__doc__)

    def test_docs(self):
        for method in _get_handler_methods(DocLibrary()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            assert_equals(handler.doc, method.expected_doc)
            assert_equals(handler.shortdoc, method.expected_shortdoc)

    def test_arguments(self):
        for method in _get_handler_methods(ArgInfoLibrary()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            args = handler.arguments
            argspec = (args.positional, args.defaults, args.varargs, args.kwargs)
            expected = eval(method.__doc__)
            assert_equals(argspec, expected, method.__name__)

    def test_arg_limits(self):
        for method in _get_handler_methods(ArgumentsPython()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            exp_mina, exp_maxa = eval(method.__doc__)
            assert_equals(handler.arguments.minargs, exp_mina)
            assert_equals(handler.arguments.maxargs, exp_maxa)

    def test_getarginfo_getattr(self):
        testlib = TestLibrary('classes.GetattrLibrary')
        handlers = testlib.handlers.values()
        assert_equals(len(handlers), 3)
        for handler in handlers:
            assert_true(handler.name in ['Foo','Bar','Zap'])
            assert_equals(handler.arguments.minargs, 0)
            assert_equals(handler.arguments.maxargs, sys.maxint)


class TestDynamicHandlerCreation(unittest.TestCase):

    def test_none_doc(self):
        self._assert_doc(None, '')

    def test_empty_doc(self):
        self._assert_doc('')

    def test_non_empty_doc(self):
        self._assert_doc('This is some documentation')

    def test_non_ascii_doc(self):
        self._assert_doc(u'P\xe4iv\xe4\xe4')

    if sys.platform != 'cli':
        def test_with_utf8_doc(self):
            doc = u'P\xe4iv\xe4\xe4'
            self._assert_doc(doc.encode('UTF-8'), doc)

    def test_invalid_doc_type(self):
        self._assert_fails('Return value must be string.', doc=True)

    def test_none_argspec(self):
        self._assert_spec(None, maxargs=sys.maxint,vararg='unknown')

    def test_empty_argspec(self):
        self._assert_spec([])

    def test_mandatory_args(self):
        for argspec in [['arg'], ['arg1', 'arg2', 'arg3']]:
            self._assert_spec(argspec, len(argspec), len(argspec), argspec)

    def test_only_default_args(self):
        self._assert_spec(['defarg1=value', 'defarg2=defvalue'], 0, 2,
                          ['defarg1', 'defarg2'], ['value', 'defvalue'])

    def test_default_value_may_contain_equal_sign(self):
        self._assert_spec(['d=foo=bar'], 0, 1, ['d'], ['foo=bar'])

    def test_varargs(self):
        self._assert_spec(['*vararg'], 0, sys.maxint, vararg='vararg')

    def test_integration(self):
        self._assert_spec(['arg', 'default=value'], 1, 2,
                          ['arg', 'default'], ['value'])
        self._assert_spec(['arg', 'default=value', '*var'], 1, sys.maxint,
                          ['arg', 'default'], ['value'], 'var')

    def test_invalid_argspec_type(self):
        for argspec in [True, [1, 2]]:
            self._assert_fails("Return value must be list of strings.", argspec)

    def test_mandatory_arg_after_default_arg(self):
        for argspec in [['d=v', 'arg'], ['a', 'b', 'c=v', 'd']]:
            self._assert_fails('Non-default argument after default arguments.',
                               argspec)

    def test_vararg_not_last(self):
        for argspec in [['*foo', 'arg'], ['arg', '*var', 'arg'],
                        ['a', 'b=d', '*var', 'c'], ['*var', '*vararg']]:
            self._assert_fails('Only last argument can be varargs.', argspec)

    def _assert_doc(self, doc, expected=None):
        expected = doc if expected is None else expected
        assert_equals(self._create_handler(doc=doc).doc, expected)

    def _assert_spec(self, argspec, minargs=0, maxargs=0, positional=[],
                     defaults=[], vararg=None):
        arguments = self._create_handler(argspec).arguments
        assert_equals(arguments.minargs, minargs)
        assert_equals(arguments.maxargs, maxargs)
        assert_equals(arguments.positional, positional)
        assert_equals(arguments.defaults, defaults)
        assert_equals(arguments.varargs, vararg)

    def _assert_fails(self, error, argspec=None, doc=None):
        assert_raises_with_msg(DataError, error,<commit_msg>utest: test_vararg_not_last(): Adapted changed exc msg for dynamic **kwargs support<commit_after>import unittest
import sys
import inspect

from robot.running.handlers import _PythonHandler, _JavaHandler, DynamicHandler
from robot import utils
from robot.utils.asserts import *
from robot.running.testlibraries import TestLibrary
from robot.running.dynamicmethods import GetKeywordArguments, GetKeywordDocumentation
from robot.errors import DataError

from classes import NameLibrary, DocLibrary, ArgInfoLibrary
from ArgumentsPython import ArgumentsPython
if utils.is_jython:
    import ArgumentsJava


def _get_handler_methods(lib):
    attrs = [getattr(lib, a) for a in dir(lib) if not a.startswith('_')]
    return [a for a in attrs if inspect.ismethod(a)]

def _get_java_handler_methods(lib):
    # This hack assumes that all java handlers used start with 'a_' -- easier
    # than excluding 'equals' etc. otherwise
    return [a for a in _get_handler_methods(lib) if a.__name__.startswith('a_') ]


class LibraryMock:
    def __init__(self, name='MyLibrary', scope='GLOBAL'):
        self.name = self.orig_name = name
        self.scope = scope


class TestPythonHandler(unittest.TestCase):

    def test_name(self):
        for method in _get_handler_methods(NameLibrary()):
            handler = _PythonHandler(LibraryMock('mylib'), method.__name__, method)
            assert_equals(handler.name, method.__doc__)
            assert_equals(handler.longname, 'mylib.'+method.__doc__)

    def test_docs(self):
        for method in _get_handler_methods(DocLibrary()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            assert_equals(handler.doc, method.expected_doc)
            assert_equals(handler.shortdoc, method.expected_shortdoc)

    def test_arguments(self):
        for method in _get_handler_methods(ArgInfoLibrary()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            args = handler.arguments
            argspec = (args.positional, args.defaults, args.varargs, args.kwargs)
            expected = eval(method.__doc__)
            assert_equals(argspec, expected, method.__name__)

    def test_arg_limits(self):
        for method in _get_handler_methods(ArgumentsPython()):
            handler = _PythonHandler(LibraryMock(), method.__name__, method)
            exp_mina, exp_maxa = eval(method.__doc__)
            assert_equals(handler.arguments.minargs, exp_mina)
            assert_equals(handler.arguments.maxargs, exp_maxa)

    def test_getarginfo_getattr(self):
        testlib = TestLibrary('classes.GetattrLibrary')
        handlers = testlib.handlers.values()
        assert_equals(len(handlers), 3)
        for handler in handlers:
            assert_true(handler.name in ['Foo','Bar','Zap'])
            assert_equals(handler.arguments.minargs, 0)
            assert_equals(handler.arguments.maxargs, sys.maxint)


class TestDynamicHandlerCreation(unittest.TestCase):

    def test_none_doc(self):
        self._assert_doc(None, '')

    def test_empty_doc(self):
        self._assert_doc('')

    def test_non_empty_doc(self):
        self._assert_doc('This is some documentation')

    def test_non_ascii_doc(self):
        self._assert_doc(u'P\xe4iv\xe4\xe4')

    if sys.platform != 'cli':
        def test_with_utf8_doc(self):
            doc = u'P\xe4iv\xe4\xe4'
            self._assert_doc(doc.encode('UTF-8'), doc)

    def test_invalid_doc_type(self):
        self._assert_fails('Return value must be string.', doc=True)

    def test_none_argspec(self):
        self._assert_spec(None, maxargs=sys.maxint,vararg='unknown')

    def test_empty_argspec(self):
        self._assert_spec([])

    def test_mandatory_args(self):
        for argspec in [['arg'], ['arg1', 'arg2', 'arg3']]:
            self._assert_spec(argspec, len(argspec), len(argspec), argspec)

    def test_only_default_args(self):
        self._assert_spec(['defarg1=value', 'defarg2=defvalue'], 0, 2,
                          ['defarg1', 'defarg2'], ['value', 'defvalue'])

    def test_default_value_may_contain_equal_sign(self):
        self._assert_spec(['d=foo=bar'], 0, 1, ['d'], ['foo=bar'])

    def test_varargs(self):
        self._assert_spec(['*vararg'], 0, sys.maxint, vararg='vararg')

    def test_integration(self):
        self._assert_spec(['arg', 'default=value'], 1, 2,
                          ['arg', 'default'], ['value'])
        self._assert_spec(['arg', 'default=value', '*var'], 1, sys.maxint,
                          ['arg', 'default'], ['value'], 'var')

    def test_invalid_argspec_type(self):
        for argspec in [True, [1, 2]]:
            self._assert_fails("Return value must be list of strings.", argspec)

    def test_mandatory_arg_after_default_arg(self):
        for argspec in [['d=v', 'arg'], ['a', 'b', 'c=v', 'd']]:
            self._assert_fails('Non-default argument after default arguments.',
                               argspec)

    def test_vararg_not_last(self):
        for argspec in [['*foo', 'arg'], ['arg', '*var', 'arg'],
                        ['a', 'b=d', '*var', 'c'], ['*var', '*vararg']]:
            self._assert_fails('Positional argument after varargs.', argspec)

    def _assert_doc(self, doc, expected=None):
        expected = doc if expected is None else expected
        assert_equals(self._create_handler(doc=doc).doc, expected)

    def _assert_spec(self, argspec, minargs=0, maxargs=0, positional=[],
                     defaults=[], vararg=None):
        arguments = self._create_handler(argspec).arguments
        assert_equals(arguments.minargs, minargs)
        assert_equals(arguments.maxargs, maxargs)
        assert_equals(arguments.positional, positional)
        assert_equals(arguments.defaults, defaults)
        assert_equals(arguments.varargs, vararg)

    def _assert_fails(self, error, argspec=None, doc=None):
        assert_raises_with_msg(DataError, error,
{'fraction_non_alphanumeric': 0.11952662721893491, 'fraction_numerical': 0.004902789518174133, 'mean_word_length': 3.2283774124374554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2244900', 'n_tokens_mistral': 3598, 'n_tokens_neox': 3486, 'n_words': 774}