Refactor to unified caesar() function with encode/decode support

- Replace separate encrypt/decrypt with shared caesar() function
- Add direction constants (DIRECTION_ENCODE, DIRECTION_DECODE) and
  prompts
- Update main.py to support interactive encode/decode loop
- Keep encrypt/decrypt as wrappers for backward compatibility
- Add tests for caesar() with both directions and error handling
- Update tests for main.py with interactive scenario tests
This commit is contained in:
2026-10-04 13:23:47 +08:00
parent d4b3e6ab19
commit 9d8beb3aef
8 changed files with 139 additions and 33 deletions
+28
View File
@@ -0,0 +1,28 @@
from caesar_cipher.main import main
def run(monkeypatch, capsys, answers):
it = iter(answers)
monkeypatch.setattr("builtins.input", lambda _="": next(it))
main()
return capsys.readouterr().out
def test_encode_then_stop(monkeypatch, capsys):
out = run(monkeypatch, capsys, ["encode", "hello", "5", "no"])
assert "mjqqt" in out and "Goodbye" in out
def test_decode_then_go_again(monkeypatch, capsys):
out = run(monkeypatch, capsys, ["decode", "mjqqt", "5", "yes", "encode", "a", "1", "no"])
assert "hello" in out and "b" in out
def test_invalid_input_reprompts(monkeypatch, capsys):
out = run(monkeypatch, capsys, ["nope", "decode", "x", "abc", "encode", "a", "1", "no"])
assert "encode' or 'decode" in out and "whole number" in out
def test_title_printed_plain_when_not_a_tty(monkeypatch, capsys):
out = run(monkeypatch, capsys, ["encode", "a", "1", "no"])
assert "____" in out and "\033[" not in out