From 41acbf4148aa7eba4be5f49158bb975cc8d15ef4 Mon Sep 17 00:00:00 2001 From: Leonardo Santagada Date: Sun, 15 Mar 2015 10:51:35 -0300 Subject: [PATCH] better parsing of comments, fix #99 --- CHANGES.rst | 6 ++++++ pytest_bdd/__init__.py | 2 +- pytest_bdd/feature.py | 10 ++++------ tests/feature/comments.feature | 5 +++++ tests/feature/test_scenario.py | 28 ++++++++++++++++++++++++++-- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 8052b33..a9500aa 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,12 @@ Changelog ========= +2.6.2 +----- + +- Parse comments only in the begining of words + + 2.6.1 ----- diff --git a/pytest_bdd/__init__.py b/pytest_bdd/__init__.py index 1d7adf2..ddc9448 100644 --- a/pytest_bdd/__init__.py +++ b/pytest_bdd/__init__.py @@ -1,6 +1,6 @@ """pytest-bdd public API.""" -__version__ = '2.6.1' +__version__ = '2.6.2' try: from pytest_bdd.steps import given, when, then diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index 7c3c6b2..01bf1a7 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -64,9 +64,8 @@ STEP_PREFIXES = [ ("And ", None), # Unknown step type, ] -COMMENT_SYMBOLS = "#" - STEP_PARAM_RE = re.compile("\<(.+?)\>") +COMMENT_RE = re.compile('(^|(?<=\s))#') def get_step_type(line): @@ -88,10 +87,9 @@ def strip_comments(line): :return: Stripped line. """ - try: - line = line[:line.index(COMMENT_SYMBOLS)] - except ValueError: - pass + res = COMMENT_RE.search(line) + if res: + line = line[:res.start()] return line.strip() diff --git a/tests/feature/comments.feature b/tests/feature/comments.feature index 10a1bbd..ce22621 100644 --- a/tests/feature/comments.feature +++ b/tests/feature/comments.feature @@ -1,3 +1,8 @@ Scenario: Comments # Comment Given I have a bar + +Scenario: Strings that are not comments + Given comments should be at the start of words + Then this is not a#comment + And this is not "#acomment" diff --git a/tests/feature/test_scenario.py b/tests/feature/test_scenario.py index a7c4e39..2b7b63b 100644 --- a/tests/feature/test_scenario.py +++ b/tests/feature/test_scenario.py @@ -1,8 +1,14 @@ """Test scenario decorator.""" import pytest +import re -from pytest_bdd import scenario -from pytest_bdd import exceptions +from pytest_bdd import ( + scenario, + given, + then, + parsers, + exceptions, +) def test_scenario_not_found(request): @@ -17,6 +23,16 @@ def test_scenario_not_found(request): feature_path=request.fspath.join('..', 'not_found.feature'))) +@given('comments should be at the start of words') +def comments(): + pass + + +@then(parsers.parse('this is not {acomment}')) +def a_comment(acomment): + assert re.search('a.*comment', acomment) + + def test_scenario_comments(request): """Test comments inside scenario.""" @@ -27,4 +43,12 @@ def test_scenario_comments(request): def test(): pass + @scenario( + 'comments.feature', + 'Strings that are not comments' + ) + def test2(): + pass + test(request) + test2(request)