• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7>>> identity(color.red) # in case of duplicated enums it always take the last enum
8enum_ext.color.blood
9
10>>> identity(color.green)
11enum_ext.color.green
12
13>>> identity(color.blue)
14enum_ext.color.blue
15
16>>> identity(color(1)) # in case of duplicated enums it always take the last enum
17enum_ext.color.blood
18
19>>> identity(color(2))
20enum_ext.color.green
21
22>>> identity(color(3))
23enum_ext.color(3)
24
25>>> identity(color(4))
26enum_ext.color.blue
27
28  --- check export to scope ---
29
30>>> identity(red)
31enum_ext.color.blood
32
33>>> identity(green)
34enum_ext.color.green
35
36>>> identity(blue)
37enum_ext.color.blue
38
39>>> try: identity(1)
40... except TypeError: pass
41... else: print('expected a TypeError')
42
43>>> c = colorized()
44>>> c.x
45enum_ext.color.blood
46>>> c.x = green
47>>> c.x
48enum_ext.color.green
49>>> red == blood
50True
51>>> red == green
52False
53>>> hash(red) == hash(blood)
54True
55>>> hash(red) == hash(green)
56False
57'''
58
59# pickling of enums only works with Python 2.3 or higher
60exercise_pickling = '''
61>>> import pickle
62>>> p = pickle.dumps(color.green, pickle.HIGHEST_PROTOCOL)
63>>> l = pickle.loads(p)
64>>> identity(l)
65enum_ext.color.green
66'''
67
68def run(args = None):
69    import sys
70    import doctest
71    import pickle
72
73    if args is not None:
74        sys.argv = args
75    self = sys.modules.get(__name__)
76    if (hasattr(pickle, "HIGHEST_PROTOCOL")):
77      self.__doc__ += exercise_pickling
78    return doctest.testmod(self)
79
80if __name__ == '__main__':
81    print("running...")
82    import sys
83    status = run()[0]
84    if (status == 0): print("Done.")
85    sys.exit(status)
86