eval_python/test_cryptarithme.py
2024-10-07 12:28:18 +02:00

52 lines
1.6 KiB
Python

from main import *
import pytest
import io
import sys
def test_cryptarithme_fails_if_invalid_characters():
with pytest.raises(ValueError) as e_info:
cryptarithme("AB_CD * WAIT ? = WHAT")
def test_cryptarithme_fails_if_invalid_format_too_many_letters():
with pytest.raises(ValueError) as e_info:
cryptarithme("ABCDEF + GHIJKL = TOOMANYLETTERS")
def test_cryptarithme_fails_if_invalid_format_too_many_equals():
with pytest.raises(ValueError) as e_info:
cryptarithme("A + B = C = D")
def test_cryptarithme_fails_if_invalid_format_too_many_plus():
with pytest.raises(ValueError) as e_info:
cryptarithme("A + + B = C")
def test_cryptarithme_fails_if_impossible():
with pytest.raises(CryptarithmeError) as e_info:
a = cryptarithme("ALPHA + BETA = GAMMA")
print(a)
def test_cryptarithme_a_a_c():
results = cryptarithme('A + A = B')
assert len(results) == 4
def test_cryptarithme_a_a_c_gives_proper_results():
results = cryptarithme('A + A = B')
assert {"B": 2, "A": 1} in results
def test_cryptarithme_planetes():
result = cryptarithme('MARS + SATURNE + NEPTUNE = PLANETES')
assert len(result) == 1
def test_cryptarithme_a_a_a():
results = cryptarithme('A + A = A')
assert len(results) == 1
def test_cryptarithme_prints_nothing():
captured_output = io.StringIO() # Make StringIO.
sys.stdout = captured_output # Redirect stdout.
cryptarithme('A + A = B') # Call function.
sys.stdout = sys.__stdout__ # Reset redirect.
assert len(captured_output.getvalue()) == 0, "The function printed an output in the console"