]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | # Copyright Joel de Guzman 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) | |
7c673cae FG |
4 | ''' |
5 | ||
6 | ##################################################################### | |
7 | # Check an object that we will use as container element | |
8 | ##################################################################### | |
9 | ||
10 | >>> from map_indexing_suite_ext import * | |
11 | >>> assert "map_indexing_suite_IntMap_entry" in dir() | |
12 | >>> assert "map_indexing_suite_TestMap_entry" in dir() | |
13 | >>> assert "map_indexing_suite_XMap_entry" in dir() | |
14 | >>> assert "map_indexing_suite_AMap_entry" in dir() | |
15 | >>> x = X('hi') | |
16 | >>> x | |
17 | hi | |
18 | >>> x.reset() # a member function that modifies X | |
19 | >>> x | |
20 | reset | |
21 | >>> x.foo() # another member function that modifies X | |
22 | >>> x | |
23 | foo | |
24 | ||
25 | # test that a string is implicitly convertible | |
26 | # to an X | |
20effc67 | 27 | >>> x_value('bochi bochi') |
7c673cae FG |
28 | 'gotya bochi bochi' |
29 | ||
30 | ##################################################################### | |
31 | # Iteration | |
32 | ##################################################################### | |
33 | >>> def print_xmap(xmap): | |
34 | ... s = '[ ' | |
20effc67 | 35 | ... for x in xmap: |
7c673cae | 36 | ... s += repr(x) |
20effc67 | 37 | ... s += ' ' |
7c673cae FG |
38 | ... s += ']' |
39 | ... print(s) | |
40 | ||
41 | ##################################################################### | |
42 | # Setting (adding entries) | |
43 | ##################################################################### | |
44 | >>> xm = XMap() | |
45 | >>> xm['joel'] = 'apple' | |
46 | >>> xm['tenji'] = 'orange' | |
47 | >>> xm['mariel'] = 'grape' | |
48 | >>> xm['tutit'] = 'banana' | |
49 | >>> xm['kim'] = 'kiwi' | |
50 | ||
51 | >>> print_xmap(xm) | |
52 | [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ] | |
53 | ||
54 | ##################################################################### | |
55 | # Changing an entry | |
56 | ##################################################################### | |
57 | >>> xm['joel'] = 'pineapple' | |
58 | >>> print_xmap(xm) | |
59 | [ (joel, pineapple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ] | |
60 | ||
61 | ##################################################################### | |
62 | # Deleting an entry | |
63 | ##################################################################### | |
64 | >>> del xm['joel'] | |
65 | >>> print_xmap(xm) | |
66 | [ (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ] | |
67 | ||
68 | ##################################################################### | |
69 | # adding an entry | |
70 | ##################################################################### | |
71 | >>> xm['joel'] = 'apple' | |
72 | >>> print_xmap(xm) | |
73 | [ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ] | |
74 | ||
75 | ##################################################################### | |
76 | # Indexing | |
77 | ##################################################################### | |
78 | >>> len(xm) | |
79 | 5 | |
80 | >>> xm['joel'] | |
81 | apple | |
82 | >>> xm['tenji'] | |
83 | orange | |
84 | >>> xm['mariel'] | |
85 | grape | |
86 | >>> xm['tutit'] | |
87 | banana | |
88 | >>> xm['kim'] | |
89 | kiwi | |
90 | ||
91 | ##################################################################### | |
92 | # Calling a mutating function of a container element | |
93 | ##################################################################### | |
94 | >>> xm['joel'].reset() | |
95 | >>> xm['joel'] | |
96 | reset | |
97 | ||
98 | ##################################################################### | |
99 | # Copying a container element | |
100 | ##################################################################### | |
101 | >>> x = X(xm['mariel']) | |
102 | >>> x | |
103 | grape | |
104 | >>> x.foo() | |
105 | >>> x | |
106 | foo | |
107 | >>> xm['mariel'] # should not be changed to 'foo' | |
108 | grape | |
109 | ||
110 | ##################################################################### | |
111 | # Referencing a container element | |
112 | ##################################################################### | |
113 | >>> x = xm['mariel'] | |
114 | >>> x | |
115 | grape | |
116 | >>> x.foo() | |
117 | >>> x | |
118 | foo | |
119 | >>> xm['mariel'] # should be changed to 'foo' | |
120 | foo | |
121 | ||
122 | >>> xm['mariel'] = 'grape' # take it back | |
123 | >>> xm['joel'] = 'apple' # take it back | |
124 | ||
125 | ##################################################################### | |
126 | # Contains | |
127 | ##################################################################### | |
128 | >>> assert 'joel' in xm | |
129 | >>> assert 'mariel' in xm | |
130 | >>> assert 'tenji' in xm | |
131 | >>> assert 'tutit' in xm | |
132 | >>> assert 'kim' in xm | |
133 | >>> assert not 'X' in xm | |
134 | >>> assert not 12345 in xm | |
135 | ||
136 | ##################################################################### | |
20effc67 | 137 | # Some references to the container elements |
7c673cae FG |
138 | ##################################################################### |
139 | ||
140 | >>> z0 = xm['joel'] | |
141 | >>> z1 = xm['mariel'] | |
142 | >>> z2 = xm['tenji'] | |
143 | >>> z3 = xm['tutit'] | |
144 | >>> z4 = xm['kim'] | |
145 | ||
146 | >>> z0 # proxy | |
147 | apple | |
148 | >>> z1 # proxy | |
149 | grape | |
150 | >>> z2 # proxy | |
151 | orange | |
152 | >>> z3 # proxy | |
153 | banana | |
154 | >>> z4 # proxy | |
155 | kiwi | |
156 | ||
157 | ##################################################################### | |
20effc67 | 158 | # Delete some container element |
7c673cae FG |
159 | ##################################################################### |
160 | ||
161 | >>> del xm['tenji'] | |
162 | >>> print_xmap(xm) | |
163 | [ (joel, apple) (kim, kiwi) (mariel, grape) (tutit, banana) ] | |
164 | ||
165 | >>> del xm['tutit'] | |
166 | >>> print_xmap(xm) | |
167 | [ (joel, apple) (kim, kiwi) (mariel, grape) ] | |
168 | ||
169 | ##################################################################### | |
20effc67 | 170 | # Show that the references are still valid |
7c673cae FG |
171 | ##################################################################### |
172 | >>> z0 # proxy | |
173 | apple | |
174 | >>> z1 # proxy | |
175 | grape | |
176 | >>> z2 # proxy detached | |
177 | orange | |
178 | >>> z3 # proxy detached | |
179 | banana | |
180 | >>> z4 # proxy | |
181 | kiwi | |
182 | ||
183 | ##################################################################### | |
184 | # Show that iteration allows mutable access to the elements | |
185 | ##################################################################### | |
186 | >>> for x in xm: | |
187 | ... x.data().reset() | |
188 | >>> print_xmap(xm) | |
189 | [ (joel, reset) (kim, reset) (mariel, reset) ] | |
190 | ||
191 | ##################################################################### | |
192 | # Some more... | |
193 | ##################################################################### | |
194 | ||
195 | >>> tm = TestMap() | |
196 | >>> tm["joel"] = X("aaa") | |
197 | >>> tm["kimpo"] = X("bbb") | |
198 | >>> print_xmap(tm) | |
199 | [ (joel, aaa) (kimpo, bbb) ] | |
200 | >>> for el in tm: #doctest: +NORMALIZE_WHITESPACE | |
20effc67 | 201 | ... print(el.key(), end=' ') |
7c673cae FG |
202 | ... dom = el.data() |
203 | joel kimpo | |
204 | ||
205 | ##################################################################### | |
206 | # Test custom converter... | |
207 | ##################################################################### | |
208 | ||
209 | >>> am = AMap() | |
210 | >>> am[3] = 4 | |
211 | >>> am[3] | |
212 | 4 | |
213 | >>> for i in am: | |
214 | ... i.data() | |
215 | 4 | |
216 | ||
217 | ##################################################################### | |
20effc67 | 218 | # END.... |
7c673cae FG |
219 | ##################################################################### |
220 | ||
221 | ''' | |
222 | ||
20effc67 | 223 | from __future__ import print_function |
7c673cae FG |
224 | |
225 | def run(args = None): | |
226 | import sys | |
227 | import doctest | |
228 | ||
229 | if args is not None: | |
230 | sys.argxm = args | |
231 | return doctest.testmod(sys.modules.get(__name__)) | |
232 | ||
233 | if __name__ == '__main__': | |
234 | print('running...') | |
235 | import sys | |
236 | status = run()[0] | |
237 | if (status == 0): print("Done.") | |
238 | sys.exit(status) |