Skip to content

Commit

Permalink
Add case insensitive matching for "# act" search (#169)
Browse files Browse the repository at this point in the history
* Add case insensitive matching for "# act" search

* Update CHANGELOG

* Add failing test for "# Act" marker
  • Loading branch information
jamescooke committed Dec 28, 2020
1 parent be5e16b commit 0a78c00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Unreleased_
See also `latest documentation
<https://flake8-aaa.readthedocs.io/en/latest/#__unreleased_marker__>`_.

Fixed
.....

* Bug which prevented act block hints containing capital letters (like ``#
Act``) from being found `#167
<https://github.com/jamescooke/flake8-aaa/issues/167>`_

0.11.0_ - 2020/07/26
--------------------

Expand Down
13 changes: 13 additions & 0 deletions examples/good/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ def test_ignore_typing() -> None:
assert shopping == ['cabbages', 'bananas', 'apples']


# Example from docs:
# mark the end of the line considered the Act block with ``# act`` (case insensitive)
def test_Act() -> None:
"""
Reverse shopping list operates in place (test of "# Act" marking)
"""
shopping = ['apples', 'bananas', 'cabbages']

shopping.reverse() # Act

assert shopping == ['cabbages', 'bananas', 'apples']


# Comments are OK in Arrange and Assert.


Expand Down
4 changes: 3 additions & 1 deletion src/flake8_aaa/act_node.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import ast
import re
from typing import List, Type, TypeVar

from .helpers import get_first_token, node_is_pytest_raises, node_is_result_assignment, node_is_unittest_raises
from .types import ActNodeType

AN = TypeVar('AN', bound='ActNode') # Place holder for ActNode instances
act_pattern = re.compile('# act$', re.IGNORECASE)


class ActNode:
Expand Down Expand Up @@ -51,7 +53,7 @@ def build(cls: Type[AN], node: ast.stmt) -> List[AN]:
return [cls(node, ActNodeType.unittest_raises)]

# Check if line marked with '# act'
if get_first_token(node).line.strip().endswith('# act'):
if act_pattern.search(get_first_token(node).line.strip()):
return [cls(node, ActNodeType.marked_act)]

# Recurse (downwards) if it's a context manager
Expand Down
17 changes: 17 additions & 0 deletions tests/function/test_load_act_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def test_act_marker(function):
assert result.node.first_token.line == ' x = y + 1 # act\n'


@pytest.mark.parametrize('code_str', ['''
def test():
y = 3
x = y + 1 # Act
assert x == 4
'''])
def test_act_marker_case(function):
"""
Act marker is case-insensitive
"""
result = function.load_act_node()

assert isinstance(result, ActNode)
assert result.block_type == ActNodeType.marked_act
assert result.node.first_token.line == ' x = y + 1 # Act\n'


@pytest.mark.parametrize(
'code_str',
[
Expand Down

0 comments on commit 0a78c00

Please sign in to comment.