test_cli.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """
  2. CLI类的单元测试
  3. """
  4. import pytest
  5. from io import StringIO
  6. import sys
  7. from todo.cli import CLI
  8. @pytest.fixture
  9. def temp_cli(tmp_path):
  10. """创建临时CLI对象"""
  11. filepath = tmp_path / "test_todos.json"
  12. return CLI(str(filepath))
  13. class TestCLI:
  14. """CLI类测试"""
  15. def test_create_cli(self, temp_cli):
  16. """测试创建CLI对象"""
  17. assert temp_cli.todo is not None
  18. assert temp_cli.storage is not None
  19. def test_cmd_add(self, temp_cli, capsys):
  20. """测试添加命令"""
  21. temp_cli.cmd_add("买菜")
  22. captured = capsys.readouterr()
  23. assert "已添加" in captured.out
  24. assert "买菜" in captured.out
  25. assert len(temp_cli.todo.items) == 1
  26. def test_cmd_list_empty(self, temp_cli, capsys):
  27. """测试列出空列表"""
  28. temp_cli.cmd_list()
  29. captured = capsys.readouterr()
  30. assert "无" in captured.out
  31. def test_cmd_list_all(self, temp_cli, capsys):
  32. """测试列出所有事项"""
  33. temp_cli.cmd_add("任务1")
  34. temp_cli.cmd_add("任务2")
  35. temp_cli.cmd_complete(1)
  36. temp_cli.cmd_list("all")
  37. captured = capsys.readouterr()
  38. assert "任务1" in captured.out
  39. assert "任务2" in captured.out
  40. assert "总计: 2" in captured.out
  41. def test_cmd_list_pending(self, temp_cli, capsys):
  42. """测试列出未完成事项"""
  43. temp_cli.cmd_add("任务1")
  44. temp_cli.cmd_add("任务2")
  45. temp_cli.cmd_complete(1)
  46. temp_cli.cmd_list("pending")
  47. captured = capsys.readouterr()
  48. assert "未完成" in captured.out
  49. assert "任务2" in captured.out
  50. def test_cmd_list_completed(self, temp_cli, capsys):
  51. """测试列出已完成事项"""
  52. temp_cli.cmd_add("任务1")
  53. temp_cli.cmd_add("任务2")
  54. temp_cli.cmd_complete(1)
  55. temp_cli.cmd_list("completed")
  56. captured = capsys.readouterr()
  57. assert "已完成" in captured.out
  58. assert "任务1" in captured.out
  59. def test_cmd_complete(self, temp_cli, capsys):
  60. """测试完成命令"""
  61. temp_cli.cmd_add("任务1")
  62. temp_cli.cmd_complete(1)
  63. captured = capsys.readouterr()
  64. assert "已完成" in captured.out
  65. assert temp_cli.todo.items[0].completed is True
  66. def test_cmd_complete_nonexistent(self, temp_cli):
  67. """测试完成不存在的事项"""
  68. with pytest.raises(SystemExit):
  69. temp_cli.cmd_complete(999)
  70. def test_cmd_uncomplete(self, temp_cli, capsys):
  71. """测试取消完成命令"""
  72. temp_cli.cmd_add("任务1")
  73. temp_cli.cmd_complete(1)
  74. temp_cli.cmd_uncomplete(1)
  75. captured = capsys.readouterr()
  76. assert "未完成" in captured.out
  77. assert temp_cli.todo.items[0].completed is False
  78. def test_cmd_delete(self, temp_cli, capsys):
  79. """测试删除命令"""
  80. temp_cli.cmd_add("任务1")
  81. temp_cli.cmd_delete(1)
  82. captured = capsys.readouterr()
  83. assert "已删除" in captured.out
  84. assert len(temp_cli.todo.items) == 0
  85. def test_cmd_delete_nonexistent(self, temp_cli):
  86. """测试删除不存在的事项"""
  87. with pytest.raises(SystemExit):
  88. temp_cli.cmd_delete(999)
  89. def test_cmd_clear(self, temp_cli, capsys):
  90. """测试清除已完成事项"""
  91. temp_cli.cmd_add("任务1")
  92. temp_cli.cmd_add("任务2")
  93. temp_cli.cmd_add("任务3")
  94. temp_cli.cmd_complete(1)
  95. temp_cli.cmd_complete(2)
  96. temp_cli.cmd_clear()
  97. captured = capsys.readouterr()
  98. assert "已清除 2 个" in captured.out
  99. assert len(temp_cli.todo.items) == 1
  100. def test_run_add_command(self, temp_cli):
  101. """测试运行add命令"""
  102. temp_cli.run(["add", "测试任务"])
  103. assert len(temp_cli.todo.items) == 1
  104. assert temp_cli.todo.items[0].title == "测试任务"
  105. def test_run_list_command(self, temp_cli, capsys):
  106. """测试运行list命令"""
  107. temp_cli.run(["add", "任务1"])
  108. temp_cli.run(["list"])
  109. captured = capsys.readouterr()
  110. assert "任务1" in captured.out
  111. def test_run_complete_command(self, temp_cli):
  112. """测试运行complete命令"""
  113. temp_cli.run(["add", "任务1"])
  114. temp_cli.run(["complete", "1"])
  115. assert temp_cli.todo.items[0].completed is True
  116. def test_run_delete_command(self, temp_cli):
  117. """测试运行delete命令"""
  118. temp_cli.run(["add", "任务1"])
  119. temp_cli.run(["delete", "1"])
  120. assert len(temp_cli.todo.items) == 0
  121. def test_run_no_command(self, temp_cli, capsys):
  122. """测试不带命令运行"""
  123. temp_cli.run([])
  124. captured = capsys.readouterr()
  125. assert "usage:" in captured.out or "Todo List" in captured.out
  126. def test_persistence(self, temp_cli):
  127. """测试数据持久化"""
  128. # 添加数据
  129. temp_cli.run(["add", "任务1"])
  130. temp_cli.run(["add", "任务2"])
  131. temp_cli.run(["complete", "1"])
  132. # 创建新的CLI实例,应该能加载之前的数据
  133. new_cli = CLI(temp_cli.storage.filepath)
  134. assert len(new_cli.todo.items) == 2
  135. assert new_cli.todo.items[0].completed is True
  136. assert new_cli.todo.items[1].completed is False