""" CLI类的单元测试 """ import pytest from io import StringIO import sys from todo.cli import CLI @pytest.fixture def temp_cli(tmp_path): """创建临时CLI对象""" filepath = tmp_path / "test_todos.json" return CLI(str(filepath)) class TestCLI: """CLI类测试""" def test_create_cli(self, temp_cli): """测试创建CLI对象""" assert temp_cli.todo is not None assert temp_cli.storage is not None def test_cmd_add(self, temp_cli, capsys): """测试添加命令""" temp_cli.cmd_add("买菜") captured = capsys.readouterr() assert "已添加" in captured.out assert "买菜" in captured.out assert len(temp_cli.todo.items) == 1 def test_cmd_list_empty(self, temp_cli, capsys): """测试列出空列表""" temp_cli.cmd_list() captured = capsys.readouterr() assert "无" in captured.out def test_cmd_list_all(self, temp_cli, capsys): """测试列出所有事项""" temp_cli.cmd_add("任务1") temp_cli.cmd_add("任务2") temp_cli.cmd_complete(1) temp_cli.cmd_list("all") captured = capsys.readouterr() assert "任务1" in captured.out assert "任务2" in captured.out assert "总计: 2" in captured.out def test_cmd_list_pending(self, temp_cli, capsys): """测试列出未完成事项""" temp_cli.cmd_add("任务1") temp_cli.cmd_add("任务2") temp_cli.cmd_complete(1) temp_cli.cmd_list("pending") captured = capsys.readouterr() assert "未完成" in captured.out assert "任务2" in captured.out def test_cmd_list_completed(self, temp_cli, capsys): """测试列出已完成事项""" temp_cli.cmd_add("任务1") temp_cli.cmd_add("任务2") temp_cli.cmd_complete(1) temp_cli.cmd_list("completed") captured = capsys.readouterr() assert "已完成" in captured.out assert "任务1" in captured.out def test_cmd_complete(self, temp_cli, capsys): """测试完成命令""" temp_cli.cmd_add("任务1") temp_cli.cmd_complete(1) captured = capsys.readouterr() assert "已完成" in captured.out assert temp_cli.todo.items[0].completed is True def test_cmd_complete_nonexistent(self, temp_cli): """测试完成不存在的事项""" with pytest.raises(SystemExit): temp_cli.cmd_complete(999) def test_cmd_uncomplete(self, temp_cli, capsys): """测试取消完成命令""" temp_cli.cmd_add("任务1") temp_cli.cmd_complete(1) temp_cli.cmd_uncomplete(1) captured = capsys.readouterr() assert "未完成" in captured.out assert temp_cli.todo.items[0].completed is False def test_cmd_delete(self, temp_cli, capsys): """测试删除命令""" temp_cli.cmd_add("任务1") temp_cli.cmd_delete(1) captured = capsys.readouterr() assert "已删除" in captured.out assert len(temp_cli.todo.items) == 0 def test_cmd_delete_nonexistent(self, temp_cli): """测试删除不存在的事项""" with pytest.raises(SystemExit): temp_cli.cmd_delete(999) def test_cmd_clear(self, temp_cli, capsys): """测试清除已完成事项""" temp_cli.cmd_add("任务1") temp_cli.cmd_add("任务2") temp_cli.cmd_add("任务3") temp_cli.cmd_complete(1) temp_cli.cmd_complete(2) temp_cli.cmd_clear() captured = capsys.readouterr() assert "已清除 2 个" in captured.out assert len(temp_cli.todo.items) == 1 def test_run_add_command(self, temp_cli): """测试运行add命令""" temp_cli.run(["add", "测试任务"]) assert len(temp_cli.todo.items) == 1 assert temp_cli.todo.items[0].title == "测试任务" def test_run_list_command(self, temp_cli, capsys): """测试运行list命令""" temp_cli.run(["add", "任务1"]) temp_cli.run(["list"]) captured = capsys.readouterr() assert "任务1" in captured.out def test_run_complete_command(self, temp_cli): """测试运行complete命令""" temp_cli.run(["add", "任务1"]) temp_cli.run(["complete", "1"]) assert temp_cli.todo.items[0].completed is True def test_run_delete_command(self, temp_cli): """测试运行delete命令""" temp_cli.run(["add", "任务1"]) temp_cli.run(["delete", "1"]) assert len(temp_cli.todo.items) == 0 def test_run_no_command(self, temp_cli, capsys): """测试不带命令运行""" temp_cli.run([]) captured = capsys.readouterr() assert "usage:" in captured.out or "Todo List" in captured.out def test_persistence(self, temp_cli): """测试数据持久化""" # 添加数据 temp_cli.run(["add", "任务1"]) temp_cli.run(["add", "任务2"]) temp_cli.run(["complete", "1"]) # 创建新的CLI实例,应该能加载之前的数据 new_cli = CLI(temp_cli.storage.filepath) assert len(new_cli.todo.items) == 2 assert new_cli.todo.items[0].completed is True assert new_cli.todo.items[1].completed is False