[12] | 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 object_ext import * |
---|
| 6 | |
---|
| 7 | >>> type(ref_to_noncopyable()) |
---|
| 8 | <class 'object_ext.NotCopyable'> |
---|
| 9 | |
---|
| 10 | >>> def print1(x): |
---|
| 11 | ... print x |
---|
| 12 | >>> call_object_3(print1) |
---|
| 13 | 3 |
---|
| 14 | >>> message() |
---|
| 15 | 'hello, world!' |
---|
| 16 | >>> number() |
---|
| 17 | 42 |
---|
| 18 | |
---|
| 19 | >>> test('hi') |
---|
| 20 | 1 |
---|
| 21 | >>> test(None) |
---|
| 22 | 0 |
---|
| 23 | >>> test_not('hi') |
---|
| 24 | 0 |
---|
| 25 | >>> test_not(0) |
---|
| 26 | 1 |
---|
| 27 | |
---|
| 28 | Attributes |
---|
| 29 | |
---|
| 30 | >>> class X: pass |
---|
| 31 | ... |
---|
| 32 | >>> x = X() |
---|
| 33 | |
---|
| 34 | >>> try: obj_getattr(x, 'foo') |
---|
| 35 | ... except AttributeError: pass |
---|
| 36 | ... else: print 'expected an exception' |
---|
| 37 | |
---|
| 38 | >>> obj_setattr(x, 'foo', 1) |
---|
| 39 | >>> x.foo |
---|
| 40 | 1 |
---|
| 41 | >>> obj_getattr(x, 'foo') |
---|
| 42 | 1 |
---|
| 43 | >>> obj_const_getattr(x, 'foo') |
---|
| 44 | 1 |
---|
| 45 | >>> obj_setattr42(x, 'foo') |
---|
| 46 | >>> x.foo |
---|
| 47 | 42 |
---|
| 48 | >>> obj_moveattr(x, 'foo', 'bar') |
---|
| 49 | >>> x.bar |
---|
| 50 | 42 |
---|
| 51 | >>> test_attr(x, 'foo') |
---|
| 52 | 1 |
---|
| 53 | >>> test_not_attr(x, 'foo') |
---|
| 54 | 0 |
---|
| 55 | >>> x.foo = None |
---|
| 56 | >>> test_attr(x, 'foo') |
---|
| 57 | 0 |
---|
| 58 | >>> test_not_attr(x, 'foo') |
---|
| 59 | 1 |
---|
| 60 | |
---|
| 61 | Items |
---|
| 62 | |
---|
| 63 | >>> d = {} |
---|
| 64 | >>> obj_setitem(d, 'foo', 1) |
---|
| 65 | >>> d['foo'] |
---|
| 66 | 1 |
---|
| 67 | >>> obj_getitem(d, 'foo') |
---|
| 68 | 1 |
---|
| 69 | >>> obj_const_getitem(d, 'foo') |
---|
| 70 | 1 |
---|
| 71 | >>> obj_setitem42(d, 'foo') |
---|
| 72 | >>> obj_getitem(d, 'foo') |
---|
| 73 | 42 |
---|
| 74 | >>> d['foo'] |
---|
| 75 | 42 |
---|
| 76 | >>> obj_moveitem(d, 'foo', 'bar') |
---|
| 77 | >>> d['bar'] |
---|
| 78 | 42 |
---|
| 79 | >>> obj_moveitem2(d, 'bar', d, 'baz') |
---|
| 80 | >>> d['baz'] |
---|
| 81 | 42 |
---|
| 82 | >>> test_item(d, 'foo') |
---|
| 83 | 1 |
---|
| 84 | >>> test_not_item(d, 'foo') |
---|
| 85 | 0 |
---|
| 86 | >>> d['foo'] = None |
---|
| 87 | >>> test_item(d, 'foo') |
---|
| 88 | 0 |
---|
| 89 | >>> test_not_item(d, 'foo') |
---|
| 90 | 1 |
---|
| 91 | |
---|
| 92 | Slices |
---|
| 93 | |
---|
| 94 | >>> assert check_string_slice() |
---|
| 95 | |
---|
| 96 | Operators |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | >>> assert check_binary_operators() |
---|
| 100 | |
---|
| 101 | >>> class X: pass |
---|
| 102 | ... |
---|
| 103 | >>> assert check_inplace(range(3), X()) |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | Now make sure that object is actually managing reference counts |
---|
| 107 | |
---|
| 108 | >>> import weakref |
---|
| 109 | >>> class Z: pass |
---|
| 110 | ... |
---|
| 111 | >>> z = Z() |
---|
| 112 | >>> def death(r): print 'death' |
---|
| 113 | ... |
---|
| 114 | >>> r = weakref.ref(z, death) |
---|
| 115 | >>> z.foo = 1 |
---|
| 116 | >>> obj_getattr(z, 'foo') |
---|
| 117 | 1 |
---|
| 118 | >>> del z |
---|
| 119 | death |
---|
| 120 | ''' |
---|
| 121 | |
---|
| 122 | def run(args = None): |
---|
| 123 | import sys |
---|
| 124 | import doctest |
---|
| 125 | |
---|
| 126 | if args is not None: |
---|
| 127 | sys.argv = args |
---|
| 128 | return doctest.testmod(sys.modules.get(__name__)) |
---|
| 129 | |
---|
| 130 | if __name__ == '__main__': |
---|
| 131 | print "running..." |
---|
| 132 | import sys |
---|
| 133 | status = run()[0] |
---|
| 134 | if (status == 0): print "Done." |
---|
| 135 | sys.exit(status) |
---|