Beispiel #1
0
def test_graph_ecs_entity_update_input_outputs():
    # test use_input_outputs() propagates input and output dict to components
    entity_id, inputs, outputs = 1, OrderedDict(), OrderedDict()
    car = GraphEntity.from_def(
        entity_def=EntityDef(components=[Position, Speed], entity_id=1),
        component_defs=[Position, Speed],
    )
    car.use_input_outputs(inputs, outputs)

    # get/set should propagate retrieve mutate to inputs and output
    car_pos_x = car[Position].x
    car[Position].x = 1
    car_speed_x = car[Speed].x
    car[Position].x = 2

    pos_attr_ref = AttributeRef(entity_id=entity_id,
                                component=Position.name,
                                attribute="x")
    speed_attr_ref = AttributeRef(entity_id=entity_id,
                                  component=Position.name,
                                  attribute="x")

    pos_expected_input = Node(retrieve_op=Node.Retrieve(
        retrieve_attr=pos_attr_ref))
    pos_expected_output = Node(
        mutate_op=Node.Mutate(mutate_attr=pos_attr_ref, to_node=wrap_const(1)))
    assert inputs[to_str_attr(pos_attr_ref)] == pos_expected_input
    assert outputs[to_str_attr(pos_attr_ref)] == pos_expected_input

    speed_expected_input = Node(retrieve_op=Node.Retrieve(
        retrieve_attr=speed_attr_ref))
    speed_expected_output = Node(mutate_op=Node.Mutate(
        mutate_attr=speed_attr_ref, to_node=wrap_const(2)))
    assert inputs[to_str_attr(speed_attr_ref)] == speed_expected_input
    assert outputs[to_str_attr(speed_attr_ref)] == speed_expected_input
Beispiel #2
0
def test_graph_ecs_component_aug_assign_node():
    entity_id, inputs, outputs = 1, OrderedDict(), OrderedDict()
    position = GraphComponent.from_def(entity_id, Position)
    position.use_input_outputs(inputs, outputs)

    # check augment assignment flags the attribute (position.x) as both input and output
    position.y += 30

    attr_ref = AttributeRef(
        entity_id=entity_id,
        component=Position.name,
        attribute="y",
    )
    expected_input = Node(retrieve_op=Node.Retrieve(retrieve_attr=attr_ref))
    expected_output = Node(mutate_op=Node.Mutate(
        mutate_attr=attr_ref,
        to_node=Node(add_op=Node.Add(
            x=expected_input,
            y=wrap_const(30),
        )),
    ))
    assert len(inputs) == 1
    assert inputs[to_str_attr(attr_ref)] == expected_input
    assert len(outputs) == 1
    assert outputs[to_str_attr(attr_ref)] == expected_output
Beispiel #3
0
def test_graph_ecs_component_set_attr_native_value():
    entity_id, inputs, outputs = 1, OrderedDict(), OrderedDict()
    position = GraphComponent.from_def(entity_id, Position)
    position.use_input_outputs(inputs, outputs)

    # check setting attribute to native sets expected output node node.
    position.y = 3

    attr_ref = AttributeRef(
        entity_id=entity_id,
        component=Position.name,
        attribute="y",
    )
    expected_node = Node(mutate_op=Node.Mutate(
        mutate_attr=attr_ref,
        to_node=wrap_const(3),
    ))
    assert outputs[to_str_attr(attr_ref)] == expected_node
Beispiel #4
0
def test_graph_ecs_component_get_attr():
    entity_id, inputs, outputs = 1, OrderedDict(), OrderedDict()
    position = GraphComponent.from_def(entity_id, Position)
    position.use_input_outputs(inputs, outputs)

    # check that getting an attribute from a component returns a GraphNode
    # wrapping a Retrieve node that retrieves the attribute
    pos_x = position.x

    attr_ref = AttributeRef(entity_id=entity_id,
                            component=Position.name,
                            attribute="x")
    expected_node = Node(retrieve_op=Node.Retrieve(retrieve_attr=attr_ref))
    assert pos_x.node == expected_node
    # check that component records the retrieve in
    assert inputs[to_str_attr(attr_ref)].node == expected_node
    # check that retrieving the same attribute only records it once
    pos_y = position.x
    assert len(inputs) == 1
Beispiel #5
0
def test_graph_ecs_component_set_attr_node():
    entity_id, inputs, outputs = 1, OrderedDict(), OrderedDict()
    position = GraphComponent.from_def(entity_id, Position)
    position.use_input_outputs(inputs, outputs)

    pos_x = position.x
    position.y = 10
    # check setting attribute to node sets expected output node.
    position.y = pos_x

    attr_ref = AttributeRef(
        entity_id=entity_id,
        component=Position.name,
        attribute="y",
    )
    expected_node = Node(mutate_op=Node.Mutate(
        mutate_attr=attr_ref,
        to_node=pos_x.node,
    ))
    assert outputs[to_str_attr(attr_ref)].node == expected_node
    # check that setting attribute only takes the last definition
    # the first definition should be ignored since the attribute is redefined
    assert len(outputs) == 1