Exemple #1
0
 def test_spaceless(self):
     for text in ['fbar', 'foobar']:
         assert Matcher('f*bar').match(text)
         assert Matcher('f * b a r').match(text)
         assert Matcher('f*bar', spaceless=False).match(text)
     for text in ['f b a r', 'f o o b a r', '   foo bar   ', 'fbar\n']:
         assert Matcher('f*bar').match(text)
         assert not Matcher('f*bar', spaceless=False).match(text)
Exemple #2
0
 def _should_run_except(self, branch, error):
     if not branch.patterns:
         return True
     matchers = {
         'GLOB:':
         lambda s, p: Matcher(p, spaceless=False, caseless=False).match(s),
         'EQUALS:':
         lambda s, p: s == p,
         'STARTS:':
         lambda s, p: s.startswith(p),
         'REGEXP:':
         lambda s, p: re.match(rf'{p}\Z', s) is not None
     }
     message = error.message
     for pattern in branch.patterns:
         if not pattern.startswith(tuple(matchers)):
             pattern = self._context.variables.replace_scalar(pattern)
             if message == pattern:
                 return True
         else:
             prefix, pat = pattern.split(':', 1)
             pat = self._context.variables.replace_scalar(pat.lstrip())
             if matchers[f'{prefix}:'](message, pat):
                 return True
     return False
 def _should_run_except(self, branch, error):
     if not branch.patterns:
         return True
     matchers = {
         'GLOB':
         lambda m, p: Matcher(p, spaceless=False, caseless=False).match(m),
         'LITERAL':
         lambda m, p: m == p,
         'REGEXP':
         lambda m, p: re.match(rf'{p}\Z', m) is not None,
         'START':
         lambda m, p: m.startswith(p)
     }
     if branch.pattern_type:
         pattern_type = self._context.variables.replace_string(
             branch.pattern_type)
     else:
         pattern_type = 'LITERAL'
     matcher = matchers.get(pattern_type.upper())
     if not matcher:
         raise DataError(f"Invalid EXCEPT pattern type '{pattern_type}', "
                         f"expected {seq2str(matchers, lastsep=' or ')}.")
     for pattern in branch.patterns:
         if matcher(error.message,
                    self._context.variables.replace_string(pattern)):
             return True
     return False
 def _error_is_expected(self, error, handler):
     if isinstance(error, ReturnFromKeyword):
         return False
     if any(e.skip for e in error.get_errors()):
         return False
     patterns = handler.patterns
     if not patterns:
         # The default (empty) except matches everything
         return True
     matchers = {
         'GLOB:':
         lambda s, p: Matcher(p, spaceless=False, caseless=False).match(s),
         'EQUALS:':
         lambda s, p: s == p,
         'STARTS:':
         lambda s, p: s.startswith(p),
         'REGEXP:':
         lambda s, p: re.match(rf'{p}\Z', s) is not None
     }
     message = error.message
     for pattern in patterns:
         if not pattern.startswith(tuple(matchers)):
             pattern = self._context.variables.replace_scalar(pattern)
             if message == pattern:
                 return True
         else:
             prefix, pat = pattern.split(':', 1)
             pat = self._context.variables.replace_scalar(pat.lstrip())
             if matchers[f'{prefix}:'](message, pat):
                 return True
     return False
Exemple #5
0
 def test_regexp_matcher(self):
     matcher = Matcher('F .*', ignore=['-'], caseless=False, spaceless=True,
                       regexp=True)
     assert matcher.pattern == 'F .*'
     assert matcher.match('Foo')
     assert matcher.match('--Foo')
     assert not matcher.match('foo')
Exemple #6
0
    def page_should_not_contain_match(
            self,
            txt: str,
            ignore_case: bool = False,
            error_message: Optional[str] = None) -> None:
        """Assert that the text displayed on the mainframe screen does NOT match the given pattern.

        Pattern matching is similar to matching files in a shell, and it is always case sensitive.
        In the pattern, * matches anything and ? matches any single character.

        Note that for this keyword the entire screen is considered a string. So if you want to search
        for the string "something" and it is somewhere other than at the beginning or end of the screen, it
        should be reported as follows: **something**

        The assertion is case sensitive. If you want it to be case insensitive, you can pass the argument ignore_case=True.

        You can change the exception message by setting a custom string to error_message.

        Example:
            | Page Should Not Contain Match | **something** |
            | Page Should Not Contain Match | **so???hing** |
            | Page Should Not Contain Match | **someTHING** | ignore_case=True |
            | Page Should Not Contain Match | **something** | error_message=New error message |
        """
        message = error_message
        all_screen = self._read_all_screen()
        if ignore_case:
            txt = txt.lower()
            all_screen = all_screen.lower()
        matcher = Matcher(txt, caseless=False, spaceless=False)
        result = matcher.match(all_screen)
        if result:
            if message is None:
                message = 'There are matches found for "' + txt + '" pattern'
            raise Exception(message)
    def page_should_not_contain_match(self, txt, ignore_case=False, error_message=None):
        """Fails if the given string matches the given pattern.

        Pattern matching is similar as matching files in a shell, and it is always case-sensitive.
        In the pattern, * matches to anything and ? matches to any single character.

        Note that the entire screen is only considered a string for this keyword, so if you want to search
        for the string "something" and it is somewhere other than at the beginning or end of the screen it
        should be reported as follows: **something**

        The search is case sensitive, if you want ignore this you can pass the argument: ignore_case=${True} and you
        can edit the raise exception message with error_message.

        Example:
            | Page Should Not Contain Match | **something** |
            | Page Should Not Contain Match | **so???hing** |
            | Page Should Not Contain Match | **someTHING** | ignore_case=${True} |
            | Page Should Not Contain Match | **something** | error_message=New error message |
        """
        message = error_message
        all_screen = self._read_all_screen()
        if ignore_case:
            txt = txt.lower()
            all_screen = all_screen.lower()
        matcher = Matcher(txt, caseless=False, spaceless=False)
        result = matcher.match(all_screen)
        if result:
            if message is None:
                message = 'There are matches found for "' + txt + '" pattern'
            raise Exception(message)
Exemple #8
0
def _get_matches_in_iterable(iterable, pattern, case_insensitive=False,
                             whitespace_insensitive=False):
    if not is_string(pattern):
        raise TypeError("Pattern must be string, got '%s'." % type_name(pattern))
    regexp = False
    if pattern.startswith('regexp='):
        pattern = pattern[7:]
        regexp = True
    elif pattern.startswith('glob='):
        pattern = pattern[5:]
    matcher = Matcher(pattern,
                      caseless=is_truthy(case_insensitive),
                      spaceless=is_truthy(whitespace_insensitive),
                      regexp=regexp)
    return [string for string in iterable
            if is_string(string) and matcher.match(string)]
 def _message_matches(self, actual, expected):
     if actual == expected:
         return True
     if expected.startswith('REGEXP:'):
         pattern = '^%s$' % expected.replace('REGEXP:', '', 1).strip()
         if re.match(pattern, actual, re.DOTALL):
             return True
     if expected.startswith('GLOB:'):
         pattern = expected.replace('GLOB:', '', 1).strip()
         matcher = Matcher(pattern, caseless=False, spaceless=False)
         if matcher.match(actual):
             return True
     if expected.startswith('STARTS:'):
         start = expected.replace('STARTS:', '', 1).strip()
         if actual.startswith(start):
             return True
     return False
Exemple #10
0
 def test_bytes(self):
     if IRONPYTHON:
         return
     elif PY2:
         assert Matcher(b'foo').match(b'foo')
         assert Matcher(b'f*').match(b'foo')
         assert Matcher('f*').match(b'foo')
         assert Matcher(b'f*').match('foo')
         assert Matcher(b'f.*', regexp=True).match(b'foo')
     else:
         assert_raises(TypeError, Matcher, b'foo')
         assert_raises(TypeError, Matcher('foo').match, b'foo')
Exemple #11
0
def _get_matches_in_iterable(iterable, pattern, case_insensitive=False,
                             whitespace_insensitive=False):
    if not iterable:
        return []
    regexp = False
    if not isinstance(pattern, basestring):
        raise TypeError(
            "Pattern must be string, got '%s'." % type(pattern).__name__)
    if pattern.startswith('regexp='):
        pattern = pattern[7:]
        regexp = True
    elif pattern.startswith('glob='):
        pattern = pattern[5:]
    matcher = Matcher(pattern, caseless=case_insensitive,
                      spaceless=whitespace_insensitive, regexp=regexp)
    return [string for string in iterable
            if isinstance(string, basestring)
            and matcher.match(string)]
Exemple #12
0
 def test_bytes(self):
     assert_raises(TypeError, Matcher, b'foo')
     assert_raises(TypeError, Matcher('foo').match, b'foo')
Exemple #13
0
 def __init__(self, pattern, doc):
     self.text = doc
     self._matcher = Matcher(pattern, ignore=['_'])
Exemple #14
0
 def test_regexp_match_any(self):
     matcher = Matcher('H.llo', regexp=True)
     assert matcher.match_any(('Hello', 'world'))
     assert matcher.match_any(['jam', 'is', 'hillo'])
     assert not matcher.match_any(('no', 'match', 'here'))
     assert not matcher.match_any(())
Exemple #15
0
 def test_match_any(self):
     matcher = Matcher('H?llo')
     assert matcher.match_any(('Hello', 'world'))
     assert matcher.match_any(['jam', 'is', 'hillo'])
     assert not matcher.match_any(('no', 'match', 'here'))
     assert not matcher.match_any(())
Exemple #16
0
 def _matches_not(self, string, pattern, **config):
     assert not Matcher(pattern, **config).match(string), pattern
Exemple #17
0
 def test_ipy_bug_workaround(self):
     # https://github.com/IronLanguages/ironpython2/issues/515
     matcher = Matcher("'12345678901234567890'")
     assert matcher.match("'12345678901234567890'")
     assert not matcher.match("'xxx'")
 def test_bytes(self):
     if IRONPYTHON:
         return
     assert Matcher(b'foo').match(b'foo')
     assert Matcher(b'f*').match(b'foo')
     assert Matcher(b'f.*', regexp=True).match(b'foo')
Exemple #19
0
 def __init__(self, pattern):
     self._matcher = Matcher(pattern, ignore='_')
Exemple #20
0
 def __init__(self, pattern):
     _KeywordRemover.__init__(self)
     self._matcher = Matcher(pattern, ignore='_')