def test_set_dispatch(ir_docsifier): IRDocsifier.set_dispatch(Var, _get_id_doc_printer("x2"), f"{__file__}-2") with ir_docsifier.dispatch_token(f"{__file__}-2"): doc = ir_docsifier.as_doc(Var("x", dtype="int8"), ObjectPath.root()) assert doc.name == "x2" doc = ir_docsifier.as_doc(Var("x", dtype="int8"), ObjectPath.root()) assert doc.name == "x"
def test_is_var_defined(): var_table = VarTable() a = Var("a", dtype="int32") object_path = ObjectPath.root().attr("a") frame = VarDefFrame() var_table.define(a, "a", object_path, frame) assert var_table.is_var_defined(a) assert a in var_table
def test_var_out_of_scope(): var_table = VarTable() var_name = "a" var_obj = Var(var_name, dtype="int32") object_path = ObjectPath.root().attr("a") frame = VarDefFrame() var_table.define(var_obj, var_name, object_path, frame) with frame: assert var_obj in var_table assert var_obj not in var_table assert var_table.get_var_doc(var_obj, object_path) is None
def test_define_by_doc(): var_table = VarTable() var_name = "a" var_obj = Var(var_name, dtype="int32") object_path = ObjectPath.root().attr("a") frame = VarDefFrame() var_table.define_by_doc(var_obj, lambda: LiteralDoc(var_name), frame) var_doc = var_table.get_var_doc(var_obj, object_path) assert isinstance(var_doc, LiteralDoc) assert var_doc.value == var_name assert list(var_doc.source_paths) == [object_path]
def test_define(): var_table = VarTable() var_name = "a" var_obj = Var(var_name, dtype="int32") object_path = ObjectPath.root().attr("a") frame = VarDefFrame() id_doc = var_table.define(var_obj, var_name, object_path, frame) assert id_doc.name == "a" assert list(id_doc.source_paths) == [object_path] id_doc = var_table.get_var_doc(var_obj, object_path) assert id_doc.name == "a" assert list(id_doc.source_paths) == [object_path]
def make_path(name: str) -> ObjectPath: return ObjectPath.root().attr(name)
def test_as_doc(ir_docsifier): object_path = ObjectPath.root() doc = ir_docsifier.as_doc(Var("x", "int8"), ObjectPath.root()) assert doc.name == "x" assert list(doc.source_paths) == [object_path]
a_path, b_path = mismatch b_path_swapped, a_path_swapped = mismatch_swapped assert a_path == a_path_swapped assert b_path == b_path_swapped return mismatch @pytest.mark.parametrize( "a, b, expected_a_path, expected_b_path", [ ( [1, 2, 3], [1, 4, 3], ObjectPath.root().array_index(1).attr("value"), ObjectPath.root().array_index(1).attr("value"), ), ( [1, 2, 3], [10, 2, 30], ObjectPath.root().array_index(0).attr("value"), ObjectPath.root().array_index(0).attr("value"), ), ( [1, 3, 4], [1, 2, 3, 4], ObjectPath.root().array_index(1).attr("value"), ObjectPath.root().array_index(1).attr("value"), ), (