示例工程 23-bytecode-dis-ast

23-bytecode-dis-ast/03-ast-count-calls/test_ast_count_calls.py

python · 0.6 KB

"""test_ast_count_calls.py"""
 
from __future__ import annotations
 
import pytest
from ast_count_calls import SOURCE, call_sites, main
 
 
def test_call_sites() -> None:
    assert call_sites(SOURCE) == [("add", 4), ("add", 5), ("print", 5)]
 
 
def test_attribute_and_nested_calls() -> None:
    source = "import math\nvalue = math.sqrt(len([1, 2]))\n"
    assert call_sites(source) == [("len", 2), ("sqrt", 2)]
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "calls: [('add', 4), ('add', 5), ('print', 5)]" in out