bdd: Make message matching work.

This commit is contained in:
Florian Bruhin 2015-11-01 22:10:48 +01:00
parent dac2898585
commit b79ccb5e79
4 changed files with 44 additions and 19 deletions

View File

@ -14,12 +14,12 @@ Feature: Going back and forward.
data/backforward/1.txt
data/backforward/2.txt
# Scenario: Going back without history
# Given I open data/backforward/1.txt
# When I run :back
# Then the error "At beginning of history." should be shown.
#
# Scenario: Going forward without history
# Given I open data/backforward/1.txt
# When I run :forward
# Then the error "At end of history." should be shown.
Scenario: Going back without history
Given I open data/backforward/1.txt
When I run :back
Then the error "At beginning of history." should be shown.
Scenario: Going forward without history
Given I open data/backforward/1.txt
When I run :forward
Then the error "At end of history." should be shown.

View File

@ -18,6 +18,7 @@
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import sys
import logging
import pytest
@ -67,6 +68,7 @@ def list_of_loaded_pages(httpbin, pages):
assert httpbin.get_requests() == requests
@bdd.then(bdd.parsers.parse('the error "{msg}" should be shown.'))
def expect_error(quteproc, msg):
quteproc.mark_expected(category='message', loglevel='ERROR', msg=msg)
@bdd.then(bdd.parsers.parse('the error "{message}" should be shown.'))
def expect_error(quteproc, message):
quteproc.mark_expected(category='message', loglevel=logging.ERROR,
message=message)

View File

@ -190,16 +190,26 @@ class QuteProc(testprocess.Process):
self.send_cmd(':open ' + url)
self.wait_for(category='webview', message=url_loaded_pattern)
def mark_expected(self, category=None, loglevel=None, msg=None):
def mark_expected(self, category=None, loglevel=None, message=None):
"""Mark a given logging message as expected."""
found_message = False
# Search existing messages
for item in self._data:
if category is not None and item.category != category:
continue
elif loglevel is not None and item.loglevel != loglevel:
continue
elif msg is not None and item.message != msg:
elif message is not None and item.message != message:
continue
item.expected = True
found_message = True
# If there is none, wait for the message
if not found_message:
line = self.wait_for(category=category, loglevel=loglevel,
message=message)
line.expected = True
@pytest.yield_fixture

View File

@ -164,9 +164,17 @@ class Process(QObject):
searched data. Every given argument is treated as a pattern which
the attribute has to match against.
If a string is passed, the patterns are treated as a fnmatch glob
pattern. Alternatively, a compiled regex can be passed to match against
that.
The behavior of this method is slightly different depending on the
types of the filtered values:
- If the value is a string or bytes object and the expected value is
too, the pattern is treated as a fnmatch glob pattern.
- If the value is a string or bytes object and the expected value is a
compiled regex, it is used for matching.
- If the value is any other type, == is used.
Return:
The matched line.
"""
# FIXME make this a context manager which inserts a marker in
@ -191,12 +199,17 @@ class Process(QObject):
matches = []
for key, expected in kwargs.items():
if expected is None:
continue
value = getattr(line, key)
if isinstance(expected, regex_type):
matches.append(expected.match(value))
else:
elif isinstance(value, (bytes, str)):
matches.append(fnmatch.fnmatchcase(value, expected))
else:
matches.append(value == expected)
if all(matches):
return
return line