Пример #1
0
    def __call__(self, step_text, step_impl):
        try:
            parser = Parser(step_impl.pattern, parsetype_registry.types)
        except ValueError:
            raise Exception("Cannot create parser")
            # raise StepPatternError(step_impl.pattern, func.__name__, e)

        match = parser.search(step_text, evaluate_result=False)
        if match:
            return self.Match(match), len(match.match.group())

        return None, 0
Пример #2
0
def test_parse_step_arguments_object(parse_pattern, string, expected_args,
                                     expected_kwargs):
    """
    Test functionality of ParseStepArguments object
    """
    # given
    parser = Parser(parse_pattern)
    match = parser.search(string, evaluate_result=False)
    args = matcher.ParseStepArguments(match)

    # when
    actual_args, actual_kwargs = args.evaluate()

    # then
    assert actual_args == expected_args
    assert actual_kwargs == expected_kwargs
Пример #3
0
def match_step(sentence, steps):
    """
        Tries to find a match from the given sentence with the given steps

        :param string sentence: the step sentence to match
        :param dict steps: the available registered steps

        :returns: the arguments and the func which were matched
        :rtype: tuple
    """
    potentional_matches = []
    for pattern, func in steps.items():
        if isinstance(pattern, re_pattern):  # pylint: disable=protected-access
            match = pattern.search(sentence)
            argument_match = RegexStepArguments(match)
            if match:
                longest_group = get_longest_group(match)
            else:
                longest_group = 0
        else:
            try:
                parser = Parser(pattern, CustomTypeRegistry().custom_types)
            except ValueError as e:
                raise StepPatternError(pattern, func.__name__, e)

            match = parser.search(sentence, evaluate_result=False)
            argument_match = ParseStepArguments(match)
            if match:
                longest_group = get_longest_group(match.match)
            else:
                longest_group = 0

        if match:
            step_match = StepMatch(argument_match=argument_match, func=func)
            if len(sentence) == longest_group:
                # if perfect match can be made we return it no
                # matter of the other potentional matches
                return step_match

            distance_to_perfect = abs(len(sentence) - longest_group)
            potentional_matches.append((step_match, distance_to_perfect))

    if potentional_matches:
        # get best match
        return min(potentional_matches, key=lambda x: x[1])[0]

    return None
Пример #4
0
def match_step(sentence, steps):
    """
        Tries to find a match from the given sentence with the given steps

        :param string sentence: the step sentence to match
        :param dict steps: the available registered steps

        :returns: the arguments and the func which were matched
        :rtype: tuple
    """
    potentional_matches = []
    for pattern, func in steps.items():
        if isinstance(pattern, re._pattern_type):  # pylint: disable=protected-access
            match = pattern.search(sentence)
            argument_match = RegexStepArguments(match)
            if match:
                longest_group = get_longest_group(match)
            else:
                longest_group = 0
        else:
            try:
                parser = Parser(pattern, CustomTypeRegistry().custom_types)
            except ValueError as e:
                raise StepPatternError(pattern, func.__name__, e)

            match = parser.search(sentence, evaluate_result=False)
            argument_match = ParseStepArguments(match)
            if match:
                longest_group = get_longest_group(match.match)
            else:
                longest_group = 0

        if match:
            step_match = StepMatch(argument_match=argument_match, func=func)
            if len(sentence) == longest_group:
                # if perfect match can be made we return it no
                # matter of the other potentional matches
                return step_match

            distance_to_perfect = abs(len(sentence) - longest_group)
            potentional_matches.append((step_match, distance_to_perfect))

    if potentional_matches:
        # get best match
        return min(potentional_matches, key=lambda x: x[1])[0]

    return None