changeset 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 e625ebf17441
children 2f9e7f03dbf7
files paraspace/structpath.py paraspace/tests/structpath_test.py
diffstat 2 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/structpath.py	Wed Jun 15 02:33:30 2011 +0800
+++ b/paraspace/structpath.py	Wed Jun 15 02:33:54 2011 +0800
@@ -66,7 +66,10 @@
         class_name = _class_name(part)
         new_objs = ctx.class_instances[class_name]
     else:
-        new_objs = [_obj_attr(obj, part)]
+        try:
+            new_objs = [_obj_attr(obj, part)]
+        except AttributeError:
+            return []
         pass
     return new_objs
 
--- a/paraspace/tests/structpath_test.py	Wed Jun 15 02:33:30 2011 +0800
+++ b/paraspace/tests/structpath_test.py	Wed Jun 15 02:33:54 2011 +0800
@@ -13,13 +13,24 @@
     pass
 
 
+class parent_context(structpath.context):
+    def get_parent(self, obj):
+        return obj.parent
+    pass
+
+
 def _build_car():
-    ctx = structpath.context(None)
+    ctx = parent_context(None)
     ctx.root = car()
     root = ctx.root
 
     root.wheels = [wheel(), wheel(), wheel(), wheel()]
+    for w in root.wheels:
+        w.parent = root
+        pass
     root.handle = handle()
+    root.handle.parent = root
+    root.parent = root
 
     ctx.all_classes = {'car': car, 'wheel': wheel, 'handle': handle}
     ctx.class_instances = {
@@ -69,3 +80,27 @@
     assert root.wheels[2] in wheels
     assert root.wheels[3] in wheels
     pass
+
+
+def structpath_parent_test():
+    car_ctx = _build_car()
+    root = car_ctx.root
+
+    wheels = structpath.find_objs_path(car_ctx, '/handle/../wheels/1')
+    assert len(wheels) == 1
+    a_wheel = wheels[0]
+    assert a_wheel is root.wheels[1]
+    
+    wheels = structpath.find_objs_path(car_ctx, '/handle/../../../wheels/1')
+    assert len(wheels) == 1
+    a_wheel = wheels[0]
+    assert a_wheel is root.wheels[1]
+    
+    wheels = structpath.find_objs_path(car_ctx, '.handle/../../../wheels/1')
+    assert len(wheels) == 1
+    a_wheel = wheels[0]
+    assert a_wheel is root.wheels[1]
+    
+    wheels = structpath.find_objs_path(car_ctx, '.handle/wheels/1')
+    assert len(wheels) == 0
+    pass