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 c5cc83d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
9 changes: 5 additions & 4 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
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 c5cc83d

Please sign in to comment.