view paraspace/tests/structpath_test.py @ 96:1769e52bdd9d

Make dexfile_insert_class() pass the testcase
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 25 Jul 2011 16:49:28 +0800
parents ee8aeb299f10
children
line wrap: on
line source

from paraspace import structpath


class car(object):
    pass


class wheel(object):

    def num_next(self):
        return self.num + 1
    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()]
    sn = 1
    for w in root.wheels:
        w.parent = root
        w.num = sn
        sn = sn + 1
        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


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

    wheels = structpath.find_objs_path(car_ctx, '.wheel')
    assert len(wheels) == 4
    
    wheels = structpath.find_objs_path(car_ctx, '.wheel[num == 2]')
    assert len(wheels) == 1
    a_wheel = wheels[0]
    assert a_wheel.num == 2
    assert a_wheel is root.wheels[1]
    
    wheels = structpath.find_objs_path(car_ctx, '.wheel[num_next() == 2]')
    assert len(wheels) == 1
    a_wheel = wheels[0]
    assert a_wheel.num == 1
    assert a_wheel is root.wheels[0]
    pass


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

    wheels = structpath.find_objs_path(car_ctx, '/wheels/*')
    assert len(wheels) == 4
    
    try:
        handles = structpath.find_objs_path(car_ctx, '/handle/*')
    except ValueError:
        pass
    else:
        raise ValueError, '* smybol should be invalid, but it is valid'
    pass