def test_linear_statement_control_flow(): """Test that graph construction doesn't set on fire.""" builder = ProgramGraphBuilder() mod = builder.AddModule("x") fn = builder.AddFunction("x", mod) a = builder.AddInstruction("a", fn) b = builder.AddInstruction("b", fn) builder.AddControlEdge(builder.root, a, position=0) builder.AddControlEdge(a, b, position=0) assert isinstance(builder.Build(), program_graph_pb2.ProgramGraph) assert len(builder.Build().node) == 3 assert builder.Build().node[builder.root].text == "[external]" assert builder.Build().node[builder.root].type == node_pb2.Node.INSTRUCTION assert builder.Build().node[a].text == "a" assert builder.Build().node[a].type == node_pb2.Node.INSTRUCTION assert builder.Build().node[a].function == fn assert builder.Build().node[b].text == "b" assert builder.Build().node[b].type == node_pb2.Node.INSTRUCTION assert builder.Build().node[b].function == fn assert len(builder.Build().edge) == 2 assert builder.Build().edge[0].source == builder.root assert builder.Build().edge[0].target == a assert builder.Build().edge[0].position == 0 assert builder.Build().edge[0].flow == edge_pb2.Edge.CONTROL assert builder.Build().edge[1].source == a assert builder.Build().edge[1].target == b assert builder.Build().edge[1].position == 0 assert builder.Build().edge[1].flow == edge_pb2.Edge.CONTROL
def test_graph_with_unconnected_node(): builder = ProgramGraphBuilder() mod = builder.AddModule("x") fn = builder.AddFunction("x", mod) builder.AddInstruction("x", fn) assert isinstance(builder.Build(), program_graph_pb2.ProgramGraph)
def test_add_empty_function(): builder = ProgramGraphBuilder() mod = builder.AddModule("foo") foo = builder.AddFunction("bar", mod) assert foo == 0 assert isinstance(builder.Build(), program_graph_pb2.ProgramGraph)
def test_graph_with_unconnected_node_strict(): builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True)) mod = builder.AddModule("x") fn = builder.AddFunction("x", mod) builder.AddInstruction("x", fn) with test.Raises(ValueError) as e_ctx: builder.Build() assert "INSTRUCTION has no connections: " in str(e_ctx.value)
def test_add_empty_function_strict(): builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True)) mod = builder.AddModule("foo") foo = builder.AddFunction("bar", mod) assert foo == 0 with test.Raises(ValueError) as e_ctx: builder.Build() assert str(e_ctx.value) == "Function `bar` is empty"