-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the behavior of the contract checking new_setattr function (#…
…929)
- Loading branch information
Showing
3 changed files
with
59 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from python_ta.contracts import check_contracts | ||
|
||
|
||
def test_class_attr_value_restores_to_original_if_violates_rep_inv() -> None: | ||
"""Test to check whether the class attribute value is restored to the original value if a representation invariant | ||
is violated.""" | ||
|
||
@check_contracts | ||
class Person: | ||
""" | ||
Representation Invariants: | ||
- self.age >= 0 | ||
""" | ||
|
||
age: int = 0 | ||
|
||
my_person = Person() | ||
|
||
try: | ||
my_person.age = -1 | ||
except AssertionError: | ||
assert my_person.age == 0 | ||
|
||
|
||
def test_class_attr_value_does_not_exist_if_violates_rep_inv() -> None: | ||
"""Test to check whether the class attribute value does not exist if a representation invariant | ||
is violated.""" | ||
|
||
@check_contracts | ||
class Person: | ||
""" | ||
Representation Invariants: | ||
- self.age >= 0 | ||
""" | ||
|
||
age: int | ||
|
||
my_person = Person() | ||
|
||
try: | ||
my_person.age = -1 | ||
except AssertionError: | ||
assert not hasattr(my_person, "age") |