示例工程 30-plugin-registry-design

30-plugin-registry-design/01-decorator-registry/test_decorator_registry.py

python · 0.7 KB

"""test_decorator_registry.py"""
 
from __future__ import annotations
 
import pytest
from decorator_registry import REGISTRY, main, register, run
 
 
def test_registry_and_run() -> None:
    assert sorted(REGISTRY) == ["bye", "hello"]
    assert run("hello", "Ada") == "hello Ada"
    assert run("bye", "Ada") == "bye Ada"
 
 
def test_duplicate_registration_rejected() -> None:
    with pytest.raises(ValueError, match="duplicate command: hello"):
 
        @register("hello")
        def duplicate(name: str) -> str:
            return name
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "commands: ['bye', 'hello']" in out
    assert "duplicate: duplicate command: hello" in out