def test_order_does_not_affect_equality(self):
     d = dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)
     od1 = OrderedDict(sorted(d.items()))
     od2 = OrderedDict(reversed(list(od1.items())))
     dd1 = DotDict(sorted(d.items()))
     dd2 = DotDict(reversed(list(dd1.items())))
     for d1, d2 in [(dd1, dd2), (dd1, d), (dd2, d), (dd1, od1), (dd2, od2)]:
         assert_equal(d1, d2)
         assert_equal(d2, d1)
     if not IRONPYTHON:
         # https://github.com/IronLanguages/main/issues/1168
         for d1, d2 in [(dd1, od2), (dd2, od1)]:
             assert_equal(d1, d2)
             assert_equal(d2, d1)
     assert_not_equal(od1, od2)
Beispiel #2
0
 def test_order_does_not_affect_equality(self):
     d = dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)
     od1 = OrderedDict(sorted(d.items()))
     od2 = OrderedDict(reversed(list(od1.items())))
     dd1 = DotDict(sorted(d.items()))
     dd2 = DotDict(reversed(list(dd1.items())))
     for d1, d2 in [(dd1, dd2), (dd1, d), (dd2, d), (dd1, od1), (dd2, od2)]:
         assert_equal(d1, d2)
         assert_equal(d2, d1)
     if not IRONPYTHON:
         # https://github.com/IronLanguages/main/issues/1168
         for d1, d2 in [(dd1, od2), (dd2, od1)]:
             assert_equal(d1, d2)
             assert_equal(d2, d1)
     assert_not_equal(od1, od2)
def get_variables(*args):
    if args:
        return dict((args[i], args[i + 1]) for i in range(0, len(args), 2))
    list_ = ['1', '2', 3]
    tuple_ = tuple(list_)
    dict_ = {'a': 1, 2: 'b'}
    ordered = OrderedDict((chr(o), o) for o in range(97, 107))
    open_file = open(__file__)
    closed_file = open(__file__)
    closed_file.close()
    return {
        'LIST__list': list_,
        'LIST__tuple': tuple_,
        'LIST__generator': (i for i in range(5)),
        'DICT__dict': dict_,
        'DICT__ordered': ordered,
        'scalar_list': list_,
        'scalar_tuple': tuple_,
        'scalar_generator': (i for i in range(5)),
        'scalar_dict': dict_,
        'failing_generator': failing_generator,
        'failing_dict': FailingDict({1: 2}),
        'open_file': open_file,
        'closed_file': closed_file
    }
 def _get_data(self, statistics, errors, basemillis):
     gentime = time.localtime()
     return OrderedDict([('stats', statistics), ('errors', errors),
                         ('baseMillis', basemillis),
                         ('generatedMillis',
                          long(time.mktime(gentime) * 1000) - basemillis),
                         ('generatedTimestamp',
                          format_time(gentime, gmtsep=' '))])
Beispiel #5
0
def get_variables(*args):
    if args:
        return dict((args[i], args[i + 1]) for i in range(0, len(args), 2))
    list_ = ['1', '2', 3]
    tuple_ = tuple(list_)
    dict_ = {'a': 1, 2: 'b'}
    ordered = OrderedDict((chr(o), o) for o in range(97, 107))
    return {
        'LIST__list': list_,
        'LIST__tuple': tuple_,
        'LIST__generator': (i for i in range(5)),
        'DICT__dict': dict_,
        'DICT__ordered': ordered,
        'scalar_list': list_,
        'scalar_tuple': tuple_,
        'scalar_generator': (i for i in range(5)),
        'scalar_dict': dict_
    }
def get_variables():
    variables = dict(
        BYTES_WITHOUT_NON_ASCII=b'hyva',
        BYTES_WITH_NON_ASCII=b'\xe4',
        TUPLE_0=(),
        TUPLE_1=('a', ),
        TUPLE_2=('a', 2),
        TUPLE_3=('a', 'b', 'c'),
        LIST=['a', 'b', 'cee', 'b', 42],
        LIST_0=[],
        LIST_1=['a'],
        LIST_2=['a', 2],
        LIST_3=['a', 'b', 'c'],
        DICT={
            'a': 1,
            'A': 2,
            u'\xe4': 3,
            u'\xc4': 4
        },
        ORDERED_DICT=OrderedDict([('a', 1), ('A', 2), (u'\xe4', 3),
                                  (u'\xc4', 4)]),
        DICT_0={},
        DICT_1={'a': 1},
        DICT_2={
            'a': 1,
            2: 'b'
        },
        DICT_3={
            'a': 1,
            'b': 2,
            'c': 3
        },
    )
    if os.name == 'java':
        variables.update(get_java_variables(**variables))
    return variables
Beispiel #7
0
 def __init__(self, resource):
     self.user_keywords = UserLibrary(resource,
                                      UserLibrary.TEST_CASE_FILE_TYPE)
     self.libraries = OrderedDict()
     self.resources = ImportCache()
     self.search_order = ()
Beispiel #8
0
class KeywordStore(object):

    def __init__(self, resource):
        self.user_keywords = UserLibrary(resource,
                                         UserLibrary.TEST_CASE_FILE_TYPE)
        self.libraries = OrderedDict()
        self.resources = ImportCache()
        self.search_order = ()

    def get_library(self, name_or_instance):
        if name_or_instance is None:
            raise DataError("Library can not be None.")
        if is_string(name_or_instance):
            return self._get_lib_by_name(name_or_instance)
        return self._get_lib_by_instance(name_or_instance)

    def _get_lib_by_name(self, name):
        if name in self.libraries:
            return self.libraries[name]
        matches = [lib for lib in self.libraries.values() if eq(lib.name, name)]
        if len(matches) == 1:
            return matches[0]
        self._no_library_found(name, multiple=bool(matches))

    def _no_library_found(self, name, multiple=False):
        if multiple:
            raise DataError("Multiple libraries matching '%s' found." % name)
        raise DataError("No library '%s' found." % name)

    def _get_lib_by_instance(self, instance):
        for lib in self.libraries.values():
            if lib.get_instance(create=False) is instance:
                return lib
        self._no_library_found(instance)

    def get_runner(self, name):
        runner = self._get_runner(name)
        if runner is None:
            self._raise_no_keyword_found(name)
        return runner

    def _raise_no_keyword_found(self, name):
        msg = "No keyword with name '%s' found." % name
        finder = KeywordRecommendationFinder(self.user_keywords,
                                             self.libraries,
                                             self.resources)
        recommendations = finder.recommend_similar_keywords(name)
        msg = finder.format_recommendations(msg, recommendations)
        raise DataError(msg)

    def _get_runner(self, name):
        runner = None
        if not name:
            raise DataError('Keyword name cannot be empty.')
        if not is_string(name):
            raise DataError('Keyword name must be a string.')
        if '.' in name:
            runner = self._get_explicit_runner(name)
        if not runner:
            runner = self._get_implicit_runner(name)
        if not runner:
            runner = self._get_bdd_style_runner(name)
        return runner

    def _get_bdd_style_runner(self, name):
        for prefix in ['given ', 'when ', 'then ', 'and ', 'but ']:
            if name.lower().startswith(prefix):
                runner = self._get_runner(name[len(prefix):])
                if runner:
                    runner = copy.copy(runner)
                    runner.name = name
                return runner
        return None

    def _get_implicit_runner(self, name):
        for method in [self._get_runner_from_test_case_file_user_keywords,
                       self._get_runner_from_resource_file_user_keywords,
                       self._get_runner_from_library_keywords]:
            runner = method(name)
            if runner:
                return runner
        return None

    def _get_runner_from_test_case_file_user_keywords(self, name):
        if name in self.user_keywords.handlers:
            return self.user_keywords.handlers.create_runner(name)

    def _get_runner_from_resource_file_user_keywords(self, name):
        found = [lib.handlers.create_runner(name)
                 for lib in self.resources.values()
                 if name in lib.handlers]
        if not found:
            return None
        if len(found) > 1:
            found = self._get_runner_based_on_search_order(found)
        if len(found) == 1:
            return found[0]
        self._raise_multiple_keywords_found(name, found)

    def _get_runner_from_library_keywords(self, name):
        found = [lib.handlers.create_runner(name) for lib in self.libraries.values()
                 if name in lib.handlers]
        if not found:
            return None
        if len(found) > 1:
            found = self._get_runner_based_on_search_order(found)
        if len(found) == 2:
            found = self._filter_stdlib_runner(*found)
        if len(found) == 1:
            return found[0]
        self._raise_multiple_keywords_found(name, found)

    def _get_runner_based_on_search_order(self, runners):
        for libname in self.search_order:
            for runner in runners:
                if eq(libname, runner.libname):
                    return [runner]
        return runners

    def _filter_stdlib_runner(self, runner1, runner2):
        stdlibs_without_remote = STDLIBS - set(['Remote'])
        if runner1.library.orig_name in stdlibs_without_remote:
            standard, custom = runner1, runner2
        elif runner2.library.orig_name in stdlibs_without_remote:
            standard, custom = runner2, runner1
        else:
            return [runner1, runner2]
        if not RUN_KW_REGISTER.is_run_keyword(custom.library.orig_name, custom.name):
            self._custom_and_standard_keyword_conflict_warning(custom, standard)
        return [custom]

    def _custom_and_standard_keyword_conflict_warning(self, custom, standard):
        custom_with_name = standard_with_name = ''
        if custom.library.name != custom.library.orig_name:
            custom_with_name = " imported as '%s'" % custom.library.name
        if standard.library.name != standard.library.orig_name:
            standard_with_name = " imported as '%s'" % standard.library.name
        warning = Message("Keyword '%s' found both from a custom test library "
                          "'%s'%s and a standard library '%s'%s. The custom "
                          "keyword is used. To select explicitly, and to get "
                          "rid of this warning, use either '%s' or '%s'."
                          % (standard.name,
                             custom.library.orig_name, custom_with_name,
                             standard.library.orig_name, standard_with_name,
                             custom.longname, standard.longname), level='WARN')
        if custom.pre_run_messages:
            custom.pre_run_messages.append(warning)
        else:
            custom.pre_run_messages = [warning]

    def _get_explicit_runner(self, name):
        found = []
        for owner_name, kw_name in self._yield_owner_and_kw_names(name):
            found.extend(self._find_keywords(owner_name, kw_name))
        if len(found) > 1:
            self._raise_multiple_keywords_found(name, found, implicit=False)
        return found[0] if found else None

    def _yield_owner_and_kw_names(self, full_name):
        tokens = full_name.split('.')
        for i in range(1, len(tokens)):
            yield '.'.join(tokens[:i]), '.'.join(tokens[i:])

    def _find_keywords(self, owner_name, name):
        return [owner.handlers.create_runner(name)
                for owner in chain(self.libraries.values(), self.resources.values())
                if eq(owner.name, owner_name) and name in owner.handlers]

    def _raise_multiple_keywords_found(self, name, found, implicit=True):
        error = "Multiple keywords with name '%s' found" % name
        if implicit:
            error += ". Give the full name of the keyword you want to use"
        names = sorted(runner.longname for runner in found)
        raise DataError('\n    '.join([error+':'] + names))
Beispiel #9
0
 def _get_data(self, statistics, errors, basemillis):
     return OrderedDict([('stats', statistics), ('errors', errors),
                         ('baseMillis', basemillis),
                         ('generated', int(time.time() * 1000) - basemillis)
                         ])
Beispiel #10
0
 def __init__(self):
     self._cache = OrderedDict({'*': self._zero_index})
Beispiel #11
0
 def __init__(self, resource):
     self.user_keywords = UserLibrary(resource,
                                      UserLibrary.TEST_CASE_FILE_TYPE)
     self.libraries = OrderedDict()
     self.resources = ImportCache()
     self.search_order = ()
Beispiel #12
0
class KeywordStore(object):
    def __init__(self, resource):
        self.user_keywords = UserLibrary(resource,
                                         UserLibrary.TEST_CASE_FILE_TYPE)
        self.libraries = OrderedDict()
        self.resources = ImportCache()
        self.search_order = ()

    def get_library(self, name_or_instance):
        if name_or_instance is None:
            raise DataError("Library can not be None.")
        if is_string(name_or_instance):
            return self._get_lib_by_name(name_or_instance)
        return self._get_lib_by_instance(name_or_instance)

    def _get_lib_by_name(self, name):
        if name in self.libraries:
            return self.libraries[name]
        matches = [
            lib for lib in self.libraries.values() if eq(lib.name, name)
        ]
        if len(matches) == 1:
            return matches[0]
        self._no_library_found(name, multiple=bool(matches))

    def _no_library_found(self, name, multiple=False):
        if multiple:
            raise DataError("Multiple libraries matching '%s' found." % name)
        raise DataError("No library '%s' found." % name)

    def _get_lib_by_instance(self, instance):
        for lib in self.libraries.values():
            if lib.get_instance(create=False) is instance:
                return lib
        self._no_library_found(instance)

    def get_runner(self, name):
        runner = self._get_runner(name)
        if runner is None:
            self._raise_no_keyword_found(name)
        return runner

    def _raise_no_keyword_found(self, name):
        msg = "No keyword with name '%s' found." % name
        finder = KeywordRecommendationFinder(self.user_keywords,
                                             self.libraries, self.resources)
        recommendations = finder.recommend_similar_keywords(name)
        msg = finder.format_recommendations(msg, recommendations)
        raise KeywordError(msg)

    def _get_runner(self, name):
        if not name:
            raise DataError('Keyword name cannot be empty.')
        if not is_string(name):
            raise DataError('Keyword name must be a string.')
        runner = self._get_runner_from_test_case_file(name)
        if not runner and '.' in name:
            runner = self._get_explicit_runner(name)
        if not runner:
            runner = self._get_implicit_runner(name)
        if not runner:
            runner = self._get_bdd_style_runner(name)
        return runner

    def _get_bdd_style_runner(self, name):
        for prefix in ['given ', 'when ', 'then ', 'and ', 'but ']:
            if name.lower().startswith(prefix):
                runner = self._get_runner(name[len(prefix):])
                if runner:
                    runner = copy.copy(runner)
                    runner.name = name
                return runner
        return None

    def _get_implicit_runner(self, name):
        runner = self._get_runner_from_resource_files(name)
        if not runner:
            runner = self._get_runner_from_libraries(name)
        return runner

    def _get_runner_from_test_case_file(self, name):
        if name in self.user_keywords.handlers:
            return self.user_keywords.handlers.create_runner(name)

    def _get_runner_from_resource_files(self, name):
        found = [
            lib.handlers.create_runner(name)
            for lib in self.resources.values() if name in lib.handlers
        ]
        if not found:
            return None
        if len(found) > 1:
            found = self._get_runner_based_on_search_order(found)
        if len(found) == 1:
            return found[0]
        self._raise_multiple_keywords_found(name, found)

    def _get_runner_from_libraries(self, name):
        found = [
            lib.handlers.create_runner(name)
            for lib in self.libraries.values() if name in lib.handlers
        ]
        if not found:
            return None
        if len(found) > 1:
            found = self._get_runner_based_on_search_order(found)
        if len(found) == 2:
            found = self._filter_stdlib_runner(*found)
        if len(found) == 1:
            return found[0]
        self._raise_multiple_keywords_found(name, found)

    def _get_runner_based_on_search_order(self, runners):
        for libname in self.search_order:
            for runner in runners:
                if eq(libname, runner.libname):
                    return [runner]
        return runners

    def _filter_stdlib_runner(self, runner1, runner2):
        stdlibs_without_remote = STDLIBS - set(['Remote'])
        if runner1.library.orig_name in stdlibs_without_remote:
            standard, custom = runner1, runner2
        elif runner2.library.orig_name in stdlibs_without_remote:
            standard, custom = runner2, runner1
        else:
            return [runner1, runner2]
        if not RUN_KW_REGISTER.is_run_keyword(custom.library.orig_name,
                                              custom.name):
            self._custom_and_standard_keyword_conflict_warning(
                custom, standard)
        return [custom]

    def _custom_and_standard_keyword_conflict_warning(self, custom, standard):
        custom_with_name = standard_with_name = ''
        if custom.library.name != custom.library.orig_name:
            custom_with_name = " imported as '%s'" % custom.library.name
        if standard.library.name != standard.library.orig_name:
            standard_with_name = " imported as '%s'" % standard.library.name
        warning = Message(
            "Keyword '%s' found both from a custom test library "
            "'%s'%s and a standard library '%s'%s. The custom "
            "keyword is used. To select explicitly, and to get "
            "rid of this warning, use either '%s' or '%s'." %
            (standard.name, custom.library.orig_name, custom_with_name,
             standard.library.orig_name, standard_with_name, custom.longname,
             standard.longname),
            level='WARN')
        if custom.pre_run_messages:
            custom.pre_run_messages.append(warning)
        else:
            custom.pre_run_messages = [warning]

    def _get_explicit_runner(self, name):
        found = []
        for owner_name, kw_name in self._yield_owner_and_kw_names(name):
            found.extend(self._find_keywords(owner_name, kw_name))
        if len(found) > 1:
            self._raise_multiple_keywords_found(name, found, implicit=False)
        return found[0] if found else None

    def _yield_owner_and_kw_names(self, full_name):
        tokens = full_name.split('.')
        for i in range(1, len(tokens)):
            yield '.'.join(tokens[:i]), '.'.join(tokens[i:])

    def _find_keywords(self, owner_name, name):
        return [
            owner.handlers.create_runner(name) for owner in chain(
                self.libraries.values(), self.resources.values())
            if eq(owner.name, owner_name) and name in owner.handlers
        ]

    def _raise_multiple_keywords_found(self, name, found, implicit=True):
        error = "Multiple keywords with name '%s' found" % name
        if implicit:
            error += ". Give the full name of the keyword you want to use"
        names = sorted(runner.longname for runner in found)
        raise KeywordError('\n    '.join([error + ':'] + names))