-
-
Save shonenada/5637204 to your computer and use it in GitHub Desktop.
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
class Enum(object): | |
def __init__(self, *args, **kwargs): | |
raise NotImplemented | |
class Person(object): | |
gendor = Enum("gendor", fields=("male", "female", "unknown")) | |
def __init__(self): | |
self.gendor = "unknown" | |
def test_person_set(): | |
person = Person() | |
assert person.gendor == "unknown" | |
assert vars(person)['gendor'] == 2 | |
person.gendor = "male" | |
assert vars(person)['gendor'] == 0 | |
person.gendor = "female" | |
assert vars(person)['gendor'] == 1 | |
try: | |
person.gendor = "yh1409" | |
except AssertionError: | |
pass | |
else: | |
assert False, "AssertionError should be raised." | |
def test_person_get(): | |
person = Person() | |
vars(person)['gendor'] = 0 | |
assert person.gendor == "male" | |
vars(person)['gendor'] = 1 | |
assert person.gendor == "female" | |
vars(person)['gendor'] = 10 | |
try: | |
person.gendor | |
except AssertionError: | |
pass | |
else: | |
assert False, "AssertionError should be raised." | |
if __name__ == "__main__": | |
test_person_set() | |
test_person_get() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment