Files
trycua--cua/libs/python/cua-cli/tests/utils/test_output.py
T
wehub-resource-sync 91e75e620b
CI: cua-driver distro-compat matrix / debian:12 (glibc 2.36) (push) Has been cancelled
CI: SPDX Headers / Check SPDX headers (warn-only) (push) Has been cancelled
CD: Docs MCP Server / build (linux/amd64) (push) Has been cancelled
CD: Docs MCP Server / build (linux/arm64) (push) Has been cancelled
CD: Docs MCP Server / merge (push) Has been cancelled
CI: cua-driver distro-compat matrix / Resolve release version (push) Has been cancelled
CI: cua-driver distro-compat matrix / fedora:41 (glibc 2.40) (push) Has been cancelled
CI: cua-driver distro-compat matrix / rockylinux:9 (glibc 2.34) (push) Has been cancelled
CI: cua-driver distro-compat matrix / ubuntu:22.04 (glibc 2.35) (push) Has been cancelled
CI: cua-driver distro-compat matrix / ubuntu:24.04 (glibc 2.39) (push) Has been cancelled
CI: cua-driver distro-compat matrix / Distro compat summary (push) Has been cancelled
CI: Rust Linux unit / Rust Linux unit and compile (push) Has been cancelled
CI: Rust Windows unit / Rust Windows unit and compile (push) Has been cancelled
CI: Nix Linux Rust source / Nix / compositor build (push) Has been cancelled
CI: Nix Linux Rust source / Nix / driver package (push) Has been cancelled
CI: Nix Linux Rust source / Nix / Rust unit tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:19 +08:00

164 lines
4.8 KiB
Python

"""Tests for output utilities."""
from unittest.mock import patch
from cua_cli.utils.output import (
print_error,
print_info,
print_json,
print_success,
print_table,
print_warning,
)
class TestPrintTable:
"""Tests for print_table function."""
def test_prints_table_with_data(self):
"""Test printing a table with data."""
data = [
{"name": "test1", "status": "running"},
{"name": "test2", "status": "stopped"},
]
columns = [("name", "NAME"), ("status", "STATUS")]
with patch("cua_cli.utils.output.console") as mock_console:
print_table(data, columns)
mock_console.print.assert_called_once()
def test_handles_empty_data(self):
"""Test handling empty data list."""
data = []
with patch("cua_cli.utils.output.console") as mock_console:
print_table(data)
mock_console.print.assert_called_once()
# Should print "No data" message
call_args = mock_console.print.call_args[0][0]
assert "No data" in call_args
def test_auto_generates_columns(self):
"""Test that columns are auto-generated from data keys."""
data = [{"foo": "bar", "baz": "qux"}]
with patch("cua_cli.utils.output.console") as mock_console:
print_table(data)
mock_console.print.assert_called_once()
def test_handles_missing_keys(self):
"""Test handling data items with missing keys."""
data = [
{"name": "test1", "status": "running"},
{"name": "test2"}, # Missing status
]
columns = [("name", "NAME"), ("status", "STATUS")]
with patch("cua_cli.utils.output.console") as mock_console:
print_table(data, columns)
mock_console.print.assert_called_once()
def test_with_title(self):
"""Test table with title."""
data = [{"name": "test"}]
with patch("cua_cli.utils.output.console") as mock_console:
print_table(data, title="Test Table")
mock_console.print.assert_called_once()
class TestPrintJson:
"""Tests for print_json function."""
def test_prints_dict(self):
"""Test printing a dictionary as JSON."""
data = {"key": "value", "number": 42}
with patch("cua_cli.utils.output.console") as mock_console:
print_json(data)
mock_console.print_json.assert_called_once()
def test_prints_list(self):
"""Test printing a list as JSON."""
data = [{"name": "test1"}, {"name": "test2"}]
with patch("cua_cli.utils.output.console") as mock_console:
print_json(data)
mock_console.print_json.assert_called_once()
def test_handles_nested_data(self):
"""Test printing nested data structures."""
data = {
"level1": {
"level2": {
"value": 123,
}
}
}
with patch("cua_cli.utils.output.console") as mock_console:
print_json(data)
mock_console.print_json.assert_called_once()
class TestPrintError:
"""Tests for print_error function."""
def test_prints_to_stderr(self):
"""Test that error is printed to stderr."""
with patch("cua_cli.utils.output.error_console") as mock_console:
print_error("Test error message")
mock_console.print.assert_called_once()
call_args = mock_console.print.call_args[0][0]
assert "Error:" in call_args
assert "Test error message" in call_args
class TestPrintSuccess:
"""Tests for print_success function."""
def test_prints_success_message(self):
"""Test printing success message."""
with patch("cua_cli.utils.output.console") as mock_console:
print_success("Operation successful")
mock_console.print.assert_called_once()
call_args = mock_console.print.call_args[0][0]
assert "Operation successful" in call_args
class TestPrintWarning:
"""Tests for print_warning function."""
def test_prints_warning_message(self):
"""Test printing warning message."""
with patch("cua_cli.utils.output.console") as mock_console:
print_warning("This is a warning")
mock_console.print.assert_called_once()
call_args = mock_console.print.call_args[0][0]
assert "Warning:" in call_args
assert "This is a warning" in call_args
class TestPrintInfo:
"""Tests for print_info function."""
def test_prints_info_message(self):
"""Test printing info message."""
with patch("cua_cli.utils.output.console") as mock_console:
print_info("Information message")
mock_console.print.assert_called_once()
call_args = mock_console.print.call_args[0][0]
assert "Information message" in call_args