view paraspace/tests/structpath_test.py @ 31:aed662c820d8

xpath like query structpath.py
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 15 Jun 2011 01:09:35 +0800
parents
children fe1ebf0c3d40
line wrap: on
line source

from paraspace import structpath


class car(object):
    pass


class wheel(object):
    pass


class handle(object):
    pass


def _build_car():
    ctx = structpath.context(None)
    ctx.root = car()
    root = ctx.root

    root.wheels = [wheel(), wheel(), wheel(), wheel()]
    root.handle = handle()

    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