Beispiel #1
0
    def test_returns_arguments_based_on_groups(self):
        func = lambda x: -x
        matcher = RegexMatcher(func, 'foo')

        regex = Mock()
        regex.groupindex = {'foo': 4, 'baz': 5}

        match = Mock()
        match.groups.return_value = ('1', '2', '3', 'bar', '-45.3')
        positions = {
            1: (13, 14),
            2: (16, 17),
            3: (22, 23),
            4: (32, 35),
            5: (39, 44),
        }
        match.start.side_effect = lambda idx: positions[idx][0]
        match.end.side_effect = lambda idx: positions[idx][1]

        regex.match.return_value = match
        matcher.regex = regex

        expected = [
            (13, 14, '1', '1', None),
            (16, 17, '2', '2', None),
            (22, 23, '3', '3', None),
            (32, 35, 'bar', 'bar', 'foo'),
            (39, 44, '-45.3', '-45.3', 'baz'),
        ]

        m = matcher.match('some numbers 1, 2 and 3 and the bar is -45.3')
        assert m.func is func
        args = m.arguments
        have = [(a.start, a.end, a.original, a.value, a.name) for a in args]
        eq_(have, expected)
 def test_returns_none_if_regex_does_not_match(self):
     RegexMatcher = self.MATCHER_CLASS
     matcher = RegexMatcher(None, 'a string')
     regex = Mock()
     regex.match.return_value = None
     matcher.regex = regex
     assert matcher.match('just a random step') is None
Beispiel #3
0
    def test_returns_arguments_based_on_groups(self):
        func = lambda x: -x
        matcher = RegexMatcher(func, 'foo')

        regex = Mock()
        regex.groupindex = {'foo': 4, 'baz': 5}

        match = Mock()
        match.groups.return_value = ('1', '2', '3', 'bar', '-45.3')
        positions = {
            1: (13, 14),
            2: (16, 17),
            3: (22, 23),
            4: (32, 35),
            5: (39, 44),
        }
        match.start.side_effect = lambda idx: positions[idx][0]
        match.end.side_effect = lambda idx: positions[idx][1]

        regex.match.return_value = match
        matcher.regex = regex

        expected = [
            (13, 14, '1', '1', None),
            (16, 17, '2', '2', None),
            (22, 23, '3', '3', None),
            (32, 35, 'bar', 'bar', 'foo'),
            (39, 44, '-45.3', '-45.3', 'baz'),
        ]

        m = matcher.match('some numbers 1, 2 and 3 and the bar is -45.3')
        assert m.func is func
        args = m.arguments
        have = [(a.start, a.end, a.original, a.value, a.name) for a in args]
        eq_(have, expected)
Beispiel #4
0
    def test_steps_with_same_prefix_are_not_ordering_sensitive(self):
        # -- RELATED-TO: issue #280
        # pylint: disable=unused-argument
        def step_func1(context):
            pass  # pylint: disable=multiple-statements

        def step_func2(context):
            pass  # pylint: disable=multiple-statements

        # pylint: enable=unused-argument
        matcher1 = RegexMatcher(step_func1, "I do something")
        matcher2 = RegexMatcher(step_func2, "I do something more")

        # -- CHECK: ORDERING SENSITIVITY
        matched1 = matcher1.match(matcher2.string)
        matched2 = matcher2.match(matcher1.string)
        assert matched1 is None
        assert matched2 is None

        # -- CHECK: Can match itself (if step text is simple)
        matched1 = matcher1.match(matcher1.string)
        matched2 = matcher2.match(matcher2.string)
        assert isinstance(matched1, Match)
        assert isinstance(matched2, Match)
Beispiel #5
0
    def test_steps_with_same_prefix_are_not_ordering_sensitive(self):
        # -- RELATED-TO: issue #280
        # pylint: disable=unused-argument
        def step_func1(context): pass   # pylint: disable=multiple-statements
        def step_func2(context): pass   # pylint: disable=multiple-statements
        # pylint: enable=unused-argument
        matcher1 = RegexMatcher(step_func1, "I do something")
        matcher2 = RegexMatcher(step_func2, "I do something more")

        # -- CHECK: ORDERING SENSITIVITY
        matched1 = matcher1.match(matcher2.string)
        matched2 = matcher2.match(matcher1.string)
        assert matched1 is None
        assert matched2 is None

        # -- CHECK: Can match itself (if step text is simple)
        matched1 = matcher1.match(matcher1.string)
        matched2 = matcher2.match(matcher2.string)
        assert isinstance(matched1, Match)
        assert isinstance(matched2, Match)
Beispiel #6
0
 def test_step_should_not_use_regex_begin_and_end_marker(self):
     RegexMatcher(None, "^I do something$")