Mercurial > paraspace
comparison paraspace/tests/structpath_test.py @ 34:fe1ebf0c3d40
test get_parent() for structpath.py
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 15 Jun 2011 02:33:54 +0800 |
parents | aed662c820d8 |
children | 2f9e7f03dbf7 |
comparison
equal
deleted
inserted
replaced
33:e625ebf17441 | 34:fe1ebf0c3d40 |
---|---|
11 | 11 |
12 class handle(object): | 12 class handle(object): |
13 pass | 13 pass |
14 | 14 |
15 | 15 |
16 class parent_context(structpath.context): | |
17 def get_parent(self, obj): | |
18 return obj.parent | |
19 pass | |
20 | |
21 | |
16 def _build_car(): | 22 def _build_car(): |
17 ctx = structpath.context(None) | 23 ctx = parent_context(None) |
18 ctx.root = car() | 24 ctx.root = car() |
19 root = ctx.root | 25 root = ctx.root |
20 | 26 |
21 root.wheels = [wheel(), wheel(), wheel(), wheel()] | 27 root.wheels = [wheel(), wheel(), wheel(), wheel()] |
28 for w in root.wheels: | |
29 w.parent = root | |
30 pass | |
22 root.handle = handle() | 31 root.handle = handle() |
32 root.handle.parent = root | |
33 root.parent = root | |
23 | 34 |
24 ctx.all_classes = {'car': car, 'wheel': wheel, 'handle': handle} | 35 ctx.all_classes = {'car': car, 'wheel': wheel, 'handle': handle} |
25 ctx.class_instances = { | 36 ctx.class_instances = { |
26 'car': [root], | 37 'car': [root], |
27 'wheel': root.wheels, | 38 'wheel': root.wheels, |
67 assert root.wheels[0] in wheels | 78 assert root.wheels[0] in wheels |
68 assert root.wheels[1] in wheels | 79 assert root.wheels[1] in wheels |
69 assert root.wheels[2] in wheels | 80 assert root.wheels[2] in wheels |
70 assert root.wheels[3] in wheels | 81 assert root.wheels[3] in wheels |
71 pass | 82 pass |
83 | |
84 | |
85 def structpath_parent_test(): | |
86 car_ctx = _build_car() | |
87 root = car_ctx.root | |
88 | |
89 wheels = structpath.find_objs_path(car_ctx, '/handle/../wheels/1') | |
90 assert len(wheels) == 1 | |
91 a_wheel = wheels[0] | |
92 assert a_wheel is root.wheels[1] | |
93 | |
94 wheels = structpath.find_objs_path(car_ctx, '/handle/../../../wheels/1') | |
95 assert len(wheels) == 1 | |
96 a_wheel = wheels[0] | |
97 assert a_wheel is root.wheels[1] | |
98 | |
99 wheels = structpath.find_objs_path(car_ctx, '.handle/../../../wheels/1') | |
100 assert len(wheels) == 1 | |
101 a_wheel = wheels[0] | |
102 assert a_wheel is root.wheels[1] | |
103 | |
104 wheels = structpath.find_objs_path(car_ctx, '.handle/wheels/1') | |
105 assert len(wheels) == 0 | |
106 pass |