Skip to content

Commit

Permalink
Merge pull request #24 from jamescooke/enums
Browse files Browse the repository at this point in the history
Enums
  • Loading branch information
jamescooke committed Jun 16, 2018
2 parents e421479 + ac23bf8 commit 6618f83
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 19 deletions.
15 changes: 6 additions & 9 deletions flake8_aaa/act_block.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
from .exceptions import NotActionBlock
from .helpers import node_is_pytest_raises, node_is_result_assignment
from .types import ActBlockType


class ActBlock:
"""
Attributes:
node
block_type (str)
block_type (ActBlockType)
"""

MARKED_ACT = 'marked_act'
PYTEST_RAISES = 'pytest_raises'
RESULT_ASSIGNMENT = 'result_assignment'

def __init__(self, node, block_type):
"""
Args:
node
block_type (str)
block_type (ActBlockType)
"""
self.node = node
self.block_type = block_type
Expand All @@ -35,12 +32,12 @@ def build(obj, node):
NotActionBlock: When ``node`` does not look like an Act block.
"""
if node_is_result_assignment(node):
return obj(node, obj.RESULT_ASSIGNMENT)
return obj(node, ActBlockType.result_assignment)
elif node_is_pytest_raises(node):
return obj(node, obj.PYTEST_RAISES)
return obj(node, ActBlockType.pytest_raises)

# Check if line marked with '# act'
if node.first_token.line.strip().endswith('# act'):
return obj(node, obj.MARKED_ACT)
return obj(node, ActBlockType.marked_act)

raise NotActionBlock()
3 changes: 2 additions & 1 deletion flake8_aaa/function.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .act_block import ActBlock
from .exceptions import NotActionBlock, ValidationError
from .helpers import function_is_noop
from .types import ActBlockType


class Function:
Expand Down Expand Up @@ -52,7 +53,7 @@ def load_act_block(self):
# are `pytest.raises` blocks that are not the first.
if len(act_blocks) > 1:
act_blocks = [act_blocks[0]
] + list(filter(lambda ab: ab.block_type != ActBlock.PYTEST_RAISES, act_blocks[1:]))
] + list(filter(lambda ab: ab.block_type != ActBlockType.pytest_raises, act_blocks[1:]))

if len(act_blocks) < 1:
raise ValidationError(self.node.lineno, self.node.col_offset, 'AAA01 no Act block found in test')
Expand Down
3 changes: 3 additions & 0 deletions flake8_aaa/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from enum import Enum

ActBlockType = Enum('ActBlockType', 'marked_act pytest_raises result_assignment')
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def readme():
py_modules=['flake8_aaa'],
install_requires=[
'asttokens >= 1.1.10',
# Skip enum34 because it's not needed for py3 and because flake8 will
# install it for py2.
'flake8 >= 3',
],
entry_points={
Expand Down
9 changes: 5 additions & 4 deletions tests/act_block/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flake8_aaa.act_block import ActBlock
from flake8_aaa.exceptions import NotActionBlock
from flake8_aaa.types import ActBlockType

# TODO act blocks need testing with 'result =' indented
# TODO act blocks need testing with indentation in general
Expand All @@ -21,14 +22,14 @@ def test_raises_block(first_node_with_tokens):

assert isinstance(result, ActBlock)
assert result.node == first_node_with_tokens.body[0]
assert result.block_type == ActBlock.PYTEST_RAISES
assert result.block_type == ActBlockType.pytest_raises


@pytest.mark.parametrize(
'code_str, expected_type', [
('result = do_thing()', ActBlock.RESULT_ASSIGNMENT),
('with pytest.raises(Exception):\n do_thing()', ActBlock.PYTEST_RAISES),
('data[new_key] = value # act', ActBlock.MARKED_ACT),
('result = do_thing()', ActBlockType.result_assignment),
('with pytest.raises(Exception):\n do_thing()', ActBlockType.pytest_raises),
('data[new_key] = value # act', ActBlockType.marked_act),
]
)
def test(expected_type, first_node_with_tokens):
Expand Down
5 changes: 3 additions & 2 deletions tests/act_block/test_init.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import ast

from flake8_aaa.act_block import ActBlock
from flake8_aaa.types import ActBlockType


def test():
node = ast.parse('result = do_thing()').body[0]

result = ActBlock(node, 'result_assignment')
result = ActBlock(node, ActBlockType.result_assignment)

assert result.node == node
assert result.block_type == ActBlock.RESULT_ASSIGNMENT
assert result.block_type == ActBlockType.result_assignment
7 changes: 4 additions & 3 deletions tests/function/test_load_act_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flake8_aaa.act_block import ActBlock
from flake8_aaa.exceptions import ValidationError
from flake8_aaa.types import ActBlockType


@pytest.mark.parametrize('code_str', ['''
Expand All @@ -14,7 +15,7 @@ def test_assignment(function):
result = function.load_act_block()

assert isinstance(result, ActBlock)
assert result.block_type == ActBlock.RESULT_ASSIGNMENT
assert result.block_type == ActBlockType.result_assignment
assert result.node.first_token.line == ' result = 1\n'


Expand All @@ -28,7 +29,7 @@ def test_act_marker(function):
result = function.load_act_block()

assert isinstance(result, ActBlock)
assert result.block_type == ActBlock.MARKED_ACT
assert result.block_type == ActBlockType.marked_act
assert result.node.first_token.line == ' x = y + 1 # act\n'


Expand All @@ -49,7 +50,7 @@ def test_raises_in_assert(function):
result = function.load_act_block()

assert isinstance(result, ActBlock)
assert result.block_type == ActBlock.RESULT_ASSIGNMENT
assert result.block_type == ActBlockType.result_assignment
assert result.node.first_token.line == ' result = existing_user.delete()\n'


Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ deps =
test,lint: -rrequirements/test.txt
commands =
install: flake8 --version
install: flake8 tests
test: pytest
lint: make lint
setenv = IN_TOX = 1
Expand Down

0 comments on commit 6618f83

Please sign in to comment.