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