view 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
line wrap: on
line source

from paraspace import structpath


class car(object):
    pass


class wheel(object):
    pass


class handle(object):
    pass


class parent_context(structpath.context):
    def get_parent(self, obj):
        return obj.parent
    pass


def _build_car():
    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 = {
        'car': [root],
        'wheel': root.wheels,
        'handle': [root.handle]
        }
    return ctx


def structpath_abs_test():
    car_ctx = _build_car()
    
    a_wheel = structpath.find_objs_path(car_ctx, '/wheels/1')[0]
    assert a_wheel is car_ctx.root.wheels[1]

    a_handle = structpath.find_objs_path(car_ctx, '/handle')[0]
    assert a_handle is car_ctx.root.handle
    
    a_car = structpath.find_objs_path(car_ctx, '/')[0]
    assert a_car is car_ctx.root
    pass


def structpath_class_test():
    car_ctx = _build_car()
    root = car_ctx.root

    wheels = structpath.find_objs_path(car_ctx, '.wheel')
    assert len(wheels) == 4
    assert root.wheels[0] in wheels
    assert root.wheels[1] in wheels
    assert root.wheels[2] in wheels
    assert root.wheels[3] in wheels

    handles = structpath.find_objs_path(car_ctx, '.car/handle')
    assert len(handles) == 1
    a_handle = handles[0]
    assert a_handle is car_ctx.root.handle

    wheelss = structpath.find_objs_path(car_ctx, '.car/wheels')
    assert len(wheelss) == 1
    wheels = wheelss[0]
    assert len(wheels) == 4
    assert root.wheels[0] in wheels
    assert root.wheels[1] in wheels
    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