| 1 | |
| 2 | """ test transparent proxy features |
| 3 | """ |
| 4 | |
| 5 | import py |
| 6 | |
| 7 | class AppProxyBasic(object): |
| 8 | spaceconfig = {"objspace.std.withtproxy": True} |
| 9 | |
| 10 | def setup_method(self, meth): |
| 11 | self.w_Controller = self.space.appexec([], """(): |
| 12 | class Controller(object): |
| 13 | def __init__(self, obj): |
| 14 | self.obj = obj |
| 15 | |
| 16 | def perform(self, name, *args, **kwargs): |
| 17 | return getattr(self.obj, name)(*args, **kwargs) |
| 18 | return Controller |
| 19 | """) |
| 20 | self.w_proxy = self.space.appexec([], """(): |
| 21 | from __pypy__ import tproxy |
| 22 | return tproxy |
| 23 | """) |
| 24 | |
| 25 | class AppTestListProxy(AppProxyBasic): |
| 26 | def setup_class(cls): |
| 27 | py.test.skip("removed support for lists") |
| 28 | |
| 29 | def test_proxy(self): |
| 30 | lst = self.proxy(list, lambda : None) |
| 31 | assert type(lst) is list |
| 32 | |
| 33 | def test_proxy_repr(self): |
| 34 | def controller(name, *args): |
| 35 | lst = [1,2,3] |
| 36 | if name == '__repr__': |
| 37 | return repr(lst) |
| 38 | |
| 39 | lst = self.proxy(list, controller) |
| 40 | assert repr(lst) == repr([1,2,3]) |
| 41 | |
| 42 | def test_proxy_append(self): |
| 43 | c = self.Controller([]) |
| 44 | lst = self.proxy(list, c.perform) |
| 45 | lst.append(1) |
| 46 | lst.append(2) |
| 47 | assert repr(lst) == repr([1,2]) |
| 48 | |
| 49 | def test_gt_lt_list(self): |
| 50 | c = self.Controller([]) |
| 51 | lst = self.proxy(list, c.perform) |
| 52 | lst.append(1) |
| 53 | lst.append(2) |
| 54 | assert lst < [1,2,3] |
| 55 | assert [1,2,3] > lst |
| 56 | assert lst == [1,2] |
| 57 | assert [1,2] == lst |
| 58 | assert [2,3] >= list(iter(lst)) |
| 59 | assert lst < [2,3] |
| 60 | assert [2,3] >= lst |
| 61 | assert lst <= [1,2] |
| 62 | |
| 63 | def test_add_list(self): |
| 64 | c = self.Controller([]) |
| 65 | lst = self.proxy(list, c.perform) |
| 66 | lst.append(1) |
| 67 | assert lst + lst == [1,1] |
| 68 | assert lst + [1] == [1,1] |
| 69 | assert [1] + lst == [1,1] |
| 70 | |
| 71 | def test_list_getitem(self): |
| 72 | c = self.Controller([1,2,3]) |
| 73 | lst = self.proxy(list, c.perform) |
| 74 | assert lst[2] == 3 |
| 75 | lst[1] = 0 |
| 76 | assert lst[0] + lst[1] == 1 |
| 77 | |
| 78 | def test_list_setitem(self): |
| 79 | c = self.Controller([1,2,3]) |
| 80 | lst = self.proxy(list, c.perform) |
| 81 | try: |
| 82 | lst[3] = "x" |
| 83 | except IndexError: |
| 84 | pass |
| 85 | else: |
| 86 | fail("Accessing outside a list didn't raise") |
| 87 | |
| 88 | def test_list_inplace_add(self): |
| 89 | c = self.Controller([1,2,3]) |
| 90 | lst = self.proxy(list, c.perform) |
| 91 | lst += [1,2,3] |
| 92 | assert len(lst) == 6 |
| 93 | |
| 94 | def test_list_reverse_add(self): |
| 95 | c = self.Controller([1,2,3]) |
| 96 | lst = self.proxy(list, c.perform) |
| 97 | l = [1] + lst |
| 98 | assert l == [1,1,2,3] |
| 99 | |
| 100 | class AppTestDictProxy(AppProxyBasic): |
| 101 | def setup_class(cls): |
| 102 | py.test.skip("removed support for dicts") |
| 103 | |
| 104 | def test_dict(self): |
| 105 | c = self.Controller({"xx":1}) |
| 106 | d = self.proxy(dict, c.perform) |
| 107 | assert d['xx'] == 1 |
| 108 | assert 'yy' not in d |
| 109 | d2 = {'yy':3} |
| 110 | d.update(d2, x=4) |
| 111 | assert sorted(d.keys()) == ['x', 'xx', 'yy'] |
| 112 | assert sorted(d.values()) == [1, 3, 4] |
| 113 | |
| 114 | def test_dict_pop(self): |
| 115 | c = self.Controller({'x':1}) |
| 116 | d = self.proxy(dict, c.perform) |
| 117 | assert d.pop('x') == 1 |
| 118 | assert d.pop('x', None) is None |
| 119 | |
| 120 | def test_dict_iter(self): |
| 121 | c = self.Controller({'a':1, 'b':2, 'c':3}) |
| 122 | d = self.proxy(dict, c.perform) |
| 123 | d['z'] = 4 |
| 124 | assert sorted(list(d.iterkeys())) == ['a', 'b', 'c', 'z'] |