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 #2
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)
Exemple #3
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')
    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 #5
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 #6
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 _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 #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)]
class TagStatDoc:
    def __init__(self, pattern, doc):
        self.text = doc
        self._matcher = Matcher(pattern, ignore=['_'])

    def matches(self, tag):
        return self._matcher.match(tag)
Exemple #10
0
 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
class TagStatDoc:

    def __init__(self, pattern, doc):
        self.text = doc
        self._matcher = Matcher(pattern, ignore=['_'])

    def matches(self, tag):
        return self._matcher.match(tag)
Exemple #12
0
class ByNameKeywordRemover(_KeywordRemover):
    def __init__(self, pattern):
        _KeywordRemover.__init__(self)
        self._matcher = Matcher(pattern, ignore='_')

    def start_keyword(self, kw):
        if self._matcher.match(kw.name) and not self._warning_or_error(kw):
            self._clear_content(kw)
 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 #14
0
class _SingleTagPattern(object):
    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore='_')

    def match(self, tags):
        return self._matcher.match_any(tags)

    def __unicode__(self):
        return self._matcher.pattern
class ByNameKeywordRemover(_KeywordRemover):

    def __init__(self, pattern):
        _KeywordRemover.__init__(self)
        self._matcher = Matcher(pattern, ignore='_')

    def start_keyword(self, kw):
        if self._matcher.match(kw.name) and not self._contains_warning(kw):
            self._clear_content(kw)
Exemple #16
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 #17
0
class _SingleTagPattern(object):
    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore=["_"])

    def match(self, tags):
        return any(self._matcher.match(tag) for tag in tags)

    def __unicode__(self):
        return self._matcher.pattern
Exemple #18
0
class _SingleTagPattern(object):

    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore='_')

    def match(self, tags):
        return self._matcher.match_any(tags)

    def __unicode__(self):
        return self._matcher.pattern
class ExcludeTests(SuiteVisitor):

    def __init__(self, pattern):
        self.matcher = Matcher(pattern)

    def start_suite(self, suite):
        """Remove tests that match the given pattern."""
        suite.tests = [t for t in suite.tests if not self._is_excluded(t)]

    def _is_excluded(self, test):
        return self.matcher.match(test.name) or self.matcher.match(test.longname)

    def end_suite(self, suite):
        """Remove suites that are empty after removing tests."""
        suite.suites = [s for s in suite.suites if s.test_count > 0]

    def visit_test(self, test):
        """Avoid visiting tests and their keywords to save a little time."""
        pass
Exemple #20
0
class _SingleTagPattern(object):

    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore='_')

    def match(self, tags):
        return self._matcher.match_any(tags)

    # FIXME: Why only this class methods below??
    def __unicode__(self):
        return self._matcher.pattern

    def __nonzero__(self):
        return bool(self._matcher)
Exemple #21
0
class SingleTagPattern(object):
    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore='_')

    def match(self, tags):
        return self._matcher.match_any(tags)

    def __iter__(self):
        yield self

    def __str__(self):
        return self._matcher.pattern

    def __bool__(self):
        return bool(self._matcher)
Exemple #22
0
class SingleTagPattern(object):

    def __init__(self, pattern):
        self._matcher = Matcher(pattern, ignore='_')

    def match(self, tags):
        return self._matcher.match_any(tags)

    def __iter__(self):
        yield self

    def __unicode__(self):
        return self._matcher.pattern

    def __nonzero__(self):
        return bool(self._matcher)
Exemple #23
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 #24
0
 def __init__(self, pattern):
     self._matcher = Matcher(pattern, ignore='_')
 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'")
Exemple #26
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 #27
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 #28
0
 def _matches_not(self, string, pattern, **config):
     assert not Matcher(pattern, **config).match(string), pattern
Exemple #29
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_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(())
 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 #32
0
 def __init__(self, pattern, doc):
     self.text = doc
     self._matcher = Matcher(pattern, ignore=['_'])
 def __init__(self, pattern):
     _KeywordRemover.__init__(self)
     self._matcher = Matcher(pattern, ignore='_')
 def __init__(self, pattern, doc):
     self.text = doc
     self._matcher = Matcher(pattern, ignore=['_'])
 def __init__(self, pattern):
     self.matcher = Matcher(pattern)
Exemple #36
0
 def __init__(self, pattern):
     _KeywordRemover.__init__(self)
     self._matcher = Matcher(pattern, ignore='_')
 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 #38
0
 def __init__(self, pattern):
     self._matcher = Matcher(pattern, ignore='_')
Exemple #39
0
 def test_bytes(self):
     assert_raises(TypeError, Matcher, b'foo')
     assert_raises(TypeError, Matcher('foo').match, b'foo')