# HG changeset patch # User Thinker K.F. Li # Date 1308076434 -28800 # Node ID fe1ebf0c3d40921d3c3fddc70e55db3ea7f60f28 # Parent e625ebf17441dccbb71850fd493a4a37cfbb77e4 test get_parent() for structpath.py diff -r e625ebf17441 -r fe1ebf0c3d40 paraspace/structpath.py --- 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 diff -r e625ebf17441 -r fe1ebf0c3d40 paraspace/tests/structpath_test.py --- 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