class NoRoles(object): pass class NoRolesChild(NoRoles): pass for dog_type in (Dog, ): #Doberman, DobermanPinscher): for dog in (dog_type, dog_type()): assert hasattr(dog, 'eat') assert hasattr(dog, 'wag') assert hasattr(dog, 'talk') assert hasattr(dog, 'sleep') assert does(dog, dog) assert not does(NoRoles, Dog) assert not does(NoRolesChild, Dog) # Roles can not inherit from other roles class BadRole(Animal): pass class ThisShouldCrash(object): pass
# Now create a role which consumes both class rFooBar(Role): def foo_bar(self): return "rFooBar::foo_bar" subroles_not_consumed = 1 try: has_role(rFooBar, rFoo, rBar) except RoleConflictDetected: subroles_not_consumed = 0 finally: assert subroles_not_consumed assert does(rFooBar, rFoo); assert does(rFooBar, rBar); assert not hasattr(rFooBar, 'baz') assert hasattr(rFooBar, 'foo_bar') # Now compose it and get the conflict: class FooBarWithConflict2(object): pass foobarwithconflict2_conflicts = 0 try: has_role(FooBarWithConflict2, rFooBar) except RoleConflictDetected: foobarwithconflict2_conflicts = 1
pass class NoRoles(object): pass class NoRolesChild(NoRoles): pass for dog_type in (Dog, ): #Doberman, DobermanPinscher): for dog in (dog_type, dog_type()): assert hasattr(dog, 'eat') assert hasattr(dog, 'wag') assert hasattr(dog, 'talk') assert hasattr(dog, 'sleep') assert does(dog, dog) assert not does(NoRoles, Dog) assert not does(NoRolesChild, Dog) # Roles can not inherit from other roles class BadRole(Animal): pass class ThisShouldCrash(object): pass role_inheriting_from_role_crashes = 0 try:
# Now create a role which consumes both class rFooBar(Role): def foo_bar(self): return "rFooBar::foo_bar" subroles_not_consumed = 1 try: has_role(rFooBar, rFoo, rBar) except RoleConflictDetected: subroles_not_consumed = 0 finally: assert subroles_not_consumed assert does(rFooBar, rFoo) assert does(rFooBar, rBar) assert not hasattr(rFooBar, 'baz') assert hasattr(rFooBar, 'foo_bar') # Now compose it and get the conflict: class FooBarWithConflict2(object): pass foobarwithconflict2_conflicts = 0 try: has_role(FooBarWithConflict2, rFooBar) except RoleConflictDetected: