Skip to content

Commit

Permalink
Added a test for generator expression with attribute on element and f…
Browse files Browse the repository at this point in the history
…ixed the contract violation output
  • Loading branch information
radomsak committed Sep 28, 2018
1 parent 391d430 commit d9a4b37
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions icontract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def __init__(self,
got = list(inspect.signature(repr_args).parameters.keys())

if got != self.condition_args:
raise ValueError("Unexpected argument(s) of repr_args. Expected {}, got {}".format(
self.condition_args, got))
raise ValueError(
"Unexpected argument(s) of repr_args. Expected {}, got {}".format(self.condition_args, got))

self._a_repr = a_repr if a_repr is not None else aRepr

Expand Down
4 changes: 2 additions & 2 deletions icontract/ast_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def visit(self, root: ast.AST) -> None:
else:
if isinstance(field[0], ast.AST):
for i, item in enumerate(field):
self._edges.append("node_{} -> node_{} [label=<{}[{}]>];".format(
id(node), id(item), name, i))
self._edges.append(
"node_{} -> node_{} [label=<{}[{}]>];".format(id(node), id(item), name, i))
stack.extend(field)

else:
Expand Down
4 changes: 2 additions & 2 deletions icontract/recompute.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def visit_NameConstant(self, node: ast.NameConstant) -> Any:
def visit_Name(self, node: ast.Name) -> Any:
"""Load the variable by looking it up in the variable look-up and in the built-ins."""
if not isinstance(node.ctx, ast.Load):
raise NotImplementedError("Can only compute a value of Load on a name {}, but got context: {}".format(
node.id, node.ctx))
raise NotImplementedError(
"Can only compute a value of Load on a name {}, but got context: {}".format(node.id, node.ctx))

result = None # type: Optional[Any]

Expand Down
14 changes: 7 additions & 7 deletions icontract/represent.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ def visit_Name(self, node: ast.Name) -> None:

def visit_Attribute(self, node: ast.Attribute) -> None:
"""Represent the attribute by dumping its source code."""
value = self._recomputed_values[node]
if node in self._recomputed_values:
value = self._recomputed_values[node]

if _representable(value=value):
text = self._atok.get_text(node)
self.reprs[text] = value
if _representable(value=value):
text = self._atok.get_text(node)
self.reprs[text] = value

self.generic_visit(node=node)

Expand Down Expand Up @@ -216,9 +217,8 @@ def inspect_lambda_condition(condition: Callable[..., bool]) -> Optional[LambdaI

assert lambda_node is not None, "Expected to find a keyword AST node with 'condition' arg, but found none"
else:
raise AssertionError(
"Expected a call AST node of a decorator to have either args or keywords, but got: {}".format(
ast.dump(call_node)))
raise AssertionError("Expected a call AST node of a decorator to have either args or keywords, but got: {}".
format(ast.dump(call_node)))

return LambdaInspection(atok=atok, node=lambda_node)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_icontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import reprlib
import time
import unittest
from typing import Optional, List, Callable, Any, Type # pylint: disable=unused-import
from typing import Optional, List, Callable, Any, Tuple, Type # pylint: disable=unused-import

import icontract

Expand Down
21 changes: 18 additions & 3 deletions tests/test_represent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
# pylint: disable=missing-docstring,invalid-name,too-many-public-methods,no-self-use

import pathlib
import unittest
from typing import Optional # pylint: disable=unused-import
from typing import Optional, List, Tuple # pylint: disable=unused-import

import icontract.represent

Expand Down Expand Up @@ -350,7 +350,22 @@ def func(x: int) -> int:

self.assertIsNotNone(not_implemented_err)

def test_generator_expression(self):
def test_generator_expression_with_attr_on_element(self):
@icontract.post(lambda result: all(single_res[1].is_absolute() for single_res in result))
def some_func() -> List[Tuple[pathlib.Path, pathlib.Path]]:
return [(pathlib.Path("/home/file1"), pathlib.Path("home/file2"))]

icontract_violation_error = None # type: Optional[icontract.ViolationError]
try:
some_func()
except icontract.ViolationError as err:
icontract_violation_error = err

self.assertIsNotNone(icontract_violation_error)
self.assertEqual('all(single_res[1].is_absolute() for single_res in result): all(single_res[1].is_absolute() '
'for single_res in result) was False', str(icontract_violation_error))

def test_generator_expression_multiple_for(self):
lst = [1, 2, 3]
another_lst = [4, 5, 6]

Expand Down

0 comments on commit d9a4b37

Please sign in to comment.