def test_code_gives_back_name_for_not_existing_file() -> None: name = "abc-123" co_code = compile("pass\n", name, "exec") assert co_code.co_filename == name code = Code(co_code) assert str(code.path) == name assert code.fullsource is None
def test_code_of_object_instance_with_call() -> None: class A: pass pytest.raises(TypeError, lambda: Source(A())) class WithCall: def __call__(self) -> None: pass code = Code.from_function(WithCall()) assert "pass" in str(code.source()) class Hello: def __call__(self) -> None: pass pytest.raises(TypeError, lambda: Code.from_function(Hello))
def test_code_getargs() -> None: def f1(x): raise NotImplementedError() c1 = Code.from_function(f1) assert c1.getargs(var=True) == ("x",) def f2(x, *y): raise NotImplementedError() c2 = Code.from_function(f2) assert c2.getargs(var=True) == ("x", "y") def f3(x, **z): raise NotImplementedError() c3 = Code.from_function(f3) assert c3.getargs(var=True) == ("x", "z") def f4(x, *y, **z): raise NotImplementedError() c4 = Code.from_function(f4) assert c4.getargs(var=True) == ("x", "y", "z")
def test_code_from_func() -> None: co = Code(test_frame_getsourcelineno_myself) assert co.firstlineno assert co.path
def test_code_source() -> None: code = Code(x) src = code.source() expected = """def x() -> None: raise NotImplementedError()""" assert str(src) == expected
def test_code_fullsource() -> None: code = Code(x) full = code.fullsource assert "test_code_fullsource()" in str(full)
def test_ne() -> None: code1 = Code(compile('foo = "bar"', "", "exec")) assert code1 == code1 code2 = Code(compile('foo = "baz"', "", "exec")) assert code2 != code1
def test_code_from_function_with_class() -> None: class A: pass with pytest.raises(TypeError): Code.from_function(A)