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:
+13
-1
@@ -1,4 +1,6 @@
|
||||
from caesar_cipher import decrypt, encrypt
|
||||
import pytest
|
||||
|
||||
from caesar_cipher import caesar, decrypt, encrypt
|
||||
from caesar_cipher.env import load_env
|
||||
|
||||
|
||||
@@ -37,3 +39,13 @@ def test_decrypt_roundtrip():
|
||||
def test_decrypt_wraps_around_alphabet():
|
||||
assert decrypt("abc", 3) == "xyz"
|
||||
assert decrypt("def", 29) == "abc"
|
||||
|
||||
|
||||
def test_caesar_encode_and_decode():
|
||||
assert caesar("hello", 5, "encode") == "mjqqt"
|
||||
assert caesar("mjqqt", 5, "decode") == "hello"
|
||||
|
||||
|
||||
def test_caesar_unknown_direction():
|
||||
with pytest.raises(ValueError):
|
||||
caesar("hello", 5, "sideways")
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user