test_enum.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. '''
  5. >>> from enum_ext import *
  6. >>> identity(color.red) # in case of duplicated enums it always take the last enum
  7. enum_ext.color.blood
  8. >>> identity(color.green)
  9. enum_ext.color.green
  10. >>> identity(color.blue)
  11. enum_ext.color.blue
  12. >>> identity(color(1)) # in case of duplicated enums it always take the last enum
  13. enum_ext.color.blood
  14. >>> identity(color(2))
  15. enum_ext.color.green
  16. >>> identity(color(3))
  17. enum_ext.color(3)
  18. >>> identity(color(4))
  19. enum_ext.color.blue
  20. --- check export to scope ---
  21. >>> identity(red)
  22. enum_ext.color.blood
  23. >>> identity(green)
  24. enum_ext.color.green
  25. >>> identity(blue)
  26. enum_ext.color.blue
  27. >>> try: identity(1)
  28. ... except TypeError: pass
  29. ... else: print('expected a TypeError')
  30. >>> c = colorized()
  31. >>> c.x
  32. enum_ext.color.blood
  33. >>> c.x = green
  34. >>> c.x
  35. enum_ext.color.green
  36. >>> red == blood
  37. True
  38. >>> red == green
  39. False
  40. >>> hash(red) == hash(blood)
  41. True
  42. >>> hash(red) == hash(green)
  43. False
  44. '''
  45. # pickling of enums only works with Python 2.3 or higher
  46. exercise_pickling = '''
  47. >>> import pickle
  48. >>> p = pickle.dumps(color.green, pickle.HIGHEST_PROTOCOL)
  49. >>> l = pickle.loads(p)
  50. >>> identity(l)
  51. enum_ext.color.green
  52. '''
  53. def run(args = None):
  54. import sys
  55. import doctest
  56. import pickle
  57. if args is not None:
  58. sys.argv = args
  59. self = sys.modules.get(__name__)
  60. if (hasattr(pickle, "HIGHEST_PROTOCOL")):
  61. self.__doc__ += exercise_pickling
  62. return doctest.testmod(self)
  63. if __name__ == '__main__':
  64. print("running...")
  65. import sys
  66. status = run()[0]
  67. if (status == 0): print("Done.")
  68. sys.exit(status)