From b79ccb5e795ccc1a8675027cd96ad66e918c0ffc Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 1 Nov 2015 22:10:48 +0100 Subject: [PATCH] bdd: Make message matching work. --- .../integration/features/backforward.feature | 18 +++++++-------- tests/integration/features/test_features.py | 8 ++++--- tests/integration/quteprocess.py | 14 +++++++++-- tests/integration/testprocess.py | 23 +++++++++++++++---- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tests/integration/features/backforward.feature b/tests/integration/features/backforward.feature index 75a2ac764..15843a777 100644 --- a/tests/integration/features/backforward.feature +++ b/tests/integration/features/backforward.feature @@ -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. diff --git a/tests/integration/features/test_features.py b/tests/integration/features/test_features.py index 2ea25316c..7614aa1c9 100644 --- a/tests/integration/features/test_features.py +++ b/tests/integration/features/test_features.py @@ -18,6 +18,7 @@ # along with qutebrowser. If not, see . 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) diff --git a/tests/integration/quteprocess.py b/tests/integration/quteprocess.py index a4373f17d..18b5276f9 100644 --- a/tests/integration/quteprocess.py +++ b/tests/integration/quteprocess.py @@ -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 diff --git a/tests/integration/testprocess.py b/tests/integration/testprocess.py index 44a5d9a07..a140a8ec7 100644 --- a/tests/integration/testprocess.py +++ b/tests/integration/testprocess.py @@ -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