Skip to content

Instantly share code, notes, and snippets.

@shonenada
Created May 23, 2013 16:04
Show Gist options
  • Save shonenada/5637204 to your computer and use it in GitHub Desktop.
Save shonenada/5637204 to your computer and use it in GitHub Desktop.
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